Compiler-Tests.st 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. while: aString inClass: aClass should: aBlock
  33. self subclassResponsibility
  34. ! !
  35. ASTMethodRunningTest subclass: #CodeGeneratorTest
  36. slots: {}
  37. package: 'Compiler-Tests'!
  38. !CodeGeneratorTest methodsFor: 'accessing'!
  39. codeGeneratorClass
  40. ^ CodeGenerator
  41. ! !
  42. !CodeGeneratorTest methodsFor: 'testing'!
  43. while: aString inClass: aClass should: aBlock
  44. self
  45. whileInstalled: aString
  46. inClass: aClass
  47. should: [ :method | aBlock value: [
  48. receiver perform: method selector ] ]
  49. ! !
  50. !CodeGeneratorTest methodsFor: 'tests'!
  51. testAssignment
  52. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  53. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  54. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  55. !
  56. testBackslashSelectors
  57. self should: '\ arg ^ 4' return: 4.
  58. self should: '\\ arg ^ 42' return: 42
  59. !
  60. testBlockReturn
  61. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  62. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  63. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  64. !
  65. testCascades
  66. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  67. !
  68. testCascadesInDynamicArray
  69. self should: 'foo | x | x := 1. ^ {x. [x:=2] value; in: [x]}' return: #(1 2)
  70. !
  71. testCascadesInDynamicDictioary
  72. self should: 'foo | x | x := 1. ^ #{''one'' -> x. ''two'' -> ([x:=2] value; in: [x])}' return: #{'one' -> 1. 'two' -> 2}
  73. !
  74. testCascadesInSend
  75. self should: 'foo | x | x := 1. ^ Array with: x with: ([x:=2] value; in: [x])' return: #(1 2)
  76. !
  77. testCascadesWithInlining
  78. self should: 'foo ^ true class; ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 1.
  79. self should: 'foo ^ false class; ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 2
  80. !
  81. testDynamicArrayElementsOrdered
  82. self should: 'foo
  83. | x |
  84. x := 1.
  85. ^ { x. x := 2 }
  86. ' return: #(1 2).
  87. self should: 'foo
  88. | x |
  89. x := 1.
  90. ^ { x. true ifTrue: [ x := 2 ] }
  91. ' return: #(1 2).
  92. !
  93. testDynamicDictionaryElementsOrdered
  94. self should: 'foo
  95. | x |
  96. x := ''foo''.
  97. ^ #{ x->1. ''bar''->(true ifTrue: [ 2 ]) }
  98. ' return: #{'foo'->1. 'bar'->2}.
  99. !
  100. testDynamicDictionaryWithMoreArrows
  101. self should: 'foo ^ #{1->2->3}' return: (HashedCollection with: 1->2->3)
  102. !
  103. testGlobalVar
  104. self should: 'foo ^ eval class' return: BlockClosure.
  105. self should: 'foo ^ Math cos: 0' return: 1.
  106. self should: 'foo ^ NonExistingVar' return: nil
  107. !
  108. testInnerTemporalDependentElementsOrdered
  109. self should: 'foo
  110. | x |
  111. x := Array.
  112. ^ x with: ''foo''->x with: ''bar''->(x := 2)
  113. ' return: {'foo'->Array. 'bar'->2}.
  114. self should: 'foo
  115. | x |
  116. x := Array.
  117. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  118. ' return: {'foo'->Array. 'bar'->2}.
  119. self should: 'foo
  120. | x |
  121. x := 1.
  122. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  123. ' return: {'foo'->1. 'bar'->2}.
  124. self should: 'foo
  125. | x |
  126. x := 1.
  127. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  128. ' return: {'foo'->1. 'bar'->2}.
  129. self should: 'foo
  130. | x |
  131. x := 1.
  132. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  133. ' return: #{'foo'->1. 'bar'->2}.
  134. !
  135. testLexicalScope
  136. self should: 'foo | a | a := 1. [ a := 2 ] value. ^ a' return: 2
  137. !
  138. testLiterals
  139. self should: 'foo ^ 1' return: 1.
  140. self should: 'foo ^ ''hello''' return: 'hello'.
  141. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  142. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  143. self should: 'foo ^ true' return: true.
  144. self should: 'foo ^ false' return: false.
  145. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  146. self should: 'foo ^ #hello' return: #hello.
  147. self should: 'foo ^ $h' return: 'h'.
  148. self should: 'foo ^ -123.456' return: -123.456.
  149. self should: 'foo ^ -2.5e4' return: -25000.
  150. !
  151. testLocalReturn
  152. self should: 'foo ^ 1' return: 1.
  153. self should: 'foo ^ 1 + 1' return: 2.
  154. self should: 'foo ' return: receiver.
  155. self should: 'foo self asString' return: receiver.
  156. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  157. !
  158. testMessageSends
  159. self should: 'foo ^ 1 asString' return: '1'.
  160. self should: 'foo ^ 1 + 1' return: 2.
  161. self should: 'foo ^ 1 + 2 * 3' return: 9.
  162. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  163. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  164. !
  165. testMultipleSequences
  166. self should: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6' return: 30
  167. !
  168. testMutableLiterals
  169. "Mutable literals must be aliased in cascades.
  170. See https://lolg.it/amber/amber/issues/428"
  171. self
  172. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  173. return: #(3 2)
  174. !
  175. testNestedIfTrue
  176. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  177. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  178. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  179. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  180. !
  181. testNestedSends
  182. self should: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString' return: (Point x: (2@3) y: 4) asString
  183. !
  184. testNilPerform
  185. self should: 'foo ^ nil perform: #yourself' return: nil
  186. !
  187. testNonLocalReturn
  188. self should: 'foo [ ^ 1 ] value' return: 1.
  189. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  190. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  191. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  192. !
  193. testPascalCaseGlobal
  194. self should: 'foo ^Object' return: (Smalltalk globals at: 'Object').
  195. self should: 'foo ^NonExistent' return: nil
  196. !
  197. testPragmaJSStatement
  198. self should: 'foo < inlineJS: ''return 2+3'' >' return: 5
  199. !
  200. testRootSuperSend
  201. self
  202. should: 'foo ^ super class'
  203. receiver: ProtoObject new
  204. raise: MessageNotUnderstood
  205. !
  206. testSendReceiverAndArgumentsOrdered
  207. self should: 'foo
  208. | x |
  209. x := 1.
  210. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  211. ' return: #(1 2).
  212. self should: 'foo
  213. | x |
  214. x := Array.
  215. ^ x with: x with: (true ifTrue: [ x := 2 ])
  216. ' return: {Array. 2}.
  217. !
  218. testSuperSend
  219. self
  220. should: 'foo ^ super isBoolean'
  221. receiver: true
  222. return: false
  223. !
  224. testSuperSend2
  225. self
  226. should: 'foo ^ super isNil'
  227. receiver: nil
  228. return: false
  229. !
  230. testSuperSend3
  231. self
  232. should: 'doo ^ super isNil'
  233. class: Object
  234. receiver: nil
  235. return: false
  236. !
  237. testSuperSend4
  238. self
  239. should: 'foo ^ super asJavaScriptObject'
  240. receiver: 'me'
  241. return: #('m' 'e')
  242. !
  243. testSuperSend5
  244. self
  245. should: 'foo [super addLast: 4] on: Error do: [ self add: 5 ]. ^ self'
  246. class: SequenceableCollection
  247. receiver: #(1 2 3)
  248. return: #(1 2 3 5)
  249. !
  250. testTempVariables
  251. self should: 'foo | a | ^ a' return: nil.
  252. self should: 'foo | AVariable | ^ AVariable' return: nil.
  253. self should: 'foo | a b c | ^ c' return: nil.
  254. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  255. self should: 'foo | a | a:= 1. ^ a' return: 1.
  256. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  257. !
  258. testThisContext
  259. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  260. !
  261. testUnknownPragma
  262. self should: 'foo < fooBar: ''return 2+3'' > | x | ^ x := 6' return: 6.
  263. self should: 'foo | x | < fooBar: ''return 2+3'' > ^ x := 6' return: 6
  264. !
  265. testifFalse
  266. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  267. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  268. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  269. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  270. !
  271. testifFalseIfTrue
  272. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  273. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  274. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  275. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  276. !
  277. testifNil
  278. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  279. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  280. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  281. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  282. !
  283. testifNilIfNotNil
  284. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  285. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  286. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  287. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  288. !
  289. testifNotNil
  290. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  291. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  292. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  293. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  294. !
  295. testifNotNilWithArgument
  296. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  297. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  298. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  299. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  300. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  301. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  302. !
  303. testifTrue
  304. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  305. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  306. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  307. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  308. !
  309. testifTrueIfFalse
  310. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  311. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  312. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  313. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  314. ! !
  315. CodeGeneratorTest subclass: #ASTInterpreterTest
  316. slots: {}
  317. package: 'Compiler-Tests'!
  318. !ASTInterpreterTest methodsFor: 'private'!
  319. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  320. "The food is a methodNode. Interpret the sequenceNode only"
  321. | ctx |
  322. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  323. ^ ctx interpreter proceed; result
  324. !
  325. prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary
  326. "The food is a methodNode. Interpret the sequenceNode only"
  327. | ctx ast |
  328. ast := self parse: aString forClass: aClass.
  329. ctx := AIContext new
  330. receiver: anObject;
  331. selector: ast selector;
  332. interpreter: ASTInterpreter new;
  333. yourself.
  334. "Define locals for the context"
  335. ast sequenceNode ifNotNil: [ :sequence |
  336. sequence temps do: [ :each |
  337. ctx defineLocal: each ] ].
  338. aDictionary keysAndValuesDo: [ :key :value |
  339. ctx localAt: key put: value ].
  340. ctx interpreter
  341. context: ctx;
  342. node: ast;
  343. enterNode.
  344. ^ctx
  345. ! !
  346. !ASTInterpreterTest methodsFor: 'testing'!
  347. while: aString inClass: aClass should: aBlock
  348. self
  349. whileInstalled: aString
  350. inClass: aClass
  351. should: [ aBlock value: [
  352. self
  353. interpret: aString
  354. forClass: aClass
  355. receiver: receiver
  356. withArguments: #{} ] ]
  357. ! !
  358. ASTInterpreterTest subclass: #ASTDebuggerTest
  359. slots: {}
  360. package: 'Compiler-Tests'!
  361. !ASTDebuggerTest methodsFor: 'private'!
  362. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  363. "The food is a methodNode. Interpret the sequenceNode only"
  364. | ctx |
  365. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  366. ^ (ASTDebugger context: ctx) proceed; result
  367. ! !
  368. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  369. slots: {}
  370. package: 'Compiler-Tests'!
  371. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  372. codeGeneratorClass
  373. ^ InliningCodeGenerator
  374. ! !
  375. TestCase subclass: #ASTPCNodeVisitorTest
  376. slots: {}
  377. package: 'Compiler-Tests'!
  378. !ASTPCNodeVisitorTest methodsFor: 'factory'!
  379. astPCNodeVisitor
  380. ^ ASTPCNodeVisitor new
  381. index: 0;
  382. yourself
  383. !
  384. astPCNodeVisitorForSelector: aString
  385. ^ ASTPCNodeVisitor new
  386. selector: aString;
  387. index: 0;
  388. yourself
  389. ! !
  390. !ASTPCNodeVisitorTest methodsFor: 'tests'!
  391. testJSStatementNode
  392. | ast visitor |
  393. ast := self parse: 'foo <inlineJS: ''consolee.log(1)''>' forClass: Object.
  394. self assert: (self astPCNodeVisitor
  395. visit: ast;
  396. currentNode) isJSStatementNode
  397. !
  398. testMessageSend
  399. | ast |
  400. ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
  401. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  402. visit: ast;
  403. currentNode) selector equals: 'yourself'
  404. !
  405. testMessageSendWithBlocks
  406. | ast |
  407. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  408. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  409. visit: ast;
  410. currentNode) selector equals: 'yourself'
  411. !
  412. testMessageSendWithInlining
  413. | ast |
  414. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  415. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  416. visit: ast;
  417. currentNode) selector equals: 'yourself'.
  418. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  419. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  420. visit: ast;
  421. currentNode) selector equals: 'asBoolean'
  422. !
  423. testNoMessageSend
  424. | ast |
  425. ast := self parse: 'foo ^ self' forClass: Object.
  426. self assert: (self astPCNodeVisitor
  427. visit: ast;
  428. currentNode) isNil
  429. ! !
  430. TestCase subclass: #ASTPositionTest
  431. slots: {}
  432. package: 'Compiler-Tests'!
  433. !ASTPositionTest methodsFor: 'tests'!
  434. testNodeAtPosition
  435. | node |
  436. node := self parse: 'yourself
  437. ^ self' forClass: Object.
  438. self assert: (node navigationNodeAt: 2@4 ifAbsent: [ nil ]) source equals: 'self'.
  439. node := self parse: 'foo
  440. true ifTrue: [ 1 ]' forClass: Object.
  441. self assert: (node navigationNodeAt: 2@7 ifAbsent: [ nil ]) selector equals: 'ifTrue:'.
  442. node := self parse: 'foo
  443. self foo; bar; baz' forClass: Object.
  444. self assert: (node navigationNodeAt: 2@8 ifAbsent: [ nil ]) selector equals: 'foo'
  445. ! !
  446. TestCase subclass: #CodeGeneratorInstallTest
  447. slots: {#receiver}
  448. package: 'Compiler-Tests'!
  449. !CodeGeneratorInstallTest methodsFor: 'accessing'!
  450. codeGeneratorClass
  451. ^ CodeGenerator
  452. !
  453. receiver
  454. ^ receiver
  455. ! !
  456. !CodeGeneratorInstallTest methodsFor: 'testing'!
  457. shouldntInstall: aString
  458. | method |
  459. [ self
  460. should: [ method := self install: aString forClass: receiver class ]
  461. raise: ParseError ]
  462. ensure: [ method ifNotNil: [ receiver class removeCompiledMethod: method ] ]
  463. ! !
  464. !CodeGeneratorInstallTest methodsFor: 'tests'!
  465. testMistypedPragmaJSStatement
  466. self shouldntInstall: 'foo < inlineJS: ''return ''foo'''' >'
  467. !
  468. testNiladicJSOverride
  469. receiver := ObjectMock new.
  470. receiver foo: 4.
  471. self while: 'baz <jsOverride: #baz> ^ (foo := foo + 3)' should: [
  472. self assert: receiver baz equals: 7.
  473. self assert: (receiver basicPerform: #baz) equals: 10.
  474. self assert: receiver baz equals: 13.
  475. self assert: receiver foo equals: 13 ]
  476. !
  477. testNiladicJSOverrideDifferentNames
  478. receiver := ObjectMock new.
  479. receiver foo: 4.
  480. self while: 'quux <jsOverride: #mux> ^ (foo := foo + 3)' should: [
  481. self should: [ receiver mux ] raise: MessageNotUnderstood.
  482. self assert: (receiver basicPerform: #mux) equals: 7.
  483. self assert: receiver quux equals: 10.
  484. self should: [ receiver basicPerform: #quux ] raise: Error.
  485. self assert: receiver foo equals: 10 ]
  486. !
  487. testPragmaInBlock
  488. self shouldntInstall: 'foo ^ [ < fooBar > 4 ] value'
  489. ! !
  490. CodeGeneratorInstallTest subclass: #InliningCodeGeneratorInstallTest
  491. slots: {}
  492. package: 'Compiler-Tests'!
  493. !InliningCodeGeneratorInstallTest methodsFor: 'accessing'!
  494. codeGeneratorClass
  495. ^ InliningCodeGenerator
  496. ! !
  497. TestCase subclass: #ScopeVarTest
  498. slots: {}
  499. package: 'Compiler-Tests'!
  500. !ScopeVarTest methodsFor: 'tests'!
  501. testClassRefVar
  502. | node |
  503. node := VariableNode new
  504. value: 'Object';
  505. yourself.
  506. SemanticAnalyzer new
  507. pushScope: MethodLexicalScope new;
  508. visit: node.
  509. self assert: node binding isClassRefVar
  510. !
  511. testInstanceVar
  512. | node scope |
  513. node := VariableNode new
  514. value: 'bzzz';
  515. yourself.
  516. scope := MethodLexicalScope new.
  517. scope addIVar: 'bzzz'.
  518. self assert: (scope bindingFor: node) isInstanceVar
  519. !
  520. testPseudoVar
  521. | node pseudoVars |
  522. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  523. pseudoVars do: [:each |
  524. node := VariableNode new
  525. value: each;
  526. yourself.
  527. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar]
  528. !
  529. testTempVar
  530. | node scope |
  531. node := VariableNode new
  532. value: 'bzzz';
  533. yourself.
  534. scope := MethodLexicalScope new.
  535. scope addTemp: 'bzzz'.
  536. self assert: (scope bindingFor: node) isTempVar
  537. !
  538. testUnknownVar
  539. | node |
  540. node := VariableNode new
  541. value: 'bzzz';
  542. yourself.
  543. self assert: (MethodLexicalScope new bindingFor: node) isNil
  544. ! !
  545. TestCase subclass: #SemanticAnalyzerTest
  546. slots: {#analyzer}
  547. package: 'Compiler-Tests'!
  548. !SemanticAnalyzerTest methodsFor: 'running'!
  549. setUp
  550. analyzer := SemanticAnalyzer on: Object
  551. ! !
  552. !SemanticAnalyzerTest methodsFor: 'tests'!
  553. testAssignment
  554. | src ast |
  555. src := 'foo self := 1'.
  556. ast := Smalltalk parse: src.
  557. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  558. !
  559. testNonLocalReturn
  560. | src ast |
  561. src := 'foo | a | a + 1. ^ a'.
  562. ast := Smalltalk parse: src.
  563. analyzer visit: ast.
  564. self deny: ast scope hasNonLocalReturn
  565. !
  566. testNonLocalReturn2
  567. | src ast |
  568. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  569. ast := Smalltalk parse: src.
  570. analyzer visit: ast.
  571. self assert: ast scope hasNonLocalReturn
  572. !
  573. testScope
  574. | src ast |
  575. src := 'foo | a | a + 1. [ | b | b := a ]'.
  576. ast := Smalltalk parse: src.
  577. analyzer visit: ast.
  578. self deny: ast dagChildren first dagChildren last scope == ast scope.
  579. !
  580. testScope2
  581. | src ast |
  582. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  583. ast := Smalltalk parse: src.
  584. analyzer visit: ast.
  585. self deny: ast dagChildren first dagChildren last dagChildren first dagChildren first scope == ast scope.
  586. !
  587. testScopeLevel
  588. | src ast |
  589. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  590. ast := Smalltalk parse: src.
  591. analyzer visit: ast.
  592. self assert: ast scope scopeLevel equals: 1.
  593. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first scope scopeLevel equals: 3
  594. !
  595. testUnknownVariables
  596. | src ast |
  597. src := 'foo | a | b + a'.
  598. ast := Smalltalk parse: src.
  599. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  600. !
  601. testUnknownVariablesWithScope
  602. | src ast |
  603. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  604. ast := Smalltalk parse: src.
  605. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  606. !
  607. testVariableShadowing
  608. | src ast |
  609. src := 'foo | a | a + 1'.
  610. ast := Smalltalk parse: src.
  611. analyzer visit: ast
  612. !
  613. testVariableShadowing2
  614. | src ast |
  615. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  616. ast := Smalltalk parse: src.
  617. self should: [analyzer visit: ast] raise: ShadowingVariableError
  618. !
  619. testVariableShadowing3
  620. | src ast |
  621. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  622. ast := Smalltalk parse: src.
  623. analyzer visit: ast
  624. !
  625. testVariableShadowing4
  626. | src ast |
  627. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  628. ast := Smalltalk parse: src.
  629. analyzer visit: ast
  630. !
  631. testVariableShadowing5
  632. | src ast |
  633. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  634. ast := Smalltalk parse: src.
  635. self should: [analyzer visit: ast] raise: ShadowingVariableError
  636. !
  637. testVariablesLookup
  638. | src ast |
  639. src := 'foo | a | a + 1. [ | b | b := a ]'.
  640. ast := Smalltalk parse: src.
  641. analyzer visit: ast.
  642. "Binding for `a` in the message send"
  643. self assert: ast dagChildren first dagChildren first receiver binding isTempVar.
  644. self assert: ast dagChildren first dagChildren first receiver binding scope == ast scope.
  645. "Binding for `b`"
  646. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding isTempVar.
  647. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding scope == ast dagChildren first dagChildren last scope.
  648. ! !
  649. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  650. slots: {}
  651. package: 'Compiler-Tests'!
  652. !AISemanticAnalyzerTest methodsFor: 'running'!
  653. setUp
  654. analyzer := (AISemanticAnalyzer on: Object)
  655. context: (AIContext new
  656. defineLocal: 'local';
  657. localAt: 'local' put: 3;
  658. yourself);
  659. yourself
  660. ! !
  661. !AISemanticAnalyzerTest methodsFor: 'tests'!
  662. testContextVariables
  663. | src ast |
  664. src := 'foo | a | local + a'.
  665. ast := Smalltalk parse: src.
  666. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  667. ! !
  668. Trait named: #TASTCompilingTest
  669. package: 'Compiler-Tests'!
  670. !TASTCompilingTest methodsFor: 'accessing'!
  671. codeGeneratorClass
  672. self subclassResponsibility
  673. ! !
  674. !TASTCompilingTest methodsFor: 'compiling'!
  675. install: aString forClass: aClass
  676. ^ self compiler
  677. install: aString
  678. forClass: aClass
  679. protocol: 'tests'
  680. ! !
  681. !TASTCompilingTest methodsFor: 'factory'!
  682. compiler
  683. ^ Compiler new
  684. codeGeneratorClass: self codeGeneratorClass;
  685. yourself
  686. ! !
  687. !TASTCompilingTest methodsFor: 'testing'!
  688. while: aString inClass: aClass should: aBlock
  689. | method |
  690. [
  691. method := self install: aString forClass: aClass.
  692. aBlock value: method ]
  693. ensure: [ method ifNotNil: [ aClass removeCompiledMethod: method ] ]
  694. !
  695. while: aString should: aBlock
  696. self while: aString inClass: self receiver class should: aBlock
  697. ! !
  698. Trait named: #TASTParsingTest
  699. package: 'Compiler-Tests'!
  700. !TASTParsingTest methodsFor: 'parsing'!
  701. parse: aString forClass: aClass
  702. ^ Compiler new
  703. ast: aString
  704. forClass: aClass
  705. protocol: 'test'
  706. ! !
  707. TASTCompilingTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  708. ASTMethodRunningTest setTraitComposition: {TASTCompilingTest @ {#whileInstalled:inClass:should: -> #while:inClass:should:} - #(while:inClass:should:)} asTraitComposition!
  709. ASTPCNodeVisitorTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  710. ASTPositionTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  711. CodeGeneratorInstallTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  712. ! !