Compiler-Interpreter.st 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. Smalltalk createPackage: 'Compiler-Interpreter'!
  2. BlockClosure subclass: #AIBlockClosure
  3. slots: {#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 sequenceNode 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. Object subclass: #AIContext
  76. slots: {#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. home
  93. ^ nil
  94. !
  95. index
  96. ^ index ifNil: [ 0 ]
  97. !
  98. index: anInteger
  99. index := anInteger
  100. !
  101. innerContext
  102. ^ innerContext
  103. !
  104. innerContext: anAIContext
  105. innerContext := anAIContext
  106. !
  107. localAt: aString
  108. "Lookup the local value up to the method context"
  109. | context |
  110. context := self lookupContextForLocal: aString.
  111. ^ context basicLocalAt: aString
  112. !
  113. localAt: aString ifAbsent: aBlock
  114. "Lookup the local value up to the method context"
  115. | context |
  116. context := self
  117. lookupContextForLocal: aString
  118. ifNone: [ ^ aBlock value ].
  119. ^ context basicLocalAt: aString
  120. !
  121. localAt: aString put: anObject
  122. | context |
  123. context := self lookupContextForLocal: aString.
  124. context basicLocalAt: aString put: anObject
  125. !
  126. locals
  127. locals ifNil: [ self initializeLocals ].
  128. ^ locals
  129. !
  130. outerContext
  131. ^ outerContext
  132. !
  133. outerContext: anAIContext
  134. outerContext := anAIContext.
  135. outerContext ifNotNil: [ :context |
  136. context innerContext: self ]
  137. !
  138. selector
  139. ^ selector
  140. !
  141. selector: aString
  142. selector := aString
  143. !
  144. sendIndexAt: aString
  145. ^ self sendIndexes at: aString ifAbsent: [ 0 ]
  146. !
  147. sendIndexes
  148. ^ sendIndexes ifNil: [ Dictionary new ]
  149. !
  150. sendIndexes: aDictionary
  151. sendIndexes := aDictionary
  152. ! !
  153. !AIContext methodsFor: 'error handling'!
  154. variableNotFound
  155. "Error thrown whenever a variable lookup fails"
  156. self error: 'Variable missing'
  157. ! !
  158. !AIContext methodsFor: 'evaluating'!
  159. evaluate: aString on: anEvaluator
  160. ^ anEvaluator evaluate: aString context: self
  161. !
  162. evaluateNode: aNode
  163. ^ ASTInterpreter new
  164. context: self;
  165. node: aNode;
  166. enterNode;
  167. proceed;
  168. result
  169. ! !
  170. !AIContext methodsFor: 'factory'!
  171. newInnerContext
  172. ^ self class new
  173. outerContext: self;
  174. yourself
  175. ! !
  176. !AIContext methodsFor: 'initialization'!
  177. initializeAST
  178. ast := self method ast.
  179. (SemanticAnalyzer on: self method origin)
  180. visit: ast
  181. !
  182. initializeFromMethodContext: aMethodContext
  183. self
  184. evaluatedSelector: aMethodContext evaluatedSelector;
  185. index: aMethodContext index;
  186. sendIndexes: aMethodContext sendIndexes;
  187. receiver: aMethodContext receiver;
  188. supercall: aMethodContext supercall;
  189. selector: aMethodContext selector.
  190. aMethodContext outerContext ifNotNil: [ :outer |
  191. "If the method context is nil, the block was defined in JS, so ignore it"
  192. outer methodContext ifNotNil: [
  193. self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].
  194. aMethodContext locals keysAndValuesDo: [ :key :value |
  195. self locals at: key put: value ] ]
  196. !
  197. initializeInterpreter
  198. interpreter := ASTInterpreter new
  199. context: self;
  200. yourself.
  201. self innerContext ifNotNil: [
  202. self setupInterpreter: interpreter ]
  203. !
  204. initializeLocals
  205. locals := Dictionary new.
  206. locals at: 'thisContext' put: self.
  207. ! !
  208. !AIContext methodsFor: 'interpreting'!
  209. arguments
  210. ^ self ast arguments collect: [ :each |
  211. self localAt: each ifAbsent: [ self error: 'Argument not in context' ] ]
  212. !
  213. ast
  214. self isBlockContext ifTrue: [
  215. ^ self outerContext ifNotNil: [ :context | context ast ] ].
  216. ast ifNil: [ self initializeAST ].
  217. ^ ast
  218. !
  219. basicReceiver
  220. ^ self localAt: 'self'
  221. !
  222. interpreter
  223. interpreter ifNil: [ self initializeInterpreter ].
  224. ^ interpreter
  225. !
  226. interpreter: anInterpreter
  227. interpreter := anInterpreter
  228. !
  229. receiver: anObject
  230. self locals at: 'self' put: anObject
  231. !
  232. setupInterpreter: anInterpreter
  233. | currentNode |
  234. "Retrieve the current node"
  235. currentNode := ASTPCNodeVisitor new
  236. selector: self evaluatedSelector;
  237. index: (self sendIndexAt: self evaluatedSelector);
  238. visit: self ast;
  239. currentNode.
  240. "Define locals for the context"
  241. self ast sequenceNode ifNotNil: [ :sequence |
  242. sequence temps do: [ :each |
  243. self defineLocal: each ] ].
  244. anInterpreter node: currentNode.
  245. "Push the send args and receiver to the interpreter stack"
  246. self innerContext arguments reversed do: [ :each |
  247. anInterpreter push: each ].
  248. anInterpreter push: (self innerContext receiver)
  249. !
  250. supercall
  251. ^ supercall ifNil: [ false ]
  252. !
  253. supercall: aBoolean
  254. supercall := aBoolean
  255. ! !
  256. !AIContext methodsFor: 'private'!
  257. basicLocalAt: aString
  258. ^ self locals at: aString
  259. !
  260. basicLocalAt: aString put: anObject
  261. self locals at: aString put: anObject
  262. !
  263. lookupContextForLocal: aString
  264. "Lookup the context defining the local named `aString`
  265. up to the method context"
  266. ^ self
  267. lookupContextForLocal: aString
  268. ifNone: [ self variableNotFound ]
  269. !
  270. lookupContextForLocal: aString ifNone: aBlock
  271. "Lookup the context defining the local named `aString`
  272. up to the method context"
  273. ^ self locals
  274. at: aString
  275. ifPresent: [ self ]
  276. ifAbsent: [
  277. self outerContext
  278. ifNil: aBlock
  279. ifNotNil: [ :context |
  280. context lookupContextForLocal: aString ifNone: aBlock ] ]
  281. ! !
  282. !AIContext methodsFor: 'testing'!
  283. isTopContext
  284. ^ self innerContext isNil
  285. ! !
  286. !AIContext class methodsFor: 'instance creation'!
  287. fromMethodContext: aMethodContext
  288. ^ self new
  289. initializeFromMethodContext: aMethodContext;
  290. yourself
  291. ! !
  292. SemanticAnalyzer subclass: #AISemanticAnalyzer
  293. slots: {#context}
  294. package: 'Compiler-Interpreter'!
  295. !AISemanticAnalyzer commentStamp!
  296. I perform the same semantic analysis than `SemanticAnalyzer`, with the difference that provided an `AIContext` context, variables are bound with the context variables.!
  297. !AISemanticAnalyzer methodsFor: 'accessing'!
  298. context
  299. ^ context
  300. !
  301. context: anAIContext
  302. context := anAIContext
  303. ! !
  304. !AISemanticAnalyzer methodsFor: 'visiting'!
  305. visitVariableNode: aNode
  306. self context
  307. localAt: aNode identifier
  308. ifAbsent: [ ^ super visitVariableNode: aNode ].
  309. aNode binding: ASTContextVar new
  310. ! !
  311. ScopeVar subclass: #ASTContextVar
  312. slots: {#context}
  313. package: 'Compiler-Interpreter'!
  314. !ASTContextVar commentStamp!
  315. I am a variable defined in a `context`.!
  316. !ASTContextVar methodsFor: 'accessing'!
  317. context
  318. ^ context
  319. !
  320. context: anObject
  321. context := anObject
  322. ! !
  323. Object subclass: #ASTDebugger
  324. slots: {#interpreter. #context. #result}
  325. package: 'Compiler-Interpreter'!
  326. !ASTDebugger commentStamp!
  327. I am a stepping debugger interface for Amber code.
  328. I internally use an instance of `ASTInterpreter` to actually step through node and interpret them.
  329. My instances are created from an `AIContext` with `ASTDebugger class >> context:`.
  330. They hold an `AIContext` instance internally, recursive copy of the `MethodContext`.
  331. ## API
  332. Use the methods of the `'stepping'` protocol to do stepping.!
  333. !ASTDebugger methodsFor: 'accessing'!
  334. context
  335. ^ context
  336. !
  337. context: aContext
  338. context := aContext
  339. !
  340. interpreter
  341. ^ self context ifNotNil: [ :ctx |
  342. ctx interpreter ]
  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. slots: {#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. slots: {#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 parent |
  488. nd := self node.
  489. parent := nd parent.
  490. (parent ifNotNil: [ parent nextSiblingNode: nd ])
  491. ifNil: [ self node: parent ]
  492. ifNotNil: [ :sibling | self node: sibling; enterNode ]
  493. !
  494. proceed
  495. "Eagerly evaluate the ast"
  496. [ self atEnd ]
  497. whileFalse: [ self step ]
  498. !
  499. restart
  500. self node: self context ast; enterNode
  501. !
  502. setNonLocalReturnFromContext: aContext
  503. aContext interpreter hasReturned ifTrue: [
  504. returned := true.
  505. self returnValue: aContext interpreter returnValue ]
  506. !
  507. skip
  508. self next
  509. !
  510. step
  511. self
  512. interpret;
  513. next
  514. !
  515. stepOver
  516. self step.
  517. [ self node isNil or: [ self node isSteppingNode ] ] whileFalse: [
  518. self step ]
  519. ! !
  520. !ASTInterpreter methodsFor: 'private'!
  521. assign: aNode to: anObject
  522. aNode binding isInstanceVar
  523. ifTrue: [ self context receiver instVarAt: aNode identifier put: anObject ]
  524. ifFalse: [ self context localAt: aNode identifier put: anObject ]
  525. !
  526. eval: aString
  527. "Evaluate aString as JS source inside an JS function.
  528. aString is not sandboxed."
  529. | source function |
  530. source := String streamContents: [ :str |
  531. str nextPutAll: '0,(function('.
  532. self context locals keys
  533. do: [ :each | str nextPutAll: each ]
  534. separatedBy: [ str nextPutAll: ',' ].
  535. str
  536. nextPutAll: '){ return (function() {';
  537. nextPutAll: aString;
  538. nextPutAll: '})()})' ].
  539. function := Compiler eval: source.
  540. ^ function valueWithPossibleArguments: self context locals values
  541. !
  542. messageFromSendNode: aSendNode arguments: anArray
  543. ^ Message selector: aSendNode selector arguments: anArray
  544. !
  545. messageNotUnderstood: aMessage receiver: anObject
  546. MessageNotUnderstood new
  547. message: aMessage;
  548. receiver: anObject;
  549. signal
  550. !
  551. sendMessage: aMessage to: anObject superSend: aBoolean
  552. | method |
  553. aBoolean ifFalse: [ ^ aMessage sendTo: anObject ].
  554. anObject class superclass ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  555. method := (self context method methodClass superclass lookupSelector: aMessage selector)
  556. ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  557. ^ method sendTo: anObject arguments: aMessage arguments
  558. ! !
  559. !ASTInterpreter methodsFor: 'stack'!
  560. peek
  561. "Peek the top object of the context stack"
  562. self stack ifEmpty: [ ^ nil ].
  563. ^ self stack last
  564. !
  565. pop
  566. "Pop an object from the context stack"
  567. | peekedValue |
  568. peekedValue := self peek.
  569. self stack removeLast.
  570. ^ peekedValue
  571. !
  572. push: anObject
  573. "Push an object to the context stack"
  574. ^ self stack add: anObject
  575. ! !
  576. !ASTInterpreter methodsFor: 'testing'!
  577. atEnd
  578. forceAtEnd ifTrue: [ ^ true ].
  579. ^ self hasReturned or: [ self node isNil ]
  580. !
  581. hasReturned
  582. ^ returned ifNil: [ false ]
  583. ! !
  584. !ASTInterpreter methodsFor: 'visiting'!
  585. visit: aNode
  586. self hasReturned ifFalse: [ super visit: aNode ]
  587. !
  588. visitAssignmentNode: aNode
  589. | poppedValue |
  590. poppedValue := self pop.
  591. "Pop the left side of the assignment.
  592. It already has been visited, and we don't need its value."
  593. self pop.
  594. self push: poppedValue.
  595. self assign: aNode left to: poppedValue
  596. !
  597. visitBlockNode: aNode
  598. "Do not evaluate the block node.
  599. Instead, put all instructions into a block that we push to the stack for later evaluation"
  600. | block |
  601. block := AIBlockClosure forContext: self context node: aNode.
  602. self push: block
  603. !
  604. visitBlockSequenceNode: aNode
  605. "If the receiver is actually visiting a BlockSequenceNode,
  606. it means the the context is a block context. Evaluation should
  607. stop right after evaluating the block sequence and the outer
  608. context's interpreter should take over.
  609. Therefore we force #atEnd."
  610. super visitBlockSequenceNode: aNode.
  611. forceAtEnd := true
  612. !
  613. visitDagNode: aNode
  614. "Do nothing by default. Especially, do not visit children recursively."
  615. !
  616. visitDynamicArrayNode: aNode
  617. | array |
  618. array := #().
  619. aNode dagChildren do: [ :each |
  620. array addFirst: self pop ].
  621. self push: array
  622. !
  623. visitDynamicDictionaryNode: aNode
  624. | keyValueList |
  625. keyValueList := OrderedCollection new.
  626. aNode dagChildren do: [ :each |
  627. keyValueList add: self pop ].
  628. self push: (HashedCollection newFromPairs: keyValueList reversed)
  629. !
  630. visitJSStatementNode: aNode
  631. returned := true.
  632. self returnValue: (self eval: aNode source)
  633. !
  634. visitReturnNode: aNode
  635. returned := true.
  636. self returnValue: self pop
  637. !
  638. visitSendNode: aNode
  639. | receiver args message result |
  640. args := aNode arguments collect: [ :each | self pop ].
  641. receiver := self peek.
  642. message := self
  643. messageFromSendNode: aNode
  644. arguments: args reversed.
  645. result := self sendMessage: message to: receiver superSend: aNode superSend.
  646. "For cascade sends, push the reciever if the send is not the last one"
  647. aNode isSideEffect ifFalse: [ self pop; push: result ]
  648. !
  649. visitValueNode: aNode
  650. self push: aNode value
  651. !
  652. visitVariableNode: aNode
  653. aNode binding isExternallyKnownVar ifTrue: [
  654. ^ self push: (Platform globals at: aNode identifier ifAbsent: [ self error: 'Unknown variable' ]) ].
  655. self push: (aNode binding isInstanceVar
  656. ifTrue: [ self context receiver instVarAt: aNode identifier ]
  657. ifFalse: [ self context
  658. localAt: (aNode binding isSuper ifTrue: [ 'self' ] ifFalse: [ aNode identifier ])
  659. ifAbsent: [
  660. aNode identifier = 'nil' ifTrue: [ nil ] ifFalse: [
  661. aNode identifier = 'true' ifTrue: [ true ] ifFalse: [
  662. aNode identifier = 'false' ifTrue: [ false ] ifFalse: [
  663. aNode identifier isCapitalized
  664. ifTrue: [
  665. Smalltalk globals
  666. at: aNode identifier
  667. ifAbsent: [ Platform globals at: aNode identifier ] ] ] ] ] ] ])
  668. ! !
  669. Error subclass: #ASTInterpreterError
  670. slots: {}
  671. package: 'Compiler-Interpreter'!
  672. !ASTInterpreterError commentStamp!
  673. I get signaled when an AST interpreter is unable to interpret a node.!
  674. NodeVisitor subclass: #ASTPCNodeVisitor
  675. slots: {#index. #trackedIndex. #selector. #currentNode}
  676. package: 'Compiler-Interpreter'!
  677. !ASTPCNodeVisitor commentStamp!
  678. I visit an AST until I get to the current node for the `context` and answer it.
  679. ## API
  680. My instances must be filled with a context object using `#context:`.
  681. After visiting the AST the current node is answered by `#currentNode`!
  682. !ASTPCNodeVisitor methodsFor: 'accessing'!
  683. currentNode
  684. ^ currentNode
  685. !
  686. increaseTrackedIndex
  687. trackedIndex := self trackedIndex + 1
  688. !
  689. index
  690. ^ index
  691. !
  692. index: aNumber
  693. index := aNumber
  694. !
  695. selector
  696. ^ selector
  697. !
  698. selector: aString
  699. selector := aString
  700. !
  701. trackedIndex
  702. ^ trackedIndex ifNil: [ trackedIndex := 0 ]
  703. ! !
  704. !ASTPCNodeVisitor methodsFor: 'visiting'!
  705. visitJSStatementNode: aNode
  706. "If a JSStatementNode is encountered, it always is the current node.
  707. Stop visiting the AST there"
  708. currentNode := aNode
  709. !
  710. visitSendNode: aNode
  711. super visitSendNode: aNode.
  712. self selector = aNode selector ifTrue: [
  713. self trackedIndex = self index ifTrue: [ currentNode := aNode ].
  714. self increaseTrackedIndex ]
  715. ! !
  716. AIContext setTraitComposition: {TMethodContext} asTraitComposition!
  717. ! !
  718. !ASTNode methodsFor: '*Compiler-Interpreter'!
  719. isSteppingNode
  720. ^ false
  721. !
  722. nextSiblingNode: aNode
  723. "Answer the next node after aNode or nil"
  724. ^ self dagChildren
  725. at: (self dagChildren indexOf: aNode) + 1
  726. ifAbsent: [ nil ]
  727. ! !
  728. !AssignmentNode methodsFor: '*Compiler-Interpreter'!
  729. isSteppingNode
  730. ^ true
  731. ! !
  732. !BlockNode methodsFor: '*Compiler-Interpreter'!
  733. isSteppingNode
  734. ^ true
  735. !
  736. nextSiblingNode: aNode
  737. "Answer nil as we want to avoid eager evaluation"
  738. "In fact, this should not have been called, ever. IMO. -- herby"
  739. ^ nil
  740. ! !
  741. !DynamicArrayNode methodsFor: '*Compiler-Interpreter'!
  742. isSteppingNode
  743. ^ true
  744. ! !
  745. !DynamicDictionaryNode methodsFor: '*Compiler-Interpreter'!
  746. isSteppingNode
  747. ^ true
  748. ! !
  749. !JSStatementNode methodsFor: '*Compiler-Interpreter'!
  750. isSteppingNode
  751. ^ true
  752. ! !
  753. !SendNode methodsFor: '*Compiler-Interpreter'!
  754. isSteppingNode
  755. ^ true
  756. ! !