1
0

Compiler-Tests.st 12 KB

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