1
0

Compiler-Interpreter.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. Smalltalk current 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. AIInterpreterError 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 newBlockContext.
  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. "Populate the arguments into the context locals"
  49. node parameters withIndexDo: [ :each :index |
  50. context localAt: each put: (aCollection at: index ifAbsent: [ nil ]) ].
  51. "Interpret the first node of the BlockSequenceNode"
  52. context interpreter
  53. node: sequenceNode nextChild;
  54. proceed.
  55. outerContext interpreter
  56. setNonLocalReturnFromContext: context.
  57. ^ context interpreter pop
  58. ! !
  59. !AIBlockClosure methodsFor: 'initialization'!
  60. initializeWithContext: aContext node: aNode
  61. node := aNode.
  62. outerContext := aContext
  63. ! !
  64. !AIBlockClosure class methodsFor: 'instance creation'!
  65. forContext: aContext node: aNode
  66. ^ self new
  67. initializeWithContext: aContext node: aNode;
  68. yourself
  69. ! !
  70. Object subclass: #AIContext
  71. instanceVariableNames: 'outerContext innerContext pc locals method index ast interpreter'
  72. package: 'Compiler-Interpreter'!
  73. !AIContext commentStamp!
  74. I am like a `MethodContext`, used by the `ASTInterpreter`.
  75. Unlike a `MethodContext`, my instances are not read-only.
  76. When debugging, my instances are created by copying the current `MethodContext` (thisContext)!
  77. !AIContext methodsFor: 'accessing'!
  78. index
  79. ^ index ifNil: [ 0 ]
  80. !
  81. index: anInteger
  82. index := anInteger
  83. !
  84. innerContext
  85. ^ innerContext
  86. !
  87. innerContext: anAIContext
  88. innerContext := anAIContext
  89. !
  90. localAt: aString
  91. "Lookup the local value up to the method context"
  92. ^ self locals at: aString ifAbsent: [
  93. self isBlockContext
  94. ifTrue: [ nil ]
  95. ifFalse: [ self outerContext localAt: aString ] ]
  96. !
  97. localAt: aString put: anObject
  98. self locals at: aString put: anObject
  99. !
  100. locals
  101. locals ifNil: [ self initializeLocals ].
  102. ^ locals
  103. !
  104. method
  105. ^ method
  106. !
  107. method: aCompiledMethod
  108. method := aCompiledMethod
  109. !
  110. methodContext
  111. self isBlockContext ifFalse: [ ^ self ].
  112. ^ self outerContext ifNotNil: [ :outer |
  113. outer methodContext ]
  114. !
  115. outerContext
  116. ^ outerContext
  117. !
  118. outerContext: anAIContext
  119. outerContext := anAIContext.
  120. outerContext innerContext: self
  121. !
  122. selector
  123. ^ self method ifNotNil: [
  124. self method selector ]
  125. ! !
  126. !AIContext methodsFor: 'converting'!
  127. asString
  128. ^self isBlockContext
  129. ifTrue: [ 'a block (in ', self methodContext asString, ')' ]
  130. ifFalse: [ self receiver class name, ' >> ', self selector ]
  131. ! !
  132. !AIContext methodsFor: 'factory'!
  133. newBlockContext
  134. ^ self class new
  135. outerContext: self;
  136. yourself
  137. ! !
  138. !AIContext methodsFor: 'initialization'!
  139. initializeAST
  140. ast := self method ast.
  141. (SemanticAnalyzer on: self method methodClass)
  142. visit: ast
  143. !
  144. initializeFromMethodContext: aMethodContext
  145. self
  146. pc: aMethodContext pc;
  147. index: aMethodContext index;
  148. receiver: aMethodContext receiver;
  149. method: aMethodContext method.
  150. aMethodContext outerContext ifNotNil: [ :outer |
  151. "If the method context is nil, the block was defined in JS, so ignore it"
  152. outer methodContext ifNotNil: [
  153. self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].
  154. aMethodContext locals keysAndValuesDo: [ :key :value |
  155. self locals at: key put: value ] ]
  156. !
  157. initializeInterpreter
  158. interpreter := Interpreter new
  159. context: self;
  160. yourself.
  161. ast ifNotNil: [ interpreter node: self retrieveNode ].
  162. (self innerContext notNil and: [
  163. self innerContext isBlockContext not ]) ifTrue: [
  164. self setupInterpreter: interpreter ]
  165. !
  166. initializeLocals
  167. locals := Dictionary new.
  168. locals at: 'thisContext' put: self.
  169. ! !
  170. !AIContext methodsFor: 'interpreting'!
  171. arguments
  172. ^ self ast arguments collect: [ :each |
  173. self localAt: each ]
  174. !
  175. ast
  176. self isBlockContext ifTrue: [ ^ self outerContext ast ].
  177. ast ifNil: [ self initializeAST ].
  178. ^ ast
  179. !
  180. interpreter
  181. interpreter ifNil: [ self initializeInterpreter ].
  182. ^ interpreter
  183. !
  184. interpreter: anInterpreter
  185. interpreter := anInterpreter
  186. !
  187. pc
  188. ^ pc ifNil: [ pc := 0 ]
  189. !
  190. pc: anInteger
  191. pc := anInteger
  192. !
  193. receiver
  194. ^ self localAt: 'self'
  195. !
  196. receiver: anObject
  197. self localAt: 'self' put: anObject
  198. !
  199. retrieveNode
  200. ^ ASTPCNodeVisitor new
  201. context: self;
  202. visit: self ast;
  203. currentNode
  204. !
  205. setupInterpreter: anInterpreter
  206. "Push the send args and receiver to the interpreter stack"
  207. self innerContext arguments reversed do: [ :each |
  208. anInterpreter push: each ].
  209. anInterpreter push: (self innerContext receiver)
  210. ! !
  211. !AIContext methodsFor: 'testing'!
  212. isBlockContext
  213. "Block context do not have selectors."
  214. ^ self selector isNil
  215. ! !
  216. !AIContext class methodsFor: 'instance creation'!
  217. fromMethodContext: aMethodContext
  218. ^ self new
  219. initializeFromMethodContext: aMethodContext;
  220. yourself
  221. ! !
  222. Error subclass: #AIInterpreterError
  223. instanceVariableNames: ''
  224. package: 'Compiler-Interpreter'!
  225. !AIInterpreterError commentStamp!
  226. I get signaled when an AST interpreter is unable to interpret a node.!
  227. Object subclass: #ASTDebugger
  228. instanceVariableNames: 'interpreter context'
  229. package: 'Compiler-Interpreter'!
  230. !ASTDebugger commentStamp!
  231. I am a stepping debugger interface for Amber code.
  232. I internally use an instance of `ASTSteppingInterpreter` to actually step through node and interpret them.
  233. My instances are created from a `MethodContext` with `ASTDebugger class >> context:`.
  234. They hold an `AIContext` instance internally, recursive copy of the `MethodContext`.
  235. ## API
  236. Use the methods of the `'stepping'` protocol to do stepping.!
  237. !ASTDebugger methodsFor: 'accessing'!
  238. context
  239. ^ context
  240. !
  241. context: aContext
  242. context := aContext
  243. !
  244. interpreter
  245. ^ interpreter ifNil: [ interpreter := self defaultInterpreterClass new ]
  246. !
  247. interpreter: anInterpreter
  248. interpreter := anInterpreter
  249. !
  250. method
  251. ^ self context method
  252. !
  253. nextNode
  254. ^ self interpreter nextNode
  255. ! !
  256. !ASTDebugger methodsFor: 'defaults'!
  257. defaultInterpreterClass
  258. ^ ASTSteppingInterpreter
  259. ! !
  260. !ASTDebugger methodsFor: 'initialization'!
  261. buildAST
  262. "Build the AST tree from the method source code.
  263. The AST is annotated with a SemanticAnalyzer,
  264. to know the semantics and bindings of each node needed for later debugging"
  265. | ast |
  266. ast := Smalltalk current parse: self method source.
  267. (SemanticAnalyzer on: self context receiver class)
  268. visit: ast.
  269. ^ ast
  270. !
  271. initializeInterpreter
  272. | ast next |
  273. ast := self buildAST.
  274. next := ASTPCNodeVisitor new
  275. context: self context;
  276. visit: ast;
  277. currentNode.
  278. self interpreter interpret: next
  279. !
  280. initializeWithContext: aContext
  281. "TODO: do we need to handle block contexts?"
  282. self context: aContext.
  283. self initializeInterpreter
  284. ! !
  285. !ASTDebugger methodsFor: 'stepping'!
  286. proceed
  287. self shouldBeImplemented
  288. !
  289. restart
  290. self shouldBeImplemented
  291. !
  292. step
  293. "The ASTSteppingInterpreter stops at each node interpretation.
  294. One step will interpret nodes until:
  295. - we get at the end
  296. - the next node is a stepping node (send, assignment, etc.)"
  297. [ (self interpreter nextNode notNil and: [ self interpreter nextNode stopOnStepping ])
  298. or: [ self interpreter atEnd not ] ]
  299. whileFalse: [
  300. self interpreter step.
  301. self step ]
  302. !
  303. stepInto
  304. self shouldBeImplemented
  305. !
  306. stepOver
  307. self step
  308. ! !
  309. !ASTDebugger methodsFor: 'testing'!
  310. atEnd
  311. ^ self interpreter atEnd
  312. ! !
  313. !ASTDebugger class methodsFor: 'instance creation'!
  314. context: aContext
  315. ^ self new
  316. initializeWithContext: aContext;
  317. yourself
  318. ! !
  319. Object subclass: #ASTInterpreter
  320. instanceVariableNames: 'currentNode nextNode context shouldReturn result'
  321. package: 'Compiler-Interpreter'!
  322. !ASTInterpreter commentStamp!
  323. I am like a `NodeVisitor`, interpreting nodes one after each other.
  324. I am built using Continuation Passing Style for stepping purposes.
  325. ## Usage example:
  326. | ast interpreter |
  327. ast := Smalltalk current parse: 'foo 1+2+4'.
  328. (SemanticAnalyzer on: Object) visit: ast.
  329. ASTInterpreter new
  330. interpret: ast nodes first;
  331. result "Answers 7"!
  332. !ASTInterpreter methodsFor: 'accessing'!
  333. context
  334. ^ context ifNil: [ context := AIContext new ]
  335. !
  336. context: anAIContext
  337. context := anAIContext
  338. !
  339. currentNode
  340. ^ currentNode
  341. !
  342. nextNode
  343. ^ nextNode ifNil: [ self currentNode ]
  344. !
  345. nextNode: aNode
  346. nextNode := aNode
  347. !
  348. result
  349. ^ result
  350. ! !
  351. !ASTInterpreter methodsFor: 'initialization'!
  352. initialize
  353. super initialize.
  354. shouldReturn := false
  355. ! !
  356. !ASTInterpreter methodsFor: 'interpreting'!
  357. interpret: aNode
  358. shouldReturn := false.
  359. self interpret: aNode continue: [ :value |
  360. result := value ]
  361. !
  362. interpret: aNode continue: aBlock
  363. shouldReturn ifTrue: [ ^ self ].
  364. aNode isNode
  365. ifTrue: [
  366. currentNode := aNode.
  367. self interpretNode: aNode continue: [ :value |
  368. self continue: aBlock value: value ] ]
  369. ifFalse: [ self continue: aBlock value: aNode ]
  370. !
  371. interpretAssignmentNode: aNode continue: aBlock
  372. self interpret: aNode right continue: [ :value |
  373. self
  374. continue: aBlock
  375. value: (self assign: aNode left to: value) ]
  376. !
  377. interpretBlockNode: aNode continue: aBlock
  378. self
  379. continue: aBlock
  380. value: [
  381. self withBlockContext: [
  382. self interpret: aNode nodes first; result ] ]
  383. !
  384. interpretBlockSequenceNode: aNode continue: aBlock
  385. self interpretSequenceNode: aNode continue: aBlock
  386. !
  387. interpretCascadeNode: aNode continue: aBlock
  388. "TODO: Handle super sends"
  389. self interpret: aNode receiver continue: [ :receiver |
  390. "Only interpret the receiver once"
  391. aNode nodes do: [ :each | each receiver: receiver ].
  392. self
  393. interpretAll: aNode nodes allButLast
  394. continue: [
  395. self
  396. interpret: aNode nodes last
  397. continue: [ :val | self continue: aBlock value: val ] ] ]
  398. !
  399. interpretClassReferenceNode: aNode continue: aBlock
  400. self continue: aBlock value: (Smalltalk current at: aNode value)
  401. !
  402. interpretDynamicArrayNode: aNode continue: aBlock
  403. self interpretAll: aNode nodes continue: [ :array |
  404. self
  405. continue: aBlock
  406. value: array ]
  407. !
  408. interpretDynamicDictionaryNode: aNode continue: aBlock
  409. self interpretAll: aNode nodes continue: [ :array | | hashedCollection |
  410. hashedCollection := HashedCollection new.
  411. array do: [ :each | hashedCollection add: each ].
  412. self
  413. continue: aBlock
  414. value: hashedCollection ]
  415. !
  416. interpretJSStatementNode: aNode continue: aBlock
  417. shouldReturn := true.
  418. self continue: aBlock value: (self eval: aNode source)
  419. !
  420. interpretMethodNode: aNode continue: aBlock
  421. self interpretAll: aNode nodes continue: [ :array |
  422. self continue: aBlock value: array first ]
  423. !
  424. interpretNode: aNode continue: aBlock
  425. aNode interpreter: self continue: aBlock
  426. !
  427. interpretReturnNode: aNode continue: aBlock
  428. self interpret: aNode nodes first continue: [ :value |
  429. shouldReturn := true.
  430. self continue: aBlock value: value ]
  431. !
  432. interpretSendNode: aNode continue: aBlock
  433. self interpret: aNode receiver continue: [ :receiver |
  434. self interpretAll: aNode arguments continue: [ :args |
  435. self
  436. messageFromSendNode: aNode
  437. arguments: args
  438. do: [ :message |
  439. self context pc: self context pc + 1.
  440. self
  441. continue: aBlock
  442. value: (self sendMessage: message to: receiver superSend: aNode superSend) ] ] ]
  443. !
  444. interpretSequenceNode: aNode continue: aBlock
  445. self interpretAll: aNode nodes continue: [ :array |
  446. self continue: aBlock value: array last ]
  447. !
  448. interpretValueNode: aNode continue: aBlock
  449. self continue: aBlock value: aNode value
  450. !
  451. interpretVariableNode: aNode continue: aBlock
  452. self
  453. continue: aBlock
  454. value: (aNode binding isInstanceVar
  455. ifTrue: [ self context receiver instVarAt: aNode value ]
  456. ifFalse: [ self context localAt: aNode value ])
  457. ! !
  458. !ASTInterpreter methodsFor: 'private'!
  459. assign: aNode to: anObject
  460. ^ aNode binding isInstanceVar
  461. ifTrue: [ self context receiver instVarAt: aNode value put: anObject ]
  462. ifFalse: [ self context localAt: aNode value put: anObject ]
  463. !
  464. continue: aBlock value: anObject
  465. result := anObject.
  466. aBlock value: anObject
  467. !
  468. eval: aString
  469. "Evaluate aString as JS source inside an JS function.
  470. aString is not sandboxed."
  471. | source function |
  472. source := String streamContents: [ :str |
  473. str nextPutAll: '(function('.
  474. self context locals keys
  475. do: [ :each | str nextPutAll: each ]
  476. separatedBy: [ str nextPutAll: ',' ].
  477. str
  478. nextPutAll: '){ return (function() {';
  479. nextPutAll: aString;
  480. nextPutAll: '})() })' ].
  481. function := Compiler new eval: source.
  482. ^ function valueWithPossibleArguments: self context locals values
  483. !
  484. interpretAll: aCollection continue: aBlock
  485. self
  486. interpretAll: aCollection
  487. continue: aBlock
  488. result: OrderedCollection new
  489. !
  490. interpretAll: nodes continue: aBlock result: aCollection
  491. nodes isEmpty
  492. ifTrue: [ self continue: aBlock value: aCollection ]
  493. ifFalse: [
  494. self interpret: nodes first continue: [:value |
  495. self
  496. interpretAll: nodes allButFirst
  497. continue: aBlock
  498. result: aCollection, { value } ] ]
  499. !
  500. messageFromSendNode: aSendNode arguments: aCollection do: aBlock
  501. self
  502. continue: aBlock
  503. value: (Message new
  504. selector: aSendNode selector;
  505. arguments: aCollection;
  506. yourself)
  507. !
  508. sendMessage: aMessage to: anObject superSend: aBoolean
  509. | method |
  510. aBoolean ifFalse: [ ^ aMessage sendTo: anObject ].
  511. anObject class superclass ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  512. method := anObject class superclass methodDictionary
  513. at: aMessage selector
  514. ifAbsent: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  515. ^ method fn applyTo: anObject arguments: aMessage arguments
  516. !
  517. withBlockContext: aBlock
  518. "Evaluate aBlock with a BlockContext:
  519. - a context is pushed before aBlock evaluation.
  520. - the context is poped after aBlock evaluation
  521. - the result of aBlock evaluation is answered"
  522. | blockResult |
  523. self context: (AIContext new
  524. outerContext: self context;
  525. yourself).
  526. blockResult := aBlock value.
  527. self context: self context outerContext.
  528. ^ blockResult
  529. ! !
  530. !ASTInterpreter methodsFor: 'testing'!
  531. shouldReturn
  532. ^ shouldReturn ifNil: [ false ]
  533. ! !
  534. ASTInterpreter subclass: #ASTSteppingInterpreter
  535. instanceVariableNames: 'continuation nextNode'
  536. package: 'Compiler-Interpreter'!
  537. !ASTSteppingInterpreter commentStamp!
  538. I am an interpreter with stepping capabilities. The higher level `ASTDebugger` class should be used as a debugger model, as it provides convenience methods for debugging.
  539. ## API
  540. Use `#step` to actually interpret the next node. Interpretation stops at each node evaluation, weither it's a message node or not.
  541. ## Usage example:
  542. | ast interpreter |
  543. ast := Smalltalk current parse: 'foo 1+2+4'.
  544. (SemanticAnalyzer on: Object) visit: ast.
  545. interpreter := ASTSteppingInterpreter new
  546. interpret: ast nodes first;
  547. yourself.
  548. interpreter step; step.
  549. interpreter step; step.
  550. interpreter result."Answers 1"
  551. interpreter step.
  552. interpreter result. "Answers 3"
  553. interpreter step.
  554. interpreter result. "Answers 7"!
  555. !ASTSteppingInterpreter methodsFor: 'accessing'!
  556. nextNode
  557. ^ nextNode
  558. ! !
  559. !ASTSteppingInterpreter methodsFor: 'initialization'!
  560. initialize
  561. super initialize.
  562. continuation := []
  563. ! !
  564. !ASTSteppingInterpreter methodsFor: 'interpreting'!
  565. interpret: aNode continue: aBlock
  566. nextNode := aNode.
  567. continuation := [
  568. super interpret: aNode continue: aBlock ]
  569. ! !
  570. !ASTSteppingInterpreter methodsFor: 'stepping'!
  571. step
  572. continuation value
  573. ! !
  574. !ASTSteppingInterpreter methodsFor: 'testing'!
  575. atEnd
  576. ^ self shouldReturn or: [ self nextNode == self currentNode ]
  577. ! !
  578. NodeVisitor subclass: #ASTPCNodeVisitor
  579. instanceVariableNames: 'useInlinings pc context blockIndex currentNode'
  580. package: 'Compiler-Interpreter'!
  581. !ASTPCNodeVisitor commentStamp!
  582. I visit an AST until I get to the current pc node and answer it.
  583. ## API
  584. My instances must be filled with a context object using `#context:`.
  585. After visiting the AST the current node corresponding to the `pc` is answered by `#currentNode`!
  586. !ASTPCNodeVisitor methodsFor: 'accessing'!
  587. blockIndex
  588. ^ blockIndex ifNil: [ blockIndex := 0 ]
  589. !
  590. context
  591. ^ context
  592. !
  593. context: aContext
  594. context := aContext
  595. !
  596. currentNode
  597. ^ currentNode
  598. !
  599. increaseBlockIndex
  600. blockIndex := self blockIndex + 1
  601. !
  602. pc
  603. ^ pc ifNil: [ 0 ]
  604. !
  605. pc: anInteger
  606. pc := anInteger
  607. !
  608. useInlinings
  609. ^ useInlinings ifNil: [ true ]
  610. !
  611. useInlinings: aBoolean
  612. useInlinings := aBoolean
  613. ! !
  614. !ASTPCNodeVisitor methodsFor: 'visiting'!
  615. visitBlockNode: aNode
  616. "Inlined send node. Assume that the block is inlined"
  617. (aNode parent isSendNode and: [ aNode parent shouldBeInlined ])
  618. ifFalse: [
  619. self blockIndex >= self context index ifFalse: [
  620. self increaseBlockIndex.
  621. super visitBlockNode: aNode ] ]
  622. ifTrue: [ super visitBlockNode: aNode ]
  623. !
  624. visitJSStatementNode: aNode
  625. currentNode := aNode
  626. !
  627. visitSendNode: aNode
  628. super visitSendNode: aNode.
  629. self pc = self context pc ifFalse: [
  630. aNode shouldBeInlined ifFalse: [
  631. self blockIndex = self context index ifTrue: [
  632. self pc: self pc + 1.
  633. currentNode := aNode ] ] ]
  634. ! !
  635. NodeVisitor subclass: #Interpreter
  636. instanceVariableNames: 'node context stack returnValue returned'
  637. package: 'Compiler-Interpreter'!
  638. !Interpreter methodsFor: 'accessing'!
  639. context
  640. ^ context
  641. !
  642. context: aContext
  643. context := aContext
  644. !
  645. node
  646. "Answer the next node, ie the node to be evaluated in the next step"
  647. ^ node
  648. !
  649. node: aNode
  650. node := aNode
  651. !
  652. result
  653. ^ self hasReturned
  654. ifTrue: [ self returnValue ]
  655. ifFalse: [ self context receiver ]
  656. !
  657. returnValue
  658. ^ returnValue
  659. !
  660. returnValue: anObject
  661. returnValue := anObject
  662. !
  663. stack
  664. ^ stack ifNil: [ stack := OrderedCollection new ]
  665. ! !
  666. !Interpreter methodsFor: 'interpreting'!
  667. interpret
  668. "Interpret the next node to be evaluated"
  669. self visit: self node
  670. !
  671. interpret: aNode
  672. self node: aNode.
  673. self interpret
  674. !
  675. next
  676. self node: self node nextNode
  677. !
  678. proceed
  679. "Eagerly evaluate the ast"
  680. [ self atEnd ] whileFalse: [
  681. self step ]
  682. !
  683. restart
  684. self node: self context ast nextChild
  685. !
  686. skip
  687. self next
  688. !
  689. step
  690. self
  691. interpret;
  692. next
  693. !
  694. stepOver
  695. self step.
  696. [ self node isSteppingNode ] whileFalse: [
  697. self step ]
  698. !
  699. setNonLocalReturnFromContext: aContext
  700. aContext interpreter hasReturned ifTrue: [
  701. returned := true.
  702. self returnValue: aContext interpreter returnValue ]
  703. ! !
  704. !Interpreter methodsFor: 'private'!
  705. assign: aNode to: anObject
  706. aNode binding isInstanceVar
  707. ifTrue: [ self context receiver instVarAt: aNode value put: anObject ]
  708. ifFalse: [ self context localAt: aNode value put: anObject ]
  709. !
  710. eval: aString
  711. "Evaluate aString as JS source inside an JS function.
  712. aString is not sandboxed."
  713. | source function |
  714. source := String streamContents: [ :str |
  715. str nextPutAll: '(function('.
  716. self context locals keys
  717. do: [ :each | str nextPutAll: each ]
  718. separatedBy: [ str nextPutAll: ',' ].
  719. str
  720. nextPutAll: '){ return (function() {';
  721. nextPutAll: aString;
  722. nextPutAll: '})() })' ].
  723. function := Compiler new eval: source.
  724. ^ function valueWithPossibleArguments: self context locals values
  725. !
  726. messageFromSendNode: aSendNode arguments: aCollection
  727. ^ Message new
  728. selector: aSendNode selector;
  729. arguments: aCollection;
  730. yourself
  731. !
  732. messageNotUnderstood: aMessage receiver: anObject
  733. MessageNotUnderstood new
  734. meesage: aMessage;
  735. receiver: anObject;
  736. signal
  737. !
  738. sendMessage: aMessage to: anObject superSend: aBoolean
  739. | method |
  740. aBoolean ifFalse: [ ^ aMessage sendTo: anObject ].
  741. anObject class superclass ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  742. method := anObject class superclass methodDictionary
  743. at: aMessage selector
  744. ifAbsent: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  745. ^ method sendTo: anObject arguments: aMessage arguments
  746. ! !
  747. !Interpreter methodsFor: 'stack'!
  748. peek
  749. "Peek the top object of the context stack"
  750. self stack ifEmpty: [ ^ nil ].
  751. ^ self stack last
  752. !
  753. pop
  754. "Pop an object from the context stack"
  755. | peekedValue |
  756. peekedValue := self peek.
  757. self stack removeLast.
  758. ^ peekedValue
  759. !
  760. push: anObject
  761. "Push an object to the context stack"
  762. ^ self stack add: anObject
  763. ! !
  764. !Interpreter methodsFor: 'testing'!
  765. atEnd
  766. ^ self hasReturned or: [ self node isNil ]
  767. !
  768. hasReturned
  769. ^ returned ifNil: [ false ]
  770. ! !
  771. !Interpreter methodsFor: 'visiting'!
  772. visit: aNode
  773. self hasReturned ifFalse: [ super visit: aNode ]
  774. !
  775. visitAssignmentNode: aNode
  776. | poppedValue |
  777. poppedValue := self pop.
  778. "Pop the left side of the assignment.
  779. It already has been visited, and we don't need its value."
  780. self pop.
  781. self push: poppedValue.
  782. self assign: aNode left to: poppedValue
  783. !
  784. visitBlockNode: aNode
  785. "Do not evaluate the block node.
  786. Instead, put all instructions into a block that we push to the stack for later evaluation"
  787. | block |
  788. block := AIBlockClosure forContext: self context node: aNode.
  789. self push: block
  790. !
  791. visitClassReferenceNode: aNode
  792. self push: (Smalltalk current
  793. at: aNode value
  794. ifAbsent: [ PlatformInterface globals at: aNode value ])
  795. !
  796. visitDynamicArrayNode: aNode
  797. | array |
  798. array := #().
  799. aNode nodes do: [ :each |
  800. array addFirst: self pop ].
  801. self push: array
  802. !
  803. visitDynamicDictionaryNode: aNode
  804. | hashedCollection |
  805. hashedCollection := HashedCollection new.
  806. aNode nodes do: [ :each |
  807. hashedCollection add: self pop ].
  808. self push: hashedCollection
  809. !
  810. visitJSStatementNode: aNode
  811. returned := true.
  812. self returnValue: (self eval: aNode source)
  813. !
  814. visitNode: aNode
  815. "Do nothing by default. Especially, do not visit children recursively."
  816. !
  817. visitReturnNode: aNode
  818. returned := true.
  819. self returnValue: self pop
  820. !
  821. visitSendNode: aNode
  822. | receiver args message result |
  823. args := aNode arguments collect: [ :each | self pop ].
  824. receiver := self pop.
  825. message := self
  826. messageFromSendNode: aNode
  827. arguments: args reversed.
  828. result := self sendMessage: message to: receiver superSend: aNode superSend.
  829. self context pc: self context pc + 1.
  830. "For cascade sends, push the reciever if the send is not the last one"
  831. (aNode isCascadeSendNode and: [ aNode isLastChild not ])
  832. ifTrue: [ self push: receiver ]
  833. ifFalse: [ self push: result ]
  834. !
  835. visitValueNode: aNode
  836. self push: aNode value
  837. !
  838. visitVariableNode: aNode
  839. aNode binding isUnknownVar ifTrue: [
  840. ^ self push: (PlatformInterface globals at: aNode value ifAbsent: [ self error: 'Unknown variable' ]) ].
  841. self push: (aNode binding isInstanceVar
  842. ifTrue: [ self context receiver instVarAt: aNode value ]
  843. ifFalse: [ self context localAt: aNode value ])
  844. ! !
  845. !Node methodsFor: '*Compiler-Interpreter'!
  846. interpreter: anInterpreter continue: aBlock
  847. ^ anInterpreter interpretNode: self continue: aBlock
  848. !
  849. isSteppingNode
  850. ^ false
  851. ! !
  852. !AssignmentNode methodsFor: '*Compiler-Interpreter'!
  853. interpreter: anInterpreter continue: aBlock
  854. ^ anInterpreter interpretAssignmentNode: self continue: aBlock
  855. !
  856. isSteppingNode
  857. ^ true
  858. ! !
  859. !BlockNode methodsFor: '*Compiler-Interpreter'!
  860. interpreter: anInterpreter continue: aBlock
  861. ^ anInterpreter interpretBlockNode: self continue: aBlock
  862. !
  863. isSteppingNode
  864. ^ true
  865. ! !
  866. !CascadeNode methodsFor: '*Compiler-Interpreter'!
  867. interpreter: anInterpreter continue: aBlock
  868. ^ anInterpreter interpretCascadeNode: self continue: aBlock
  869. ! !
  870. !DynamicArrayNode methodsFor: '*Compiler-Interpreter'!
  871. interpreter: anInterpreter continue: aBlock
  872. ^ anInterpreter interpretDynamicArrayNode: self continue: aBlock
  873. !
  874. isSteppingNode
  875. ^ true
  876. ! !
  877. !DynamicDictionaryNode methodsFor: '*Compiler-Interpreter'!
  878. interpreter: anInterpreter continue: aBlock
  879. ^ anInterpreter interpretDynamicDictionaryNode: self continue: aBlock
  880. !
  881. isSteppingNode
  882. ^ true
  883. ! !
  884. !JSStatementNode methodsFor: '*Compiler-Interpreter'!
  885. interpreter: anInterpreter continue: aBlock
  886. ^ anInterpreter interpretJSStatementNode: self continue: aBlock
  887. !
  888. isSteppingNode
  889. ^ true
  890. ! !
  891. !MethodNode methodsFor: '*Compiler-Interpreter'!
  892. interpreter: anInterpreter continue: aBlock
  893. ^ anInterpreter interpretMethodNode: self continue: aBlock
  894. ! !
  895. !ReturnNode methodsFor: '*Compiler-Interpreter'!
  896. interpreter: anInterpreter continue: aBlock
  897. ^ anInterpreter interpretReturnNode: self continue: aBlock
  898. ! !
  899. !SendNode methodsFor: '*Compiler-Interpreter'!
  900. interpreter: anInterpreter continue: aBlock
  901. ^ anInterpreter interpretSendNode: self continue: aBlock
  902. !
  903. isSteppingNode
  904. ^ true
  905. ! !
  906. !SequenceNode methodsFor: '*Compiler-Interpreter'!
  907. interpreter: anInterpreter continue: aBlock
  908. ^ anInterpreter interpretSequenceNode: self continue: aBlock
  909. ! !
  910. !BlockSequenceNode methodsFor: '*Compiler-Interpreter'!
  911. interpreter: anInterpreter continue: aBlock
  912. ^ anInterpreter interpretBlockSequenceNode: self continue: aBlock
  913. ! !
  914. !ValueNode methodsFor: '*Compiler-Interpreter'!
  915. interpreter: anInterpreter continue: aBlock
  916. ^ anInterpreter interpretValueNode: self continue: aBlock
  917. ! !
  918. !VariableNode methodsFor: '*Compiler-Interpreter'!
  919. interpreter: anInterpreter continue: aBlock
  920. ^ anInterpreter interpretVariableNode: self continue: aBlock
  921. ! !
  922. !ClassReferenceNode methodsFor: '*Compiler-Interpreter'!
  923. interpreter: anInterpreter continue: aBlock
  924. ^ anInterpreter interpretClassReferenceNode: self continue: aBlock
  925. ! !