Class: AjLisp::PrimitiveClosure

Inherits:
Primitive show all
Defined in:
lib/ajlisp/primitive_closure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Primitive

#evaluate

Constructor Details

#initialize(arguments, body, context = nil) ⇒ PrimitiveClosure

Returns a new instance of PrimitiveClosure.



9
10
11
12
13
# File 'lib/ajlisp/primitive_closure.rb', line 9

def initialize(arguments, body, context=nil)
    @arguments = arguments
    @body = body
    @context = context
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



5
6
7
# File 'lib/ajlisp/primitive_closure.rb', line 5

def arguments
  @arguments
end

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/ajlisp/primitive_closure.rb', line 6

def body
  @body
end

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/ajlisp/primitive_closure.rb', line 7

def context
  @context
end

Instance Method Details

#apply(context, args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ajlisp/primitive_closure.rb', line 15

def apply(context, args)
    newcontext = context
    
    if @context then
        newcontext = @context
    end

    if @arguments
        newcontext = AjLisp::Context.new newcontext
        
        if @arguments.is_a? NamedAtom
            newcontext.setValue @arguments.name, AjLisp::List.make(args)
        else
            names = @arguments
        
            args.each do |arg|
                name = names.first.name
                newcontext.setValue name, arg
                names = names.rest
            end
        end
    end
    
    result = nil

    @body.each do |form|
        result = AjLisp::evaluate newcontext, form
    end
    
    return result
end