Class: PartialRuby::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/partialruby.rb

Direct Known Subclasses

PureRubyContext

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



57
58
59
# File 'lib/partialruby.rb', line 57

def initialize
  @preprocessors = Array.new
end

Instance Method Details

#emul(tree) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/partialruby.rb', line 70

def emul(tree)
  begin
    # first, try to emul the node
    return ruby_emul(tree)
  rescue NoMethodError => e
    if tree
      "#{object_ref self}.run(#{object_ref tree}, PartialRuby::Frame.new(binding,self) )"
    else
      "nil; "
    end
  end
end

#object_ref(obj) ⇒ Object



61
62
63
# File 'lib/partialruby.rb', line 61

def object_ref(obj)
  "ObjectSpace._id2ref(#{obj.object_id})"
end

#packet(code) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/partialruby.rb', line 87

def packet(code)
  tree = nil

  begin
    tree = RubyParser.new.parse code
  rescue
    raise SyntaxError
  end

  context = PartialRuby::PureRubyContext.new

  @preprocessors.each do |preprocessor|
    tree = preprocessor.call(tree)
  end

  emulationcode = context.emul tree

  PartialRuby::Packet.new(emulationcode)
end

#pre_process(&blk) ⇒ Object



83
84
85
# File 'lib/partialruby.rb', line 83

def pre_process(&blk)
  @preprocessors << blk
end

#ruby_emul(tree) ⇒ Object



65
66
67
68
# File 'lib/partialruby.rb', line 65

def ruby_emul(tree)
  nodetype = tree.first
  send("ruby_emul_"+nodetype.to_s, tree)
end

#run(tree, frame) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/partialruby.rb', line 107

def run(tree, frame)
  return nil unless tree

  nodetype = tree.first

  code = nil
  begin
    # first, try to emul the node
    code = ruby_emul(tree)
  rescue NoMethodError => e
  end

  if code then
    eval(code, frame._binding)
  else
    send("handle_node_"+nodetype.to_s, tree, frame)
  end
end