Compiler-Tests.st 17 KB

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