Compiler-Tests.st 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. self should: 'foo ^ -2.5e4' return: -25000.
  180. !
  181. testLocalReturn
  182. self should: 'foo ^ 1' return: 1.
  183. self should: 'foo ^ 1 + 1' return: 2.
  184. self should: 'foo ' return: receiver.
  185. self should: 'foo self asString' return: receiver.
  186. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  187. !
  188. testMessageSends
  189. self should: 'foo ^ 1 asString' return: '1'.
  190. self should: 'foo ^ 1 + 1' return: 2.
  191. self should: 'foo ^ 1 + 2 * 3' return: 9.
  192. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  193. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  194. !
  195. testMultipleSequences
  196. self should: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6' return: 30
  197. !
  198. testMutableLiterals
  199. "Mutable literals must be aliased in cascades.
  200. See https://github.com/amber-smalltalk/amber/issues/428"
  201. self
  202. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  203. return: #(3 2)
  204. !
  205. testNestedIfTrue
  206. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  207. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  208. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  209. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  210. !
  211. testNestedSends
  212. self should: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString' return: (Point x: (2@3) y: 4) asString
  213. !
  214. testNonLocalReturn
  215. self should: 'foo [ ^ 1 ] value' return: 1.
  216. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  217. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  218. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  219. !
  220. testPascalCaseGlobal
  221. self should: 'foo ^Object' return: (smalltalk at: 'Object').
  222. self should: 'foo ^NonExistent' return: nil
  223. !
  224. testSendReceiverAndArgumentsOrdered
  225. self should: 'foo
  226. | x |
  227. x := 1.
  228. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  229. ' return: #(1 2).
  230. self should: 'foo
  231. | x |
  232. x := Array.
  233. ^ x with: x with: (true ifTrue: [ x := 2 ])
  234. ' return: {Array. 2}.
  235. !
  236. testSuperSend
  237. self
  238. should: 'foo ^ super isBoolean'
  239. receiver: true
  240. return: false
  241. !
  242. testTempVariables
  243. self should: 'foo | a | ^ a' return: nil.
  244. self should: 'foo | AVariable | ^ AVariable' return: nil.
  245. self should: 'foo | a b c | ^ c' return: nil.
  246. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  247. self should: 'foo | a | a:= 1. ^ a' return: 1.
  248. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  249. !
  250. testThisContext
  251. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  252. !
  253. testifFalse
  254. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  255. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  256. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  257. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  258. !
  259. testifFalseIfTrue
  260. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  261. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  262. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  263. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  264. !
  265. testifNil
  266. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  267. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  268. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  269. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  270. !
  271. testifNilIfNotNil
  272. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  273. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  274. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  275. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  276. !
  277. testifNotNil
  278. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  279. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  280. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  281. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  282. !
  283. testifNotNilWithArgument
  284. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  285. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  286. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  287. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  288. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  289. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  290. !
  291. testifTrue
  292. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  293. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  294. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  295. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  296. !
  297. testifTrueIfFalse
  298. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  299. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  300. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  301. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  302. ! !
  303. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  304. instanceVariableNames: ''
  305. package: 'Compiler-Tests'!
  306. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  307. codeGeneratorClass
  308. ^ InliningCodeGenerator
  309. ! !
  310. CodeGeneratorTest subclass: #InterpreterTest
  311. instanceVariableNames: ''
  312. package: 'Compiler-Tests'!
  313. !InterpreterTest methodsFor: 'parsing'!
  314. analyze: aNode forClass: aClass
  315. (SemanticAnalyzer on: aClass) visit: aNode.
  316. ^ aNode
  317. !
  318. parse: aString
  319. ^ Smalltalk current parse: aString
  320. !
  321. parse: aString forClass: aClass
  322. ^ self analyze: (self parse: aString) forClass: aClass
  323. ! !
  324. !InterpreterTest methodsFor: 'private'!
  325. interpret: aString receiver: anObject withArguments: aDictionary
  326. "The food is a methodNode. Interpret the sequenceNode only"
  327. | ctx interpreter |
  328. interpreter := ASTInterpreter new.
  329. ctx := AIContext new
  330. receiver: anObject;
  331. interpreter: interpreter;
  332. yourself.
  333. aDictionary keysAndValuesDo: [ :key :value |
  334. ctx localAt: key put: value ].
  335. ^ interpreter
  336. context: ctx;
  337. interpret: (self parse: aString forClass: anObject class) nextChild;
  338. proceed;
  339. result
  340. ! !
  341. !InterpreterTest methodsFor: 'testing'!
  342. should: aString receiver: anObject return: aResult
  343. receiver := anObject.
  344. ^ self
  345. assert: (self interpret: aString receiver: receiver withArguments: #{})
  346. equals: aResult
  347. !
  348. should: aString return: anObject
  349. ^ self
  350. should: aString
  351. receiver: receiver
  352. return: anObject
  353. ! !
  354. TestCase subclass: #ScopeVarTest
  355. instanceVariableNames: ''
  356. package: 'Compiler-Tests'!
  357. !ScopeVarTest methodsFor: 'tests'!
  358. testClassRefVar
  359. | node |
  360. node := VariableNode new
  361. value: 'Object';
  362. yourself.
  363. SemanticAnalyzer new
  364. pushScope: MethodLexicalScope new;
  365. visit: node.
  366. self assert: node binding isClassRefVar
  367. !
  368. testInstanceVar
  369. | node scope |
  370. node := VariableNode new
  371. value: 'bzzz';
  372. yourself.
  373. scope := MethodLexicalScope new.
  374. scope addIVar: 'bzzz'.
  375. self assert: (scope bindingFor: node) isInstanceVar
  376. !
  377. testPseudoVar
  378. | node pseudoVars |
  379. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  380. pseudoVars do: [:each |
  381. node := VariableNode new
  382. value: each;
  383. yourself.
  384. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  385. !
  386. testTempVar
  387. | node scope |
  388. node := VariableNode new
  389. value: 'bzzz';
  390. yourself.
  391. scope := MethodLexicalScope new.
  392. scope addTemp: 'bzzz'.
  393. self assert: (scope bindingFor: node) isTempVar
  394. !
  395. testUnknownVar
  396. | node |
  397. node := VariableNode new
  398. value: 'bzzz';
  399. yourself.
  400. self assert: (MethodLexicalScope new bindingFor: node) isNil
  401. ! !
  402. TestCase subclass: #SemanticAnalyzerTest
  403. instanceVariableNames: 'analyzer'
  404. package: 'Compiler-Tests'!
  405. !SemanticAnalyzerTest methodsFor: 'running'!
  406. setUp
  407. analyzer := SemanticAnalyzer on: Object
  408. ! !
  409. !SemanticAnalyzerTest methodsFor: 'tests'!
  410. testAssignment
  411. | src ast |
  412. src := 'foo self := 1'.
  413. ast := smalltalk parse: src.
  414. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  415. !
  416. testNonLocalReturn
  417. | src ast |
  418. src := 'foo | a | a + 1. ^ a'.
  419. ast := smalltalk parse: src.
  420. analyzer visit: ast.
  421. self deny: ast scope hasNonLocalReturn
  422. !
  423. testNonLocalReturn2
  424. | src ast |
  425. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  426. ast := smalltalk parse: src.
  427. analyzer visit: ast.
  428. self assert: ast scope hasNonLocalReturn
  429. !
  430. testScope
  431. | src ast |
  432. src := 'foo | a | a + 1. [ | b | b := a ]'.
  433. ast := smalltalk parse: src.
  434. analyzer visit: ast.
  435. self deny: ast nodes first nodes last scope == ast scope.
  436. !
  437. testScope2
  438. | src ast |
  439. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  440. ast := smalltalk parse: src.
  441. analyzer visit: ast.
  442. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  443. !
  444. testScopeLevel
  445. | src ast |
  446. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  447. ast := smalltalk parse: src.
  448. analyzer visit: ast.
  449. self assert: ast scope scopeLevel equals: 1.
  450. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel equals: 3
  451. !
  452. testUnknownVariables
  453. | src ast |
  454. src := 'foo | a | b + a'.
  455. ast := smalltalk parse: src.
  456. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  457. !
  458. testUnknownVariablesWithScope
  459. | src ast |
  460. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  461. ast := smalltalk parse: src.
  462. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  463. !
  464. testVariableShadowing
  465. | src ast |
  466. src := 'foo | a | a + 1'.
  467. ast := smalltalk parse: src.
  468. analyzer visit: ast
  469. !
  470. testVariableShadowing2
  471. | src ast |
  472. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  473. ast := smalltalk parse: src.
  474. self should: [analyzer visit: ast] raise: ShadowingVariableError
  475. !
  476. testVariableShadowing3
  477. | src ast |
  478. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  479. ast := smalltalk parse: src.
  480. analyzer visit: ast
  481. !
  482. testVariableShadowing4
  483. | src ast |
  484. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  485. ast := smalltalk parse: src.
  486. analyzer visit: ast
  487. !
  488. testVariableShadowing5
  489. | src ast |
  490. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  491. ast := smalltalk parse: src.
  492. self should: [analyzer visit: ast] raise: ShadowingVariableError
  493. !
  494. testVariablesLookup
  495. | src ast |
  496. src := 'foo | a | a + 1. [ | b | b := a ]'.
  497. ast := smalltalk parse: src.
  498. analyzer visit: ast.
  499. "Binding for `a` in the message send"
  500. self assert: ast nodes first nodes first receiver binding isTempVar.
  501. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  502. "Binding for `b`"
  503. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  504. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  505. ! !