Compiler-Interpreter.st 21 KB

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