Compiler-Tests.st 23 KB

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