Compiler-Tests.st 18 KB

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