2
0

Compiler-Tests.st 17 KB

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