2
0

Compiler-Tests.st 10 KB

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