Class: Trxl::Function::Closure

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, env = Environment.new) ⇒ Closure

Returns a new instance of Closure.



135
136
137
138
# File 'lib/trxl/trxl.rb', line 135

def initialize(function, env = Environment.new)
  @function = function
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



133
134
135
# File 'lib/trxl/trxl.rb', line 133

def env
  @env
end

#functionObject (readonly)

Returns the value of attribute function.



133
134
135
# File 'lib/trxl/trxl.rb', line 133

def function
  @function
end

Instance Method Details

#apply(args) ⇒ Object



140
141
142
143
144
145
# File 'lib/trxl/trxl.rb', line 140

def apply(args)
  env.enter_scope
  return_value = function.body.eval(function.formal_parameter_list.bind(args, env))
  env.exit_scope
  return_value
end

#to_s(other_env = Environment.new) ⇒ Object



147
148
149
# File 'lib/trxl/trxl.rb', line 147

def to_s(other_env = Environment.new)
  function.text_value #"fun#{function.formal_parameter_list.to_s(env)} {#{function.body.to_s(other_env.merge(env.local))}}"
end