Class: Nydp::Invocation::Base

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/nydp/function_invocation.rb

Instance Method Summary collapse

Methods included from Helper

#cons, #list, #literal?, #pair?, #sig, #sym, #sym?

Methods included from Converter

#n2r, #r2n, #rubify

Constructor Details

#initialize(expr, source, sig = nil) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/nydp/function_invocation.rb', line 8

def initialize expr, source, sig=nil
  @expr, @source, @sig = expr, source, sig
end

Instance Method Details

#compile_do_expr_to_ruby(indent, srcs) ⇒ Object



20
21
22
23
24
# File 'lib/nydp/function_invocation.rb', line 20

def compile_do_expr_to_ruby indent, srcs
  if @expr.car.is_a?(InterpretedFunction) && !@expr.cdr && @expr.car.can_do?
    @expr.car.compile_do_expr_to_ruby indent, srcs
  end
end

#compile_to_ruby(indent, srcs, opts = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/nydp/function_invocation.rb', line 12

def compile_to_ruby indent, srcs, opts={}
  ruby = if opts[:cando]
           compile_do_expr_to_ruby indent, srcs
         end

  ruby ||= normal_compile_to_ruby indent, srcs
end

#handle(e, f, invoker, *args) ⇒ Object

if ra.empty?

  "#{indent}#{fn}._nydp_callable(#{@expr.first.to_s.inspect})._nydp_call()"
else
  "#{indent}#{fn}._nydp_callable(#{@expr.first.to_s.inspect})._nydp_call(

#Nydp::Invocation::Base.rara.join(“,n”) #indent)“

  end
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nydp/function_invocation.rb', line 65

def handle e, f, invoker, *args
  case e
  when Nydp::Error, InvocationFailed
    raise
  else
    if e.is_a?(NoMethodError) && !f.respond_to?(invoker)
      raise InvocationFailed.new("#{f._nydp_inspect} is not a function: args were #{args._nydp_inspect} in #{source._nydp_inspect}")
    else
      msg  = args.map { |a| "  #{a._nydp_inspect}"}.join("\n")
      msg  =  "failed to execute invocation #{f._nydp_inspect}\n#{msg}"
      msg +=  "\nsource was #{source._nydp_inspect}"
      msg +=  "\nfunction name was #{source.car._nydp_inspect}"
      raise InvocationFailed.new msg
    end
  end
end

#inspectObject



87
# File 'lib/nydp/function_invocation.rb', line 87

def inspect ; "(" + @expr.map { |x| x._nydp_inspect }.join(' ') + ")" ; end

#lexical_reach(n) ⇒ Object

TODO: speed up compilation by writing custom #lexical_reach for sig-based subclasses (when you know which elements of #expr are lexical symbols)



83
84
85
# File 'lib/nydp/function_invocation.rb', line 83

def lexical_reach n
  @expr.map { |x| x.lexical_reach n}.max
end

#normal_compile_to_ruby(indent, srcs) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nydp/function_invocation.rb', line 26

def normal_compile_to_ruby indent, srcs
  ra = @expr.map { |e| e.compile_to_ruby "#{indent}  ", srcs}.to_a
  fn_expr = @expr.first.to_s.inspect.gsub("\'", "\\\\'")
  fn = ra.shift

  src_expr = @expr.inspect.split(/\n/).join('\n')

  if ra.empty?
    "#{indent}  ##> #{src_expr}
#{indent}  (begin ; #{fn}.
#{indent}    ##> #{src_expr}
#{indent}    _nydp_call()
#{indent}   rescue CantCallNil => e
#{indent}    (raise 'can\\'t call nil : #{fn_expr}')
#{indent}   end)"
  else
    "#{indent}  ##> #{src_expr}
#{indent}  (begin ; #{fn}.
#{indent}    ##> #{src_expr}
#{indent}    _nydp_call(#{ra.join(",\n")})
#{indent}   rescue CantCallNil => e
#{indent}    (raise 'can\\'t call nil : #{fn_expr}')
#{indent}   end)"
  end
end

#sourceObject



88
# File 'lib/nydp/function_invocation.rb', line 88

def source  ; @source       ; end

#to_sObject



89
# File 'lib/nydp/function_invocation.rb', line 89

def to_s    ; source.to_s   ; end