Compiler-Tests.st 12 KB

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