Compiler-Tests.st 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. testBlockLiteral
  254. self assert: (self interpret: 'foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 1.
  255. self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ 2 ]') equals: 1.
  256. self assert: (self interpret: 'foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 2
  257. !
  258. testCascade
  259. self assert: (self interpret: 'foo ^ OrderedCollection new add: 2; add: 3; yourself') equals: (OrderedCollection with: 2 with: 3)
  260. !
  261. testDynamicArray
  262. self assert: (self interpret: 'foo ^ {1+1. 2+2}') equals: #(2 4)
  263. !
  264. testDynamicDictionary
  265. self assert: (self interpret: 'foo ^ #{1->1. 2->3}') equals: #{1->1. 2->3}
  266. !
  267. testGlobalVar
  268. self assert: (self interpret: 'foo ^ window document title') equals: window document title
  269. !
  270. testInlinedJSStatement
  271. self assert: (self interpret: 'foo <return 2+3>') equals: 5.
  272. self
  273. assert: (self
  274. interpret: 'foo: anInteger <return 2 + anInteger>'
  275. withArguments: #{ 'anInteger' -> 3})
  276. equals: 5
  277. !
  278. testInstVarAccess
  279. self
  280. assert: (self
  281. interpret: 'foo ^ x'
  282. receiver: 2@3
  283. withArguments: #{})
  284. equals: 2
  285. !
  286. testInstVarAssignment
  287. self
  288. assert: (self
  289. interpret: 'foo: anInteger x := anInteger. ^ x'
  290. receiver: Point new
  291. withArguments: #{'anInteger' -> 2})
  292. equals: 2
  293. !
  294. testKeywordSend
  295. self assert: (self interpret: 'foo ^ Point x: 1 y: 2') equals: 1@2
  296. !
  297. testMultipleSequences
  298. self assert: (self interpret: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6') equals: 30
  299. !
  300. testNestedSends
  301. self assert: (self interpret: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString') equals: (Point x: (2@3) y: 4) asString
  302. !
  303. testNonlocalReturn
  304. self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ]. ^2') equals: 1
  305. !
  306. testReceiver
  307. self
  308. assert: (self
  309. interpret: 'foo ^ self'
  310. receiver: 2@3
  311. withArguments: #{})
  312. equals: 2@3
  313. !
  314. testSuper
  315. self
  316. assert: (self
  317. interpret: 'foo ^ super isBoolean'
  318. receiver: true
  319. withArguments: Dictionary new)
  320. equals: false
  321. !
  322. testTempAssignment
  323. self assert: (self interpret: 'foo | a | a := 2. ^ a') equals: 2
  324. !
  325. testThisContext
  326. self assert: (self interpret: 'foo ^ thisContext') outerContext isNil.
  327. self assert: (self interpret: 'foo ^ [ thisContext ] value') outerContext notNil.
  328. self assert: (self interpret: 'foo ^ [ thisContext ] value outerContext == thisContext')
  329. !
  330. testUnarySend
  331. self assert: (self interpret: 'foo ^ 1 asString') equals: '1'
  332. ! !
  333. TestCase subclass: #CodeGeneratorTest
  334. instanceVariableNames: 'receiver'
  335. package: 'Compiler-Tests'!
  336. !CodeGeneratorTest methodsFor: 'accessing'!
  337. codeGeneratorClass
  338. ^ CodeGenerator
  339. !
  340. targetClass
  341. ^ DoIt
  342. ! !
  343. !CodeGeneratorTest methodsFor: 'factory'!
  344. compiler
  345. ^ Compiler new
  346. codeGeneratorClass: self codeGeneratorClass;
  347. yourself
  348. ! !
  349. !CodeGeneratorTest methodsFor: 'initialization'!
  350. setUp
  351. receiver := self targetClass new
  352. !
  353. tearDown
  354. "receiver := nil"
  355. ! !
  356. !CodeGeneratorTest methodsFor: 'testing'!
  357. should: aString return: anObject
  358. | method result |
  359. method := self compiler install: aString forClass: self targetClass category: 'tests'.
  360. result := receiver perform: method selector.
  361. self targetClass removeCompiledMethod: method.
  362. self assert: anObject equals: result
  363. ! !
  364. !CodeGeneratorTest methodsFor: 'tests'!
  365. testAssignment
  366. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  367. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  368. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  369. !
  370. testBackslashSelectors
  371. self should: '\ arg ^ 4' return: 4.
  372. self should: '\\ arg ^ 42' return: 42
  373. !
  374. testBlockReturn
  375. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  376. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  377. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  378. !
  379. testCascades
  380. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  381. !
  382. testDynamicArrayElementsOrdered
  383. self should: 'foo
  384. | x |
  385. x := 1.
  386. ^ { x. true ifTrue: [ x := 2 ] }
  387. ' return: #(1 2).
  388. !
  389. testDynamicDictionaryElementsOrdered
  390. self should: 'foo
  391. | x |
  392. x := ''foo''->1.
  393. ^ #{ x. (true ifTrue: [ x := ''bar''->2 ]) }
  394. ' return: #{'foo'->1. 'bar'->2}.
  395. !
  396. testInnerTemporalDependentElementsOrdered
  397. self should: 'foo
  398. | x |
  399. x := Array.
  400. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  401. ' return: {'foo'->Array. 'bar'->2}.
  402. self should: 'foo
  403. | x |
  404. x := 1.
  405. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  406. ' return: {'foo'->1. 'bar'->2}.
  407. self should: 'foo
  408. | x |
  409. x := 1.
  410. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  411. ' return: {'foo'->1. 'bar'->2}.
  412. self should: 'foo
  413. | x |
  414. x := 1.
  415. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  416. ' return: #{'foo'->1. 'bar'->2}.
  417. !
  418. testLiterals
  419. self should: 'foo ^ 1' return: 1.
  420. self should: 'foo ^ ''hello''' return: 'hello'.
  421. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  422. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  423. self should: 'foo ^ true' return: true.
  424. self should: 'foo ^ false' return: false.
  425. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  426. self should: 'foo ^ #hello' return: #hello.
  427. self should: 'foo ^ -123.456' return: -123.456
  428. !
  429. testLocalReturn
  430. self should: 'foo ^ 1' return: 1.
  431. self should: 'foo ^ 1 + 1' return: 2.
  432. self should: 'foo ' return: receiver.
  433. self should: 'foo self asString' return: receiver.
  434. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  435. !
  436. testMessageSends
  437. self should: 'foo ^ 1 asString' return: '1'.
  438. self should: 'foo ^ 1 + 1' return: 2.
  439. self should: 'foo ^ 1 + 2 * 3' return: 9.
  440. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  441. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  442. !
  443. testMutableLiterals
  444. "Mutable literals must be aliased in cascades.
  445. See https://github.com/amber-smalltalk/amber/issues/428"
  446. self
  447. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  448. return: #(3 2)
  449. !
  450. testNestedIfTrue
  451. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  452. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  453. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  454. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  455. !
  456. testNonLocalReturn
  457. self should: 'foo [ ^ 1 ] value' return: 1.
  458. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  459. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  460. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  461. !
  462. testPascalCaseGlobal
  463. self should: 'foo ^Object' return: (smalltalk at: 'Object').
  464. self should: 'foo ^NonExistent' return: nil
  465. !
  466. testSendReceiverAndArgumentsOrdered
  467. self should: 'foo
  468. | x |
  469. x := 1.
  470. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  471. ' return: #(1 2).
  472. self should: 'foo
  473. | x |
  474. x := Array.
  475. ^ x with: x with: (true ifTrue: [ x := 2 ])
  476. ' return: {Array. 2}.
  477. !
  478. testifFalse
  479. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  480. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  481. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  482. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  483. !
  484. testifFalseIfTrue
  485. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  486. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  487. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  488. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  489. !
  490. testifNil
  491. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  492. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  493. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  494. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  495. !
  496. testifNilIfNotNil
  497. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  498. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  499. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  500. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  501. !
  502. testifNotNil
  503. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  504. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  505. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  506. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  507. !
  508. testifNotNilWithArgument
  509. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  510. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  511. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  512. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  513. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  514. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  515. !
  516. testifTrue
  517. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  518. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  519. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  520. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  521. !
  522. testifTrueIfFalse
  523. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  524. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  525. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  526. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  527. ! !
  528. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  529. instanceVariableNames: ''
  530. package: 'Compiler-Tests'!
  531. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  532. codeGeneratorClass
  533. ^ InliningCodeGenerator
  534. ! !
  535. TestCase subclass: #ScopeVarTest
  536. instanceVariableNames: ''
  537. package: 'Compiler-Tests'!
  538. !ScopeVarTest methodsFor: 'tests'!
  539. testClassRefVar
  540. | node |
  541. node := ClassReferenceNode new
  542. value: 'Object';
  543. yourself.
  544. SemanticAnalyzer new 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. instanceVariableNames: '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 nodes first nodes 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 nodes first nodes last nodes first nodes 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 nodes first nodes last nodes first nodes 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 nodes first nodes first receiver binding isTempVar.
  680. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  681. "Binding for `b`"
  682. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  683. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  684. ! !