Compiler-Tests.st 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. testRegression1242
  186. self should: '
  187. foo
  188. |x|
  189. x := 2.
  190. x := nil ifNil: [].
  191. ^ x
  192. ' return: nil.
  193. self should: '
  194. foo
  195. |x|
  196. x := 2.
  197. x := 1 ifNotNil: [].
  198. ^ x
  199. ' return: nil.
  200. self should: '
  201. foo
  202. |x|
  203. x := 2.
  204. x := false ifFalse: [].
  205. ^ x
  206. ' return: nil.
  207. self should: '
  208. foo
  209. |x|
  210. x := 2.
  211. x := true ifTrue: [].
  212. ^ x
  213. ' return: nil.
  214. !
  215. testRegression1244
  216. self should: 'foo [ ^ true ifTrue: [1] ifFalse: [2] ] value' return: 1
  217. !
  218. testRootSuperSend
  219. self
  220. should: 'foo ^ super class'
  221. receiver: ProtoObject new
  222. raise: MessageNotUnderstood
  223. !
  224. testSendReceiverAndArgumentsOrdered
  225. self should: 'foo
  226. | x |
  227. x := 1.
  228. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  229. ' return: #(1 2).
  230. self should: 'foo
  231. | x |
  232. x := Array.
  233. ^ x with: x with: (true ifTrue: [ x := 2 ])
  234. ' return: {Array. 2}.
  235. !
  236. testSuperSend
  237. self
  238. should: 'foo ^ super isBoolean'
  239. receiver: true
  240. return: false
  241. !
  242. testSuperSend2
  243. self
  244. should: 'foo ^ super isNil'
  245. receiver: nil
  246. return: false
  247. !
  248. testSuperSend3
  249. self
  250. should: 'doo ^ super isNil'
  251. class: Object
  252. receiver: nil
  253. return: false
  254. !
  255. testSuperSend4
  256. self
  257. should: 'foo ^ super asJavaScriptObject'
  258. receiver: 'me'
  259. return: #('m' 'e')
  260. !
  261. testSuperSend5
  262. self
  263. should: 'foo [super addLast: 4] on: Error do: [ self add: 5 ]. ^ self'
  264. class: SequenceableCollection
  265. receiver: #(1 2 3)
  266. return: #(1 2 3 5)
  267. !
  268. testTempVariables
  269. self should: 'foo | a | ^ a' return: nil.
  270. self should: 'foo | AVariable | ^ AVariable' return: nil.
  271. self should: 'foo | a b c | ^ c' return: nil.
  272. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  273. self should: 'foo | a | a:= 1. ^ a' return: 1.
  274. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  275. !
  276. testThisContext
  277. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  278. !
  279. testUnknownPragma
  280. self should: 'foo < fooBar: ''return 2+3'' > | x | ^ x := 6' return: 6.
  281. self should: 'foo | x | < fooBar: ''return 2+3'' > ^ x := 6' return: 6
  282. !
  283. testifFalse
  284. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  285. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  286. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  287. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  288. !
  289. testifFalseIfTrue
  290. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  291. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  292. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  293. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  294. !
  295. testifNil
  296. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  297. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  298. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  299. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  300. !
  301. testifNilIfNotNil
  302. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  303. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  304. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  305. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  306. !
  307. testifNotNil
  308. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  309. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  310. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  311. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  312. !
  313. testifNotNilWithArgument
  314. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  315. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  316. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  317. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  318. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  319. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  320. !
  321. testifTrue
  322. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  323. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  324. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  325. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  326. !
  327. testifTrueIfFalse
  328. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  329. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  330. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  331. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  332. ! !
  333. !AbstractCompilerTest class methodsFor: 'testing'!
  334. isAbstract
  335. ^ self name = AbstractCompilerTest name
  336. ! !
  337. AbstractCompilerTest subclass: #ASTDebuggerTest
  338. slots: {}
  339. package: 'Compiler-Tests'!
  340. AbstractCompilerTest subclass: #ASTInterpreterTest
  341. slots: {}
  342. package: 'Compiler-Tests'!
  343. AbstractCompilerTest subclass: #CodeGeneratorTest
  344. slots: {}
  345. package: 'Compiler-Tests'!
  346. AbstractCompilerTest subclass: #InliningCodeGeneratorTest
  347. slots: {}
  348. package: 'Compiler-Tests'!
  349. TestCase subclass: #ASTPCNodeVisitorTest
  350. slots: {}
  351. package: 'Compiler-Tests'!
  352. !ASTPCNodeVisitorTest methodsFor: 'factory'!
  353. astPCNodeVisitor
  354. ^ ASTPCNodeVisitor new
  355. index: 0;
  356. yourself
  357. !
  358. astPCNodeVisitorForSelector: aString
  359. ^ ASTPCNodeVisitor new
  360. selector: aString;
  361. index: 0;
  362. yourself
  363. ! !
  364. !ASTPCNodeVisitorTest methodsFor: 'tests'!
  365. testJSStatementNode
  366. | ast visitor |
  367. ast := self parse: 'foo <inlineJS: ''consolee.log(1)''>' forClass: Object.
  368. self assert: (self astPCNodeVisitor
  369. visit: ast;
  370. currentNode) isJSStatementNode
  371. !
  372. testMessageSend
  373. | ast |
  374. ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
  375. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  376. visit: ast;
  377. currentNode) selector equals: 'yourself'
  378. !
  379. testMessageSendWithBlocks
  380. | ast |
  381. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  382. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  383. visit: ast;
  384. currentNode) selector equals: 'yourself'
  385. !
  386. testMessageSendWithInlining
  387. | ast |
  388. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  389. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  390. visit: ast;
  391. currentNode) selector equals: 'yourself'.
  392. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  393. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  394. visit: ast;
  395. currentNode) selector equals: 'asBoolean'
  396. !
  397. testNoMessageSend
  398. | ast |
  399. ast := self parse: 'foo ^ self' forClass: Object.
  400. self assert: (self astPCNodeVisitor
  401. visit: ast;
  402. currentNode) isNil
  403. ! !
  404. TestCase subclass: #ASTPositionTest
  405. slots: {}
  406. package: 'Compiler-Tests'!
  407. !ASTPositionTest methodsFor: 'tests'!
  408. testNodeAtPosition
  409. | node |
  410. node := self parse: 'yourself
  411. ^ self' forClass: Object.
  412. self assert: (node navigationNodeAt: 2@4 ifAbsent: [ nil ]) source equals: 'self'.
  413. node := self parse: 'foo
  414. true ifTrue: [ 1 ]' forClass: Object.
  415. self assert: (node navigationNodeAt: 2@7 ifAbsent: [ nil ]) selector equals: 'ifTrue:'.
  416. node := self parse: 'foo
  417. self foo; bar; baz' forClass: Object.
  418. self assert: (node navigationNodeAt: 2@8 ifAbsent: [ nil ]) selector equals: 'foo'
  419. ! !
  420. TestCase subclass: #AbstractCodeGeneratorInstallTest
  421. slots: {#receiver}
  422. package: 'Compiler-Tests'!
  423. !AbstractCodeGeneratorInstallTest methodsFor: 'accessing'!
  424. receiver
  425. ^ receiver
  426. ! !
  427. !AbstractCodeGeneratorInstallTest methodsFor: 'testing'!
  428. shouldntInstall: aString
  429. | method |
  430. [ self
  431. should: [ method := self install: aString forClass: receiver class ]
  432. raise: ParseError ]
  433. ensure: [ method ifNotNil: [ receiver class removeCompiledMethod: method ] ]
  434. ! !
  435. !AbstractCodeGeneratorInstallTest methodsFor: 'tests'!
  436. testMistypedPragmaJSStatement
  437. self shouldntInstall: 'foo < inlineJS: ''return ''foo'''' >'
  438. !
  439. testNiladicJSOverride
  440. receiver := ObjectMock new.
  441. receiver foo: 4.
  442. self while: 'baz <jsOverride: #baz> ^ (foo := foo + 3)' should: [
  443. self assert: receiver baz equals: 7.
  444. self assert: (receiver basicPerform: #baz) equals: 10.
  445. self assert: receiver baz equals: 13.
  446. self assert: receiver foo equals: 13 ]
  447. !
  448. testNiladicJSOverrideDifferentNames
  449. receiver := ObjectMock new.
  450. receiver foo: 4.
  451. self while: 'quux <jsOverride: #mux> ^ (foo := foo + 3)' should: [
  452. self should: [ receiver mux ] raise: MessageNotUnderstood.
  453. self assert: (receiver basicPerform: #mux) equals: 7.
  454. self assert: receiver quux equals: 10.
  455. self should: [ receiver basicPerform: #quux ] raise: Error.
  456. self assert: receiver foo equals: 10 ]
  457. !
  458. testPragmaInBlock
  459. self shouldntInstall: 'foo ^ [ < fooBar > 4 ] value'
  460. ! !
  461. !AbstractCodeGeneratorInstallTest class methodsFor: 'testing'!
  462. isAbstract
  463. ^ self name = AbstractCodeGeneratorInstallTest name
  464. ! !
  465. AbstractCodeGeneratorInstallTest subclass: #CodeGeneratorInstallTest
  466. slots: {}
  467. package: 'Compiler-Tests'!
  468. AbstractCodeGeneratorInstallTest subclass: #InliningCodeGeneratorInstallTest
  469. slots: {}
  470. package: 'Compiler-Tests'!
  471. TestCase subclass: #ScopeVarTest
  472. slots: {}
  473. package: 'Compiler-Tests'!
  474. !ScopeVarTest methodsFor: 'tests'!
  475. testClassRefVar
  476. | node |
  477. node := VariableNode new
  478. value: 'Object';
  479. yourself.
  480. SemanticAnalyzer new
  481. pushScope: MethodLexicalScope new;
  482. visit: node.
  483. self assert: node binding isClassRefVar
  484. !
  485. testInstanceVar
  486. | node scope |
  487. node := VariableNode new
  488. value: 'bzzz';
  489. yourself.
  490. scope := MethodLexicalScope new.
  491. scope addIVar: 'bzzz'.
  492. self assert: (scope bindingFor: node) isInstanceVar
  493. !
  494. testPseudoVar
  495. | node pseudoVars |
  496. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  497. pseudoVars do: [:each |
  498. node := VariableNode new
  499. value: each;
  500. yourself.
  501. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar]
  502. !
  503. testTempVar
  504. | node scope |
  505. node := VariableNode new
  506. value: 'bzzz';
  507. yourself.
  508. scope := MethodLexicalScope new.
  509. scope addTemp: 'bzzz'.
  510. self assert: (scope bindingFor: node) isTempVar
  511. !
  512. testUnknownVar
  513. | node |
  514. node := VariableNode new
  515. value: 'bzzz';
  516. yourself.
  517. self assert: (MethodLexicalScope new bindingFor: node) isNil
  518. ! !
  519. TestCase subclass: #SemanticAnalyzerTest
  520. slots: {#analyzer}
  521. package: 'Compiler-Tests'!
  522. !SemanticAnalyzerTest methodsFor: 'running'!
  523. setUp
  524. analyzer := SemanticAnalyzer on: Object
  525. ! !
  526. !SemanticAnalyzerTest methodsFor: 'tests'!
  527. testAssignment
  528. | src ast |
  529. src := 'foo self := 1'.
  530. ast := Smalltalk parse: src.
  531. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  532. !
  533. testNonLocalReturn
  534. | src ast |
  535. src := 'foo | a | a + 1. ^ a'.
  536. ast := Smalltalk parse: src.
  537. analyzer visit: ast.
  538. self deny: ast scope hasNonLocalReturn
  539. !
  540. testNonLocalReturn2
  541. | src ast |
  542. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  543. ast := Smalltalk parse: src.
  544. analyzer visit: ast.
  545. self assert: ast scope hasNonLocalReturn
  546. !
  547. testScope
  548. | src ast |
  549. src := 'foo | a | a + 1. [ | b | b := a ]'.
  550. ast := Smalltalk parse: src.
  551. analyzer visit: ast.
  552. self deny: ast dagChildren first dagChildren last scope == ast scope.
  553. !
  554. testScope2
  555. | src ast |
  556. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  557. ast := Smalltalk parse: src.
  558. analyzer visit: ast.
  559. self deny: ast dagChildren first dagChildren last dagChildren first dagChildren first scope == ast scope.
  560. !
  561. testScopeLevel
  562. | src ast |
  563. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  564. ast := Smalltalk parse: src.
  565. analyzer visit: ast.
  566. self assert: ast scope scopeLevel equals: 1.
  567. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first scope scopeLevel equals: 3
  568. !
  569. testUnknownVariables
  570. | src ast |
  571. src := 'foo | a | b + a'.
  572. ast := Smalltalk parse: src.
  573. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  574. !
  575. testUnknownVariablesWithScope
  576. | src ast |
  577. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  578. ast := Smalltalk parse: src.
  579. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  580. !
  581. testVariableShadowing
  582. | src ast |
  583. src := 'foo | a | a + 1'.
  584. ast := Smalltalk parse: src.
  585. analyzer visit: ast
  586. !
  587. testVariableShadowing2
  588. | src ast |
  589. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  590. ast := Smalltalk parse: src.
  591. self should: [analyzer visit: ast] raise: ShadowingVariableError
  592. !
  593. testVariableShadowing3
  594. | src ast |
  595. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  596. ast := Smalltalk parse: src.
  597. analyzer visit: ast
  598. !
  599. testVariableShadowing4
  600. | src ast |
  601. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  602. ast := Smalltalk parse: src.
  603. analyzer visit: ast
  604. !
  605. testVariableShadowing5
  606. | src ast |
  607. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  608. ast := Smalltalk parse: src.
  609. self should: [analyzer visit: ast] raise: ShadowingVariableError
  610. !
  611. testVariablesLookup
  612. | src ast |
  613. src := 'foo | a | a + 1. [ | b | b := a ]'.
  614. ast := Smalltalk parse: src.
  615. analyzer visit: ast.
  616. "Binding for `a` in the message send"
  617. self assert: ast dagChildren first dagChildren first receiver binding isTempVar.
  618. self assert: ast dagChildren first dagChildren first receiver binding scope == ast scope.
  619. "Binding for `b`"
  620. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding isTempVar.
  621. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding scope == ast dagChildren first dagChildren last scope.
  622. ! !
  623. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  624. slots: {}
  625. package: 'Compiler-Tests'!
  626. !AISemanticAnalyzerTest methodsFor: 'running'!
  627. setUp
  628. analyzer := (AISemanticAnalyzer on: Object)
  629. context: (AIContext new
  630. defineLocal: 'local';
  631. localAt: 'local' put: 3;
  632. yourself);
  633. yourself
  634. ! !
  635. !AISemanticAnalyzerTest methodsFor: 'tests'!
  636. testContextVariables
  637. | src ast |
  638. src := 'foo | a | local + a'.
  639. ast := Smalltalk parse: src.
  640. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  641. ! !
  642. Trait named: #TASTCompilingTest
  643. package: 'Compiler-Tests'!
  644. !TASTCompilingTest methodsFor: 'accessing'!
  645. codeGeneratorClass
  646. self subclassResponsibility
  647. ! !
  648. !TASTCompilingTest methodsFor: 'compiling'!
  649. install: aString forClass: aClass
  650. ^ self compiler
  651. install: aString
  652. forClass: aClass
  653. protocol: 'tests'
  654. ! !
  655. !TASTCompilingTest methodsFor: 'factory'!
  656. compiler
  657. ^ Compiler new
  658. codeGeneratorClass: self codeGeneratorClass;
  659. yourself
  660. ! !
  661. !TASTCompilingTest methodsFor: 'testing'!
  662. while: aString inClass: aClass should: aBlock
  663. | method |
  664. [
  665. method := self install: aString forClass: aClass.
  666. aBlock value: method ]
  667. ensure: [ method ifNotNil: [ aClass removeCompiledMethod: method ] ]
  668. !
  669. while: aString should: aBlock
  670. self while: aString inClass: self receiver class should: aBlock
  671. ! !
  672. Trait named: #TASTParsingTest
  673. package: 'Compiler-Tests'!
  674. !TASTParsingTest methodsFor: 'parsing'!
  675. parse: aString forClass: aClass
  676. ^ Compiler new
  677. ast: aString
  678. forClass: aClass
  679. protocol: 'test'
  680. ! !
  681. Trait named: #TCTDebugged
  682. package: 'Compiler-Tests'!
  683. !TCTDebugged methodsFor: 'private'!
  684. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  685. "The food is a methodNode. Interpret the sequenceNode only"
  686. | ctx |
  687. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  688. ^ (ASTDebugger context: ctx) proceed; result
  689. ! !
  690. Trait named: #TCTExecuted
  691. package: 'Compiler-Tests'!
  692. !TCTExecuted methodsFor: 'testing'!
  693. while: aString inClass: aClass should: aBlock
  694. super
  695. while: aString
  696. inClass: aClass
  697. should: [ :method | aBlock value: [
  698. self receiver perform: method selector ] ]
  699. ! !
  700. Trait named: #TCTInlined
  701. package: 'Compiler-Tests'!
  702. !TCTInlined methodsFor: 'accessing'!
  703. codeGeneratorClass
  704. ^ InliningCodeGenerator
  705. ! !
  706. Trait named: #TCTInterpreted
  707. package: 'Compiler-Tests'!
  708. !TCTInterpreted methodsFor: 'private'!
  709. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  710. "The food is a methodNode. Interpret the sequenceNode only"
  711. | ctx |
  712. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  713. ^ ctx interpreter proceed; result
  714. !
  715. prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary
  716. "The food is a methodNode. Interpret the sequenceNode only"
  717. | ctx ast |
  718. ast := self parse: aString forClass: aClass.
  719. ctx := AIContext new
  720. receiver: anObject;
  721. selector: ast selector;
  722. interpreter: ASTInterpreter new;
  723. yourself.
  724. "Define locals for the context"
  725. ast sequenceNode ifNotNil: [ :sequence |
  726. sequence temps do: [ :each |
  727. ctx defineLocal: each ] ].
  728. aDictionary keysAndValuesDo: [ :key :value |
  729. ctx localAt: key put: value ].
  730. ctx interpreter
  731. context: ctx;
  732. node: ast;
  733. enterNode.
  734. ^ctx
  735. ! !
  736. !TCTInterpreted methodsFor: 'testing'!
  737. while: aString inClass: aClass should: aBlock
  738. super
  739. while: aString
  740. inClass: aClass
  741. should: [ aBlock value: [
  742. self
  743. interpret: aString
  744. forClass: aClass
  745. receiver: self receiver
  746. withArguments: #{} ] ]
  747. ! !
  748. Trait named: #TCTNonInlined
  749. package: 'Compiler-Tests'!
  750. !TCTNonInlined methodsFor: 'accessing'!
  751. codeGeneratorClass
  752. ^ CodeGenerator
  753. ! !
  754. TASTCompilingTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  755. TCTDebugged setTraitComposition: {TCTInterpreted} asTraitComposition!
  756. ASTMethodRunningTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  757. ASTDebuggerTest setTraitComposition: {TCTNonInlined. TCTDebugged} asTraitComposition!
  758. ASTInterpreterTest setTraitComposition: {TCTNonInlined. TCTInterpreted} asTraitComposition!
  759. CodeGeneratorTest setTraitComposition: {TCTNonInlined. TCTExecuted} asTraitComposition!
  760. InliningCodeGeneratorTest setTraitComposition: {TCTInlined. TCTExecuted} asTraitComposition!
  761. ASTPCNodeVisitorTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  762. ASTPositionTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  763. AbstractCodeGeneratorInstallTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  764. CodeGeneratorInstallTest setTraitComposition: {TCTNonInlined} asTraitComposition!
  765. InliningCodeGeneratorInstallTest setTraitComposition: {TCTInlined} asTraitComposition!
  766. ! !