1
0

Compiler-Tests.st 11 KB

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