Compiler-Tests.st 19 KB

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