Compiler-Tests.st 24 KB

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