Compiler-Tests.st 22 KB

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