Compiler-Tests.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. Smalltalk current createPackage: 'Compiler-Tests' properties: #{}!
  2. TestCase subclass: #ASTInterpreterTest
  3. instanceVariableNames: ''
  4. package: 'Compiler-Tests'!
  5. !ASTInterpreterTest methodsFor: 'accessing'!
  6. analyze: aNode forClass: aClass
  7. (SemanticAnalyzer on: aClass) visit: aNode.
  8. ^ aNode
  9. !
  10. interpret: aString
  11. ^ self
  12. interpret: aString
  13. withArguments: Dictionary new
  14. !
  15. interpret: aString receiver: anObject withArguments: aDictionary
  16. "The food is a methodNode. Interpret the sequenceNode only"
  17. | ctx |
  18. ctx := AIContext new.
  19. ctx receiver: anObject.
  20. aDictionary keysAndValuesDo: [ :key :value |
  21. ctx localAt: key put: value ].
  22. ^ self interpreter
  23. context: ctx;
  24. interpret: (self parse: aString forClass: anObject class)
  25. nodes first;
  26. result
  27. !
  28. interpret: aString withArguments: aDictionary
  29. ^ self
  30. interpret: aString
  31. receiver: Object new
  32. withArguments: aDictionary
  33. !
  34. interpreter
  35. ^ ASTInterpreter new
  36. !
  37. parse: aString
  38. ^ Smalltalk current parse: aString
  39. !
  40. parse: aString forClass: aClass
  41. ^ self analyze: (self parse: aString) forClass: aClass
  42. ! !
  43. !ASTInterpreterTest methodsFor: 'tests'!
  44. testBinarySend
  45. self assert: (self interpret: 'foo 2+3+4') equals: 9
  46. !
  47. testBlockLiteral
  48. self assert: (self interpret: 'foo ^ true ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 1.
  49. self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ 2 ]') equals: 1.
  50. self assert: (self interpret: 'foo ^ false ifTrue: [ 1 ] ifFalse: [ 2 ]') equals: 2
  51. !
  52. testCascade
  53. self assert: (self interpret: 'foo ^ OrderedCollection new add: 2; add: 3; yourself') equals: (OrderedCollection with: 2 with: 3)
  54. !
  55. testDynamicArray
  56. self assert: (self interpret: 'foo ^ {1+1. 2+2}') equals: #(2 4)
  57. !
  58. testDynamicDictionary
  59. self assert: (self interpret: 'foo ^ #{1->1. 2->3}') equals: #{1->1. 2->3}
  60. !
  61. testInlinedJSStatement
  62. self assert: (self interpret: 'foo <return 2+3>') equals: 5.
  63. self
  64. assert: (self
  65. interpret: 'foo: anInteger <return 2 + anInteger>'
  66. withArguments: #{ 'anInteger' -> 3})
  67. equals: 5
  68. !
  69. testInstVarAccess
  70. self
  71. assert: (self
  72. interpret: 'foo ^ x'
  73. receiver: 2@3
  74. withArguments: #{})
  75. equals: 2
  76. !
  77. testInstVarAssignment
  78. self
  79. assert: (self
  80. interpret: 'foo: anInteger x := anInteger. ^ x'
  81. receiver: Point new
  82. withArguments: #{'anInteger' -> 2})
  83. equals: 2
  84. !
  85. testNonlocalReturn
  86. self assert: (self interpret: 'foo true ifTrue: [ ^ 1 ]. ^2') equals: 1
  87. !
  88. testReceiver
  89. self
  90. assert: (self
  91. interpret: 'foo ^ self'
  92. receiver: 2@3
  93. withArguments: #{})
  94. equals: 2@3
  95. !
  96. testTempAssignment
  97. self assert: (self interpret: 'foo | a | a := 2. ^ a') equals: 2
  98. ! !
  99. TestCase subclass: #CodeGeneratorTest
  100. instanceVariableNames: 'receiver'
  101. package: 'Compiler-Tests'!
  102. !CodeGeneratorTest methodsFor: 'accessing'!
  103. codeGeneratorClass
  104. ^ CodeGenerator
  105. !
  106. targetClass
  107. ^ DoIt
  108. ! !
  109. !CodeGeneratorTest methodsFor: 'factory'!
  110. compiler
  111. ^ Compiler new
  112. codeGeneratorClass: self codeGeneratorClass;
  113. yourself
  114. ! !
  115. !CodeGeneratorTest methodsFor: 'initialization'!
  116. setUp
  117. receiver := self targetClass new
  118. !
  119. tearDown
  120. "receiver := nil"
  121. ! !
  122. !CodeGeneratorTest methodsFor: 'testing'!
  123. should: aString return: anObject
  124. | method result |
  125. method := self compiler install: aString forClass: self targetClass category: 'tests'.
  126. result := receiver perform: method selector.
  127. self targetClass removeCompiledMethod: method.
  128. self assert: anObject equals: result
  129. ! !
  130. !CodeGeneratorTest methodsFor: 'tests'!
  131. testAssignment
  132. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  133. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  134. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  135. !
  136. testBlockReturn
  137. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  138. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  139. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  140. !
  141. testCascades
  142. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  143. !
  144. testDynamicArrayElementsOrdered
  145. self should: 'foo
  146. | x |
  147. x := 1.
  148. ^ { x. true ifTrue: [ x := 2 ] }
  149. ' return: #(1 2).
  150. !
  151. testDynamicDictionaryElementsOrdered
  152. self should: 'foo
  153. | x |
  154. x := ''foo''->1.
  155. ^ #{ x. (true ifTrue: [ x := ''bar''->2 ]) }
  156. ' return: #{'foo'->1. 'bar'->2}.
  157. !
  158. testInnerTemporalDependentElementsOrdered
  159. self should: 'foo
  160. | x |
  161. x := Array.
  162. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  163. ' return: {'foo'->Array. 'bar'->2}.
  164. self should: 'foo
  165. | x |
  166. x := 1.
  167. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  168. ' return: {'foo'->1. 'bar'->2}.
  169. self should: 'foo
  170. | x |
  171. x := 1.
  172. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  173. ' return: {'foo'->1. 'bar'->2}.
  174. self should: 'foo
  175. | x |
  176. x := 1.
  177. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  178. ' return: #{'foo'->1. 'bar'->2}.
  179. !
  180. testLiterals
  181. self should: 'foo ^ 1' return: 1.
  182. self should: 'foo ^ ''hello''' return: 'hello'.
  183. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  184. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  185. self should: 'foo ^ true' return: true.
  186. self should: 'foo ^ false' return: false.
  187. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  188. self should: 'foo ^ #hello' return: #hello.
  189. self should: 'foo ^ -123.456' return: -123.456
  190. !
  191. testLocalReturn
  192. self should: 'foo ^ 1' return: 1.
  193. self should: 'foo ^ 1 + 1' return: 2.
  194. self should: 'foo ' return: receiver.
  195. self should: 'foo self asString' return: receiver.
  196. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  197. !
  198. testMessageSends
  199. self should: 'foo ^ 1 asString' return: '1'.
  200. self should: 'foo ^ 1 + 1' return: 2.
  201. self should: 'foo ^ 1 + 2 * 3' return: 9.
  202. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  203. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  204. !
  205. testNestedIfTrue
  206. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  207. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  208. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  209. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  210. !
  211. testNonLocalReturn
  212. self should: 'foo [ ^ 1 ] value' return: 1.
  213. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  214. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  215. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  216. !
  217. testSendReceiverAndArgumentsOrdered
  218. self should: 'foo
  219. | x |
  220. x := 1.
  221. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  222. ' return: #(1 2).
  223. self should: 'foo
  224. | x |
  225. x := Array.
  226. ^ x with: x with: (true ifTrue: [ x := 2 ])
  227. ' return: {Array. 2}.
  228. !
  229. testifFalse
  230. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  231. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  232. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  233. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  234. !
  235. testifFalseIfTrue
  236. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  237. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  238. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  239. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  240. !
  241. testifNil
  242. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  243. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  244. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  245. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  246. !
  247. testifNilIfNotNil
  248. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  249. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  250. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  251. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  252. !
  253. testifNotNil
  254. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  255. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  256. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  257. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  258. !
  259. testifTrue
  260. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  261. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  262. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  263. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  264. !
  265. testifTrueIfFalse
  266. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  267. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  268. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  269. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  270. ! !
  271. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  272. instanceVariableNames: ''
  273. package: 'Compiler-Tests'!
  274. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  275. codeGeneratorClass
  276. ^ InliningCodeGenerator
  277. ! !
  278. TestCase subclass: #ScopeVarTest
  279. instanceVariableNames: ''
  280. package: 'Compiler-Tests'!
  281. !ScopeVarTest methodsFor: 'tests'!
  282. testClassRefVar
  283. | node |
  284. node := ClassReferenceNode new
  285. value: 'Object';
  286. yourself.
  287. SemanticAnalyzer new visit: node.
  288. self assert: node binding isClassRefVar
  289. !
  290. testInstanceVar
  291. | node scope |
  292. node := VariableNode new
  293. value: 'bzzz';
  294. yourself.
  295. scope := MethodLexicalScope new.
  296. scope addIVar: 'bzzz'.
  297. self assert: (scope bindingFor: node) isInstanceVar
  298. !
  299. testPseudoVar
  300. | node pseudoVars |
  301. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  302. pseudoVars do: [:each |
  303. node := VariableNode new
  304. value: each;
  305. yourself.
  306. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  307. !
  308. testTempVar
  309. | node scope |
  310. node := VariableNode new
  311. value: 'bzzz';
  312. yourself.
  313. scope := MethodLexicalScope new.
  314. scope addTemp: 'bzzz'.
  315. self assert: (scope bindingFor: node) isTempVar
  316. !
  317. testUnknownVar
  318. | node |
  319. node := VariableNode new
  320. value: 'bzzz';
  321. yourself.
  322. self assert: (MethodLexicalScope new bindingFor: node) isNil
  323. ! !
  324. TestCase subclass: #SemanticAnalyzerTest
  325. instanceVariableNames: 'analyzer'
  326. package: 'Compiler-Tests'!
  327. !SemanticAnalyzerTest methodsFor: 'running'!
  328. setUp
  329. analyzer := SemanticAnalyzer on: Object
  330. ! !
  331. !SemanticAnalyzerTest methodsFor: 'tests'!
  332. testAssignment
  333. | src ast |
  334. src := 'foo self := 1'.
  335. ast := smalltalk parse: src.
  336. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  337. !
  338. testNonLocalReturn
  339. | src ast |
  340. src := 'foo | a | a + 1. ^ a'.
  341. ast := smalltalk parse: src.
  342. analyzer visit: ast.
  343. self deny: ast scope hasNonLocalReturn
  344. !
  345. testNonLocalReturn2
  346. | src ast |
  347. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  348. ast := smalltalk parse: src.
  349. analyzer visit: ast.
  350. self assert: ast scope hasNonLocalReturn
  351. !
  352. testScope
  353. | src ast |
  354. src := 'foo | a | a + 1. [ | b | b := a ]'.
  355. ast := smalltalk parse: src.
  356. analyzer visit: ast.
  357. self deny: ast nodes first nodes last scope == ast scope.
  358. !
  359. testScope2
  360. | src ast |
  361. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  362. ast := smalltalk parse: src.
  363. analyzer visit: ast.
  364. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  365. !
  366. testScopeLevel
  367. | src ast |
  368. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  369. ast := smalltalk parse: src.
  370. analyzer visit: ast.
  371. self assert: ast scope scopeLevel = 1.
  372. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel = 3
  373. !
  374. testUnknownVariables
  375. | src ast |
  376. src := 'foo | a | b + a'.
  377. ast := smalltalk parse: src.
  378. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  379. !
  380. testUnknownVariablesWithScope
  381. | src ast |
  382. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  383. ast := smalltalk parse: src.
  384. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  385. !
  386. testVariableShadowing
  387. | src ast |
  388. src := 'foo | a | a + 1'.
  389. ast := smalltalk parse: src.
  390. analyzer visit: ast
  391. !
  392. testVariableShadowing2
  393. | src ast |
  394. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  395. ast := smalltalk parse: src.
  396. self should: [analyzer visit: ast] raise: ShadowingVariableError
  397. !
  398. testVariableShadowing3
  399. | src ast |
  400. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  401. ast := smalltalk parse: src.
  402. analyzer visit: ast
  403. !
  404. testVariableShadowing4
  405. | src ast |
  406. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  407. ast := smalltalk parse: src.
  408. analyzer visit: ast
  409. !
  410. testVariableShadowing5
  411. | src ast |
  412. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  413. ast := smalltalk parse: src.
  414. self should: [analyzer visit: ast] raise: ShadowingVariableError
  415. !
  416. testVariablesLookup
  417. | src ast |
  418. src := 'foo | a | a + 1. [ | b | b := a ]'.
  419. ast := smalltalk parse: src.
  420. analyzer visit: ast.
  421. "Binding for `a` in the message send"
  422. self assert: ast nodes first nodes first receiver binding isTempVar.
  423. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  424. "Binding for `b`"
  425. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  426. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  427. ! !