Compiler-Tests.st 16 KB

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