Compiler-Tests.st 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. !ASTPCNodeVisitorTest methodsFor: 'tests'!
  381. testJSStatementNode
  382. | ast visitor |
  383. ast := self parse: 'foo <inlineJS: ''consolee.log(1)''>' forClass: Object.
  384. self assert: (self astPCNodeVisitor
  385. visit: ast;
  386. currentNode) isJSStatementNode
  387. !
  388. testMessageSend
  389. | ast |
  390. ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
  391. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  392. visit: ast;
  393. currentNode) selector equals: 'yourself'
  394. !
  395. testMessageSendWithBlocks
  396. | ast |
  397. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  398. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  399. visit: ast;
  400. currentNode) selector equals: 'yourself'
  401. !
  402. testMessageSendWithInlining
  403. | ast |
  404. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  405. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  406. visit: ast;
  407. currentNode) selector equals: 'yourself'.
  408. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  409. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  410. visit: ast;
  411. currentNode) selector equals: 'asBoolean'
  412. !
  413. testNoMessageSend
  414. | ast |
  415. ast := self parse: 'foo ^ self' forClass: Object.
  416. self assert: (self astPCNodeVisitor
  417. visit: ast;
  418. currentNode) isNil
  419. ! !
  420. TestCase subclass: #ASTPositionTest
  421. slots: {}
  422. package: 'Compiler-Tests'!
  423. !ASTPositionTest methodsFor: 'tests'!
  424. testNodeAtPosition
  425. | node |
  426. node := self parse: 'yourself
  427. ^ self' forClass: Object.
  428. self assert: (node navigationNodeAt: 2@4 ifAbsent: [ nil ]) source equals: 'self'.
  429. node := self parse: 'foo
  430. true ifTrue: [ 1 ]' forClass: Object.
  431. self assert: (node navigationNodeAt: 2@7 ifAbsent: [ nil ]) selector equals: 'ifTrue:'.
  432. node := self parse: 'foo
  433. self foo; bar; baz' forClass: Object.
  434. self assert: (node navigationNodeAt: 2@8 ifAbsent: [ nil ]) selector equals: 'foo'
  435. ! !
  436. TestCase subclass: #AbstractCodeGeneratorInstallTest
  437. slots: {#receiver}
  438. package: 'Compiler-Tests'!
  439. !AbstractCodeGeneratorInstallTest methodsFor: 'accessing'!
  440. receiver
  441. ^ receiver
  442. ! !
  443. !AbstractCodeGeneratorInstallTest methodsFor: 'testing'!
  444. shouldntInstall: aString
  445. | method |
  446. [ self
  447. should: [ method := self install: aString forClass: receiver class ]
  448. raise: ParseError ]
  449. ensure: [ method ifNotNil: [ receiver class removeCompiledMethod: method ] ]
  450. ! !
  451. !AbstractCodeGeneratorInstallTest methodsFor: 'tests'!
  452. testMistypedPragmaJSStatement
  453. self shouldntInstall: 'foo < inlineJS: ''return ''foo'''' >'
  454. !
  455. testNiladicJSOverride
  456. receiver := ObjectMock new.
  457. receiver foo: 4.
  458. self while: 'baz <jsOverride: #baz> ^ (foo := foo + 3)' should: [
  459. self assert: receiver baz equals: 7.
  460. self assert: (receiver basicPerform: #baz) equals: 10.
  461. self assert: receiver baz equals: 13.
  462. self assert: receiver foo equals: 13 ]
  463. !
  464. testNiladicJSOverrideDifferentNames
  465. receiver := ObjectMock new.
  466. receiver foo: 4.
  467. self while: 'quux <jsOverride: #mux> ^ (foo := foo + 3)' should: [
  468. self should: [ receiver mux ] raise: MessageNotUnderstood.
  469. self assert: (receiver basicPerform: #mux) equals: 7.
  470. self assert: receiver quux equals: 10.
  471. self should: [ receiver basicPerform: #quux ] raise: Error.
  472. self assert: receiver foo equals: 10 ]
  473. !
  474. testPragmaInBlock
  475. self shouldntInstall: 'foo ^ [ < fooBar > 4 ] value'
  476. ! !
  477. !AbstractCodeGeneratorInstallTest class methodsFor: 'testing'!
  478. isAbstract
  479. ^ self name = AbstractCodeGeneratorInstallTest name
  480. ! !
  481. AbstractCodeGeneratorInstallTest subclass: #CodeGeneratorInstallTest
  482. slots: {}
  483. package: 'Compiler-Tests'!
  484. AbstractCodeGeneratorInstallTest subclass: #InliningCodeGeneratorInstallTest
  485. slots: {}
  486. package: 'Compiler-Tests'!
  487. TestCase subclass: #ScopeVarTest
  488. slots: {}
  489. package: 'Compiler-Tests'!
  490. !ScopeVarTest methodsFor: 'tests'!
  491. testClassRefVar
  492. | node |
  493. node := VariableNode new
  494. value: 'Object';
  495. yourself.
  496. SemanticAnalyzer new
  497. pushScope: MethodLexicalScope new;
  498. visit: node.
  499. self assert: node binding isClassRefVar
  500. !
  501. testInstanceVar
  502. | node scope |
  503. node := VariableNode new
  504. value: 'bzzz';
  505. yourself.
  506. scope := MethodLexicalScope new.
  507. scope addIVar: 'bzzz'.
  508. self assert: (scope bindingFor: node) isInstanceVar
  509. !
  510. testPseudoVar
  511. | node pseudoVars |
  512. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  513. pseudoVars do: [:each |
  514. node := VariableNode new
  515. value: each;
  516. yourself.
  517. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar]
  518. !
  519. testTempVar
  520. | node scope |
  521. node := VariableNode new
  522. value: 'bzzz';
  523. yourself.
  524. scope := MethodLexicalScope new.
  525. scope addTemp: 'bzzz'.
  526. self assert: (scope bindingFor: node) isTempVar
  527. !
  528. testUnknownVar
  529. | node |
  530. node := VariableNode new
  531. value: 'bzzz';
  532. yourself.
  533. self assert: (MethodLexicalScope new bindingFor: node) isNil
  534. ! !
  535. TestCase subclass: #SemanticAnalyzerTest
  536. slots: {#analyzer}
  537. package: 'Compiler-Tests'!
  538. !SemanticAnalyzerTest methodsFor: 'running'!
  539. setUp
  540. analyzer := SemanticAnalyzer on: Object
  541. ! !
  542. !SemanticAnalyzerTest methodsFor: 'tests'!
  543. testAssignment
  544. | src ast |
  545. src := 'foo self := 1'.
  546. ast := Smalltalk parse: src.
  547. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  548. !
  549. testNonLocalReturn
  550. | src ast |
  551. src := 'foo | a | a + 1. ^ a'.
  552. ast := Smalltalk parse: src.
  553. analyzer visit: ast.
  554. self deny: ast scope hasNonLocalReturn
  555. !
  556. testNonLocalReturn2
  557. | src ast |
  558. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  559. ast := Smalltalk parse: src.
  560. analyzer visit: ast.
  561. self assert: ast scope hasNonLocalReturn
  562. !
  563. testScope
  564. | src ast |
  565. src := 'foo | a | a + 1. [ | b | b := a ]'.
  566. ast := Smalltalk parse: src.
  567. analyzer visit: ast.
  568. self deny: ast dagChildren first dagChildren last scope == ast scope.
  569. !
  570. testScope2
  571. | src ast |
  572. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  573. ast := Smalltalk parse: src.
  574. analyzer visit: ast.
  575. self deny: ast dagChildren first dagChildren last dagChildren first dagChildren first scope == ast scope.
  576. !
  577. testScopeLevel
  578. | src ast |
  579. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  580. ast := Smalltalk parse: src.
  581. analyzer visit: ast.
  582. self assert: ast scope scopeLevel equals: 1.
  583. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first scope scopeLevel equals: 3
  584. !
  585. testUnknownVariables
  586. | src ast |
  587. src := 'foo | a | b + a'.
  588. ast := Smalltalk parse: src.
  589. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  590. !
  591. testUnknownVariablesWithScope
  592. | src ast |
  593. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  594. ast := Smalltalk parse: src.
  595. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  596. !
  597. testVariableShadowing
  598. | src ast |
  599. src := 'foo | a | a + 1'.
  600. ast := Smalltalk parse: src.
  601. analyzer visit: ast
  602. !
  603. testVariableShadowing2
  604. | src ast |
  605. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  606. ast := Smalltalk parse: src.
  607. self should: [analyzer visit: ast] raise: ShadowingVariableError
  608. !
  609. testVariableShadowing3
  610. | src ast |
  611. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  612. ast := Smalltalk parse: src.
  613. analyzer visit: ast
  614. !
  615. testVariableShadowing4
  616. | src ast |
  617. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  618. ast := Smalltalk parse: src.
  619. analyzer visit: ast
  620. !
  621. testVariableShadowing5
  622. | src ast |
  623. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  624. ast := Smalltalk parse: src.
  625. self should: [analyzer visit: ast] raise: ShadowingVariableError
  626. !
  627. testVariablesLookup
  628. | src ast |
  629. src := 'foo | a | a + 1. [ | b | b := a ]'.
  630. ast := Smalltalk parse: src.
  631. analyzer visit: ast.
  632. "Binding for `a` in the message send"
  633. self assert: ast dagChildren first dagChildren first receiver binding isTempVar.
  634. self assert: ast dagChildren first dagChildren first receiver binding scope == ast scope.
  635. "Binding for `b`"
  636. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding isTempVar.
  637. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding scope == ast dagChildren first dagChildren last scope.
  638. ! !
  639. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  640. slots: {}
  641. package: 'Compiler-Tests'!
  642. !AISemanticAnalyzerTest methodsFor: 'running'!
  643. setUp
  644. analyzer := (AISemanticAnalyzer on: Object)
  645. context: (AIContext new
  646. defineLocal: 'local';
  647. localAt: 'local' put: 3;
  648. yourself);
  649. yourself
  650. ! !
  651. !AISemanticAnalyzerTest methodsFor: 'tests'!
  652. testContextVariables
  653. | src ast |
  654. src := 'foo | a | local + a'.
  655. ast := Smalltalk parse: src.
  656. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  657. ! !
  658. Trait named: #TASTCompilingTest
  659. package: 'Compiler-Tests'!
  660. !TASTCompilingTest methodsFor: 'accessing'!
  661. codeGeneratorClass
  662. self subclassResponsibility
  663. ! !
  664. !TASTCompilingTest methodsFor: 'compiling'!
  665. install: aString forClass: aClass
  666. ^ self compiler
  667. install: aString
  668. forClass: aClass
  669. protocol: 'tests'
  670. ! !
  671. !TASTCompilingTest methodsFor: 'factory'!
  672. compiler
  673. ^ Compiler new
  674. codeGeneratorClass: self codeGeneratorClass;
  675. yourself
  676. ! !
  677. !TASTCompilingTest methodsFor: 'testing'!
  678. while: aString inClass: aClass should: aBlock
  679. | method |
  680. [
  681. method := self install: aString forClass: aClass.
  682. aBlock value: method ]
  683. ensure: [ method ifNotNil: [ aClass removeCompiledMethod: method ] ]
  684. !
  685. while: aString should: aBlock
  686. self while: aString inClass: self receiver class should: aBlock
  687. ! !
  688. Trait named: #TASTParsingTest
  689. package: 'Compiler-Tests'!
  690. !TASTParsingTest methodsFor: 'parsing'!
  691. parse: aString forClass: aClass
  692. ^ Compiler new
  693. ast: aString
  694. forClass: aClass
  695. protocol: 'test'
  696. ! !
  697. Trait named: #TCTDebugged
  698. package: 'Compiler-Tests'!
  699. !TCTDebugged methodsFor: 'private'!
  700. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  701. "The food is a methodNode. Interpret the sequenceNode only"
  702. | ctx |
  703. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  704. ^ (ASTDebugger context: ctx) proceed; result
  705. ! !
  706. Trait named: #TCTExecuted
  707. package: 'Compiler-Tests'!
  708. !TCTExecuted methodsFor: 'testing'!
  709. while: aString inClass: aClass should: aBlock
  710. super
  711. while: aString
  712. inClass: aClass
  713. should: [ :method | aBlock value: [
  714. self receiver perform: method selector ] ]
  715. ! !
  716. Trait named: #TCTInlined
  717. package: 'Compiler-Tests'!
  718. !TCTInlined methodsFor: 'accessing'!
  719. codeGeneratorClass
  720. ^ InliningCodeGenerator
  721. ! !
  722. Trait named: #TCTInterpreted
  723. package: 'Compiler-Tests'!
  724. !TCTInterpreted methodsFor: 'private'!
  725. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  726. "The food is a methodNode. Interpret the sequenceNode only"
  727. | ctx |
  728. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  729. ^ ctx interpreter proceed; result
  730. !
  731. prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary
  732. "The food is a methodNode. Interpret the sequenceNode only"
  733. | ctx ast |
  734. ast := self parse: aString forClass: aClass.
  735. ctx := AIContext new
  736. receiver: anObject;
  737. selector: ast selector;
  738. interpreter: ASTInterpreter new;
  739. yourself.
  740. "Define locals for the context"
  741. ast sequenceNode ifNotNil: [ :sequence |
  742. sequence temps do: [ :each |
  743. ctx defineLocal: each ] ].
  744. aDictionary keysAndValuesDo: [ :key :value |
  745. ctx localAt: key put: value ].
  746. ctx interpreter
  747. context: ctx;
  748. node: ast;
  749. enterNode.
  750. ^ctx
  751. ! !
  752. !TCTInterpreted methodsFor: 'testing'!
  753. while: aString inClass: aClass should: aBlock
  754. super
  755. while: aString
  756. inClass: aClass
  757. should: [ aBlock value: [
  758. self
  759. interpret: aString
  760. forClass: aClass
  761. receiver: self receiver
  762. withArguments: #{} ] ]
  763. ! !
  764. Trait named: #TCTNonInlined
  765. package: 'Compiler-Tests'!
  766. !TCTNonInlined methodsFor: 'accessing'!
  767. codeGeneratorClass
  768. ^ CodeGenerator
  769. ! !
  770. TASTCompilingTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  771. TCTDebugged setTraitComposition: {TCTInterpreted} asTraitComposition!
  772. ASTMethodRunningTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  773. ASTDebuggerTest setTraitComposition: {TCTNonInlined. TCTDebugged} asTraitComposition!
  774. ASTInterpreterTest setTraitComposition: {TCTNonInlined. TCTInterpreted} asTraitComposition!
  775. CodeGeneratorTest setTraitComposition: {TCTNonInlined. TCTExecuted} asTraitComposition!
  776. InliningCodeGeneratorTest setTraitComposition: {TCTInlined. TCTExecuted} asTraitComposition!
  777. ASTPCNodeVisitorTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  778. ASTPositionTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  779. AbstractCodeGeneratorInstallTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  780. CodeGeneratorInstallTest setTraitComposition: {TCTNonInlined} asTraitComposition!
  781. InliningCodeGeneratorInstallTest setTraitComposition: {TCTInlined} asTraitComposition!
  782. ! !