Compiler-Tests.st 24 KB

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