2
0

Compiler-Tests.st 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. Smalltalk 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 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. testMessageSendWithBlocks
  50. | ast |
  51. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  52. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  53. visit: ast;
  54. currentNode) selector equals: 'yourself'
  55. !
  56. testMessageSendWithInlining
  57. | ast |
  58. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  59. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  60. visit: ast;
  61. currentNode) selector equals: 'yourself'.
  62. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  63. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  64. visit: ast;
  65. currentNode) selector equals: 'asBoolean'
  66. !
  67. testNoMessageSend
  68. | ast |
  69. ast := self parse: 'foo ^ self' forClass: Object.
  70. self assert: (self astPCNodeVisitor
  71. visit: ast;
  72. currentNode) isNil
  73. !
  74. testPC
  75. | ast visitor |
  76. ast := self parse: 'foo <console.log(1)>' forClass: Object.
  77. self assert: (self astPCNodeVisitor
  78. visit: ast;
  79. currentNode) isJSStatementNode
  80. ! !
  81. ASTParsingTest subclass: #ASTPositionTest
  82. instanceVariableNames: ''
  83. package: 'Compiler-Tests'!
  84. !ASTPositionTest methodsFor: 'tests'!
  85. testNodeAtPosition
  86. | node |
  87. node := self parse: 'yourself
  88. ^ self'.
  89. self assert: (node navigationNodeAt: 2@4 ifAbsent: [ nil ]) source equals: 'self'.
  90. node := self parse: 'foo
  91. true ifTrue: [ 1 ]'.
  92. self assert: (node navigationNodeAt: 2@7 ifAbsent: [ nil ]) selector equals: 'ifTrue:'.
  93. node := self parse: 'foo
  94. self foo; bar; baz'.
  95. self assert: (node navigationNodeAt: 2@8 ifAbsent: [ nil ]) selector equals: 'foo'
  96. ! !
  97. ASTParsingTest subclass: #CodeGeneratorTest
  98. instanceVariableNames: 'receiver'
  99. package: 'Compiler-Tests'!
  100. !CodeGeneratorTest methodsFor: 'accessing'!
  101. codeGeneratorClass
  102. ^ CodeGenerator
  103. ! !
  104. !CodeGeneratorTest methodsFor: 'factory'!
  105. compiler
  106. ^ Compiler new
  107. codeGeneratorClass: self codeGeneratorClass;
  108. yourself
  109. ! !
  110. !CodeGeneratorTest methodsFor: 'initialization'!
  111. setUp
  112. receiver := DoIt new
  113. !
  114. tearDown
  115. "receiver := nil"
  116. ! !
  117. !CodeGeneratorTest methodsFor: 'testing'!
  118. should: aString receiver: anObject raise: anErrorClass
  119. | method result |
  120. receiver := anObject.
  121. [ self should: [
  122. method := self compiler install: aString forClass: anObject class protocol: 'tests'.
  123. receiver perform: method selector ] raise: anErrorClass ]
  124. ensure: [ method ifNotNil: [ anObject class removeCompiledMethod: method ] ]
  125. !
  126. should: aString receiver: anObject return: aResult
  127. | method result |
  128. receiver := anObject.
  129. method := self compiler install: aString forClass: anObject class protocol: 'tests'.
  130. result := receiver perform: method selector.
  131. anObject class removeCompiledMethod: method.
  132. self assert: aResult equals: result
  133. !
  134. should: aString return: anObject
  135. ^ self
  136. should: aString
  137. receiver: receiver
  138. return: anObject
  139. ! !
  140. !CodeGeneratorTest methodsFor: 'tests'!
  141. testAssignment
  142. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  143. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  144. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  145. !
  146. testAugments
  147. self should: 'foo ^ (Array new) (add: 3) (add: 4)' return: #(3 4).
  148. self should: 'foo ^ (Array new) (add: 3; add: 4)' return: #(3 4).
  149. self should: 'foo ^ (Array new) (add: 3) (add: 4) size' return: 2
  150. !
  151. testBackslashSelectors
  152. self should: '\ arg ^ 4' return: 4.
  153. self should: '\\ arg ^ 42' return: 42
  154. !
  155. testBlockReturn
  156. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  157. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  158. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  159. !
  160. testCascades
  161. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  162. !
  163. testCascadesWithInlining
  164. self should: 'foo ^ true class; ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 1.
  165. self should: 'foo ^ false class; ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 2
  166. !
  167. testDynamicArrayElementsOrdered
  168. self should: 'foo
  169. | x |
  170. x := 1.
  171. ^ { x. x := 2 }
  172. ' return: #(1 2).
  173. self should: 'foo
  174. | x |
  175. x := 1.
  176. ^ { x. true ifTrue: [ x := 2 ] }
  177. ' return: #(1 2).
  178. !
  179. testDynamicDictionaryElementsOrdered
  180. self should: 'foo
  181. | x |
  182. x := ''foo''.
  183. ^ #{ x->1. ''bar''->(true ifTrue: [ 2 ]) }
  184. ' return: #{'foo'->1. 'bar'->2}.
  185. !
  186. testDynamicDictionaryWithMoreArrows
  187. self should: 'foo ^ #{1->2->3}' return: (HashedCollection with: 1->2->3)
  188. !
  189. testGlobalVar
  190. self should: 'foo ^ eval class' return: BlockClosure.
  191. self should: 'foo ^ Math cos: 0' return: 1.
  192. self should: 'foo ^ NonExistingVar' return: nil
  193. !
  194. testInnerCascades
  195. self should: 'foo ^ Array new add: (3 squared; negated); add: (4 negated; squared); yourself' return: #(-3 16)
  196. !
  197. testInnerTemporalDependentElementsOrdered
  198. self should: 'foo
  199. | x |
  200. x := Array.
  201. ^ x with: ''foo''->x with: ''bar''->(x := 2)
  202. ' return: {'foo'->Array. 'bar'->2}.
  203. self should: 'foo
  204. | x |
  205. x := Array.
  206. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  207. ' return: {'foo'->Array. 'bar'->2}.
  208. self should: 'foo
  209. | x |
  210. x := 1.
  211. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  212. ' return: {'foo'->1. 'bar'->2}.
  213. self should: 'foo
  214. | x |
  215. x := 1.
  216. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  217. ' return: {'foo'->1. 'bar'->2}.
  218. self should: 'foo
  219. | x |
  220. x := 1.
  221. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  222. ' return: #{'foo'->1. 'bar'->2}.
  223. !
  224. testJSStatement
  225. self should: 'foo <return 2+3>' return: 5
  226. !
  227. testLexicalScope
  228. self should: 'foo | a | a := 1. [ a := 2 ] value. ^ a' return: 2
  229. !
  230. testLiterals
  231. self should: 'foo ^ 1' return: 1.
  232. self should: 'foo ^ ''hello''' return: 'hello'.
  233. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  234. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  235. self should: 'foo ^ true' return: true.
  236. self should: 'foo ^ false' return: false.
  237. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  238. self should: 'foo ^ #hello' return: #hello.
  239. self should: 'foo ^ $h' return: 'h'.
  240. self should: 'foo ^ -123.456' return: -123.456.
  241. self should: 'foo ^ -2.5e4' return: -25000.
  242. !
  243. testLocalReturn
  244. self should: 'foo ^ 1' return: 1.
  245. self should: 'foo ^ 1 + 1' return: 2.
  246. self should: 'foo ' return: receiver.
  247. self should: 'foo self asString' return: receiver.
  248. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  249. !
  250. testMessageSends
  251. self should: 'foo ^ 1 asString' return: '1'.
  252. self should: 'foo ^ 1 + 1' return: 2.
  253. self should: 'foo ^ 1 + 2 * 3' return: 9.
  254. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  255. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  256. !
  257. testMultipleSequences
  258. self should: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6' return: 30
  259. !
  260. testMutableLiterals
  261. "Mutable literals must be aliased in cascades.
  262. See https://github.com/amber-smalltalk/amber/issues/428"
  263. self
  264. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  265. return: #(3 2)
  266. !
  267. testNestedIfTrue
  268. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  269. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  270. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  271. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  272. !
  273. testNestedSends
  274. self should: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString' return: (Point x: (2@3) y: 4) asString
  275. !
  276. testNonLocalReturn
  277. self should: 'foo [ ^ 1 ] value' return: 1.
  278. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  279. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  280. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  281. !
  282. testPascalCaseGlobal
  283. self should: 'foo ^Object' return: (Smalltalk globals at: 'Object').
  284. self should: 'foo ^NonExistent' return: nil
  285. !
  286. testRootSuperSend
  287. self
  288. should: 'foo ^ super class'
  289. receiver: ProtoObject new
  290. raise: MessageNotUnderstood
  291. !
  292. testSendReceiverAndArgumentsOrdered
  293. self should: 'foo
  294. | x |
  295. x := 1.
  296. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  297. ' return: #(1 2).
  298. self should: 'foo
  299. | x |
  300. x := Array.
  301. ^ x with: x with: (true ifTrue: [ x := 2 ])
  302. ' return: {Array. 2}.
  303. !
  304. testSuperSend
  305. self
  306. should: 'foo ^ super isBoolean'
  307. receiver: true
  308. return: false
  309. !
  310. testTempVariables
  311. self should: 'foo | a | ^ a' return: nil.
  312. self should: 'foo | AVariable | ^ AVariable' return: nil.
  313. self should: 'foo | a b c | ^ c' return: nil.
  314. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  315. self should: 'foo | a | a:= 1. ^ a' return: 1.
  316. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  317. !
  318. testThisContext
  319. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  320. !
  321. testifFalse
  322. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  323. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  324. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  325. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  326. !
  327. testifFalseIfTrue
  328. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  329. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  330. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  331. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  332. !
  333. testifNil
  334. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  335. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  336. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  337. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  338. !
  339. testifNilIfNotNil
  340. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  341. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  342. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  343. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  344. !
  345. testifNotNil
  346. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  347. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  348. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  349. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  350. !
  351. testifNotNilWithArgument
  352. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  353. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  354. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  355. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  356. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  357. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  358. !
  359. testifTrue
  360. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  361. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  362. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  363. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  364. !
  365. testifTrueIfFalse
  366. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  367. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  368. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  369. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  370. ! !
  371. CodeGeneratorTest subclass: #ASTInterpreterTest
  372. instanceVariableNames: ''
  373. package: 'Compiler-Tests'!
  374. !ASTInterpreterTest methodsFor: 'parsing'!
  375. analyze: aNode forClass: aClass
  376. (SemanticAnalyzer on: aClass) visit: aNode.
  377. ^ aNode
  378. !
  379. parse: aString
  380. ^ Smalltalk parse: aString
  381. !
  382. parse: aString forClass: aClass
  383. ^ self analyze: (self parse: aString) forClass: aClass
  384. ! !
  385. !ASTInterpreterTest methodsFor: 'private'!
  386. interpret: aString receiver: anObject withArguments: aDictionary
  387. "The food is a methodNode. Interpret the sequenceNode only"
  388. | ctx ast interpreter |
  389. interpreter := ASTInterpreter new.
  390. ast := self parse: aString forClass: anObject class.
  391. ctx := AIContext new
  392. receiver: anObject;
  393. interpreter: interpreter;
  394. yourself.
  395. "Define locals for the context"
  396. ast sequenceNode ifNotNil: [ :sequence |
  397. sequence temps do: [ :each |
  398. ctx defineLocal: each ] ].
  399. aDictionary keysAndValuesDo: [ :key :value |
  400. ctx localAt: key put: value ].
  401. ^ interpreter
  402. context: ctx;
  403. node: ast;
  404. enterNode;
  405. proceed;
  406. result
  407. ! !
  408. !ASTInterpreterTest methodsFor: 'testing'!
  409. should: aString receiver: anObject return: aResult
  410. receiver := anObject.
  411. ^ self
  412. assert: (self interpret: aString receiver: receiver withArguments: #{})
  413. equals: aResult
  414. ! !
  415. ASTInterpreterTest subclass: #ASTDebuggerTest
  416. instanceVariableNames: ''
  417. package: 'Compiler-Tests'!
  418. !ASTDebuggerTest methodsFor: 'private'!
  419. interpret: aString receiver: anObject withArguments: aDictionary
  420. | ctx ast debugger |
  421. ctx := AIContext new
  422. receiver: anObject;
  423. interpreter: ASTInterpreter new;
  424. yourself.
  425. ast := self parse: aString forClass: anObject class.
  426. "Define locals for the context"
  427. ast sequenceNode ifNotNil: [ :sequence |
  428. sequence temps do: [ :each |
  429. ctx defineLocal: each ] ].
  430. aDictionary keysAndValuesDo: [ :key :value |
  431. ctx localAt: key put: value ].
  432. ctx interpreter context: ctx.
  433. ctx interpreter node: ast; enterNode.
  434. debugger := ASTDebugger context: ctx.
  435. ^ debugger
  436. proceed;
  437. result
  438. ! !
  439. CodeGeneratorTest subclass: #InliningCodeGeneratorTest
  440. instanceVariableNames: ''
  441. package: 'Compiler-Tests'!
  442. !InliningCodeGeneratorTest methodsFor: 'accessing'!
  443. codeGeneratorClass
  444. ^ InliningCodeGenerator
  445. ! !
  446. TestCase subclass: #ScopeVarTest
  447. instanceVariableNames: ''
  448. package: 'Compiler-Tests'!
  449. !ScopeVarTest methodsFor: 'tests'!
  450. testClassRefVar
  451. | node |
  452. node := VariableNode new
  453. value: 'Object';
  454. yourself.
  455. SemanticAnalyzer new
  456. pushScope: MethodLexicalScope new;
  457. visit: node.
  458. self assert: node binding isClassRefVar
  459. !
  460. testInstanceVar
  461. | node scope |
  462. node := VariableNode new
  463. value: 'bzzz';
  464. yourself.
  465. scope := MethodLexicalScope new.
  466. scope addIVar: 'bzzz'.
  467. self assert: (scope bindingFor: node) isInstanceVar
  468. !
  469. testPseudoVar
  470. | node pseudoVars |
  471. pseudoVars := #('self' 'super' 'true' 'false' 'nil').
  472. pseudoVars do: [:each |
  473. node := VariableNode new
  474. value: each;
  475. yourself.
  476. self assert: (MethodLexicalScope new bindingFor: node) isPseudoVar]
  477. !
  478. testTempVar
  479. | node scope |
  480. node := VariableNode new
  481. value: 'bzzz';
  482. yourself.
  483. scope := MethodLexicalScope new.
  484. scope addTemp: 'bzzz'.
  485. self assert: (scope bindingFor: node) isTempVar
  486. !
  487. testUnknownVar
  488. | node |
  489. node := VariableNode new
  490. value: 'bzzz';
  491. yourself.
  492. self assert: (MethodLexicalScope new bindingFor: node) isNil
  493. ! !
  494. TestCase subclass: #SemanticAnalyzerTest
  495. instanceVariableNames: 'analyzer'
  496. package: 'Compiler-Tests'!
  497. !SemanticAnalyzerTest methodsFor: 'running'!
  498. setUp
  499. analyzer := SemanticAnalyzer on: Object
  500. ! !
  501. !SemanticAnalyzerTest methodsFor: 'tests'!
  502. testAssignment
  503. | src ast |
  504. src := 'foo self := 1'.
  505. ast := Smalltalk parse: src.
  506. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  507. !
  508. testNonLocalReturn
  509. | src ast |
  510. src := 'foo | a | a + 1. ^ a'.
  511. ast := Smalltalk parse: src.
  512. analyzer visit: ast.
  513. self deny: ast scope hasNonLocalReturn
  514. !
  515. testNonLocalReturn2
  516. | src ast |
  517. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  518. ast := Smalltalk parse: src.
  519. analyzer visit: ast.
  520. self assert: ast scope hasNonLocalReturn
  521. !
  522. testScope
  523. | src ast |
  524. src := 'foo | a | a + 1. [ | b | b := a ]'.
  525. ast := Smalltalk parse: src.
  526. analyzer visit: ast.
  527. self deny: ast nodes first nodes last scope == ast scope.
  528. !
  529. testScope2
  530. | src ast |
  531. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  532. ast := Smalltalk parse: src.
  533. analyzer visit: ast.
  534. self deny: ast nodes first nodes last nodes first nodes first scope == ast scope.
  535. !
  536. testScopeLevel
  537. | src ast |
  538. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  539. ast := Smalltalk parse: src.
  540. analyzer visit: ast.
  541. self assert: ast scope scopeLevel equals: 1.
  542. self assert: ast nodes first nodes last nodes first nodes first scope scopeLevel equals: 3
  543. !
  544. testUnknownVariables
  545. | src ast |
  546. src := 'foo | a | b + a'.
  547. ast := Smalltalk parse: src.
  548. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  549. !
  550. testUnknownVariablesWithScope
  551. | src ast |
  552. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  553. ast := Smalltalk parse: src.
  554. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  555. !
  556. testVariableShadowing
  557. | src ast |
  558. src := 'foo | a | a + 1'.
  559. ast := Smalltalk parse: src.
  560. analyzer visit: ast
  561. !
  562. testVariableShadowing2
  563. | src ast |
  564. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  565. ast := Smalltalk parse: src.
  566. self should: [analyzer visit: ast] raise: ShadowingVariableError
  567. !
  568. testVariableShadowing3
  569. | src ast |
  570. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  571. ast := Smalltalk parse: src.
  572. analyzer visit: ast
  573. !
  574. testVariableShadowing4
  575. | src ast |
  576. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  577. ast := Smalltalk parse: src.
  578. analyzer visit: ast
  579. !
  580. testVariableShadowing5
  581. | src ast |
  582. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  583. ast := Smalltalk parse: src.
  584. self should: [analyzer visit: ast] raise: ShadowingVariableError
  585. !
  586. testVariablesLookup
  587. | src ast |
  588. src := 'foo | a | a + 1. [ | b | b := a ]'.
  589. ast := Smalltalk parse: src.
  590. analyzer visit: ast.
  591. "Binding for `a` in the message send"
  592. self assert: ast nodes first nodes first receiver binding isTempVar.
  593. self assert: ast nodes first nodes first receiver binding scope == ast scope.
  594. "Binding for `b`"
  595. self assert: ast nodes first nodes last nodes first nodes first left binding isTempVar.
  596. self assert: ast nodes first nodes last nodes first nodes first left binding scope == ast nodes first nodes last scope.
  597. ! !
  598. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  599. instanceVariableNames: ''
  600. package: 'Compiler-Tests'!
  601. !AISemanticAnalyzerTest methodsFor: 'running'!
  602. setUp
  603. analyzer := (AISemanticAnalyzer on: Object)
  604. context: (AIContext new
  605. defineLocal: 'local';
  606. localAt: 'local' put: 3;
  607. yourself);
  608. yourself
  609. ! !
  610. !AISemanticAnalyzerTest methodsFor: 'tests'!
  611. testContextVariables
  612. | src ast |
  613. src := 'foo | a | local + a'.
  614. ast := Smalltalk parse: src.
  615. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  616. ! !