Compiler-Tests.st 15 KB

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