Compiler-Tests.st 20 KB

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