Class: Plasma::Interpreter::Closure

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, params, body) ⇒ Closure

Returns a new instance of Closure.



83
84
85
86
87
88
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 83

def initialize(env, params, body)
  @env = env.dup
  @params = params
  @body = body
  @proc = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



81
82
83
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 81

def body
  @body
end

#envObject (readonly)

Returns the value of attribute env.



81
82
83
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 81

def env
  @env
end

#paramsObject (readonly)

Returns the value of attribute params.



81
82
83
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 81

def params
  @params
end

Instance Method Details

#apply(*args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 90

def apply(*args)
  left = params.dup
  args = args.slice(0...left.length) if left.length < args.length
  zipped = args.inject({}){|hash, arg| hash.merge(left.shift => arg)}
  zipped.merge!(:env => @env)

  if left.empty?
    @env.scope(zipped) do |env|
      @body.evaluate(env)
    end
  else
    return Closure.new(@env.dup.merge!(zipped), left, body)
  end
end

#to_plasmaObject



111
112
113
114
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 111

def to_plasma
  p = params.map{|p| p.to_s}.join(' ')
  "fun (#{p}) #{@body.text_value}"
end

#to_procObject



105
106
107
108
109
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 105

def to_proc
  @proc ||= Proc.new do |*args|
    self.apply *args
  end
end