Module: Nydp::LexicalContextBuilder

Extended by:
Helper
Defined in:
lib/nydp/lexical_context_builder.rb

Class Method Summary collapse

Methods included from Helper

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

Methods included from Converter

#n2r, #r2n, #rubify

Class Method Details

.build_arg_conses(arg_names, n) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/nydp/lexical_context_builder.rb', line 35

def self.build_arg_conses arg_names, n
  if n == arg_names.size - 1
    "cons(#{arg_names[n]})"
  else
    "cons(#{arg_names[n]}, #{build_arg_conses(arg_names, n+1)})"
  end
end

.build_arg_names(ngiven) ⇒ Object



19
20
21
# File 'lib/nydp/lexical_context_builder.rb', line 19

def self.build_arg_names ngiven
  (["lc"] + (0...ngiven      ).map { |i| "arg_#{i}"}).join ", "
end

.build_builder_class(name, expected_arg_count) ⇒ Object



88
89
90
91
92
# File 'lib/nydp/lexical_context_builder.rb', line 88

def self.build_builder_class name, expected_arg_count
  n_methods = (0..3).map { |given| build_set_args_n_method given, expected_arg_count }
  x_method  = build_set_args_method expected_arg_count
  define_module name, "#{n_methods.join "\n"}\n#{x_method}"
end

.build_builder_rest_class(name, proper_arg_count) ⇒ Object



94
95
96
97
98
# File 'lib/nydp/lexical_context_builder.rb', line 94

def self.build_builder_rest_class name, proper_arg_count
  n_methods = (0..3).map { |given| build_set_args_n_rest_method given, proper_arg_count }
  x_method  = build_set_args_rest_method proper_arg_count
  define_module name, "#{n_methods.join "\n"}\n#{x_method}"
end

.build_set_args_method(nexpected) ⇒ Object



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

def self.build_set_args_method nexpected
  setters   = (0...nexpected).map { |i| "lc.at_#{i}= args#{ ([".cdr"] * i).join }.car"}
"  def set_args lc, args
  #{mklc nexpected}#{setters.join "\n    "}
  lc
 rescue StandardError => e
   raise \"error in \#{self.class.name}#set_args\"
end
"
end

.build_set_args_n_method(ngiven, nexpected) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nydp/lexical_context_builder.rb', line 23

def self.build_set_args_n_method ngiven, nexpected
  setter_count = [ngiven, nexpected].min
  setters   = (0...setter_count).map { |i| "lc.at_#{i}= arg_#{i}"}
"  def set_args_#{ngiven} #{build_arg_names ngiven}
  #{mklc nexpected}#{setters.join "\n    "}
  lc
 rescue StandardError => e
   raise \"error in \#{self.class.name}#set_args_#{ngiven}\"
end
"
end

.build_set_args_n_rest_method(ngiven, nexpected) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nydp/lexical_context_builder.rb', line 43

def self.build_set_args_n_rest_method ngiven, nexpected
  setter_count = [ngiven, nexpected].min
  arg_names = (0...ngiven      ).map { |i| "arg_#{i}"}
  setters   = (0...setter_count).map { |i| "lc.at_#{i}= arg_#{i}"}
  if ngiven > setter_count
    rest_setter = "lc.at_#{nexpected}= #{build_arg_conses arg_names[setter_count..-1], 0}"
  end
"  def set_args_#{ngiven} #{build_arg_names ngiven}
  #{mklc 1}#{setters.join "\n    "}
  #{rest_setter}
  lc
 rescue StandardError => e
   raise \"error in \#{self.class.name}#set_args_#{ngiven}\"
end
"
end

.build_set_args_rest_method(nexpected) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nydp/lexical_context_builder.rb', line 71

def self.build_set_args_rest_method nexpected
  setters   = (0...nexpected).map { |i| "lc.at_#{i}= args#{ ([".cdr"] * i).join }.car"}
  rest_set  = "lc.at_#{nexpected}= args#{ ([".cdr"] *(nexpected)).join }"
"  def set_args lc, args
  #{mklc 1}#{setters.join "\n    "}
  #{rest_set}
  lc
 rescue StandardError => e
   raise \"error in \#{self.class.name}#set_args\"
end
"
end

.const_missing(const) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/nydp/lexical_context_builder.rb', line 4

def self.const_missing(const)
  if const.to_s =~ /^B_\d+(_Rest)?$/
    name = const.to_s.split(/_/)
    size = name[1].to_i
    name[2] ? build_builder_rest_class(const, size) : build_builder_class(const, size)
    const_get const
  else
    super(const)
  end
end

.define_module(name, code) ⇒ Object



84
85
86
# File 'lib/nydp/lexical_context_builder.rb', line 84

def self.define_module name, code
  const_set name.to_sym, Module.new { eval code }
end

.mklc(nexpected) ⇒ Object



15
16
17
# File 'lib/nydp/lexical_context_builder.rb', line 15

def self.mklc nexpected
  (nexpected > 0) ? "lc = Nydp::LexicalContext.new lc\n    " : ""
end

.select(arg_names) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/nydp/lexical_context_builder.rb', line 100

def self.select arg_names
  if pair?(arg_names)
    size   = arg_names.size
    proper = arg_names.proper?
  else
    size   = 0
    proper = (!arg_names)
  end
  const_get(proper ? :"B_#{size}" : :"B_#{size}_Rest")
end