Compiler-Tests.st 24 KB

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