2
0

Compiler-Tests.st 16 KB

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