Class: Twostroke::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/twostroke/context.rb,
lib/twostroke/context/object_proxy.rb

Defined Under Namespace

Classes: ObjectProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



5
6
7
8
9
# File 'lib/twostroke/context.rb', line 5

def initialize
  @ai = 0
  @vm = Twostroke::Runtime::VM.new({})
  Twostroke::Runtime::Lib.setup_environment vm
end

Instance Attribute Details

#vmObject (readonly)

Returns the value of attribute vm.



3
4
5
# File 'lib/twostroke/context.rb', line 3

def vm
  @vm
end

Instance Method Details

#[](var) ⇒ Object



11
12
13
# File 'lib/twostroke/context.rb', line 11

def [](var)
  vm.global_scope.get_var(var.intern).to_ruby
end

#eval(src) ⇒ Object



24
25
26
# File 'lib/twostroke/context.rb', line 24

def eval(src)
  raw_eval(src + ";").to_ruby
end

#exec(src) ⇒ Object



35
36
37
# File 'lib/twostroke/context.rb', line 35

def exec(src)
  raw_exec(src).to_ruby
end

#inspectObject



39
40
41
# File 'lib/twostroke/context.rb', line 39

def inspect
  to_s
end

#raw_eval(src) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/twostroke/context.rb', line 15

def raw_eval(src)
  prefix = make_prefix
  bytecode = compile src, prefix
  main = :"#{prefix}main"
  bytecode[main][-2] = [:ret]
  vm.bytecode.merge! bytecode
  vm.execute main, vm.global_scope.close
end

#raw_exec(src) ⇒ Object



28
29
30
31
32
33
# File 'lib/twostroke/context.rb', line 28

def raw_exec(src)
  prefix = make_prefix
  bytecode = compile src, prefix
  vm.bytecode.merge! bytecode
  vm.execute :"#{prefix}_main"
end