1
0

Compiler-Tests.st 15 KB

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