Compiler-Tests.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. Smalltalk createPackage: 'Compiler-Tests'!
  2. TestCase subclass: #ASTParsingTest
  3. instanceVariableNames: ''
  4. package: 'Compiler-Tests'!
  5. !ASTParsingTest methodsFor: 'convenience'!
  6. analyze: aNode forClass: aClass
  7. (SemanticAnalyzer on: aClass) visit: aNode.
  8. ^ aNode
  9. ! !
  10. !ASTParsingTest methodsFor: 'parsing'!
  11. parse: aString
  12. ^ Smalltalk parse: aString
  13. !
  14. parse: aString forClass: aClass
  15. ^ self analyze: (self parse: aString) forClass: aClass
  16. ! !
  17. ASTParsingTest subclass: #ASTPCNodeVisitorTest
  18. instanceVariableNames: ''
  19. package: 'Compiler-Tests'!
  20. !ASTPCNodeVisitorTest methodsFor: 'factory'!
  21. astPCNodeVisitor
  22. ^ ASTPCNodeVisitor new
  23. context: (AIContext new
  24. yourself);
  25. yourself
  26. !
  27. astPCNodeVisitorForSelector: aString
  28. ^ ASTPCNodeVisitor new
  29. selector: aString;
  30. context: (AIContext new
  31. yourself);
  32. yourself
  33. ! !
  34. !ASTPCNodeVisitorTest methodsFor: 'tests'!
  35. testJSStatementNode
  36. | ast visitor |
  37. ast := self parse: 'foo <consolee.log(1)>' forClass: Object.
  38. self assert: (self astPCNodeVisitor
  39. visit: ast;
  40. currentNode) isJSStatementNode
  41. !
  42. testMessageSend
  43. | ast |
  44. ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
  45. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  46. visit: ast;
  47. currentNode) selector equals: 'yourself'
  48. !
  49. testMessageSendWithBlocks
  50. | ast |
  51. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  52. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  53. visit: ast;
  54. currentNode) selector equals: 'yourself'
  55. !
  56. testMessageSendWithInlining
  57. | ast |
  58. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  59. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  60. visit: ast;
  61. currentNode) selector equals: 'yourself'.
  62. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  63. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  64. visit: ast;
  65. currentNode) selector equals: 'asBoolean'
  66. !
  67. testNoMessageSend
  68. | ast |
  69. ast := self parse: 'foo ^ self' forClass: Object.
  70. self assert: (self astPCNodeVisitor
  71. visit: ast;
  72. currentNode) isNil
  73. !
  74. testPC
  75. | ast visitor |
  76. ast := self parse: 'foo <console.log(1)>' forClass: Object.
  77. self assert: (self astPCNodeVisitor
  78. visit: ast;
  79. currentNode) isJSStatementNode
  80. ! !
  81. ASTParsingTest subclass: #ASTPositionTest
  82. instanceVariableNames: ''
  83. package: 'Compiler-Tests'!
  84. !ASTPositionTest methodsFor: 'tests'!
  85. testAtPosition
  86. | node |
  87. node := self parse: 'yourself
  88. ^ self'.
  89. self assert: (node atPosition: 2@4) source equals: 'self'.
  90. node := self parse: 'foo
  91. true ifTrue: [ 1 ]'.
  92. self assert: (node atPosition: 2@7) selector equals: 'ifTrue:'
  93. ! !
  94. ASTParsingTest subclass: #CodeGeneratorTest
  95. instanceVariableNames: 'receiver'
  96. package: 'Compiler-Tests'!
  97. !CodeGeneratorTest methodsFor: 'accessing'!
  98. codeGeneratorClass
  99. ^ CodeGenerator
  100. ! !
  101. !CodeGeneratorTest methodsFor: 'factory'!
  102. compiler
  103. ^ Compiler new
  104. codeGeneratorClass: self codeGeneratorClass;
  105. yourself
  106. ! !
  107. !CodeGeneratorTest methodsFor: 'initialization'!
  108. setUp
  109. receiver := DoIt new
  110. !
  111. tearDown
  112. "receiver := nil"
  113. ! !
  114. !CodeGeneratorTest methodsFor: 'testing'!
  115. should: aString receiver: anObject return: aResult
  116. | method result |
  117. receiver := anObject.
  118. method := self compiler install: aString forClass: anObject class protocol: 'tests'.
  119. result := receiver perform: method selector.
  120. anObject class removeCompiledMethod: method.
  121. self assert: aResult equals: result
  122. !
  123. should: aString return: anObject
  124. ^ self
  125. should: aString
  126. receiver: receiver
  127. return: anObject
  128. ! !
  129. !CodeGeneratorTest methodsFor: 'tests'!
  130. testAssignment
  131. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  132. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  133. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  134. !
  135. testBackslashSelectors
  136. self should: '\ arg ^ 4' return: 4.
  137. self should: '\\ arg ^ 42' return: 42
  138. !
  139. testBlockReturn
  140. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  141. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  142. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  143. !
  144. testCascades
  145. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  146. !
  147. testCascadesWithInlining
  148. self should: 'foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 1.
  149. self should: 'foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 2
  150. !
  151. testDynamicArrayElementsOrdered
  152. self should: 'foo
  153. | x |
  154. x := 1.
  155. ^ { x. x := 2 }
  156. ' return: #(1 2).
  157. self should: 'foo
  158. | x |
  159. x := 1.
  160. ^ { x. true ifTrue: [ x := 2 ] }
  161. ' return: #(1 2).
  162. !
  163. testDynamicDictionaryElementsOrdered
  164. self should: 'foo
  165. | x |
  166. x := ''foo''.
  167. ^ #{ x->1. ''bar''->(true ifTrue: [ 2 ]) }
  168. ' return: #{'foo'->1. 'bar'->2}.
  169. !
  170. testDynamicDictionaryWithMoreArrows
  171. self should: 'foo ^ #{1->2->3}' return: (HashedCollection with: 1->2->3)
  172. !
  173. testGlobalVar
  174. self should: 'foo ^ eval class' return: BlockClosure.
  175. self should: 'foo ^ Math cos: 0' return: 1.
  176. self should: 'foo ^ NonExistingVar' return: nil
  177. !
  178. testInnerTemporalDependentElementsOrdered
  179. self should: 'foo
  180. | x |
  181. x := Array.
  182. ^ x with: ''foo''->x with: ''bar''->(x := 2)
  183. ' return: {'foo'->Array. 'bar'->2}.
  184. self should: 'foo
  185. | x |
  186. x := Array.
  187. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  188. ' return: {'foo'->Array. 'bar'->2}.
  189. self should: 'foo
  190. | x |
  191. x := 1.
  192. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  193. ' return: {'foo'->1. 'bar'->2}.
  194. self should: 'foo
  195. | x |
  196. x := 1.
  197. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  198. ' return: {'foo'->1. 'bar'->2}.
  199. self should: 'foo
  200. | x |
  201. x := 1.
  202. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  203. ' return: #{'foo'->1. 'bar'->2}.
  204. !
  205. testJSStatement
  206. self should: 'foo <return 2+3>' return: 5
  207. !
  208. testLiterals
  209. self should: 'foo ^ 1' return: 1.
  210. self should: 'foo ^ ''hello''' return: 'hello'.
  211. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  212. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  213. self should: 'foo ^ true' return: true.
  214. self should: 'foo ^ false' return: false.
  215. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  216. self should: 'foo ^ #hello' return: #hello.
  217. self should: 'foo ^ -123.456' return: -123.456.
  218. self should: 'foo ^ -2.5e4' return: -25000.
  219. !
  220. testLocalReturn
  221. self should: 'foo ^ 1' return: 1.
  222. self should: 'foo ^ 1 + 1' return: 2.
  223. self should: 'foo ' return: receiver.
  224. self should: 'foo self asString' return: receiver.
  225. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  226. !
  227. testMessageSends
  228. self should: 'foo ^ 1 asString' return: '1'.
  229. self should: 'foo ^ 1 + 1' return: 2.
  230. self should: 'foo ^ 1 + 2 * 3' return: 9.
  231. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  232. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  233. !
  234. testMultipleSequences
  235. self should: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6' return: 30
  236. !
  237. testMutableLiterals
  238. "Mutable literals must be aliased in cascades.
  239. See https://github.com/amber-smalltalk/amber/issues/428"
  240. self
  241. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  242. return: #(3 2)
  243. !
  244. testNestedIfTrue
  245. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  246. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  247. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  248. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  249. !
  250. testNestedSends
  251. self should: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString' return: (Point x: (2@3) y: 4) asString
  252. !
  253. testNonLocalReturn
  254. self should: 'foo [ ^ 1 ] value' return: 1.
  255. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  256. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  257. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  258. !
  259. testPascalCaseGlobal
  260. self should: 'foo ^Object' return: (Smalltalk globals at: 'Object').
  261. self should: 'foo ^NonExistent' return: nil
  262. !
  263. testSendReceiverAndArgumentsOrdered
  264. self should: 'foo
  265. | x |
  266. x := 1.
  267. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  268. ' return: #(1 2).
  269. self should: 'foo
  270. | x |
  271. x := Array.
  272. ^ x with: x with: (true ifTrue: [ x := 2 ])
  273. ' return: {Array. 2}.
  274. !
  275. testSuperSend
  276. self
  277. should: 'foo ^ super isBoolean'
  278. receiver: true
  279. return: false
  280. !
  281. testTempVariables
  282. self should: 'foo | a | ^ a' return: nil.
  283. self should: 'foo | AVariable | ^ AVariable' return: nil.
  284. self should: 'foo | a b c | ^ c' return: nil.
  285. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  286. self should: 'foo | a | a:= 1. ^ a' return: 1.
  287. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  288. !
  289. testThisContext
  290. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  291. !
  292. testifFalse
  293. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  294. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  295. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  296. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  297. !
  298. testifFalseIfTrue
  299. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  300. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  301. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  302. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  303. !
  304. testifNil
  305. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  306. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  307. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  308. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  309. !
  310. testifNilIfNotNil
  311. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  312. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  313. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  314. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  315. !
  316. testifNotNil
  317. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  318. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  319. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  320. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  321. !
  322. testifNotNilWithArgument
  323. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  324. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  325. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  326. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  327. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  328. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  329. !
  330. testifTrue
  331. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  332. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  333. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  334. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  335. !
  336. testifTrueIfFalse
  337. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  338. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  339. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  340. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  341. ! !
  342. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  343. instanceVariableNames: ''
  344. package: 'Compiler-Tests'!
  345. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  346. codeGeneratorClass
  347. ^ InliningCodeGenerator
  348. ! !
  349. CodeGeneratorTest subclass: #InterpreterTest
  350. instanceVariableNames: ''
  351. package: 'Compiler-Tests'!
  352. !InterpreterTest methodsFor: 'parsing'!
  353. analyze: aNode forClass: aClass
  354. (SemanticAnalyzer on: aClass) visit: aNode.
  355. ^ aNode
  356. !
  357. parse: aString
  358. ^ Smalltalk parse: aString
  359. !
  360. parse: aString forClass: aClass
  361. ^ self analyze: (self parse: aString) forClass: aClass
  362. ! !
  363. !InterpreterTest methodsFor: 'private'!
  364. interpret: aString receiver: anObject withArguments: aDictionary
  365. "The food is a methodNode. Interpret the sequenceNode only"
  366. | ctx interpreter |
  367. interpreter := ASTInterpreter new.
  368. ctx := AIContext new
  369. receiver: anObject;
  370. interpreter: interpreter;
  371. yourself.
  372. aDictionary keysAndValuesDo: [ :key :value |
  373. ctx localAt: key put: value ].
  374. ^ interpreter
  375. context: ctx;
  376. interpret: (self parse: aString forClass: anObject class) nextChild;
  377. proceed;
  378. result
  379. ! !
  380. !InterpreterTest methodsFor: 'testing'!
  381. should: aString receiver: anObject return: aResult
  382. receiver := anObject.
  383. ^ self
  384. assert: (self interpret: aString receiver: receiver withArguments: #{})
  385. equals: aResult
  386. !
  387. should: aString return: anObject
  388. ^ self
  389. should: aString
  390. receiver: receiver
  391. return: anObject
  392. ! !
  393. TestCase subclass: #ScopeVarTest
  394. instanceVariableNames: ''
  395. package: 'Compiler-Tests'!
  396. !ScopeVarTest methodsFor: 'tests'!
  397. testClassRefVar
  398. | node |
  399. node := VariableNode new
  400. value: 'Object';
  401. yourself.
  402. SemanticAnalyzer new
  403. pushScope: MethodLexicalScope new;
  404. visit: node.
  405. self assert: node binding isClassRefVar
  406. !
  407. testInstanceVar
  408. | node scope |
  409. node := VariableNode new
  410. value: 'bzzz';
  411. yourself.
  412. scope := MethodLexicalScope new.
  413. scope addIVar: 'bzzz'.
  414. self assert: (scope bindingFor: node) isInstanceVar
  415. !
  416. testPseudoVar
  417. | node pseudoVars |
  418. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  419. pseudoVars do: [:each |
  420. node := VariableNode new
  421. value: each;
  422. yourself.
  423. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  424. !
  425. testTempVar
  426. | node scope |
  427. node := VariableNode new
  428. value: 'bzzz';
  429. yourself.
  430. scope := MethodLexicalScope new.
  431. scope addTemp: 'bzzz'.
  432. self assert: (scope bindingFor: node) isTempVar
  433. !
  434. testUnknownVar
  435. | node |
  436. node := VariableNode new
  437. value: 'bzzz';
  438. yourself.
  439. self assert: (MethodLexicalScope new bindingFor: node) isNil
  440. ! !
  441. TestCase subclass: #SemanticAnalyzerTest
  442. instanceVariableNames: 'analyzer'
  443. package: 'Compiler-Tests'!
  444. !SemanticAnalyzerTest methodsFor: 'running'!
  445. setUp
  446. analyzer := SemanticAnalyzer on: Object
  447. ! !
  448. !SemanticAnalyzerTest methodsFor: 'tests'!
  449. testAssignment
  450. | src ast |
  451. src := 'foo self := 1'.
  452. ast := Smalltalk parse: src.
  453. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  454. !
  455. testNonLocalReturn
  456. | src ast |
  457. src := 'foo | a | a + 1. ^ a'.
  458. ast := Smalltalk parse: src.
  459. analyzer visit: ast.
  460. self deny: ast scope hasNonLocalReturn
  461. !
  462. testNonLocalReturn2
  463. | src ast |
  464. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  465. ast := Smalltalk parse: src.
  466. analyzer visit: ast.
  467. self assert: ast scope hasNonLocalReturn
  468. !
  469. testScope
  470. | src ast |
  471. src := 'foo | a | a + 1. [ | b | b := a ]'.
  472. ast := Smalltalk parse: src.
  473. analyzer visit: ast.
  474. self deny: ast nodes first nodes last scope == ast scope.
  475. !
  476. testScope2
  477. | src ast |
  478. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  479. ast := Smalltalk parse: src.
  480. analyzer visit: ast.
  481. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  482. !
  483. testScopeLevel
  484. | src ast |
  485. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  486. ast := Smalltalk parse: src.
  487. analyzer visit: ast.
  488. self assert: ast scope scopeLevel equals: 1.
  489. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel equals: 3
  490. !
  491. testUnknownVariables
  492. | src ast |
  493. src := 'foo | a | b + a'.
  494. ast := Smalltalk parse: src.
  495. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  496. !
  497. testUnknownVariablesWithScope
  498. | src ast |
  499. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  500. ast := Smalltalk parse: src.
  501. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  502. !
  503. testVariableShadowing
  504. | src ast |
  505. src := 'foo | a | a + 1'.
  506. ast := Smalltalk parse: src.
  507. analyzer visit: ast
  508. !
  509. testVariableShadowing2
  510. | src ast |
  511. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  512. ast := Smalltalk parse: src.
  513. self should: [analyzer visit: ast] raise: ShadowingVariableError
  514. !
  515. testVariableShadowing3
  516. | src ast |
  517. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  518. ast := Smalltalk parse: src.
  519. analyzer visit: ast
  520. !
  521. testVariableShadowing4
  522. | src ast |
  523. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  524. ast := Smalltalk parse: src.
  525. analyzer visit: ast
  526. !
  527. testVariableShadowing5
  528. | src ast |
  529. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  530. ast := Smalltalk parse: src.
  531. self should: [analyzer visit: ast] raise: ShadowingVariableError
  532. !
  533. testVariablesLookup
  534. | src ast |
  535. src := 'foo | a | a + 1. [ | b | b := a ]'.
  536. ast := Smalltalk parse: src.
  537. analyzer visit: ast.
  538. "Binding for `a` in the message send"
  539. self assert: ast nodes first nodes first receiver binding isTempVar.
  540. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  541. "Binding for `b`"
  542. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  543. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  544. ! !