Class: Twostroke::Runtime::VM
- Inherits:
-
Object
- Object
- Twostroke::Runtime::VM
- Defined in:
- lib/twostroke/runtime/vm.rb
Defined Under Namespace
Classes: Frame
Instance Attribute Summary collapse
-
#bytecode ⇒ Object
Returns the value of attribute bytecode.
-
#global_scope ⇒ Object
readonly
Returns the value of attribute global_scope.
-
#lib ⇒ Object
readonly
Returns the value of attribute lib.
Instance Method Summary collapse
- #eval(source, scope = nil, this = nil) ⇒ Object
- #execute(section = :main, scope = nil, this = nil) ⇒ Object
-
#initialize(bytecode) ⇒ VM
constructor
A new instance of VM.
- #section_name_args(section) ⇒ Object
- #throw_error(type, message) ⇒ Object
Constructor Details
#initialize(bytecode) ⇒ VM
Returns a new instance of VM.
6 7 8 9 10 11 12 |
# File 'lib/twostroke/runtime/vm.rb', line 6 def initialize(bytecode) @bytecode = bytecode @global_scope = GlobalScope.new self @lib = {} @name_args = {} @vm_eval_counter = 0 end |
Instance Attribute Details
#bytecode ⇒ Object
Returns the value of attribute bytecode.
3 4 5 |
# File 'lib/twostroke/runtime/vm.rb', line 3 def bytecode @bytecode end |
#global_scope ⇒ Object (readonly)
Returns the value of attribute global_scope.
4 5 6 |
# File 'lib/twostroke/runtime/vm.rb', line 4 def global_scope @global_scope end |
#lib ⇒ Object (readonly)
Returns the value of attribute lib.
4 5 6 |
# File 'lib/twostroke/runtime/vm.rb', line 4 def lib @lib end |
Instance Method Details
#eval(source, scope = nil, this = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/twostroke/runtime/vm.rb', line 34 def eval(source, scope = nil, this = nil) parser = Twostroke::Parser.new Twostroke::Lexer.new source parser.parse prefix = "#{@vm_eval_counter += 1}_" compiler = Twostroke::Compiler::TSASM.new parser.statements, prefix compiler.compile compiler.bytecode[:"#{prefix}main"][-2] = [:ret] bytecode.merge! compiler.bytecode execute :"#{prefix}main", scope, this end |
#execute(section = :main, scope = nil, this = nil) ⇒ Object
14 15 16 |
# File 'lib/twostroke/runtime/vm.rb', line 14 def execute(section = :main, scope = nil, this = nil) Frame.new(self, section).execute scope, this end |
#section_name_args(section) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/twostroke/runtime/vm.rb', line 22 def section_name_args(section) unless @name_args[section] ops = bytecode[section].take_while { |ins,arg| [:".name", :".arg"].include? ins } @name_args[section] = [ ops.select { |ins,arg| :".name" == ins }.map { |ins,arg| arg }.first, ops.select { |ins,arg| :".arg" == ins }.map { |ins,arg| arg.to_s } ] else @name_args[section] end end |