Compiler-Tests.st 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 interpreter |
  236. interpreter := self interpreter.
  237. ctx := AIContext new
  238. receiver: anObject;
  239. interpreter: interpreter;
  240. yourself.
  241. aDictionary keysAndValuesDo: [ :key :value |
  242. ctx localAt: key put: value ].
  243. ^ interpreter
  244. context: ctx;
  245. interpret: (self parse: aString forClass: anObject class) nextChild;
  246. proceed;
  247. result
  248. ! !
  249. !InterpreterTest methodsFor: 'tests'!
  250. testBinarySend
  251. self assert: (self interpret: 'foo ^ 2+3+4') equals: 9
  252. !
  253. testBlockEvaluation
  254. self assert: (self interpret: 'foo ^ true ifTrue: [ 1 asString, ''2'' ]') equals: '12'.
  255. self assert: (self interpret: 'foo ^ #(1 2 3) collect: [ :each | each + 3 ]') equals: #(4 5 6).
  256. !
  257. testBlockLiteral
  258. self assert: (self interpret: 'foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 1.
  259. self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ 2 ]') equals: 1.
  260. self assert: (self interpret: 'foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 2
  261. !
  262. testCascade
  263. self assert: (self interpret: 'foo ^ OrderedCollection new add: 2; add: 3; yourself') equals: (OrderedCollection with: 2 with: 3)
  264. !
  265. testDynamicArray
  266. self assert: (self interpret: 'foo ^ {1+1. 2+2}') equals: #(2 4)
  267. !
  268. testDynamicDictionary
  269. self assert: (self interpret: 'foo ^ #{1->1. 2->3}') equals: #{1->1. 2->3}
  270. !
  271. testGlobalVar
  272. self assert: (self interpret: 'foo ^ eval class') equals: BlockClosure.
  273. self assert: (self interpret: 'foo ^ Math cos: 0') equals: 1.
  274. self assert: (self interpret: 'foo ^ NonExistingVar') equals: nil.
  275. !
  276. testInlinedJSStatement
  277. self assert: (self interpret: 'foo <return 2+3>') equals: 5.
  278. self
  279. assert: (self
  280. interpret: 'foo: anInteger <return 2 + anInteger>'
  281. withArguments: #{ 'anInteger' -> 3})
  282. equals: 5
  283. !
  284. testInstVarAccess
  285. self
  286. assert: (self
  287. interpret: 'foo ^ x'
  288. receiver: 2@3
  289. withArguments: #{})
  290. equals: 2
  291. !
  292. testInstVarAssignment
  293. self
  294. assert: (self
  295. interpret: 'foo: anInteger x := anInteger. ^ x'
  296. receiver: Point new
  297. withArguments: #{'anInteger' -> 2})
  298. equals: 2
  299. !
  300. testKeywordSend
  301. self assert: (self interpret: 'foo ^ Point x: 1 y: 2') equals: 1@2
  302. !
  303. testMultipleSequences
  304. self assert: (self interpret: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6') equals: 30
  305. !
  306. testNestedSends
  307. self assert: (self interpret: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString') equals: (Point x: (2@3) y: 4) asString
  308. !
  309. testNonlocalReturn
  310. self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ]. ^2') equals: 1
  311. !
  312. testReceiver
  313. self
  314. assert: (self
  315. interpret: 'foo ^ self'
  316. receiver: 2@3
  317. withArguments: #{})
  318. equals: 2@3
  319. !
  320. testSuper
  321. self
  322. assert: (self
  323. interpret: 'foo ^ super isBoolean'
  324. receiver: true
  325. withArguments: Dictionary new)
  326. equals: false
  327. !
  328. testTempAssignment
  329. self assert: (self interpret: 'foo | a | a := 2. ^ a') equals: 2
  330. !
  331. testThisContext
  332. self assert: (self interpret: 'foo ^ thisContext') outerContext isNil.
  333. self assert: (self interpret: 'foo ^ [ thisContext ] value') outerContext notNil.
  334. self assert: (self interpret: 'foo ^ [ thisContext ] value outerContext == thisContext')
  335. !
  336. testUnarySend
  337. self assert: (self interpret: 'foo ^ 1 asString') equals: '1'
  338. ! !
  339. TestCase subclass: #CodeGeneratorTest
  340. instanceVariableNames: 'receiver'
  341. package: 'Compiler-Tests'!
  342. !CodeGeneratorTest methodsFor: 'accessing'!
  343. codeGeneratorClass
  344. ^ CodeGenerator
  345. !
  346. targetClass
  347. ^ DoIt
  348. ! !
  349. !CodeGeneratorTest methodsFor: 'factory'!
  350. compiler
  351. ^ Compiler new
  352. codeGeneratorClass: self codeGeneratorClass;
  353. yourself
  354. ! !
  355. !CodeGeneratorTest methodsFor: 'initialization'!
  356. setUp
  357. receiver := self targetClass new
  358. !
  359. tearDown
  360. "receiver := nil"
  361. ! !
  362. !CodeGeneratorTest methodsFor: 'testing'!
  363. should: aString return: anObject
  364. | method result |
  365. method := self compiler install: aString forClass: self targetClass category: 'tests'.
  366. result := receiver perform: method selector.
  367. self targetClass removeCompiledMethod: method.
  368. self assert: anObject equals: result
  369. ! !
  370. !CodeGeneratorTest methodsFor: 'tests'!
  371. testAssignment
  372. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  373. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  374. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  375. !
  376. testBackslashSelectors
  377. self should: '\ arg ^ 4' return: 4.
  378. self should: '\\ arg ^ 42' return: 42
  379. !
  380. testBlockReturn
  381. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  382. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  383. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  384. !
  385. testCascades
  386. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  387. !
  388. testDynamicArrayElementsOrdered
  389. self should: 'foo
  390. | x |
  391. x := 1.
  392. ^ { x. true ifTrue: [ x := 2 ] }
  393. ' return: #(1 2).
  394. !
  395. testDynamicDictionaryElementsOrdered
  396. self should: 'foo
  397. | x |
  398. x := ''foo''->1.
  399. ^ #{ x. (true ifTrue: [ x := ''bar''->2 ]) }
  400. ' return: #{'foo'->1. 'bar'->2}.
  401. !
  402. testInnerTemporalDependentElementsOrdered
  403. self should: 'foo
  404. | x |
  405. x := Array.
  406. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  407. ' return: {'foo'->Array. 'bar'->2}.
  408. self should: 'foo
  409. | x |
  410. x := 1.
  411. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  412. ' return: {'foo'->1. 'bar'->2}.
  413. self should: 'foo
  414. | x |
  415. x := 1.
  416. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  417. ' return: {'foo'->1. 'bar'->2}.
  418. self should: 'foo
  419. | x |
  420. x := 1.
  421. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  422. ' return: #{'foo'->1. 'bar'->2}.
  423. !
  424. testLiterals
  425. self should: 'foo ^ 1' return: 1.
  426. self should: 'foo ^ ''hello''' return: 'hello'.
  427. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  428. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  429. self should: 'foo ^ true' return: true.
  430. self should: 'foo ^ false' return: false.
  431. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  432. self should: 'foo ^ #hello' return: #hello.
  433. self should: 'foo ^ -123.456' return: -123.456
  434. !
  435. testLocalReturn
  436. self should: 'foo ^ 1' return: 1.
  437. self should: 'foo ^ 1 + 1' return: 2.
  438. self should: 'foo ' return: receiver.
  439. self should: 'foo self asString' return: receiver.
  440. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  441. !
  442. testMessageSends
  443. self should: 'foo ^ 1 asString' return: '1'.
  444. self should: 'foo ^ 1 + 1' return: 2.
  445. self should: 'foo ^ 1 + 2 * 3' return: 9.
  446. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  447. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  448. !
  449. testMutableLiterals
  450. "Mutable literals must be aliased in cascades.
  451. See https://github.com/amber-smalltalk/amber/issues/428"
  452. self
  453. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  454. return: #(3 2)
  455. !
  456. testNestedIfTrue
  457. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  458. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  459. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  460. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  461. !
  462. testNonLocalReturn
  463. self should: 'foo [ ^ 1 ] value' return: 1.
  464. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  465. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  466. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  467. !
  468. testPascalCaseGlobal
  469. self should: 'foo ^Object' return: (smalltalk at: 'Object').
  470. self should: 'foo ^NonExistent' return: nil
  471. !
  472. testSendReceiverAndArgumentsOrdered
  473. self should: 'foo
  474. | x |
  475. x := 1.
  476. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  477. ' return: #(1 2).
  478. self should: 'foo
  479. | x |
  480. x := Array.
  481. ^ x with: x with: (true ifTrue: [ x := 2 ])
  482. ' return: {Array. 2}.
  483. !
  484. testifFalse
  485. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  486. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  487. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  488. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  489. !
  490. testifFalseIfTrue
  491. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  492. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  493. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  494. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  495. !
  496. testifNil
  497. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  498. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  499. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  500. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  501. !
  502. testifNilIfNotNil
  503. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  504. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  505. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  506. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  507. !
  508. testifNotNil
  509. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  510. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  511. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  512. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  513. !
  514. testifNotNilWithArgument
  515. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  516. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  517. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  518. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  519. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  520. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  521. !
  522. testifTrue
  523. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  524. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  525. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  526. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  527. !
  528. testifTrueIfFalse
  529. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  530. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  531. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  532. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  533. ! !
  534. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  535. instanceVariableNames: ''
  536. package: 'Compiler-Tests'!
  537. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  538. codeGeneratorClass
  539. ^ InliningCodeGenerator
  540. ! !
  541. TestCase subclass: #ScopeVarTest
  542. instanceVariableNames: ''
  543. package: 'Compiler-Tests'!
  544. !ScopeVarTest methodsFor: 'tests'!
  545. testClassRefVar
  546. | node |
  547. node := ClassReferenceNode new
  548. value: 'Object';
  549. yourself.
  550. SemanticAnalyzer new visit: node.
  551. self assert: node binding isClassRefVar
  552. !
  553. testInstanceVar
  554. | node scope |
  555. node := VariableNode new
  556. value: 'bzzz';
  557. yourself.
  558. scope := MethodLexicalScope new.
  559. scope addIVar: 'bzzz'.
  560. self assert: (scope bindingFor: node) isInstanceVar
  561. !
  562. testPseudoVar
  563. | node pseudoVars |
  564. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  565. pseudoVars do: [:each |
  566. node := VariableNode new
  567. value: each;
  568. yourself.
  569. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  570. !
  571. testTempVar
  572. | node scope |
  573. node := VariableNode new
  574. value: 'bzzz';
  575. yourself.
  576. scope := MethodLexicalScope new.
  577. scope addTemp: 'bzzz'.
  578. self assert: (scope bindingFor: node) isTempVar
  579. !
  580. testUnknownVar
  581. | node |
  582. node := VariableNode new
  583. value: 'bzzz';
  584. yourself.
  585. self assert: (MethodLexicalScope new bindingFor: node) isNil
  586. ! !
  587. TestCase subclass: #SemanticAnalyzerTest
  588. instanceVariableNames: 'analyzer'
  589. package: 'Compiler-Tests'!
  590. !SemanticAnalyzerTest methodsFor: 'running'!
  591. setUp
  592. analyzer := SemanticAnalyzer on: Object
  593. ! !
  594. !SemanticAnalyzerTest methodsFor: 'tests'!
  595. testAssignment
  596. | src ast |
  597. src := 'foo self := 1'.
  598. ast := smalltalk parse: src.
  599. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  600. !
  601. testNonLocalReturn
  602. | src ast |
  603. src := 'foo | a | a + 1. ^ a'.
  604. ast := smalltalk parse: src.
  605. analyzer visit: ast.
  606. self deny: ast scope hasNonLocalReturn
  607. !
  608. testNonLocalReturn2
  609. | src ast |
  610. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  611. ast := smalltalk parse: src.
  612. analyzer visit: ast.
  613. self assert: ast scope hasNonLocalReturn
  614. !
  615. testScope
  616. | src ast |
  617. src := 'foo | a | a + 1. [ | b | b := a ]'.
  618. ast := smalltalk parse: src.
  619. analyzer visit: ast.
  620. self deny: ast nodes first nodes last scope == ast scope.
  621. !
  622. testScope2
  623. | src ast |
  624. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  625. ast := smalltalk parse: src.
  626. analyzer visit: ast.
  627. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  628. !
  629. testScopeLevel
  630. | src ast |
  631. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  632. ast := smalltalk parse: src.
  633. analyzer visit: ast.
  634. self assert: ast scope scopeLevel equals: 1.
  635. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel equals: 3
  636. !
  637. testUnknownVariables
  638. | src ast |
  639. src := 'foo | a | b + a'.
  640. ast := smalltalk parse: src.
  641. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  642. !
  643. testUnknownVariablesWithScope
  644. | src ast |
  645. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  646. ast := smalltalk parse: src.
  647. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  648. !
  649. testVariableShadowing
  650. | src ast |
  651. src := 'foo | a | a + 1'.
  652. ast := smalltalk parse: src.
  653. analyzer visit: ast
  654. !
  655. testVariableShadowing2
  656. | src ast |
  657. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  658. ast := smalltalk parse: src.
  659. self should: [analyzer visit: ast] raise: ShadowingVariableError
  660. !
  661. testVariableShadowing3
  662. | src ast |
  663. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  664. ast := smalltalk parse: src.
  665. analyzer visit: ast
  666. !
  667. testVariableShadowing4
  668. | src ast |
  669. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  670. ast := smalltalk parse: src.
  671. analyzer visit: ast
  672. !
  673. testVariableShadowing5
  674. | src ast |
  675. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  676. ast := smalltalk parse: src.
  677. self should: [analyzer visit: ast] raise: ShadowingVariableError
  678. !
  679. testVariablesLookup
  680. | src ast |
  681. src := 'foo | a | a + 1. [ | b | b := a ]'.
  682. ast := smalltalk parse: src.
  683. analyzer visit: ast.
  684. "Binding for `a` in the message send"
  685. self assert: ast nodes first nodes first receiver binding isTempVar.
  686. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  687. "Binding for `b`"
  688. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  689. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  690. ! !