Class: Nydp::InterpretedFunction

Inherits:
Object
  • Object
show all
Extended by:
Helper
Includes:
Helper
Defined in:
lib/nydp/interpreted_function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

cons, list, literal?, pair?, sym, sym?

Instance Attribute Details

#arg_namesObject

Returns the value of attribute arg_names.



23
24
25
# File 'lib/nydp/interpreted_function.rb', line 23

def arg_names
  @arg_names
end

#bodyObject

Returns the value of attribute body.



23
24
25
# File 'lib/nydp/interpreted_function.rb', line 23

def body
  @body
end

Class Method Details

.build(arg_list, body, bindings) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/nydp/interpreted_function.rb', line 40

def self.build arg_list, body, bindings
  my_params = { }
  index_parameters arg_list, my_params
  ifn = Nydp::InterpretedFunction.new
  ifn.arg_names = arg_list
  ifn.body = compile_body body, cons(my_params, bindings), []
  ifn
end

.compile_body(body_forms, bindings, instructions) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nydp/interpreted_function.rb', line 49

def self.compile_body body_forms, bindings, instructions
  instructions << Nydp::Compiler.compile(body_forms.car, bindings)

  rest = body_forms.cdr
  if Nydp.NIL.is? rest
    return Pair.from_list(instructions)
  else
    instructions << PopArg
    compile_body rest, bindings, instructions
  end
end

.index_parameters(arg_list, hsh) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/nydp/interpreted_function.rb', line 61

def self.index_parameters arg_list, hsh
  if pair? arg_list
    index_parameters arg_list.car, hsh
    index_parameters arg_list.cdr, hsh
  elsif Nydp.NIL.isnt? arg_list
    hsh[arg_list] = hsh.size
  end
end

Instance Method Details

#execute(vm) ⇒ Object



70
71
72
# File 'lib/nydp/interpreted_function.rb', line 70

def execute vm
  vm.push_arg Closure.new(self, vm.peek_context)
end

#inspectObject



74
# File 'lib/nydp/interpreted_function.rb', line 74

def inspect; to_s; end

#invoke(vm, parent_context, arg_values) ⇒ Object



25
26
27
28
29
# File 'lib/nydp/interpreted_function.rb', line 25

def invoke vm, parent_context, arg_values
  lc = LexicalContext.new parent_context
  setup_context lc, arg_names, arg_values
  vm.push_instructions self.body, lc
end

#setup_context(context, names, values) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/nydp/interpreted_function.rb', line 31

def setup_context context, names, values
  if pair? names
    context.set names.car, values.car
    setup_context context, names.cdr, values.cdr
  elsif Nydp.NIL.isnt? names
    context.set names, values
  end
end

#to_sObject



75
76
77
# File 'lib/nydp/interpreted_function.rb', line 75

def to_s
  "(fn #{arg_names.to_s} #{body.map { |b| b.inspect}.join(' ')})"
end