Compiler-Tests.st 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. testThisContext
  243. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  244. !
  245. testifFalse
  246. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  247. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  248. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  249. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  250. !
  251. testifFalseIfTrue
  252. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  253. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  254. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  255. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  256. !
  257. testifNil
  258. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  259. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  260. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  261. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  262. !
  263. testifNilIfNotNil
  264. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  265. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  266. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  267. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  268. !
  269. testifNotNil
  270. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  271. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  272. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  273. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  274. !
  275. testifNotNilWithArgument
  276. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  277. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  278. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  279. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  280. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  281. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  282. !
  283. testifTrue
  284. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  285. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  286. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  287. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  288. !
  289. testifTrueIfFalse
  290. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  291. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  292. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  293. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  294. ! !
  295. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  296. instanceVariableNames: ''
  297. package: 'Compiler-Tests'!
  298. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  299. codeGeneratorClass
  300. ^ InliningCodeGenerator
  301. ! !
  302. CodeGeneratorTest subclass: #InterpreterTest
  303. instanceVariableNames: ''
  304. package: 'Compiler-Tests'!
  305. !InterpreterTest methodsFor: 'parsing'!
  306. analyze: aNode forClass: aClass
  307. (SemanticAnalyzer on: aClass) visit: aNode.
  308. ^ aNode
  309. !
  310. parse: aString
  311. ^ Smalltalk current parse: aString
  312. !
  313. parse: aString forClass: aClass
  314. ^ self analyze: (self parse: aString) forClass: aClass
  315. ! !
  316. !InterpreterTest methodsFor: 'private'!
  317. interpret: aString receiver: anObject withArguments: aDictionary
  318. "The food is a methodNode. Interpret the sequenceNode only"
  319. | ctx interpreter |
  320. interpreter := ASTInterpreter new.
  321. ctx := AIContext new
  322. receiver: anObject;
  323. interpreter: interpreter;
  324. yourself.
  325. aDictionary keysAndValuesDo: [ :key :value |
  326. ctx localAt: key put: value ].
  327. ^ interpreter
  328. context: ctx;
  329. interpret: (self parse: aString forClass: anObject class) nextChild;
  330. proceed;
  331. result
  332. ! !
  333. !InterpreterTest methodsFor: 'testing'!
  334. should: aString receiver: anObject return: aResult
  335. receiver := anObject.
  336. ^ self
  337. assert: (self interpret: aString receiver: receiver withArguments: #{})
  338. equals: aResult
  339. !
  340. should: aString return: anObject
  341. ^ self
  342. should: aString
  343. receiver: receiver
  344. return: anObject
  345. ! !
  346. TestCase subclass: #ScopeVarTest
  347. instanceVariableNames: ''
  348. package: 'Compiler-Tests'!
  349. !ScopeVarTest methodsFor: 'tests'!
  350. testClassRefVar
  351. | node |
  352. node := ClassReferenceNode new
  353. value: 'Object';
  354. yourself.
  355. SemanticAnalyzer new visit: node.
  356. self assert: node binding isClassRefVar
  357. !
  358. testInstanceVar
  359. | node scope |
  360. node := VariableNode new
  361. value: 'bzzz';
  362. yourself.
  363. scope := MethodLexicalScope new.
  364. scope addIVar: 'bzzz'.
  365. self assert: (scope bindingFor: node) isInstanceVar
  366. !
  367. testPseudoVar
  368. | node pseudoVars |
  369. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  370. pseudoVars do: [:each |
  371. node := VariableNode new
  372. value: each;
  373. yourself.
  374. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar ]
  375. !
  376. testTempVar
  377. | node scope |
  378. node := VariableNode new
  379. value: 'bzzz';
  380. yourself.
  381. scope := MethodLexicalScope new.
  382. scope addTemp: 'bzzz'.
  383. self assert: (scope bindingFor: node) isTempVar
  384. !
  385. testUnknownVar
  386. | node |
  387. node := VariableNode new
  388. value: 'bzzz';
  389. yourself.
  390. self assert: (MethodLexicalScope new bindingFor: node) isNil
  391. ! !
  392. TestCase subclass: #SemanticAnalyzerTest
  393. instanceVariableNames: 'analyzer'
  394. package: 'Compiler-Tests'!
  395. !SemanticAnalyzerTest methodsFor: 'running'!
  396. setUp
  397. analyzer := SemanticAnalyzer on: Object
  398. ! !
  399. !SemanticAnalyzerTest methodsFor: 'tests'!
  400. testAssignment
  401. | src ast |
  402. src := 'foo self := 1'.
  403. ast := smalltalk parse: src.
  404. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  405. !
  406. testNonLocalReturn
  407. | src ast |
  408. src := 'foo | a | a + 1. ^ a'.
  409. ast := smalltalk parse: src.
  410. analyzer visit: ast.
  411. self deny: ast scope hasNonLocalReturn
  412. !
  413. testNonLocalReturn2
  414. | src ast |
  415. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  416. ast := smalltalk parse: src.
  417. analyzer visit: ast.
  418. self assert: ast scope hasNonLocalReturn
  419. !
  420. testScope
  421. | src ast |
  422. src := 'foo | a | a + 1. [ | b | b := a ]'.
  423. ast := smalltalk parse: src.
  424. analyzer visit: ast.
  425. self deny: ast nodes first nodes last scope == ast scope.
  426. !
  427. testScope2
  428. | src ast |
  429. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  430. ast := smalltalk parse: src.
  431. analyzer visit: ast.
  432. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  433. !
  434. testScopeLevel
  435. | src ast |
  436. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  437. ast := smalltalk parse: src.
  438. analyzer visit: ast.
  439. self assert: ast scope scopeLevel equals: 1.
  440. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel equals: 3
  441. !
  442. testUnknownVariables
  443. | src ast |
  444. src := 'foo | a | b + a'.
  445. ast := smalltalk parse: src.
  446. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  447. !
  448. testUnknownVariablesWithScope
  449. | src ast |
  450. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  451. ast := smalltalk parse: src.
  452. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  453. !
  454. testVariableShadowing
  455. | src ast |
  456. src := 'foo | a | a + 1'.
  457. ast := smalltalk parse: src.
  458. analyzer visit: ast
  459. !
  460. testVariableShadowing2
  461. | src ast |
  462. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  463. ast := smalltalk parse: src.
  464. self should: [analyzer visit: ast] raise: ShadowingVariableError
  465. !
  466. testVariableShadowing3
  467. | src ast |
  468. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  469. ast := smalltalk parse: src.
  470. analyzer visit: ast
  471. !
  472. testVariableShadowing4
  473. | src ast |
  474. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  475. ast := smalltalk parse: src.
  476. analyzer visit: ast
  477. !
  478. testVariableShadowing5
  479. | src ast |
  480. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  481. ast := smalltalk parse: src.
  482. self should: [analyzer visit: ast] raise: ShadowingVariableError
  483. !
  484. testVariablesLookup
  485. | src ast |
  486. src := 'foo | a | a + 1. [ | b | b := a ]'.
  487. ast := smalltalk parse: src.
  488. analyzer visit: ast.
  489. "Binding for `a` in the message send"
  490. self assert: ast nodes first nodes first receiver binding isTempVar.
  491. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  492. "Binding for `b`"
  493. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  494. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  495. ! !