Compiler-Tests.st 19 KB

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