Compiler-Tests.st 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. testPragmaInBlock
  434. self shouldntInstall: 'foo ^ [ < fooBar > 4 ] value'
  435. ! !
  436. CodeGeneratorInstallTest subclass: #InliningCodeGeneratorInstallTest
  437. slots: {}
  438. package: 'Compiler-Tests'!
  439. !InliningCodeGeneratorInstallTest methodsFor: 'accessing'!
  440. codeGeneratorClass
  441. ^ InliningCodeGenerator
  442. ! !
  443. ASTParsingTest subclass: #ASTPCNodeVisitorTest
  444. slots: {}
  445. package: 'Compiler-Tests'!
  446. !ASTPCNodeVisitorTest methodsFor: 'factory'!
  447. astPCNodeVisitor
  448. ^ ASTPCNodeVisitor new
  449. index: 0;
  450. yourself
  451. !
  452. astPCNodeVisitorForSelector: aString
  453. ^ ASTPCNodeVisitor new
  454. selector: aString;
  455. index: 0;
  456. yourself
  457. ! !
  458. !ASTPCNodeVisitorTest methodsFor: 'tests'!
  459. testJSStatementNode
  460. | ast visitor |
  461. ast := self parse: 'foo <inlineJS: ''consolee.log(1)''>' forClass: Object.
  462. self assert: (self astPCNodeVisitor
  463. visit: ast;
  464. currentNode) isJSStatementNode
  465. !
  466. testMessageSend
  467. | ast |
  468. ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
  469. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  470. visit: ast;
  471. currentNode) selector equals: 'yourself'
  472. !
  473. testMessageSendWithBlocks
  474. | ast |
  475. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  476. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  477. visit: ast;
  478. currentNode) selector equals: 'yourself'
  479. !
  480. testMessageSendWithInlining
  481. | ast |
  482. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  483. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  484. visit: ast;
  485. currentNode) selector equals: 'yourself'.
  486. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  487. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  488. visit: ast;
  489. currentNode) selector equals: 'asBoolean'
  490. !
  491. testNoMessageSend
  492. | ast |
  493. ast := self parse: 'foo ^ self' forClass: Object.
  494. self assert: (self astPCNodeVisitor
  495. visit: ast;
  496. currentNode) isNil
  497. ! !
  498. ASTParsingTest subclass: #ASTPositionTest
  499. slots: {}
  500. package: 'Compiler-Tests'!
  501. !ASTPositionTest methodsFor: 'tests'!
  502. testNodeAtPosition
  503. | node |
  504. node := self parse: 'yourself
  505. ^ self' forClass: Object.
  506. self assert: (node navigationNodeAt: 2@4 ifAbsent: [ nil ]) source equals: 'self'.
  507. node := self parse: 'foo
  508. true ifTrue: [ 1 ]' forClass: Object.
  509. self assert: (node navigationNodeAt: 2@7 ifAbsent: [ nil ]) selector equals: 'ifTrue:'.
  510. node := self parse: 'foo
  511. self foo; bar; baz' forClass: Object.
  512. self assert: (node navigationNodeAt: 2@8 ifAbsent: [ nil ]) selector equals: 'foo'
  513. ! !
  514. TestCase subclass: #ScopeVarTest
  515. slots: {}
  516. package: 'Compiler-Tests'!
  517. !ScopeVarTest methodsFor: 'tests'!
  518. testClassRefVar
  519. | node |
  520. node := VariableNode new
  521. value: 'Object';
  522. yourself.
  523. SemanticAnalyzer new
  524. pushScope: MethodLexicalScope new;
  525. visit: node.
  526. self assert: node binding isClassRefVar
  527. !
  528. testInstanceVar
  529. | node scope |
  530. node := VariableNode new
  531. value: 'bzzz';
  532. yourself.
  533. scope := MethodLexicalScope new.
  534. scope addIVar: 'bzzz'.
  535. self assert: (scope bindingFor: node) isInstanceVar
  536. !
  537. testPseudoVar
  538. | node pseudoVars |
  539. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  540. pseudoVars do: [:each |
  541. node := VariableNode new
  542. value: each;
  543. yourself.
  544. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar]
  545. !
  546. testTempVar
  547. | node scope |
  548. node := VariableNode new
  549. value: 'bzzz';
  550. yourself.
  551. scope := MethodLexicalScope new.
  552. scope addTemp: 'bzzz'.
  553. self assert: (scope bindingFor: node) isTempVar
  554. !
  555. testUnknownVar
  556. | node |
  557. node := VariableNode new
  558. value: 'bzzz';
  559. yourself.
  560. self assert: (MethodLexicalScope new bindingFor: node) isNil
  561. ! !
  562. TestCase subclass: #SemanticAnalyzerTest
  563. slots: {#analyzer}
  564. package: 'Compiler-Tests'!
  565. !SemanticAnalyzerTest methodsFor: 'running'!
  566. setUp
  567. analyzer := SemanticAnalyzer on: Object
  568. ! !
  569. !SemanticAnalyzerTest methodsFor: 'tests'!
  570. testAssignment
  571. | src ast |
  572. src := 'foo self := 1'.
  573. ast := Smalltalk parse: src.
  574. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  575. !
  576. testNonLocalReturn
  577. | src ast |
  578. src := 'foo | a | a + 1. ^ a'.
  579. ast := Smalltalk parse: src.
  580. analyzer visit: ast.
  581. self deny: ast scope hasNonLocalReturn
  582. !
  583. testNonLocalReturn2
  584. | src ast |
  585. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  586. ast := Smalltalk parse: src.
  587. analyzer visit: ast.
  588. self assert: ast scope hasNonLocalReturn
  589. !
  590. testScope
  591. | src ast |
  592. src := 'foo | a | a + 1. [ | b | b := a ]'.
  593. ast := Smalltalk parse: src.
  594. analyzer visit: ast.
  595. self deny: ast dagChildren first dagChildren last scope == ast scope.
  596. !
  597. testScope2
  598. | src ast |
  599. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  600. ast := Smalltalk parse: src.
  601. analyzer visit: ast.
  602. self deny: ast dagChildren first dagChildren last dagChildren first dagChildren first scope == ast scope.
  603. !
  604. testScopeLevel
  605. | src ast |
  606. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  607. ast := Smalltalk parse: src.
  608. analyzer visit: ast.
  609. self assert: ast scope scopeLevel equals: 1.
  610. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first scope scopeLevel equals: 3
  611. !
  612. testUnknownVariables
  613. | src ast |
  614. src := 'foo | a | b + a'.
  615. ast := Smalltalk parse: src.
  616. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  617. !
  618. testUnknownVariablesWithScope
  619. | src ast |
  620. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  621. ast := Smalltalk parse: src.
  622. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  623. !
  624. testVariableShadowing
  625. | src ast |
  626. src := 'foo | a | a + 1'.
  627. ast := Smalltalk parse: src.
  628. analyzer visit: ast
  629. !
  630. testVariableShadowing2
  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. testVariableShadowing3
  637. | src ast |
  638. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  639. ast := Smalltalk parse: src.
  640. analyzer visit: ast
  641. !
  642. testVariableShadowing4
  643. | src ast |
  644. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  645. ast := Smalltalk parse: src.
  646. analyzer visit: ast
  647. !
  648. testVariableShadowing5
  649. | src ast |
  650. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  651. ast := Smalltalk parse: src.
  652. self should: [analyzer visit: ast] raise: ShadowingVariableError
  653. !
  654. testVariablesLookup
  655. | src ast |
  656. src := 'foo | a | a + 1. [ | b | b := a ]'.
  657. ast := Smalltalk parse: src.
  658. analyzer visit: ast.
  659. "Binding for `a` in the message send"
  660. self assert: ast dagChildren first dagChildren first receiver binding isTempVar.
  661. self assert: ast dagChildren first dagChildren first receiver binding scope == ast scope.
  662. "Binding for `b`"
  663. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding isTempVar.
  664. self assert: ast dagChildren first dagChildren last dagChildren first dagChildren first left binding scope == ast dagChildren first dagChildren last scope.
  665. ! !
  666. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  667. slots: {}
  668. package: 'Compiler-Tests'!
  669. !AISemanticAnalyzerTest methodsFor: 'running'!
  670. setUp
  671. analyzer := (AISemanticAnalyzer on: Object)
  672. context: (AIContext new
  673. defineLocal: 'local';
  674. localAt: 'local' put: 3;
  675. yourself);
  676. yourself
  677. ! !
  678. !AISemanticAnalyzerTest methodsFor: 'tests'!
  679. testContextVariables
  680. | src ast |
  681. src := 'foo | a | local + a'.
  682. ast := Smalltalk parse: src.
  683. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  684. ! !