Compiler-Tests.st 13 KB

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