Compiler-Tests.st 15 KB

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