Compiler-Tests.st 22 KB

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