Compiler-Interpreter.st 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Smalltalk current createPackage: 'Compiler-Interpreter' properties: #{}!
  2. NodeVisitor subclass: #AIContext
  3. instanceVariableNames: 'outerContext pc locals receiver selector'
  4. package: 'Compiler-Interpreter'!
  5. !AIContext methodsFor: 'accessing'!
  6. initializeFromMethodContext: aMethodContext
  7. self pc: aMethodContext pc.
  8. self receiver: aMethodContext receiver.
  9. self selector: aMethodContext selector.
  10. aMethodContext outerContext ifNotNil: [
  11. self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].
  12. aMethodContext locals keysAndValuesDo: [ :key :value |
  13. self locals at: key put: value ]
  14. !
  15. localAt: aString put: anObject
  16. self locals at: aString put: anObject
  17. !
  18. locals
  19. ^ locals ifNil: [ locals := Dictionary new ]
  20. !
  21. outerContext
  22. ^ outerContext
  23. !
  24. outerContext: anAIContext
  25. outerContext := anAIContext
  26. !
  27. pc
  28. ^ pc ifNil: [ pc := 0 ]
  29. !
  30. pc: anInteger
  31. pc := anInteger
  32. !
  33. receiver
  34. ^ receiver
  35. !
  36. receiver: anObject
  37. receiver := anObject
  38. !
  39. selector
  40. ^ selector
  41. !
  42. selector: aString
  43. selector := aString
  44. ! !
  45. !AIContext class methodsFor: 'instance creation'!
  46. fromMethodContext: aMethodContext
  47. ^ self new
  48. initializeFromMethodContext: aMethodContext;
  49. yourself
  50. ! !
  51. NodeVisitor subclass: #ASTInterpreter
  52. instanceVariableNames: 'currentNode context shouldReturn'
  53. package: 'Compiler-Interpreter'!
  54. !ASTInterpreter methodsFor: 'accessing'!
  55. context
  56. ^ context ifNil: [ context := AIContext new ]
  57. !
  58. context: anAIContext
  59. context := anAIContext
  60. ! !
  61. !ASTInterpreter methodsFor: 'initialization'!
  62. initialize
  63. super initialize.
  64. shouldReturn := false
  65. ! !
  66. !ASTInterpreter methodsFor: 'interpreting'!
  67. eval: aString
  68. "Evaluate aString as JS source inside an immediately evaluated JS function.
  69. aString is not sandboxed."
  70. ^ Compiler new eval: '(function() { ', aString, ' })()'
  71. !
  72. interpret: aNode
  73. shouldReturn := false.
  74. ^ self interpretNode: aNode
  75. !
  76. interpretNode: aNode
  77. currentNode := aNode.
  78. ^ self visit: aNode
  79. !
  80. messageFromSendNode: aSendNode
  81. ^ Message new
  82. selector: aSendNode selector;
  83. arguments: (aSendNode arguments collect: [ :each |
  84. self interpretNode: each ]);
  85. yourself
  86. ! !
  87. !ASTInterpreter methodsFor: 'visiting'!
  88. visitBlockNode: aNode
  89. ^ [ self interpretNode: aNode nodes first ]
  90. !
  91. visitCascadeNode: aNode
  92. "TODO: Handle super sends"
  93. | receiver |
  94. receiver := self interpretNode: aNode receiver.
  95. aNode nodes allButLast
  96. do: [ :each |
  97. (self messageFromSendNode: each)
  98. sendTo: receiver ].
  99. ^ (self messageFromSendNode: aNode nodes last)
  100. sendTo: receiver
  101. !
  102. visitClassReferenceNode: aNode
  103. ^ Smalltalk current at: aNode value
  104. !
  105. visitJSStatementNode: aNode
  106. shouldReturn := true.
  107. ^ self eval: aNode source
  108. !
  109. visitReturnNode: aNode
  110. shouldReturn := true.
  111. ^ self interpretNode: aNode nodes first
  112. !
  113. visitSendNode: aNode
  114. "TODO: Handle super sends"
  115. ^ (self messageFromSendNode: aNode)
  116. sendTo: (self interpretNode: aNode receiver)
  117. !
  118. visitSequenceNode: aNode
  119. aNode nodes allButLast do: [ :each | | value |
  120. value := self interpretNode: each.
  121. shouldReturn ifTrue: [ ^ value ] ].
  122. ^ self interpretNode: aNode nodes last
  123. !
  124. visitValueNode: aNode
  125. ^ aNode value
  126. ! !