Compiler-Interpreter.st 20 KB

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