Compiler-Tests.st 15 KB

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