Compiler-Tests.st 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. testNilPerform
  267. self should: 'foo ^ nil perform: #yourself' return: nil
  268. !
  269. testNonLocalReturn
  270. self should: 'foo [ ^ 1 ] value' return: 1.
  271. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  272. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  273. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  274. !
  275. testPascalCaseGlobal
  276. self should: 'foo ^Object' return: (Smalltalk globals at: 'Object').
  277. self should: 'foo ^NonExistent' return: nil
  278. !
  279. testPragmaInBlock
  280. self should: 'foo ^ [ < fooBar > 4 ] value' receiver: receiver raise: ParseError
  281. !
  282. testPragmaJSStatement
  283. self should: 'foo < inlineJS: ''return 2+3'' >' return: 5
  284. !
  285. testRootSuperSend
  286. self
  287. should: 'foo ^ super class'
  288. receiver: ProtoObject new
  289. raise: MessageNotUnderstood
  290. !
  291. testSendReceiverAndArgumentsOrdered
  292. self should: 'foo
  293. | x |
  294. x := 1.
  295. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  296. ' return: #(1 2).
  297. self should: 'foo
  298. | x |
  299. x := Array.
  300. ^ x with: x with: (true ifTrue: [ x := 2 ])
  301. ' return: {Array. 2}.
  302. !
  303. testSuperSend
  304. self
  305. should: 'foo ^ super isBoolean'
  306. receiver: true
  307. return: false
  308. !
  309. testSuperSend2
  310. self
  311. should: 'foo ^ super isNil'
  312. receiver: nil
  313. return: false
  314. !
  315. testSuperSend3
  316. self
  317. should: 'doo ^ super isNil'
  318. class: Object
  319. receiver: nil
  320. return: false
  321. !
  322. testSuperSend4
  323. self
  324. should: 'foo ^ super asJavaScriptObject'
  325. receiver: 'me'
  326. return: #('m' 'e')
  327. !
  328. testSuperSend5
  329. self
  330. should: 'foo [super addLast: 4] on: Error do: [ self add: 5 ]. ^ self'
  331. class: SequenceableCollection
  332. receiver: #(1 2 3)
  333. return: #(1 2 3 5)
  334. !
  335. testTempVariables
  336. self should: 'foo | a | ^ a' return: nil.
  337. self should: 'foo | AVariable | ^ AVariable' return: nil.
  338. self should: 'foo | a b c | ^ c' return: nil.
  339. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  340. self should: 'foo | a | a:= 1. ^ a' return: 1.
  341. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  342. !
  343. testThisContext
  344. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  345. !
  346. testUnknownPragma
  347. self should: 'foo < fooBar: ''return 2+3'' > | x | ^ x := 6' return: 6.
  348. self should: 'foo | x | < fooBar: ''return 2+3'' > ^ x := 6' return: 6
  349. !
  350. testifFalse
  351. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  352. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  353. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  354. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  355. !
  356. testifFalseIfTrue
  357. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  358. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  359. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  360. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  361. !
  362. testifNil
  363. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  364. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  365. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  366. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  367. !
  368. testifNilIfNotNil
  369. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  370. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  371. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  372. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  373. !
  374. testifNotNil
  375. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  376. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  377. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  378. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  379. !
  380. testifNotNilWithArgument
  381. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  382. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  383. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  384. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  385. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  386. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  387. !
  388. testifTrue
  389. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  390. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  391. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  392. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  393. !
  394. testifTrueIfFalse
  395. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  396. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  397. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  398. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  399. ! !
  400. CodeGeneratorTest subclass: #ASTInterpreterTest
  401. slots: {}
  402. package: 'Compiler-Tests'!
  403. !ASTInterpreterTest methodsFor: 'private'!
  404. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  405. "The food is a methodNode. Interpret the sequenceNode only"
  406. | ctx |
  407. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  408. ^ ctx interpreter proceed; result
  409. !
  410. prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary
  411. "The food is a methodNode. Interpret the sequenceNode only"
  412. | ctx ast |
  413. ast := self parse: aString forClass: aClass.
  414. ctx := AIContext new
  415. receiver: anObject;
  416. selector: ast selector;
  417. interpreter: ASTInterpreter new;
  418. yourself.
  419. "Define locals for the context"
  420. ast sequenceNode ifNotNil: [ :sequence |
  421. sequence temps do: [ :each |
  422. ctx defineLocal: each ] ].
  423. aDictionary keysAndValuesDo: [ :key :value |
  424. ctx localAt: key put: value ].
  425. ctx interpreter
  426. context: ctx;
  427. node: ast;
  428. enterNode.
  429. ^ctx
  430. ! !
  431. !ASTInterpreterTest methodsFor: 'testing'!
  432. should: aString class: aClass receiver: anObject return: aResult
  433. | method result |
  434. receiver := anObject.
  435. method := self compiler install: aString forClass: aClass protocol: 'tests'.
  436. result := self interpret: aString forClass: aClass receiver: receiver withArguments: #{}.
  437. aClass removeCompiledMethod: method.
  438. self assert: aResult equals: result
  439. ! !
  440. ASTInterpreterTest subclass: #ASTDebuggerTest
  441. slots: {}
  442. package: 'Compiler-Tests'!
  443. !ASTDebuggerTest methodsFor: 'private'!
  444. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  445. "The food is a methodNode. Interpret the sequenceNode only"
  446. | ctx |
  447. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  448. ^ (ASTDebugger context: ctx) proceed; result
  449. ! !
  450. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  451. slots: {}
  452. package: 'Compiler-Tests'!
  453. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  454. codeGeneratorClass
  455. ^ InliningCodeGenerator
  456. ! !
  457. TestCase subclass: #ScopeVarTest
  458. slots: {}
  459. package: 'Compiler-Tests'!
  460. !ScopeVarTest methodsFor: 'tests'!
  461. testClassRefVar
  462. | node |
  463. node := VariableNode new
  464. value: 'Object';
  465. yourself.
  466. SemanticAnalyzer new
  467. pushScope: MethodLexicalScope new;
  468. visit: node.
  469. self assert: node binding isClassRefVar
  470. !
  471. testInstanceVar
  472. | node scope |
  473. node := VariableNode new
  474. value: 'bzzz';
  475. yourself.
  476. scope := MethodLexicalScope new.
  477. scope addIVar: 'bzzz'.
  478. self assert: (scope bindingFor: node) isInstanceVar
  479. !
  480. testPseudoVar
  481. | node pseudoVars |
  482. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  483. pseudoVars do: [:each |
  484. node := VariableNode new
  485. value: each;
  486. yourself.
  487. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar]
  488. !
  489. testTempVar
  490. | node scope |
  491. node := VariableNode new
  492. value: 'bzzz';
  493. yourself.
  494. scope := MethodLexicalScope new.
  495. scope addTemp: 'bzzz'.
  496. self assert: (scope bindingFor: node) isTempVar
  497. !
  498. testUnknownVar
  499. | node |
  500. node := VariableNode new
  501. value: 'bzzz';
  502. yourself.
  503. self assert: (MethodLexicalScope new bindingFor: node) isNil
  504. ! !
  505. TestCase subclass: #SemanticAnalyzerTest
  506. slots: {#analyzer}
  507. package: 'Compiler-Tests'!
  508. !SemanticAnalyzerTest methodsFor: 'running'!
  509. setUp
  510. analyzer := SemanticAnalyzer on: Object
  511. ! !
  512. !SemanticAnalyzerTest methodsFor: 'tests'!
  513. testAssignment
  514. | src ast |
  515. src := 'foo self := 1'.
  516. ast := Smalltalk parse: src.
  517. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  518. !
  519. testNonLocalReturn
  520. | src ast |
  521. src := 'foo | a | a + 1. ^ a'.
  522. ast := Smalltalk parse: src.
  523. analyzer visit: ast.
  524. self deny: ast scope hasNonLocalReturn
  525. !
  526. testNonLocalReturn2
  527. | src ast |
  528. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  529. ast := Smalltalk parse: src.
  530. analyzer visit: ast.
  531. self assert: ast scope hasNonLocalReturn
  532. !
  533. testScope
  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 dagChildren first dagChildren last scope == ast scope.
  539. !
  540. testScope2
  541. | src ast |
  542. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  543. ast := Smalltalk parse: src.
  544. analyzer visit: ast.
  545. self deny: ast dagChildren first dagChildren last dagChildren first dagChildren first scope == ast scope.
  546. !
  547. testScopeLevel
  548. | src ast |
  549. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  550. ast := Smalltalk parse: src.
  551. analyzer visit: ast.
  552. self assert: ast scope scopeLevel equals: 1.
  553. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first scope scopeLevel equals: 3
  554. !
  555. testUnknownVariables
  556. | src ast |
  557. src := 'foo | a | b + a'.
  558. ast := Smalltalk parse: src.
  559. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  560. !
  561. testUnknownVariablesWithScope
  562. | src ast |
  563. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  564. ast := Smalltalk parse: src.
  565. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  566. !
  567. testVariableShadowing
  568. | src ast |
  569. src := 'foo | a | a + 1'.
  570. ast := Smalltalk parse: src.
  571. analyzer visit: ast
  572. !
  573. testVariableShadowing2
  574. | src ast |
  575. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  576. ast := Smalltalk parse: src.
  577. self should: [analyzer visit: ast] raise: ShadowingVariableError
  578. !
  579. testVariableShadowing3
  580. | src ast |
  581. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  582. ast := Smalltalk parse: src.
  583. analyzer visit: ast
  584. !
  585. testVariableShadowing4
  586. | src ast |
  587. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  588. ast := Smalltalk parse: src.
  589. analyzer visit: ast
  590. !
  591. testVariableShadowing5
  592. | src ast |
  593. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  594. ast := Smalltalk parse: src.
  595. self should: [analyzer visit: ast] raise: ShadowingVariableError
  596. !
  597. testVariablesLookup
  598. | src ast |
  599. src := 'foo | a | a + 1. [ | b | b := a ]'.
  600. ast := Smalltalk parse: src.
  601. analyzer visit: ast.
  602. "Binding for `a` in the message send"
  603. self assert: ast dagChildren first dagChildren first receiver binding isTempVar.
  604. self assert: ast dagChildren first dagChildren first receiver binding scope == ast scope.
  605. "Binding for `b`"
  606. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding isTempVar.
  607. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding scope == ast dagChildren first dagChildren last scope.
  608. ! !
  609. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  610. slots: {}
  611. package: 'Compiler-Tests'!
  612. !AISemanticAnalyzerTest methodsFor: 'running'!
  613. setUp
  614. analyzer := (AISemanticAnalyzer on: Object)
  615. context: (AIContext new
  616. defineLocal: 'local';
  617. localAt: 'local' put: 3;
  618. yourself);
  619. yourself
  620. ! !
  621. !AISemanticAnalyzerTest methodsFor: 'tests'!
  622. testContextVariables
  623. | src ast |
  624. src := 'foo | a | local + a'.
  625. ast := Smalltalk parse: src.
  626. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  627. ! !