Compiler-Tests.st 19 KB

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