Compiler-Tests.st 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. AbstractASTInterpreterTest subclass: #ASTSteppingInterpreterTest
  109. instanceVariableNames: 'interpreter'
  110. package: 'Compiler-Tests'!
  111. !ASTSteppingInterpreterTest methodsFor: 'accessing'!
  112. interpreter
  113. ^ interpreter ifNil: [ interpreter := ASTSteppingInterpreter new ]
  114. ! !
  115. !ASTSteppingInterpreterTest methodsFor: 'tests'!
  116. testMessageSend
  117. self interpret: 'foo 1 + 2'.
  118. "SequenceNode"
  119. self interpreter step.
  120. "SendNode"
  121. self interpreter step.
  122. "ValueNode"
  123. self interpreter step.
  124. self assert: self interpreter currentNode value equals: 1.
  125. "ValueNode"
  126. self interpreter step.
  127. self assert: self interpreter currentNode value equals: 2.
  128. "Result"
  129. self interpreter step.
  130. self assert: self interpreter result equals: 3
  131. !
  132. testSimpleStepping
  133. self interpret: 'foo 1'.
  134. "SequenceNode"
  135. self interpreter step.
  136. self assert: self interpreter result isNil.
  137. "ValueNode"
  138. self interpreter step.
  139. self assert: self interpreter result equals: 1
  140. ! !
  141. TestCase subclass: #CodeGeneratorTest
  142. instanceVariableNames: 'receiver'
  143. package: 'Compiler-Tests'!
  144. !CodeGeneratorTest methodsFor: 'accessing'!
  145. codeGeneratorClass
  146. ^ CodeGenerator
  147. !
  148. targetClass
  149. ^ DoIt
  150. ! !
  151. !CodeGeneratorTest methodsFor: 'factory'!
  152. compiler
  153. ^ Compiler new
  154. codeGeneratorClass: self codeGeneratorClass;
  155. yourself
  156. ! !
  157. !CodeGeneratorTest methodsFor: 'initialization'!
  158. setUp
  159. receiver := self targetClass new
  160. !
  161. tearDown
  162. "receiver := nil"
  163. ! !
  164. !CodeGeneratorTest methodsFor: 'testing'!
  165. should: aString return: anObject
  166. | method result |
  167. method := self compiler install: aString forClass: self targetClass category: 'tests'.
  168. result := receiver perform: method selector.
  169. self targetClass removeCompiledMethod: method.
  170. self assert: anObject equals: result
  171. ! !
  172. !CodeGeneratorTest methodsFor: 'tests'!
  173. testAssignment
  174. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  175. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  176. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  177. !
  178. testBlockReturn
  179. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  180. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  181. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  182. !
  183. testCascades
  184. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  185. !
  186. testDynamicArrayElementsOrdered
  187. self should: 'foo
  188. | x |
  189. x := 1.
  190. ^ { x. true ifTrue: [ x := 2 ] }
  191. ' return: #(1 2).
  192. !
  193. testDynamicDictionaryElementsOrdered
  194. self should: 'foo
  195. | x |
  196. x := ''foo''->1.
  197. ^ #{ x. (true ifTrue: [ x := ''bar''->2 ]) }
  198. ' return: #{'foo'->1. 'bar'->2}.
  199. !
  200. testInnerTemporalDependentElementsOrdered
  201. self should: 'foo
  202. | x |
  203. x := Array.
  204. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  205. ' return: {'foo'->Array. 'bar'->2}.
  206. self should: 'foo
  207. | x |
  208. x := 1.
  209. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  210. ' return: {'foo'->1. 'bar'->2}.
  211. self should: 'foo
  212. | x |
  213. x := 1.
  214. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  215. ' return: {'foo'->1. 'bar'->2}.
  216. self should: 'foo
  217. | x |
  218. x := 1.
  219. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  220. ' return: #{'foo'->1. 'bar'->2}.
  221. !
  222. testLiterals
  223. self should: 'foo ^ 1' return: 1.
  224. self should: 'foo ^ ''hello''' return: 'hello'.
  225. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  226. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  227. self should: 'foo ^ true' return: true.
  228. self should: 'foo ^ false' return: false.
  229. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  230. self should: 'foo ^ #hello' return: #hello.
  231. self should: 'foo ^ -123.456' return: -123.456
  232. !
  233. testLocalReturn
  234. self should: 'foo ^ 1' return: 1.
  235. self should: 'foo ^ 1 + 1' return: 2.
  236. self should: 'foo ' return: receiver.
  237. self should: 'foo self asString' return: receiver.
  238. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  239. !
  240. testMessageSends
  241. self should: 'foo ^ 1 asString' return: '1'.
  242. self should: 'foo ^ 1 + 1' return: 2.
  243. self should: 'foo ^ 1 + 2 * 3' return: 9.
  244. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  245. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  246. !
  247. testNestedIfTrue
  248. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  249. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  250. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  251. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  252. !
  253. testNonLocalReturn
  254. self should: 'foo [ ^ 1 ] value' return: 1.
  255. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  256. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  257. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  258. !
  259. testSendReceiverAndArgumentsOrdered
  260. self should: 'foo
  261. | x |
  262. x := 1.
  263. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  264. ' return: #(1 2).
  265. self should: 'foo
  266. | x |
  267. x := Array.
  268. ^ x with: x with: (true ifTrue: [ x := 2 ])
  269. ' return: {Array. 2}.
  270. !
  271. testifFalse
  272. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  273. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  274. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  275. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  276. !
  277. testifFalseIfTrue
  278. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  279. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  280. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  281. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  282. !
  283. testifNil
  284. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  285. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  286. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  287. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  288. !
  289. testifNilIfNotNil
  290. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  291. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  292. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  293. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  294. !
  295. testifNotNil
  296. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  297. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  298. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  299. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  300. !
  301. testifTrue
  302. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  303. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  304. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  305. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  306. !
  307. testifTrueIfFalse
  308. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  309. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  310. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  311. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  312. ! !
  313. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  314. instanceVariableNames: ''
  315. package: 'Compiler-Tests'!
  316. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  317. codeGeneratorClass
  318. ^ InliningCodeGenerator
  319. ! !
  320. TestCase subclass: #ScopeVarTest
  321. instanceVariableNames: ''
  322. package: 'Compiler-Tests'!
  323. !ScopeVarTest methodsFor: 'tests'!
  324. testClassRefVar
  325. | node |
  326. node := ClassReferenceNode new
  327. value: 'Object';
  328. yourself.
  329. SemanticAnalyzer new visit: node.
  330. self assert: node binding isClassRefVar
  331. !
  332. testInstanceVar
  333. | node scope |
  334. node := VariableNode new
  335. value: 'bzzz';
  336. yourself.
  337. scope := MethodLexicalScope new.
  338. scope addIVar: 'bzzz'.
  339. self assert: (scope bindingFor: node) isInstanceVar
  340. !
  341. testPseudoVar
  342. | node pseudoVars |
  343. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  344. pseudoVars do: [:each |
  345. node := VariableNode new
  346. value: each;
  347. yourself.
  348. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  349. !
  350. testTempVar
  351. | node scope |
  352. node := VariableNode new
  353. value: 'bzzz';
  354. yourself.
  355. scope := MethodLexicalScope new.
  356. scope addTemp: 'bzzz'.
  357. self assert: (scope bindingFor: node) isTempVar
  358. !
  359. testUnknownVar
  360. | node |
  361. node := VariableNode new
  362. value: 'bzzz';
  363. yourself.
  364. self assert: (MethodLexicalScope new bindingFor: node) isNil
  365. ! !
  366. TestCase subclass: #SemanticAnalyzerTest
  367. instanceVariableNames: 'analyzer'
  368. package: 'Compiler-Tests'!
  369. !SemanticAnalyzerTest methodsFor: 'running'!
  370. setUp
  371. analyzer := SemanticAnalyzer on: Object
  372. ! !
  373. !SemanticAnalyzerTest methodsFor: 'tests'!
  374. testAssignment
  375. | src ast |
  376. src := 'foo self := 1'.
  377. ast := smalltalk parse: src.
  378. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  379. !
  380. testNonLocalReturn
  381. | src ast |
  382. src := 'foo | a | a + 1. ^ a'.
  383. ast := smalltalk parse: src.
  384. analyzer visit: ast.
  385. self deny: ast scope hasNonLocalReturn
  386. !
  387. testNonLocalReturn2
  388. | src ast |
  389. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  390. ast := smalltalk parse: src.
  391. analyzer visit: ast.
  392. self assert: ast scope hasNonLocalReturn
  393. !
  394. testScope
  395. | src ast |
  396. src := 'foo | a | a + 1. [ | b | b := a ]'.
  397. ast := smalltalk parse: src.
  398. analyzer visit: ast.
  399. self deny: ast nodes first nodes last scope == ast scope.
  400. !
  401. testScope2
  402. | src ast |
  403. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  404. ast := smalltalk parse: src.
  405. analyzer visit: ast.
  406. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  407. !
  408. testScopeLevel
  409. | src ast |
  410. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  411. ast := smalltalk parse: src.
  412. analyzer visit: ast.
  413. self assert: ast scope scopeLevel = 1.
  414. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel = 3
  415. !
  416. testUnknownVariables
  417. | src ast |
  418. src := 'foo | a | b + a'.
  419. ast := smalltalk parse: src.
  420. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  421. !
  422. testUnknownVariablesWithScope
  423. | src ast |
  424. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  425. ast := smalltalk parse: src.
  426. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  427. !
  428. testVariableShadowing
  429. | src ast |
  430. src := 'foo | a | a + 1'.
  431. ast := smalltalk parse: src.
  432. analyzer visit: ast
  433. !
  434. testVariableShadowing2
  435. | src ast |
  436. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  437. ast := smalltalk parse: src.
  438. self should: [analyzer visit: ast] raise: ShadowingVariableError
  439. !
  440. testVariableShadowing3
  441. | src ast |
  442. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  443. ast := smalltalk parse: src.
  444. analyzer visit: ast
  445. !
  446. testVariableShadowing4
  447. | src ast |
  448. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  449. ast := smalltalk parse: src.
  450. analyzer visit: ast
  451. !
  452. testVariableShadowing5
  453. | src ast |
  454. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  455. ast := smalltalk parse: src.
  456. self should: [analyzer visit: ast] raise: ShadowingVariableError
  457. !
  458. testVariablesLookup
  459. | src ast |
  460. src := 'foo | a | a + 1. [ | b | b := a ]'.
  461. ast := smalltalk parse: src.
  462. analyzer visit: ast.
  463. "Binding for `a` in the message send"
  464. self assert: ast nodes first nodes first receiver binding isTempVar.
  465. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  466. "Binding for `b`"
  467. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  468. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  469. ! !