2
0

Compiler-Interpreter.st 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. Smalltalk createPackage: 'Compiler-Interpreter'!
  2. BlockClosure subclass: #AIBlockClosure
  3. instanceVariableNames: 'node outerContext'
  4. package: 'Compiler-Interpreter'!
  5. !AIBlockClosure commentStamp!
  6. I am a special `BlockClosure` subclass used by an interpreter to interpret a block node.
  7. While I am polymorphic with `BlockClosure`, some methods such as `#new` will raise interpretation errors. Unlike a `BlockClosure`, my instance are not JavaScript functions.
  8. Evaluating an instance will result in interpreting the `node` instance variable (instance of `BlockNode`).!
  9. !AIBlockClosure methodsFor: 'accessing'!
  10. compiledSource
  11. "Unlike blocks, the receiver doesn't represent a JS function"
  12. ^ '[ AST Block closure ]'
  13. !
  14. numArgs
  15. ^ node temps size
  16. ! !
  17. !AIBlockClosure methodsFor: 'converting'!
  18. currySelf
  19. self interpreterError
  20. ! !
  21. !AIBlockClosure methodsFor: 'error handling'!
  22. interpreterError
  23. ASTInterpreterError signal: 'Method cannot be interpreted by the interpreter.'
  24. ! !
  25. !AIBlockClosure methodsFor: 'evaluating'!
  26. applyTo: anObject arguments: aCollection
  27. self interpreterError
  28. !
  29. value
  30. ^ self valueWithPossibleArguments: #()
  31. !
  32. value: anArgument
  33. ^ self valueWithPossibleArguments: {anArgument}
  34. !
  35. value: firstArgument value: secondArgument
  36. ^ self valueWithPossibleArguments: {firstArgument . secondArgument}
  37. !
  38. value: firstArgument value: secondArgument value: thirdArgument
  39. ^ self valueWithPossibleArguments: {firstArgument . secondArgument . thirdArgument}
  40. !
  41. valueWithPossibleArguments: aCollection
  42. | context sequenceNode |
  43. context := outerContext newInnerContext.
  44. "Interpret a copy of the sequence node to avoid creating a new AIBlockClosure"
  45. sequenceNode := node dagChildren first copy
  46. parent: nil;
  47. yourself.
  48. "Define locals in the context"
  49. sequenceNode temps do: [ :each |
  50. context defineLocal: each ].
  51. "Populate the arguments into the context locals"
  52. node parameters withIndexDo: [ :each :index |
  53. context defineLocal: each.
  54. context localAt: each put: (aCollection at: index ifAbsent: [ nil ]) ].
  55. "Interpret the first node of the BlockSequenceNode"
  56. context interpreter
  57. node: sequenceNode;
  58. enterNode;
  59. proceed.
  60. outerContext interpreter
  61. setNonLocalReturnFromContext: context.
  62. ^ context interpreter pop
  63. ! !
  64. !AIBlockClosure methodsFor: 'initialization'!
  65. initializeWithContext: aContext node: aNode
  66. node := aNode.
  67. outerContext := aContext
  68. ! !
  69. !AIBlockClosure class methodsFor: 'instance creation'!
  70. forContext: aContext node: aNode
  71. ^ self new
  72. initializeWithContext: aContext node: aNode;
  73. yourself
  74. ! !
  75. MethodContext subclass: #AIContext
  76. instanceVariableNames: 'outerContext innerContext pc locals selector index sendIndexes evaluatedSelector ast interpreter supercall'
  77. package: 'Compiler-Interpreter'!
  78. !AIContext commentStamp!
  79. I am like a `MethodContext`, used by the `ASTInterpreter`.
  80. Unlike a `MethodContext`, my instances are not read-only.
  81. When debugging, my instances are created by copying the current `MethodContext` (thisContext)!
  82. !AIContext methodsFor: 'accessing'!
  83. defineLocal: aString
  84. self locals at: aString put: nil
  85. !
  86. evaluatedSelector
  87. ^ evaluatedSelector
  88. !
  89. evaluatedSelector: aString
  90. evaluatedSelector := aString
  91. !
  92. index
  93. ^ index ifNil: [ 0 ]
  94. !
  95. index: anInteger
  96. index := anInteger
  97. !
  98. innerContext
  99. ^ innerContext
  100. !
  101. innerContext: anAIContext
  102. innerContext := anAIContext
  103. !
  104. localAt: aString
  105. "Lookup the local value up to the method context"
  106. | context |
  107. context := self lookupContextForLocal: aString.
  108. ^ context basicLocalAt: aString
  109. !
  110. localAt: aString ifAbsent: aBlock
  111. "Lookup the local value up to the method context"
  112. | context |
  113. context := self
  114. lookupContextForLocal: aString
  115. ifNone: [ ^ aBlock value ].
  116. ^ context basicLocalAt: aString
  117. !
  118. localAt: aString put: anObject
  119. | context |
  120. context := self lookupContextForLocal: aString.
  121. context basicLocalAt: aString put: anObject
  122. !
  123. locals
  124. locals ifNil: [ self initializeLocals ].
  125. ^ locals
  126. !
  127. outerContext
  128. ^ outerContext
  129. !
  130. outerContext: anAIContext
  131. outerContext := anAIContext.
  132. outerContext ifNotNil: [ :context |
  133. context innerContext: self ]
  134. !
  135. selector
  136. ^ selector
  137. !
  138. selector: aString
  139. selector := aString
  140. !
  141. sendIndexAt: aString
  142. ^ self sendIndexes at: aString ifAbsent: [ 0 ]
  143. !
  144. sendIndexes
  145. ^ sendIndexes ifNil: [ Dictionary new ]
  146. !
  147. sendIndexes: aDictionary
  148. sendIndexes := aDictionary
  149. ! !
  150. !AIContext methodsFor: 'error handling'!
  151. variableNotFound
  152. "Error thrown whenever a variable lookup fails"
  153. self error: 'Variable missing'
  154. ! !
  155. !AIContext methodsFor: 'evaluating'!
  156. evaluate: aString on: anEvaluator
  157. ^ anEvaluator evaluate: aString context: self
  158. !
  159. evaluateNode: aNode
  160. ^ ASTInterpreter new
  161. context: self;
  162. node: aNode;
  163. enterNode;
  164. proceed;
  165. result
  166. ! !
  167. !AIContext methodsFor: 'factory'!
  168. newInnerContext
  169. ^ self class new
  170. outerContext: self;
  171. yourself
  172. ! !
  173. !AIContext methodsFor: 'initialization'!
  174. initializeAST
  175. ast := self method ast.
  176. (SemanticAnalyzer on: self method methodClass)
  177. visit: ast
  178. !
  179. initializeFromMethodContext: aMethodContext
  180. self
  181. evaluatedSelector: aMethodContext evaluatedSelector;
  182. index: aMethodContext index;
  183. sendIndexes: aMethodContext sendIndexes;
  184. receiver: aMethodContext receiver;
  185. supercall: aMethodContext supercall;
  186. selector: aMethodContext selector.
  187. aMethodContext outerContext ifNotNil: [ :outer |
  188. "If the method context is nil, the block was defined in JS, so ignore it"
  189. outer methodContext ifNotNil: [
  190. self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].
  191. aMethodContext locals keysAndValuesDo: [ :key :value |
  192. self locals at: key put: value ] ]
  193. !
  194. initializeInterpreter
  195. interpreter := ASTInterpreter new
  196. context: self;
  197. yourself.
  198. self innerContext ifNotNil: [
  199. self setupInterpreter: interpreter ]
  200. !
  201. initializeLocals
  202. locals := Dictionary new.
  203. locals at: 'thisContext' put: self.
  204. ! !
  205. !AIContext methodsFor: 'interpreting'!
  206. arguments
  207. ^ self ast arguments collect: [ :each |
  208. self localAt: each ifAbsent: [ self error: 'Argument not in context' ] ]
  209. !
  210. ast
  211. self isBlockContext ifTrue: [
  212. ^ self outerContext ifNotNil: [ :context | context ast ] ].
  213. ast ifNil: [ self initializeAST ].
  214. ^ ast
  215. !
  216. basicReceiver
  217. ^ self localAt: 'self'
  218. !
  219. interpreter
  220. interpreter ifNil: [ self initializeInterpreter ].
  221. ^ interpreter
  222. !
  223. interpreter: anInterpreter
  224. interpreter := anInterpreter
  225. !
  226. receiver: anObject
  227. self locals at: 'self' put: anObject
  228. !
  229. setupInterpreter: anInterpreter
  230. | currentNode |
  231. "Retrieve the current node"
  232. currentNode := ASTPCNodeVisitor new
  233. selector: self evaluatedSelector;
  234. index: (self sendIndexAt: self evaluatedSelector);
  235. visit: self ast;
  236. currentNode.
  237. "Define locals for the context"
  238. self ast sequenceNode ifNotNil: [ :sequence |
  239. sequence temps do: [ :each |
  240. self defineLocal: each ] ].
  241. anInterpreter node: currentNode.
  242. "Push the send args and receiver to the interpreter stack"
  243. self innerContext arguments reversed do: [ :each |
  244. anInterpreter push: each ].
  245. anInterpreter push: (self innerContext receiver)
  246. !
  247. supercall
  248. ^ supercall ifNil: [ false ]
  249. !
  250. supercall: aBoolean
  251. supercall := aBoolean
  252. ! !
  253. !AIContext methodsFor: 'private'!
  254. basicLocalAt: aString
  255. ^ self locals at: aString
  256. !
  257. basicLocalAt: aString put: anObject
  258. self locals at: aString put: anObject
  259. !
  260. lookupContextForLocal: aString
  261. "Lookup the context defining the local named `aString`
  262. up to the method context"
  263. ^ self
  264. lookupContextForLocal: aString
  265. ifNone: [ self variableNotFound ]
  266. !
  267. lookupContextForLocal: aString ifNone: aBlock
  268. "Lookup the context defining the local named `aString`
  269. up to the method context"
  270. ^ self locals
  271. at: aString
  272. ifPresent: [ self ]
  273. ifAbsent: [
  274. self outerContext
  275. ifNil: aBlock
  276. ifNotNil: [ :context |
  277. context lookupContextForLocal: aString ] ]
  278. ! !
  279. !AIContext methodsFor: 'testing'!
  280. isTopContext
  281. ^ self innerContext isNil
  282. ! !
  283. !AIContext class methodsFor: 'instance creation'!
  284. fromMethodContext: aMethodContext
  285. ^ self new
  286. initializeFromMethodContext: aMethodContext;
  287. yourself
  288. ! !
  289. SemanticAnalyzer subclass: #AISemanticAnalyzer
  290. instanceVariableNames: 'context'
  291. package: 'Compiler-Interpreter'!
  292. !AISemanticAnalyzer commentStamp!
  293. I perform the same semantic analysis than `SemanticAnalyzer`, with the difference that provided an `AIContext` context, variables are bound with the context variables.!
  294. !AISemanticAnalyzer methodsFor: 'accessing'!
  295. context
  296. ^ context
  297. !
  298. context: anAIContext
  299. context := anAIContext
  300. ! !
  301. !AISemanticAnalyzer methodsFor: 'visiting'!
  302. visitVariableNode: aNode
  303. self context
  304. localAt: aNode value
  305. ifAbsent: [ ^ super visitVariableNode: aNode ].
  306. aNode binding: ASTContextVar new
  307. ! !
  308. ScopeVar subclass: #ASTContextVar
  309. instanceVariableNames: 'context'
  310. package: 'Compiler-Interpreter'!
  311. !ASTContextVar commentStamp!
  312. I am a variable defined in a `context`.!
  313. !ASTContextVar methodsFor: 'accessing'!
  314. context
  315. ^ context
  316. !
  317. context: anObject
  318. context := anObject
  319. ! !
  320. Object subclass: #ASTDebugger
  321. instanceVariableNames: 'interpreter context result'
  322. package: 'Compiler-Interpreter'!
  323. !ASTDebugger commentStamp!
  324. I am a stepping debugger interface for Amber code.
  325. I internally use an instance of `ASTInterpreter` to actually step through node and interpret them.
  326. My instances are created from an `AIContext` with `ASTDebugger class >> context:`.
  327. They hold an `AIContext` instance internally, recursive copy of the `MethodContext`.
  328. ## API
  329. Use the methods of the `'stepping'` protocol to do stepping.!
  330. !ASTDebugger methodsFor: 'accessing'!
  331. context
  332. ^ context
  333. !
  334. context: aContext
  335. context := aContext
  336. !
  337. interpreter
  338. ^ self context ifNotNil: [ :ctx |
  339. ctx interpreter ]
  340. !
  341. method
  342. ^ self context method
  343. !
  344. node
  345. ^ self interpreter ifNotNil: [
  346. self interpreter node ]
  347. !
  348. result
  349. ^ result
  350. ! !
  351. !ASTDebugger methodsFor: 'actions'!
  352. flushInnerContexts
  353. "When stepping, the inner contexts are not relevent anymore,
  354. and can be flushed"
  355. self context ifNotNil: [ :cxt |
  356. cxt innerContext: nil ]
  357. ! !
  358. !ASTDebugger methodsFor: 'private'!
  359. onStep
  360. "After each step, check if the interpreter is at the end,
  361. and if it is move to its outer context if any, skipping its
  362. current node (which was just evaluated by the current
  363. interpreter).
  364. After each step we also flush inner contexts."
  365. result := self interpreter result.
  366. self interpreter atEnd ifTrue: [
  367. self context outerContext ifNotNil: [ :outerContext |
  368. self context: outerContext ].
  369. self interpreter atEnd ifFalse: [ self interpreter skip ] ].
  370. self flushInnerContexts
  371. ! !
  372. !ASTDebugger methodsFor: 'stepping'!
  373. proceed
  374. [ self atEnd ] whileFalse: [ self stepOver ]
  375. !
  376. restart
  377. self interpreter restart.
  378. self flushInnerContexts
  379. !
  380. stepInto
  381. self shouldBeImplemented
  382. !
  383. stepOver
  384. self context isTopContext
  385. ifFalse: [ self interpreter skip ]
  386. ifTrue: [ self interpreter stepOver ].
  387. self onStep
  388. ! !
  389. !ASTDebugger methodsFor: 'testing'!
  390. atEnd
  391. self context ifNil: [ ^ true ].
  392. ^ self interpreter atEnd and: [
  393. self context isTopContext ]
  394. ! !
  395. !ASTDebugger class methodsFor: 'instance creation'!
  396. context: aContext
  397. ^ self new
  398. context: aContext;
  399. yourself
  400. ! !
  401. NodeVisitor subclass: #ASTEnterNode
  402. instanceVariableNames: 'interpreter'
  403. package: 'Compiler-Interpreter'!
  404. !ASTEnterNode methodsFor: 'accessing'!
  405. interpreter
  406. ^ interpreter
  407. !
  408. interpreter: anObject
  409. interpreter := anObject
  410. ! !
  411. !ASTEnterNode methodsFor: 'visiting'!
  412. visitBlockNode: aNode
  413. "Answer the node as we want to avoid eager evaluation"
  414. ^ aNode
  415. !
  416. visitDagNode: aNode
  417. aNode dagChildren
  418. ifEmpty: [ ^ aNode ]
  419. ifNotEmpty: [ :nodes | ^ self visit: nodes first ]
  420. !
  421. visitSequenceNode: aNode
  422. aNode temps do: [ :each |
  423. self interpreter context defineLocal: each ].
  424. ^ super visitSequenceNode: aNode
  425. ! !
  426. !ASTEnterNode class methodsFor: 'instance creation'!
  427. on: anInterpreter
  428. ^ self new
  429. interpreter: anInterpreter;
  430. yourself
  431. ! !
  432. NodeVisitor subclass: #ASTInterpreter
  433. instanceVariableNames: 'node context stack returnValue returned forceAtEnd'
  434. package: 'Compiler-Interpreter'!
  435. !ASTInterpreter commentStamp!
  436. I visit an AST, interpreting (evaluating) nodes one after the other, using a small stack machine.
  437. ## API
  438. While my instances should be used from within an `ASTDebugger`, which provides a more high level interface,
  439. you can use methods from the `interpreting` protocol:
  440. - `#step` evaluates the current `node` only
  441. - `#stepOver` evaluates the AST from the current `node` up to the next stepping node (most likely the next send node)
  442. - `#proceed` evaluates eagerly the AST
  443. - `#restart` select the first node of the AST
  444. - `#skip` skips the current node, moving to the next one if any!
  445. !ASTInterpreter methodsFor: 'accessing'!
  446. context
  447. ^ context
  448. !
  449. context: aContext
  450. context := aContext
  451. !
  452. node
  453. "Answer the next node, ie the node to be evaluated in the next step"
  454. ^ node
  455. !
  456. node: aNode
  457. node := aNode
  458. !
  459. result
  460. ^ self hasReturned
  461. ifTrue: [ self returnValue ]
  462. ifFalse: [ self context receiver ]
  463. !
  464. returnValue
  465. ^ returnValue
  466. !
  467. returnValue: anObject
  468. returnValue := anObject
  469. !
  470. stack
  471. ^ stack ifNil: [ stack := OrderedCollection new ]
  472. ! !
  473. !ASTInterpreter methodsFor: 'initialization'!
  474. initialize
  475. super initialize.
  476. forceAtEnd := false
  477. ! !
  478. !ASTInterpreter methodsFor: 'interpreting'!
  479. enterNode
  480. self node: ((ASTEnterNode on: self) visit: self node)
  481. !
  482. interpret
  483. "Interpret the next node to be evaluated"
  484. self visit: self node
  485. !
  486. next
  487. | nd nextNode |
  488. nd := self node.
  489. nextNode := nd parent ifNotNil: [ :parent |
  490. (parent nextSiblingNode: nd)
  491. ifNil: [ parent ]
  492. ifNotNil: [ :sibling | (ASTEnterNode on: self) visit: sibling ] ].
  493. self node: nextNode
  494. !
  495. proceed
  496. "Eagerly evaluate the ast"
  497. [ self atEnd ]
  498. whileFalse: [ self step ]
  499. !
  500. restart
  501. self node: self context ast; enterNode
  502. !
  503. setNonLocalReturnFromContext: aContext
  504. aContext interpreter hasReturned ifTrue: [
  505. returned := true.
  506. self returnValue: aContext interpreter returnValue ]
  507. !
  508. skip
  509. self next
  510. !
  511. step
  512. self
  513. interpret;
  514. next
  515. !
  516. stepOver
  517. self step.
  518. [ self node isNil or: [ self node isSteppingNode ] ] whileFalse: [
  519. self step ]
  520. ! !
  521. !ASTInterpreter methodsFor: 'private'!
  522. assign: aNode to: anObject
  523. aNode binding isInstanceVar
  524. ifTrue: [ self context receiver instVarAt: aNode value put: anObject ]
  525. ifFalse: [ self context localAt: aNode value put: anObject ]
  526. !
  527. eval: aString
  528. "Evaluate aString as JS source inside an JS function.
  529. aString is not sandboxed."
  530. | source function |
  531. source := String streamContents: [ :str |
  532. str nextPutAll: '0,(function('.
  533. self context locals keys
  534. do: [ :each | str nextPutAll: each ]
  535. separatedBy: [ str nextPutAll: ',' ].
  536. str
  537. nextPutAll: '){ return (function() {';
  538. nextPutAll: aString;
  539. nextPutAll: '})()})' ].
  540. function := Compiler new eval: source.
  541. ^ function valueWithPossibleArguments: self context locals values
  542. !
  543. messageFromSendNode: aSendNode arguments: aCollection
  544. ^ Message new
  545. selector: aSendNode selector;
  546. arguments: aCollection;
  547. yourself
  548. !
  549. messageNotUnderstood: aMessage receiver: anObject
  550. MessageNotUnderstood new
  551. message: aMessage;
  552. receiver: anObject;
  553. signal
  554. !
  555. sendMessage: aMessage to: anObject superSend: aBoolean
  556. | method |
  557. aBoolean ifFalse: [ ^ aMessage sendTo: anObject ].
  558. anObject class superclass ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  559. method := anObject class superclass methodDictionary
  560. at: aMessage selector
  561. ifAbsent: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  562. ^ method sendTo: anObject arguments: aMessage arguments
  563. ! !
  564. !ASTInterpreter methodsFor: 'stack'!
  565. peek
  566. "Peek the top object of the context stack"
  567. self stack ifEmpty: [ ^ nil ].
  568. ^ self stack last
  569. !
  570. pop
  571. "Pop an object from the context stack"
  572. | peekedValue |
  573. peekedValue := self peek.
  574. self stack removeLast.
  575. ^ peekedValue
  576. !
  577. push: anObject
  578. "Push an object to the context stack"
  579. ^ self stack add: anObject
  580. ! !
  581. !ASTInterpreter methodsFor: 'testing'!
  582. atEnd
  583. forceAtEnd ifTrue: [ ^ true ].
  584. ^ self hasReturned or: [ self node isNil ]
  585. !
  586. hasReturned
  587. ^ returned ifNil: [ false ]
  588. ! !
  589. !ASTInterpreter methodsFor: 'visiting'!
  590. visit: aNode
  591. self hasReturned ifFalse: [ super visit: aNode ]
  592. !
  593. visitAssignmentNode: aNode
  594. | poppedValue |
  595. poppedValue := self pop.
  596. "Pop the left side of the assignment.
  597. It already has been visited, and we don't need its value."
  598. self pop.
  599. self push: poppedValue.
  600. self assign: aNode left to: poppedValue
  601. !
  602. visitBlockNode: aNode
  603. "Do not evaluate the block node.
  604. Instead, put all instructions into a block that we push to the stack for later evaluation"
  605. | block |
  606. block := AIBlockClosure forContext: self context node: aNode.
  607. self push: block
  608. !
  609. visitBlockSequenceNode: aNode
  610. "If the receiver is actually visiting a BlockSequenceNode,
  611. it means the the context is a block context. Evaluation should
  612. stop right after evaluating the block sequence and the outer
  613. context's interpreter should take over.
  614. Therefore we force #atEnd."
  615. super visitBlockSequenceNode: aNode.
  616. forceAtEnd := true
  617. !
  618. visitDagNode: aNode
  619. "Do nothing by default. Especially, do not visit children recursively."
  620. !
  621. visitDynamicArrayNode: aNode
  622. | array |
  623. array := #().
  624. aNode dagChildren do: [ :each |
  625. array addFirst: self pop ].
  626. self push: array
  627. !
  628. visitDynamicDictionaryNode: aNode
  629. | keyValueList |
  630. keyValueList := OrderedCollection new.
  631. aNode dagChildren do: [ :each |
  632. keyValueList add: self pop ].
  633. self push: (HashedCollection newFromPairs: keyValueList reversed)
  634. !
  635. visitJSStatementNode: aNode
  636. returned := true.
  637. self returnValue: (self eval: aNode source)
  638. !
  639. visitReturnNode: aNode
  640. returned := true.
  641. self returnValue: self pop
  642. !
  643. visitSendNode: aNode
  644. | receiver args message result |
  645. args := aNode arguments collect: [ :each | self pop ].
  646. receiver := self peek.
  647. message := self
  648. messageFromSendNode: aNode
  649. arguments: args reversed.
  650. result := self sendMessage: message to: receiver superSend: aNode superSend.
  651. "For cascade sends, push the reciever if the send is not the last one"
  652. (aNode isCascadeSendNode and: [ aNode isLastChild not ])
  653. ifFalse: [ self pop; push: result ]
  654. !
  655. visitValueNode: aNode
  656. self push: aNode value
  657. !
  658. visitVariableNode: aNode
  659. aNode binding isUnknownVar ifTrue: [
  660. ^ self push: (Platform globals at: aNode value ifAbsent: [ self error: 'Unknown variable' ]) ].
  661. self push: (aNode binding isInstanceVar
  662. ifTrue: [ self context receiver instVarAt: aNode value ]
  663. ifFalse: [ self context
  664. localAt: aNode value
  665. ifAbsent: [
  666. aNode value isCapitalized
  667. ifTrue: [
  668. Smalltalk globals
  669. at: aNode value
  670. ifAbsent: [ Platform globals at: aNode value ] ] ] ])
  671. ! !
  672. Error subclass: #ASTInterpreterError
  673. instanceVariableNames: ''
  674. package: 'Compiler-Interpreter'!
  675. !ASTInterpreterError commentStamp!
  676. I get signaled when an AST interpreter is unable to interpret a node.!
  677. NodeVisitor subclass: #ASTPCNodeVisitor
  678. instanceVariableNames: 'index trackedIndex selector currentNode'
  679. package: 'Compiler-Interpreter'!
  680. !ASTPCNodeVisitor commentStamp!
  681. I visit an AST until I get to the current node for the `context` and answer it.
  682. ## API
  683. My instances must be filled with a context object using `#context:`.
  684. After visiting the AST the current node is answered by `#currentNode`!
  685. !ASTPCNodeVisitor methodsFor: 'accessing'!
  686. currentNode
  687. ^ currentNode
  688. !
  689. increaseTrackedIndex
  690. trackedIndex := self trackedIndex + 1
  691. !
  692. index
  693. ^ index
  694. !
  695. index: aNumber
  696. index := aNumber
  697. !
  698. selector
  699. ^ selector
  700. !
  701. selector: aString
  702. selector := aString
  703. !
  704. trackedIndex
  705. ^ trackedIndex ifNil: [ trackedIndex := 0 ]
  706. ! !
  707. !ASTPCNodeVisitor methodsFor: 'visiting'!
  708. visitJSStatementNode: aNode
  709. "If a JSStatementNode is encountered, it always is the current node.
  710. Stop visiting the AST there"
  711. currentNode := aNode
  712. !
  713. visitSendNode: aNode
  714. super visitSendNode: aNode.
  715. self selector = aNode selector ifTrue: [
  716. self trackedIndex = self index ifTrue: [ currentNode := aNode ].
  717. self increaseTrackedIndex ]
  718. ! !
  719. !ASTNode methodsFor: '*Compiler-Interpreter'!
  720. isLastChild
  721. ^ self parent dagChildren last = self
  722. !
  723. isSteppingNode
  724. ^ false
  725. !
  726. nextSiblingNode: aNode
  727. "Answer the next node after aNode or nil"
  728. ^ self dagChildren
  729. at: (self dagChildren indexOf: aNode) + 1
  730. ifAbsent: [ ^ nil ]
  731. ! !
  732. !AssignmentNode methodsFor: '*Compiler-Interpreter'!
  733. isSteppingNode
  734. ^ true
  735. ! !
  736. !BlockNode methodsFor: '*Compiler-Interpreter'!
  737. isSteppingNode
  738. ^ true
  739. !
  740. nextSiblingNode: aNode
  741. "Answer nil as we want to avoid eager evaluation"
  742. "In fact, this should not have been called, ever. IMO. -- herby"
  743. ^ nil
  744. ! !
  745. !DynamicArrayNode methodsFor: '*Compiler-Interpreter'!
  746. isSteppingNode
  747. ^ true
  748. ! !
  749. !DynamicDictionaryNode methodsFor: '*Compiler-Interpreter'!
  750. isSteppingNode
  751. ^ true
  752. ! !
  753. !JSStatementNode methodsFor: '*Compiler-Interpreter'!
  754. isSteppingNode
  755. ^ true
  756. ! !
  757. !SendNode methodsFor: '*Compiler-Interpreter'!
  758. isCascadeSendNode
  759. ^ self parent isCascadeNode
  760. !
  761. isSteppingNode
  762. ^ true
  763. ! !