1
0

Compiler-Interpreter.st 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. Smalltalk current createPackage: 'Compiler-Interpreter' properties: #{}!
  2. BlockClosure subclass: #ASTBlockClosure
  3. instanceVariableNames: 'astNode'
  4. package: 'Compiler-Interpreter'!
  5. !ASTBlockClosure commentStamp!
  6. ASTBlockClosure is polymorph with BlockClosure.
  7. An ASTBlockClosure is used to interpret a BlockNode, and override all "primitive" methods (#value and co).!
  8. !ASTBlockClosure methodsFor: 'accessing'!
  9. astNode
  10. ^ astNode
  11. !
  12. astNode: aNode
  13. astNode := aNode
  14. ! !
  15. !ASTBlockClosure methodsFor: 'evaluating'!
  16. value
  17. ^ ASTInterpreter current blockValue: self
  18. ! !
  19. NodeVisitor subclass: #ASTInterpreter
  20. instanceVariableNames: 'currentNode context shouldReturn'
  21. package: 'Compiler-Interpreter'!
  22. !ASTInterpreter methodsFor: 'accessing'!
  23. context
  24. ^ context
  25. !
  26. context: aMethodContext
  27. context := aMethodContext
  28. ! !
  29. !ASTInterpreter methodsFor: 'initialization'!
  30. initialize
  31. super initialize.
  32. shouldReturn := false
  33. ! !
  34. !ASTInterpreter methodsFor: 'interpreting'!
  35. blockValue: anASTBlockClosure
  36. ^ self interpret: anASTBlockClosure astNode nodes first
  37. !
  38. interpret: aNode
  39. shouldReturn := false.
  40. ^ self interpretNode: aNode
  41. !
  42. interpretNode: aNode
  43. currentNode := aNode.
  44. ^ self visit: aNode
  45. !
  46. send: aSelector to: anObject arguments: aCollection
  47. ^ anObject perform: aSelector withArguments: aCollection
  48. ! !
  49. !ASTInterpreter methodsFor: 'visiting'!
  50. visitBlockNode: aNode
  51. ^ ASTBlockClosure new
  52. astNode: aNode;
  53. yourself
  54. !
  55. visitReturnNode: aNode
  56. shouldReturn := true.
  57. ^ self interpret: aNode nodes first
  58. !
  59. visitSendNode: aNode
  60. "TODO: Handle super sends"
  61. | receiver arguments |
  62. receiver := self interpret: aNode receiver.
  63. arguments := aNode arguments collect: [ :each |
  64. self interpret: each ].
  65. ^ self send: aNode selector to: receiver arguments: arguments
  66. !
  67. visitSequenceNode: aNode
  68. aNode nodes allButLast do: [ :each | | value |
  69. value := self interpret: each.
  70. shouldReturn ifTrue: [ ^ value ] ].
  71. ^ self interpret: aNode nodes last
  72. !
  73. visitValueNode: aNode
  74. ^ aNode value
  75. ! !
  76. ASTInterpreter class instanceVariableNames: 'current'!
  77. !ASTInterpreter class methodsFor: 'instance creation'!
  78. current
  79. ^ current ifNil: [ current := super new ]
  80. !
  81. new
  82. self shouldNotImplement
  83. ! !