Class: Plasma::Interpreter::RubyClosure

Inherits:
Object
  • Object
show all
Defined in:
lib/plasma/interpreter/plasma_grammarnode.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, interp, &body) ⇒ RubyClosure

Returns a new instance of RubyClosure.



118
119
120
121
122
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 118

def initialize(name, interp, &body)
  @name = name
  @interp = interp
  @body = Proc.new(&body)
end

Instance Method Details

#apply(*args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 124

def apply(*args)
  args = args.slice(0..@body.arity) if @body.arity < args.length
  diff = @body.arity - args.length
  if diff == 0
    value = @body.call(*args)
  else
    fresh = {}
    args.each_with_index {|arg, index| fresh.merge(index => arg)}
    params = (args.length..args.length+diff).join(' ')
    keys = fresh.keys.join(' ')
    env = Env.new(fresh.merge(@name => self))

    interp.interpret("fun (#{params}) (#{@name} #{keys} #{params})", env)
  end
end