1
0

Compiler-Tests.st 13 KB

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