Compiler-Tests.st 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. testInnerTemporalDependentElementsOrdered
  62. self should: 'foo
  63. | x |
  64. x := Array.
  65. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  66. ' return: {'foo'->Array. 'bar'->2}.
  67. self should: 'foo
  68. | x |
  69. x := 1.
  70. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  71. ' return: {'foo'->1. 'bar'->2}.
  72. self should: 'foo
  73. | x |
  74. x := 1.
  75. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  76. ' return: {'foo'->1. 'bar'->2}.
  77. self should: 'foo
  78. | x |
  79. x := 1.
  80. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  81. ' return: #{'foo'->1. 'bar'->2}.
  82. !
  83. testLiterals
  84. self should: 'foo ^ 1' return: 1.
  85. self should: 'foo ^ ''hello''' return: 'hello'.
  86. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  87. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  88. self should: 'foo ^ true' return: true.
  89. self should: 'foo ^ false' return: false.
  90. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  91. self should: 'foo ^ #hello' return: #hello.
  92. self should: 'foo ^ -123.456' return: -123.456
  93. !
  94. testLocalReturn
  95. self should: 'foo ^ 1' return: 1.
  96. self should: 'foo ^ 1 + 1' return: 2.
  97. self should: 'foo ' return: receiver.
  98. self should: 'foo self asString' return: receiver.
  99. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  100. !
  101. testMessageSends
  102. self should: 'foo ^ 1 asString' return: '1'.
  103. self should: 'foo ^ 1 + 1' return: 2.
  104. self should: 'foo ^ 1 + 2 * 3' return: 9.
  105. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  106. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  107. !
  108. testNestedIfTrue
  109. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  110. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  111. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  112. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  113. !
  114. testNonLocalReturn
  115. self should: 'foo [ ^ 1 ] value' return: 1.
  116. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  117. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  118. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  119. !
  120. testSendReceiverAndArgumentsOrdered
  121. self should: 'foo
  122. | x |
  123. x := 1.
  124. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  125. ' return: #(1 2).
  126. self should: 'foo
  127. | x |
  128. x := Array.
  129. ^ x with: x with: (true ifTrue: [ x := 2 ])
  130. ' return: {Array. 2}.
  131. !
  132. testifFalse
  133. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  134. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  135. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  136. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  137. !
  138. testifFalseIfTrue
  139. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  140. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  141. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  142. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  143. !
  144. testifNil
  145. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  146. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  147. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  148. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  149. !
  150. testifNilIfNotNil
  151. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  152. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  153. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  154. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  155. !
  156. testifNotNil
  157. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  158. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  159. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  160. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  161. !
  162. testifTrue
  163. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  164. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  165. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  166. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  167. !
  168. testifTrueIfFalse
  169. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  170. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  171. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  172. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  173. ! !
  174. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  175. instanceVariableNames: ''
  176. package: 'Compiler-Tests'!
  177. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  178. codeGeneratorClass
  179. ^ InliningCodeGenerator
  180. ! !
  181. TestCase subclass: #ScopeVarTest
  182. instanceVariableNames: ''
  183. package: 'Compiler-Tests'!
  184. !ScopeVarTest methodsFor: 'tests'!
  185. testClassRefVar
  186. | node |
  187. node := ClassReferenceNode new
  188. value: 'Object';
  189. yourself.
  190. SemanticAnalyzer new visit: node.
  191. self assert: node binding isClassRefVar
  192. !
  193. testInstanceVar
  194. | node scope |
  195. node := VariableNode new
  196. value: 'bzzz';
  197. yourself.
  198. scope := MethodLexicalScope new.
  199. scope addIVar: 'bzzz'.
  200. self assert: (scope bindingFor: node) isInstanceVar
  201. !
  202. testPseudoVar
  203. | node pseudoVars |
  204. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  205. pseudoVars do: [:each |
  206. node := VariableNode new
  207. value: each;
  208. yourself.
  209. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  210. !
  211. testTempVar
  212. | node scope |
  213. node := VariableNode new
  214. value: 'bzzz';
  215. yourself.
  216. scope := MethodLexicalScope new.
  217. scope addTemp: 'bzzz'.
  218. self assert: (scope bindingFor: node) isTempVar
  219. !
  220. testUnknownVar
  221. | node |
  222. node := VariableNode new
  223. value: 'bzzz';
  224. yourself.
  225. self assert: (MethodLexicalScope new bindingFor: node) isNil
  226. ! !
  227. TestCase subclass: #SemanticAnalyzerTest
  228. instanceVariableNames: 'analyzer'
  229. package: 'Compiler-Tests'!
  230. !SemanticAnalyzerTest methodsFor: 'running'!
  231. setUp
  232. analyzer := SemanticAnalyzer on: Object
  233. ! !
  234. !SemanticAnalyzerTest methodsFor: 'tests'!
  235. testAssignment
  236. | src ast |
  237. src := 'foo self := 1'.
  238. ast := smalltalk parse: src.
  239. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  240. !
  241. testNonLocalReturn
  242. | src ast |
  243. src := 'foo | a | a + 1. ^ a'.
  244. ast := smalltalk parse: src.
  245. analyzer visit: ast.
  246. self deny: ast scope hasNonLocalReturn
  247. !
  248. testNonLocalReturn2
  249. | src ast |
  250. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  251. ast := smalltalk parse: src.
  252. analyzer visit: ast.
  253. self assert: ast scope hasNonLocalReturn
  254. !
  255. testScope
  256. | src ast |
  257. src := 'foo | a | a + 1. [ | b | b := a ]'.
  258. ast := smalltalk parse: src.
  259. analyzer visit: ast.
  260. self deny: ast nodes first nodes last scope == ast scope.
  261. !
  262. testScope2
  263. | src ast |
  264. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  265. ast := smalltalk parse: src.
  266. analyzer visit: ast.
  267. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  268. !
  269. testScopeLevel
  270. | src ast |
  271. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  272. ast := smalltalk parse: src.
  273. analyzer visit: ast.
  274. self assert: ast scope scopeLevel = 1.
  275. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel = 3
  276. !
  277. testUnknownVariables
  278. | src ast |
  279. src := 'foo | a | b + a'.
  280. ast := smalltalk parse: src.
  281. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  282. !
  283. testUnknownVariablesDefinedInJS
  284. < var someVariable = 1 >.
  285. self shouldnt: [ smalltalk parse: 'foo someVariable' ] raise: UnknownVariableError
  286. !
  287. testUnknownVariablesWithScope
  288. | src ast |
  289. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  290. ast := smalltalk parse: src.
  291. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  292. !
  293. testVariableShadowing
  294. | src ast |
  295. src := 'foo | a | a + 1'.
  296. ast := smalltalk parse: src.
  297. analyzer visit: ast
  298. !
  299. testVariableShadowing2
  300. | src ast |
  301. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  302. ast := smalltalk parse: src.
  303. self should: [analyzer visit: ast] raise: ShadowingVariableError
  304. !
  305. testVariableShadowing3
  306. | src ast |
  307. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  308. ast := smalltalk parse: src.
  309. analyzer visit: ast
  310. !
  311. testVariableShadowing4
  312. | src ast |
  313. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  314. ast := smalltalk parse: src.
  315. analyzer visit: ast
  316. !
  317. testVariableShadowing5
  318. | src ast |
  319. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  320. ast := smalltalk parse: src.
  321. self should: [analyzer visit: ast] raise: ShadowingVariableError
  322. !
  323. testVariablesLookup
  324. | src ast |
  325. src := 'foo | a | a + 1. [ | b | b := a ]'.
  326. ast := smalltalk parse: src.
  327. analyzer visit: ast.
  328. "Binding for `a` in the message send"
  329. self assert: ast nodes first nodes first receiver binding isTempVar.
  330. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  331. "Binding for `b`"
  332. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  333. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  334. ! !