Compiler-Interpreter.st 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 nodes 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. context: self;
  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. visitNode: aNode
  417. aNode nodes
  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. visitDynamicArrayNode: aNode
  619. | array |
  620. array := #().
  621. aNode nodes do: [ :each |
  622. array addFirst: self pop ].
  623. self push: array
  624. !
  625. visitDynamicDictionaryNode: aNode
  626. | keyValueList |
  627. keyValueList := OrderedCollection new.
  628. aNode nodes do: [ :each |
  629. keyValueList add: self pop ].
  630. self push: (HashedCollection newFromPairs: keyValueList reversed)
  631. !
  632. visitJSStatementNode: aNode
  633. returned := true.
  634. self returnValue: (self eval: aNode source)
  635. !
  636. visitNode: aNode
  637. "Do nothing by default. Especially, do not visit children recursively."
  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: 'context index 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. context
  687. ^ context
  688. !
  689. context: aContext
  690. context := aContext
  691. !
  692. currentNode
  693. ^ currentNode
  694. !
  695. increaseIndex
  696. index := self index + 1
  697. !
  698. index
  699. ^ index ifNil: [ index := 0 ]
  700. !
  701. selector
  702. ^ selector
  703. !
  704. selector: aString
  705. selector := aString
  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. | sendIndex |
  715. sendIndex := self context sendIndexAt: self selector.
  716. super visitSendNode: aNode.
  717. self selector = aNode selector ifTrue: [
  718. self index = sendIndex ifTrue: [ currentNode := aNode ].
  719. self increaseIndex ]
  720. ! !
  721. !AssignmentNode methodsFor: '*Compiler-Interpreter'!
  722. isSteppingNode
  723. ^ true
  724. ! !
  725. !BlockNode methodsFor: '*Compiler-Interpreter'!
  726. isSteppingNode
  727. ^ true
  728. !
  729. nextSiblingNode: aNode
  730. "Answer nil as we want to avoid eager evaluation"
  731. "In fact, this should not have been called, ever. IMO. -- herby"
  732. ^ nil
  733. ! !
  734. !DynamicArrayNode methodsFor: '*Compiler-Interpreter'!
  735. isSteppingNode
  736. ^ true
  737. ! !
  738. !DynamicDictionaryNode methodsFor: '*Compiler-Interpreter'!
  739. isSteppingNode
  740. ^ true
  741. ! !
  742. !JSStatementNode methodsFor: '*Compiler-Interpreter'!
  743. isSteppingNode
  744. ^ true
  745. ! !
  746. !Node methodsFor: '*Compiler-Interpreter'!
  747. isSteppingNode
  748. ^ false
  749. !
  750. nextSiblingNode: aNode
  751. "Answer the next node after aNode or nil"
  752. ^ self nodes
  753. at: (self nodes indexOf: aNode) + 1
  754. ifAbsent: [ ^ nil ]
  755. ! !
  756. !SendNode methodsFor: '*Compiler-Interpreter'!
  757. isSteppingNode
  758. ^ true
  759. ! !