Class: Plasma::Interpreter::Closure
- Defined in:
- lib/plasma/interpreter/plasma_grammarnode.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #apply(*args) ⇒ Object
-
#initialize(env, params, body) ⇒ Closure
constructor
A new instance of Closure.
- #to_plasma ⇒ Object
- #to_proc ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
81 82 83 |
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 81 def body @body end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
81 82 83 |
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 81 def env @env end |
#params ⇒ Object (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_plasma ⇒ Object
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_proc ⇒ Object
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 |