Class: JSE::Ast::LambdaNode

Inherits:
AstNode
  • Object
show all
Defined in:
lib/jse/ast/nodes.rb

Instance Attribute Summary

Attributes inherited from AstNode

#env

Instance Method Summary collapse

Constructor Details

#initialize(params, body, closure_env) ⇒ LambdaNode

Returns a new instance of LambdaNode.



181
182
183
184
185
186
# File 'lib/jse/ast/nodes.rb', line 181

def initialize(params, body, closure_env)
  super(closure_env)
  @params = params
  @body = body
  @closure_env = closure_env
end

Instance Method Details

#apply(call_env, *args) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/jse/ast/nodes.rb', line 188

def apply(call_env, *args)
  if args.length != @params.length
    raise ArgumentError, "Lambda expects #{@params.length} args, got #{args.length}"
  end

  call_env = JSE::Env.new(parent: @closure_env)
  @params.zip(args).each { |param, arg| call_env.set(param, arg) }
  call_env.eval(@body)
end

#to_jsonObject



198
199
200
# File 'lib/jse/ast/nodes.rb', line 198

def to_json
  "<lambda>"
end