Compiler-Interpreter.st 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. visitRefNode: aNode
  422. | ref |
  423. ref := aNode node.
  424. self interpreter refResults at: ref nodeId
  425. ifPresent: [ ^ aNode ]
  426. ifAbsent: [ ref parent: aNode. ^ super visitRefNode: aNode ]
  427. ! !
  428. !ASTEnterNode class methodsFor: 'instance creation'!
  429. on: anInterpreter
  430. ^ self new
  431. interpreter: anInterpreter;
  432. yourself
  433. ! !
  434. NodeVisitor subclass: #ASTInterpreter
  435. instanceVariableNames: 'node context stack returnValue returned forceAtEnd refResults'
  436. package: 'Compiler-Interpreter'!
  437. !ASTInterpreter commentStamp!
  438. I visit an AST, interpreting (evaluating) nodes one after the other, using a small stack machine.
  439. ## API
  440. While my instances should be used from within an `ASTDebugger`, which provides a more high level interface,
  441. you can use methods from the `interpreting` protocol:
  442. - `#step` evaluates the current `node` only
  443. - `#stepOver` evaluates the AST from the current `node` up to the next stepping node (most likely the next send node)
  444. - `#proceed` evaluates eagerly the AST
  445. - `#restart` select the first node of the AST
  446. - `#skip` skips the current node, moving to the next one if any!
  447. !ASTInterpreter methodsFor: 'accessing'!
  448. context
  449. ^ context
  450. !
  451. context: aContext
  452. context := aContext
  453. !
  454. node
  455. "Answer the next node, ie the node to be evaluated in the next step"
  456. ^ node
  457. !
  458. node: aNode
  459. node := aNode
  460. !
  461. refResults
  462. ^ refResults
  463. !
  464. result
  465. ^ self hasReturned
  466. ifTrue: [ self returnValue ]
  467. ifFalse: [ self context receiver ]
  468. !
  469. returnValue
  470. ^ returnValue
  471. !
  472. returnValue: anObject
  473. returnValue := anObject
  474. !
  475. stack
  476. ^ stack ifNil: [ stack := OrderedCollection new ]
  477. ! !
  478. !ASTInterpreter methodsFor: 'initialization'!
  479. initialize
  480. super initialize.
  481. refResults := #{}.
  482. forceAtEnd := false
  483. ! !
  484. !ASTInterpreter methodsFor: 'interpreting'!
  485. enterNode
  486. self node: ((ASTEnterNode on: self) visit: self node)
  487. !
  488. interpret
  489. "Interpret the next node to be evaluated"
  490. self visit: self node
  491. !
  492. next
  493. | nd nextNode |
  494. nd := self node.
  495. nextNode := nd parent ifNotNil: [ :parent |
  496. (parent nextSiblingNode: nd)
  497. ifNil: [ parent ]
  498. ifNotNil: [ :sibling | (ASTEnterNode on: self) visit: sibling ] ].
  499. self node: nextNode
  500. !
  501. proceed
  502. "Eagerly evaluate the ast"
  503. [ self atEnd ]
  504. whileFalse: [ self step ]
  505. !
  506. restart
  507. self node: self context ast; enterNode
  508. !
  509. setNonLocalReturnFromContext: aContext
  510. aContext interpreter hasReturned ifTrue: [
  511. returned := true.
  512. self returnValue: aContext interpreter returnValue ]
  513. !
  514. skip
  515. self next
  516. !
  517. step
  518. self
  519. interpret;
  520. next
  521. !
  522. stepOver
  523. self step.
  524. [ self node isNil or: [ self node isSteppingNode ] ] whileFalse: [
  525. self step ]
  526. ! !
  527. !ASTInterpreter methodsFor: 'private'!
  528. assign: aNode to: anObject
  529. aNode binding isInstanceVar
  530. ifTrue: [ self context receiver instVarAt: aNode value put: anObject ]
  531. ifFalse: [ self context localAt: aNode value put: anObject ]
  532. !
  533. eval: aString
  534. "Evaluate aString as JS source inside an JS function.
  535. aString is not sandboxed."
  536. | source function |
  537. source := String streamContents: [ :str |
  538. str nextPutAll: '0,(function('.
  539. self context locals keys
  540. do: [ :each | str nextPutAll: each ]
  541. separatedBy: [ str nextPutAll: ',' ].
  542. str
  543. nextPutAll: '){ return (function() {';
  544. nextPutAll: aString;
  545. nextPutAll: '})()})' ].
  546. function := Compiler new eval: source.
  547. ^ function valueWithPossibleArguments: self context locals values
  548. !
  549. messageFromSendNode: aSendNode arguments: aCollection
  550. ^ Message new
  551. selector: aSendNode selector;
  552. arguments: aCollection;
  553. yourself
  554. !
  555. messageNotUnderstood: aMessage receiver: anObject
  556. MessageNotUnderstood new
  557. message: aMessage;
  558. receiver: anObject;
  559. signal
  560. !
  561. sendMessage: aMessage to: anObject superSend: aBoolean
  562. | method |
  563. aBoolean ifFalse: [ ^ aMessage sendTo: anObject ].
  564. anObject class superclass ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  565. method := anObject class superclass methodDictionary
  566. at: aMessage selector
  567. ifAbsent: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  568. ^ method sendTo: anObject arguments: aMessage arguments
  569. ! !
  570. !ASTInterpreter methodsFor: 'stack'!
  571. peek
  572. "Peek the top object of the context stack"
  573. self stack ifEmpty: [ ^ nil ].
  574. ^ self stack last
  575. !
  576. pop
  577. "Pop an object from the context stack"
  578. | peekedValue |
  579. peekedValue := self peek.
  580. self stack removeLast.
  581. ^ peekedValue
  582. !
  583. push: anObject
  584. "Push an object to the context stack"
  585. ^ self stack add: anObject
  586. ! !
  587. !ASTInterpreter methodsFor: 'testing'!
  588. atEnd
  589. forceAtEnd ifTrue: [ ^ true ].
  590. ^ self hasReturned or: [ self node isNil ]
  591. !
  592. hasReturned
  593. ^ returned ifNil: [ false ]
  594. ! !
  595. !ASTInterpreter methodsFor: 'visiting'!
  596. visit: aNode
  597. self hasReturned ifFalse: [ super visit: aNode ]
  598. !
  599. visitAssignmentNode: aNode
  600. | poppedValue |
  601. poppedValue := self pop.
  602. "Pop the left side of the assignment.
  603. It already has been visited, and we don't need its value."
  604. self pop.
  605. self push: poppedValue.
  606. self assign: aNode left to: poppedValue
  607. !
  608. visitBlockNode: aNode
  609. "Do not evaluate the block node.
  610. Instead, put all instructions into a block that we push to the stack for later evaluation"
  611. | block |
  612. block := AIBlockClosure forContext: self context node: aNode.
  613. self push: block
  614. !
  615. visitBlockSequenceNode: aNode
  616. "If the receiver is actually visiting a BlockSequenceNode,
  617. it means the the context is a block context. Evaluation should
  618. stop right after evaluating the block sequence and the outer
  619. context's interpreter should take over.
  620. Therefore we force #atEnd."
  621. super visitBlockSequenceNode: aNode.
  622. forceAtEnd := true
  623. !
  624. visitDynamicArrayNode: aNode
  625. | array |
  626. array := #().
  627. aNode nodes do: [ :each |
  628. array addFirst: self pop ].
  629. self push: array
  630. !
  631. visitDynamicDictionaryNode: aNode
  632. | keyValueList |
  633. keyValueList := OrderedCollection new.
  634. aNode nodes do: [ :each |
  635. keyValueList add: self pop ].
  636. self push: (HashedCollection newFromPairs: keyValueList reversed)
  637. !
  638. visitJSStatementNode: aNode
  639. returned := true.
  640. self returnValue: (self eval: aNode source)
  641. !
  642. visitNode: aNode
  643. "Do nothing by default. Especially, do not visit children recursively."
  644. !
  645. visitRefNode: aNode
  646. | ref refid |
  647. ref := aNode node.
  648. refid := ref nodeId.
  649. self refResults at: refid ifAbsentPut: [ self pop ].
  650. self push: (self refResults at: refid)
  651. !
  652. visitReturnNode: aNode
  653. returned := true.
  654. self returnValue: self pop
  655. !
  656. visitSendNode: aNode
  657. | receiver args message result |
  658. args := aNode arguments collect: [ :each | self pop ].
  659. receiver := self pop.
  660. message := self
  661. messageFromSendNode: aNode
  662. arguments: args reversed.
  663. result := self sendMessage: message to: receiver superSend: aNode superSend.
  664. "For cascade sends, push the reciever if the send is not the last one"
  665. (aNode isCascadeSendNode and: [ aNode isLastChild not ])
  666. ifFalse: [ self push: result ]
  667. !
  668. visitSequenceNode: aNode
  669. aNode temps do: [ :each |
  670. self context defineLocal: each ]
  671. !
  672. visitValueNode: aNode
  673. self push: aNode value
  674. !
  675. visitVariableNode: aNode
  676. aNode binding isUnknownVar ifTrue: [
  677. ^ self push: (Platform globals at: aNode value ifAbsent: [ self error: 'Unknown variable' ]) ].
  678. self push: (aNode binding isInstanceVar
  679. ifTrue: [ self context receiver instVarAt: aNode value ]
  680. ifFalse: [ self context
  681. localAt: aNode value
  682. ifAbsent: [
  683. aNode value isCapitalized
  684. ifTrue: [
  685. Smalltalk globals
  686. at: aNode value
  687. ifAbsent: [ Platform globals at: aNode value ] ] ] ])
  688. ! !
  689. Error subclass: #ASTInterpreterError
  690. instanceVariableNames: ''
  691. package: 'Compiler-Interpreter'!
  692. !ASTInterpreterError commentStamp!
  693. I get signaled when an AST interpreter is unable to interpret a node.!
  694. NodeVisitor subclass: #ASTPCNodeVisitor
  695. instanceVariableNames: 'context index selector currentNode'
  696. package: 'Compiler-Interpreter'!
  697. !ASTPCNodeVisitor commentStamp!
  698. I visit an AST until I get to the current node for the `context` and answer it.
  699. ## API
  700. My instances must be filled with a context object using `#context:`.
  701. After visiting the AST the current node is answered by `#currentNode`!
  702. !ASTPCNodeVisitor methodsFor: 'accessing'!
  703. context
  704. ^ context
  705. !
  706. context: aContext
  707. context := aContext
  708. !
  709. currentNode
  710. ^ currentNode
  711. !
  712. increaseIndex
  713. index := self index + 1
  714. !
  715. index
  716. ^ index ifNil: [ index := 0 ]
  717. !
  718. selector
  719. ^ selector
  720. !
  721. selector: aString
  722. selector := aString
  723. ! !
  724. !ASTPCNodeVisitor methodsFor: 'visiting'!
  725. visitJSStatementNode: aNode
  726. "If a JSStatementNode is encountered, it always is the current node.
  727. Stop visiting the AST there"
  728. currentNode := aNode
  729. !
  730. visitSendNode: aNode
  731. | sendIndex |
  732. sendIndex := self context sendIndexAt: self selector.
  733. super visitSendNode: aNode.
  734. self selector = aNode selector ifTrue: [
  735. self index = sendIndex ifTrue: [ currentNode := aNode ].
  736. self increaseIndex ]
  737. ! !
  738. !AssignmentNode methodsFor: '*Compiler-Interpreter'!
  739. isSteppingNode
  740. ^ true
  741. ! !
  742. !BlockNode methodsFor: '*Compiler-Interpreter'!
  743. isSteppingNode
  744. ^ true
  745. !
  746. nextSiblingNode: aNode
  747. "Answer nil as we want to avoid eager evaluation"
  748. "In fact, this should not have been called, ever. IMO. -- herby"
  749. ^ nil
  750. ! !
  751. !DynamicArrayNode methodsFor: '*Compiler-Interpreter'!
  752. isSteppingNode
  753. ^ true
  754. ! !
  755. !DynamicDictionaryNode methodsFor: '*Compiler-Interpreter'!
  756. isSteppingNode
  757. ^ true
  758. ! !
  759. !JSStatementNode methodsFor: '*Compiler-Interpreter'!
  760. isSteppingNode
  761. ^ true
  762. ! !
  763. !Node methodsFor: '*Compiler-Interpreter'!
  764. isSteppingNode
  765. ^ false
  766. !
  767. nextSiblingNode: aNode
  768. "Answer the next node after aNode or nil"
  769. ^ self nodes
  770. at: (self nodes indexOf: aNode) + 1
  771. ifAbsent: [ ^ nil ]
  772. ! !
  773. !RefNode methodsFor: '*Compiler-Interpreter'!
  774. nextSiblingNode: aNode
  775. "no siblings in ref node"
  776. ^ nil
  777. ! !
  778. !SendNode methodsFor: '*Compiler-Interpreter'!
  779. isSteppingNode
  780. ^ true
  781. ! !