Compiler-Tests.st 9.1 KB

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