1
0

Compiler-Tests.st 9.9 KB

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