nydp 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +77 -56
  4. data/lib/lisp/core-000.nydp +1 -1
  5. data/lib/lisp/core-010-precompile.nydp +49 -29
  6. data/lib/lisp/core-012-utils.nydp +12 -8
  7. data/lib/lisp/core-015-documentation.nydp +41 -15
  8. data/lib/lisp/core-017-builtin-dox.nydp +621 -100
  9. data/lib/lisp/core-020-utils.nydp +33 -6
  10. data/lib/lisp/core-025-warnings.nydp +1 -1
  11. data/lib/lisp/core-030-syntax.nydp +64 -48
  12. data/lib/lisp/core-035-flow-control.nydp +20 -28
  13. data/lib/lisp/core-037-list-utils.nydp +84 -21
  14. data/lib/lisp/core-040-utils.nydp +8 -5
  15. data/lib/lisp/core-041-string-utils.nydp +17 -11
  16. data/lib/lisp/core-043-list-utils.nydp +140 -77
  17. data/lib/lisp/core-045-dox-utils.nydp +1 -0
  18. data/lib/lisp/core-050-test-runner.nydp +8 -12
  19. data/lib/lisp/core-070-prefix-list.nydp +19 -15
  20. data/lib/lisp/core-080-pretty-print.nydp +13 -5
  21. data/lib/lisp/core-090-hook.nydp +11 -11
  22. data/lib/lisp/core-100-utils.nydp +51 -66
  23. data/lib/lisp/core-110-hash-utils.nydp +34 -7
  24. data/lib/lisp/core-120-settings.nydp +14 -9
  25. data/lib/lisp/core-130-validations.nydp +28 -13
  26. data/lib/lisp/core-900-benchmarking.nydp +420 -47
  27. data/lib/lisp/tests/000-empty-args-examples.nydp +5 -0
  28. data/lib/lisp/tests/andify-examples.nydp +1 -1
  29. data/lib/lisp/tests/auto-hash-examples.nydp +6 -1
  30. data/lib/lisp/tests/best-examples.nydp +1 -1
  31. data/lib/lisp/tests/boot-tests.nydp +1 -1
  32. data/lib/lisp/tests/date-examples.nydp +129 -102
  33. data/lib/lisp/tests/destructuring-examples.nydp +1 -1
  34. data/lib/lisp/tests/dox-tests.nydp +2 -2
  35. data/lib/lisp/tests/hash-examples.nydp +58 -33
  36. data/lib/lisp/tests/list-tests.nydp +137 -1
  37. data/lib/lisp/tests/pretty-print-tests.nydp +12 -0
  38. data/lib/lisp/tests/rotate-2d-array-examples.nydp +26 -0
  39. data/lib/lisp/tests/sort-examples.nydp +5 -5
  40. data/lib/lisp/tests/string-tests.nydp +16 -5
  41. data/lib/lisp/tests/syntax-tests.nydp +10 -2
  42. data/lib/lisp/tests/time-examples.nydp +8 -1
  43. data/lib/lisp/tests/unparse-tests.nydp +13 -7
  44. data/lib/nydp/assignment.rb +15 -28
  45. data/lib/nydp/builtin/abs.rb +4 -3
  46. data/lib/nydp/builtin/apply.rb +8 -10
  47. data/lib/nydp/builtin/cdr_set.rb +1 -1
  48. data/lib/nydp/builtin/comment.rb +1 -3
  49. data/lib/nydp/builtin/date.rb +11 -28
  50. data/lib/nydp/builtin/divide.rb +3 -10
  51. data/lib/nydp/builtin/ensuring.rb +6 -21
  52. data/lib/nydp/builtin/error.rb +2 -4
  53. data/lib/nydp/builtin/eval.rb +9 -4
  54. data/lib/nydp/builtin/greater_than.rb +7 -8
  55. data/lib/nydp/builtin/handle_error.rb +10 -34
  56. data/lib/nydp/builtin/hash.rb +24 -45
  57. data/lib/nydp/builtin/inspect.rb +1 -3
  58. data/lib/nydp/builtin/is_equal.rb +4 -7
  59. data/lib/nydp/builtin/less_than.rb +6 -7
  60. data/lib/nydp/builtin/log.rb +7 -0
  61. data/lib/nydp/builtin/math_ceiling.rb +1 -3
  62. data/lib/nydp/builtin/math_floor.rb +1 -3
  63. data/lib/nydp/builtin/math_power.rb +1 -3
  64. data/lib/nydp/builtin/math_round.rb +2 -2
  65. data/lib/nydp/builtin/minus.rb +7 -14
  66. data/lib/nydp/builtin/parse.rb +5 -5
  67. data/lib/nydp/builtin/parse_in_string.rb +5 -7
  68. data/lib/nydp/builtin/plus.rb +14 -31
  69. data/lib/nydp/builtin/pre_compile.rb +1 -3
  70. data/lib/nydp/builtin/puts.rb +4 -8
  71. data/lib/nydp/builtin/quit.rb +1 -1
  72. data/lib/nydp/builtin/rand.rb +6 -11
  73. data/lib/nydp/builtin/random_string.rb +2 -4
  74. data/lib/nydp/builtin/rng.rb +25 -0
  75. data/lib/nydp/builtin/ruby_wrap.rb +27 -14
  76. data/lib/nydp/builtin/script_run.rb +1 -3
  77. data/lib/nydp/builtin/set_intersection.rb +3 -4
  78. data/lib/nydp/builtin/set_union.rb +3 -4
  79. data/lib/nydp/builtin/sort.rb +2 -7
  80. data/lib/nydp/builtin/string_match.rb +5 -13
  81. data/lib/nydp/builtin/string_replace.rb +2 -7
  82. data/lib/nydp/builtin/string_split.rb +3 -8
  83. data/lib/nydp/builtin/sym.rb +2 -9
  84. data/lib/nydp/builtin/thread_locals.rb +2 -2
  85. data/lib/nydp/builtin/time.rb +38 -44
  86. data/lib/nydp/builtin/times.rb +6 -15
  87. data/lib/nydp/builtin/to_integer.rb +8 -14
  88. data/lib/nydp/builtin/to_string.rb +2 -13
  89. data/lib/nydp/builtin/type_of.rb +10 -16
  90. data/lib/nydp/builtin/vm_info.rb +2 -10
  91. data/lib/nydp/builtin.rb +15 -37
  92. data/lib/nydp/compiler.rb +29 -19
  93. data/lib/nydp/cond.rb +95 -88
  94. data/lib/nydp/context_symbol.rb +11 -9
  95. data/lib/nydp/core.rb +74 -73
  96. data/lib/nydp/core_ext.rb +87 -26
  97. data/lib/nydp/date.rb +22 -19
  98. data/lib/nydp/error.rb +2 -3
  99. data/lib/nydp/function_invocation.rb +76 -289
  100. data/lib/nydp/helper.rb +18 -9
  101. data/lib/nydp/interpreted_function.rb +159 -25
  102. data/lib/nydp/lexical_context.rb +9 -8
  103. data/lib/nydp/lexical_context_builder.rb +1 -1
  104. data/lib/nydp/literal.rb +3 -7
  105. data/lib/nydp/loop.rb +72 -0
  106. data/lib/nydp/namespace.rb +52 -0
  107. data/lib/nydp/pair.rb +146 -50
  108. data/lib/nydp/parser.rb +9 -11
  109. data/lib/nydp/plugin.rb +88 -19
  110. data/lib/nydp/runner.rb +141 -23
  111. data/lib/nydp/symbol.rb +16 -26
  112. data/lib/nydp/symbol_lookup.rb +3 -2
  113. data/lib/nydp/tokeniser.rb +1 -1
  114. data/lib/nydp/truth.rb +2 -37
  115. data/lib/nydp/version.rb +1 -1
  116. data/lib/nydp.rb +33 -44
  117. data/nydp.gemspec +2 -1
  118. data/spec/date_spec.rb +26 -32
  119. data/spec/embedded_spec.rb +22 -22
  120. data/spec/error_spec.rb +12 -16
  121. data/spec/foreign_hash_spec.rb +21 -36
  122. data/spec/hash_non_hash_behaviour_spec.rb +12 -29
  123. data/spec/hash_spec.rb +36 -49
  124. data/spec/literal_spec.rb +6 -6
  125. data/spec/nydp_spec.rb +14 -14
  126. data/spec/pair_spec.rb +8 -8
  127. data/spec/parser_spec.rb +41 -37
  128. data/spec/rand_spec.rb +1 -4
  129. data/spec/spec_helper.rb +3 -3
  130. data/spec/string_atom_spec.rb +15 -16
  131. data/spec/symbol_spec.rb +27 -52
  132. data/spec/thread_local_spec.rb +23 -8
  133. data/spec/time_spec.rb +4 -10
  134. data/spec/tokeniser_spec.rb +10 -10
  135. metadata +25 -13
  136. data/lib/nydp/builtin/modulo.rb +0 -11
  137. data/lib/nydp/builtin/regexp.rb +0 -7
  138. data/lib/nydp/builtin/sqrt.rb +0 -7
  139. data/lib/nydp/builtin/string_pad_left.rb +0 -7
  140. data/lib/nydp/builtin/string_pad_right.rb +0 -7
  141. data/lib/nydp/hash.rb +0 -9
  142. data/lib/nydp/image_store.rb +0 -21
  143. data/lib/nydp/vm.rb +0 -129
data/lib/nydp/core_ext.rb CHANGED
@@ -1,57 +1,118 @@
1
1
  class Object
2
- def _nydp_get a ; raise "_nydp_get : not gettable: #{a.inspect} on #{self.class.name}" ; end
3
- def _nydp_set a, v ; raise "_nydp_get : not settable: #{a.inspect} on #{self.class.name}" ; end
4
- def _nydp_keys ; [] ; end
5
- def _nydp_wrapper ; self ; end
6
- def lexical_reach n ; n ; end
2
+ def _nydp_get a ; raise "_nydp_get : not gettable: #{a._nydp_inspect} on #{self.class.name}" ; end
3
+ def _nydp_set a, v ; raise "_nydp_get : not settable: #{a._nydp_inspect} on #{self.class.name}" ; end
4
+ def _nydp_keys ; [] ; end
5
+ def _nydp_wrapper ; self ; end
6
+ def _nydp_inspect ; inspect ; end
7
+ def _nydp_to_s ; to_s ; end
8
+ def _nydp_compact_inspect ; _nydp_inspect ; end
9
+ def lexical_reach n ; n ; end
10
+ def to_ruby ; self ; end
11
+ def compile_to_ruby indent, srcs, opts=nil ; "#{indent}#{inspect}" ; end
7
12
  end
8
13
 
9
14
  class Method
10
15
  include Nydp::Converter
11
- def invoke_1 vm ; vm.push_arg call._nydp_wrapper ; end
12
- def invoke_2 vm, a0 ; vm.push_arg call(n2r(a0))._nydp_wrapper ; end
13
- def invoke_3 vm, a0, a1 ; vm.push_arg call(n2r(a0), n2r(a1))._nydp_wrapper ; end
14
- def invoke_4 vm, a0, a1, a2 ; vm.push_arg call(n2r(a0), n2r(a1), n2r(a2))._nydp_wrapper ; end
15
- def invoke vm, args ; vm.push_arg call(*(args.map { |a| n2r a}))._nydp_wrapper ; end
16
+ def _nydp_call *args ; (call *(rubify args))._nydp_wrapper ; end
16
17
  end
17
18
 
18
- class NilClass
19
- def _nydp_wrapper ; Nydp::NIL ; end
19
+ class Proc
20
+ alias _nydp_call call
20
21
  end
21
22
 
22
- class FalseClass
23
- def _nydp_wrapper ; Nydp::NIL ; end
23
+ class TrueClass
24
+ def _nydp_inspect ; 't' ; end
25
+ def assign *_ ; self ; end
26
+ def nydp_type ; :truth ; end
27
+ def _nydp_get a ; self ; end
28
+ def _nydp_set a, v ; self ; end
29
+ def _nydp_to_s ; "t" ; end
30
+ def compile_to_ruby indent, srcs, opts=nil ; "#{indent}true" ; end
24
31
  end
25
32
 
26
- class TrueClass
27
- def _nydp_wrapper ; Nydp::T ; end
33
+ class CantCallNil < NoMethodError
34
+ end
35
+
36
+ class NilClass
37
+ def car ; self ; end
38
+ def cdr ; self ; end
39
+ def size ; 0 ; end
40
+ def is? other ; self.equal? other ; end
41
+ def isnt? other ; !self.equal? other ; end
42
+ def + other ; other ; end
43
+ def copy ; self ; end
44
+ def assign *_ ; self ; end
45
+ def nydp_type ; :nil ; end
46
+ def _nydp_get a ; self ; end
47
+ def _nydp_set a, v ; self ; end
48
+ def & other ; self ; end
49
+ def | other ; other ; end
50
+ def _nydp_call *_ ; raise CantCallNil ; end
51
+ def compile_to_ruby i, s, o=nil ; "#{i}nil" ; end
52
+ end
53
+
54
+ class FalseClass
55
+ def _nydp_wrapper ; nil ; end
28
56
  end
29
57
 
30
58
  class ::Symbol
31
- def _nydp_wrapper ; Nydp::FrozenSymbol.mk(self) ; end
59
+ def _nydp_inspect
60
+ _ins = to_s
61
+ _nydp_untidy?(_ins) ? "|#{_ins.gsub(/\|/, '\|')}|" : _ins
62
+ end
63
+
64
+ def nydp_type ; :symbol ; end
65
+ # def execute vm ; self ; end
66
+
67
+ alias :inspect_before_nydp :inspect
68
+ def inspect
69
+ if self == :"#="
70
+ ':"#="'
71
+ else
72
+ inspect_before_nydp
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def _nydp_untidy? s
79
+ (s == "") || (s == nil) || (s =~ /[\s\|,\(\)"]/)
80
+ end
32
81
  end
33
82
 
34
83
  class ::Date
35
- def _nydp_wrapper ; Nydp::Date.new self ; end
84
+ def _nydp_get key ; _nydp_date.lookup key, self ; end
85
+ def _nydp_date ; @__nydp_date ||= Nydp::Date.new(self) ; end
86
+ def nydp_type ; :date ; end
36
87
  end
37
88
 
38
- class ::Array
39
- def _nydp_wrapper ; Nydp::Pair.from_list map &:_nydp_wrapper ; end
89
+ class ::Time
90
+ @@wl = Set.new %i{ year month day hour min sec }
91
+ def _nydp_time_get key ; send(key) if @@wl.include?(key) ; end
92
+ def _nydp_get key ; _nydp_time_get(key.to_s.to_sym) ; end
93
+ def nydp_type ; :time ; end
40
94
  end
41
95
 
42
- class ::Object
43
- def to_ruby ; self ; end
96
+ class ::Array
97
+ def _nydp_wrapper ; Nydp::Pair.from_list map &:_nydp_wrapper ; end
98
+ def _nydp_inspect ; "[" + map(&:_nydp_inspect).join(" ") + "]" ; end
44
99
  end
45
100
 
46
101
  class ::String
47
- # def _nydp_wrapper ; Nydp::StringAtom.new self ; end
102
+ # _hex_ord only works for characters whose #ord is < 256, ie #bytes returns a single-element array
103
+ # this should allow for two-way conversion of names containing the characters in the regexp
104
+ # even though we don't use two-way conversion anywhere just yet
105
+ def _nydp_name_to_rb_name ; self.gsub(/[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]/) { |chr| "_#{chr._hex_ord}"}.to_sym ; end
48
106
  def as_method_name ; self.gsub(/-/, '_').to_sym ; end
49
107
  def nydp_type ; :string ; end
108
+ def _hex_ord ; ord.to_s(16).rjust(2, '0') ; end
50
109
  end
51
110
 
52
111
  class ::Hash
53
112
  include Nydp::Helper
54
- def _nydp_get a ; self[n2r a] ; end
55
- def _nydp_set a, v ; self[n2r a] = n2r(v) ; end
56
- def _nydp_keys ; keys ; end
113
+ def _nydp_get a ; self[a]._nydp_wrapper ; end
114
+ def _nydp_set a, v ; self[a] = v ; end
115
+ def _nydp_keys ; keys ; end
116
+ def _nydp_inspect ; "{" + map { |k,v| [k._nydp_inspect, v._nydp_inspect].join(" ")}.join(" ") + "}" ; end
117
+ def nydp_type ; :hash ; end
57
118
  end
data/lib/nydp/date.rb CHANGED
@@ -18,19 +18,18 @@ module Nydp
18
18
 
19
19
  def initialize ruby_date ; @ruby_date = ruby_date ; end
20
20
 
21
- def to_date ; ruby_date ; end
22
- def to_s ; ruby_date.to_s ; end
23
- def to_ruby ; ruby_date ; end
24
- def inspect ; ruby_date.inspect ; end
25
- def nydp_type ; :date ; end
26
- def > other ; is_date?(other) && ruby_date > other.ruby_date ; end
27
- def < other ; is_date?(other) && ruby_date < other.ruby_date ; end
28
- def == other ; is_date?(other) && ruby_date == other.ruby_date ; end
29
- def <=> other ; is_date?(other) && ruby_date <=> other.ruby_date ; end
30
- def eql? d ; self == d ; end
31
- def hash ; ruby_date.hash ; end
32
- def is_date? other ; other.is_a? Nydp::Date ; end
33
- def - other ; r2n(ruby_date - (is_date?(other) ? other.ruby_date : other)) ; end
21
+ def to_date ; ruby_date ; end
22
+ def to_s ; ruby_date.to_s ; end
23
+ def to_ruby ; ruby_date ; end
24
+ def inspect ; ruby_date.inspect ; end
25
+ def > other ; is_date?(other) && ruby_date > other ; end
26
+ def < other ; is_date?(other) && ruby_date < other ; end
27
+ def == other ; is_date?(other) && ruby_date == other ; end
28
+ def <=> other ; is_date?(other) && ruby_date <=> other ; end
29
+ def eql? d ; self == d ; end
30
+ def hash ; ruby_date.hash ; end
31
+ def is_date? other ; other.is_a? ::Date ; end
32
+ def - other ; ruby_date - other ; end
34
33
  def + int ; int.is_a?(Integer) ? r2n(ruby_date + int) : r2n(change(*int.to_ruby)) ; end
35
34
 
36
35
  @@pass_through = %i{ monday? tuesday? wednesday? thursday? friday? saturday? sunday? }
@@ -39,7 +38,8 @@ module Nydp
39
38
  last_year next_year beginning_of_year end_of_year
40
39
  last_month next_month beginning_of_month end_of_month
41
40
  last_week next_week beginning_of_week end_of_week
42
- yesterday tomorrow age
41
+ yesterday tomorrow age past?
42
+ future?
43
43
  } + @@pass_through
44
44
 
45
45
  def year y, m, d, w ; y ; end
@@ -47,6 +47,9 @@ module Nydp
47
47
  def day y, m, d, w ; d ; end
48
48
  def week_day y, m, d, w ; w ; end
49
49
 
50
+ def future? *_ ; ruby_date > ::Date.today ; end
51
+ def past? *_ ; ruby_date < ::Date.today ; end
52
+
50
53
  def last_year y, m, d, w ; ruby_date.prev_year ; end
51
54
  def next_year y, m, d, w ; ruby_date.next_year ; end
52
55
  def beginning_of_year y, m, d, w ; build(y, 1, 1) ; end
@@ -76,11 +79,11 @@ module Nydp
76
79
  class_eval "def #{n} * ; ruby_date.#{n} ; end"
77
80
  end
78
81
 
79
- def _nydp_keys ; @@keys.to_a ; end
80
- def dispatch key, y, m, d, w ; self.send(key, y, m, d, w) if _nydp_keys.include?(key) ; end
81
- def splat date ; [date.year, date.month, date.day, date.wday] ; end
82
- def lookup key, date ; r2n(dispatch(key.to_s.gsub(/-/, '_').to_sym, *splat(date))) ; end
83
- def _nydp_get key ; lookup key, ruby_date ; end
82
+ def _nydp_keys ; @@keys.to_a ; end
83
+ def dispatch key, y, m, d, w ; self.send(key, y, m, d, w) if _nydp_keys.include?(key) ; end
84
+ def splat date ; [date.year, date.month, date.day, date.wday] ; end
85
+ def lookup key, date ; r2n(dispatch(key.to_s.gsub(/-/, '_').to_sym, *splat(date))) ; end
86
+ def _nydp_get key ; lookup key, ruby_date ; end
84
87
  def change amount, attr
85
88
  if attr == :day ; (ruby_date + amount)
86
89
  elsif attr == :week ; (ruby_date + (7 * amount))
data/lib/nydp/error.rb CHANGED
@@ -9,8 +9,7 @@ module Nydp
9
9
  @nydp_cause = nydp_cause
10
10
  end
11
11
 
12
- def cause
13
- @nydp_cause || super
14
- end
12
+ def nydp_type ; :error ; end
13
+ def cause ; @nydp_cause || super ; end
15
14
  end
16
15
  end
@@ -3,32 +3,77 @@ module Nydp
3
3
  end
4
4
 
5
5
  module Invocation
6
- @@sig_counts = Hash.new { |h,k| h[k] = 0}
7
-
8
- # def self.sig name ; @@sig_counts[name] += 1 ; end
9
-
10
- # def self.whazzup
11
- # puts @@sig_counts.to_a.sort_by { |c| c[1] }.map { |c| "#{c[1]}\t#{c[0]}"}
12
- # end
13
-
14
6
  class Base
15
7
  include Helper
16
8
  def initialize expr, source, sig=nil
17
9
  @expr, @source, @sig = expr, source, sig
18
10
  end
19
11
 
12
+ def compile_to_ruby indent, srcs, opts={}
13
+ ruby = if opts[:cando]
14
+ compile_do_expr_to_ruby indent, srcs
15
+ end
16
+
17
+ ruby ||= normal_compile_to_ruby indent, srcs
18
+ end
19
+
20
+ def compile_do_expr_to_ruby indent, srcs
21
+ if @expr.car.is_a?(InterpretedFunction) && !@expr.cdr && @expr.car.can_do?
22
+ @expr.car.compile_do_expr_to_ruby indent, srcs
23
+ end
24
+ end
25
+
26
+ def normal_compile_to_ruby indent, srcs
27
+ ra = @expr.map { |e| e.compile_to_ruby "#{indent} ", srcs}.to_a
28
+ fn_expr = @expr.first.to_s.inspect.gsub("\'", "\\\\'")
29
+ fn = ra.shift
30
+
31
+ src_expr = @expr.inspect.split(/\n/).join('\n')
32
+
33
+ if ra.empty?
34
+ "#{indent} ##> #{src_expr}
35
+ #{indent} (begin ; #{fn}.
36
+ #{indent} ##> #{src_expr}
37
+ #{indent} _nydp_call()
38
+ #{indent} rescue CantCallNil => e
39
+ #{indent} (raise 'can\\'t call nil : #{fn_expr}')
40
+ #{indent} end)"
41
+ else
42
+ "#{indent} ##> #{src_expr}
43
+ #{indent} (begin ; #{fn}.
44
+ #{indent} ##> #{src_expr}
45
+ #{indent} _nydp_call(#{ra.join(",\n")})
46
+ #{indent} rescue CantCallNil => e
47
+ #{indent} (raise 'can\\'t call nil : #{fn_expr}')
48
+ #{indent} end)"
49
+ end
50
+ end
51
+
52
+ # def compile_to_ruby indent, srcs
53
+ # ra = @expr.map { |e| e.compile_to_ruby "#{indent} ", srcs}
54
+ # fn = ra.shift
55
+
56
+ # if ra.empty?
57
+ # "#{indent}#{fn}._nydp_callable(#{@expr.first.to_s.inspect})._nydp_call()"
58
+ # else
59
+ # "#{indent}#{fn}._nydp_callable(#{@expr.first.to_s.inspect})._nydp_call(
60
+ # #{ra.join(",\n")}
61
+ # #{indent})"
62
+ # end
63
+ # end
64
+
20
65
  def handle e, f, invoker, *args
21
66
  case e
22
67
  when Nydp::Error, InvocationFailed
23
68
  raise
24
69
  else
25
70
  if e.is_a?(NoMethodError) && !f.respond_to?(invoker)
26
- raise InvocationFailed.new("#{f.inspect} is not a function: args were #{args.inspect} in #{source.inspect}")
71
+ raise InvocationFailed.new("#{f._nydp_inspect} is not a function: args were #{args._nydp_inspect} in #{source._nydp_inspect}")
27
72
  else
28
- msg = args.map { |a| " #{a.inspect}"}.join("\n")
29
- msg = "failed to execute invocation #{f.inspect}\n#{msg}"
30
- msg += "\nsource was #{source.inspect}"
31
- msg += "\nfunction name was #{source.car.inspect}"
73
+ msg = args.map { |a| " #{a._nydp_inspect}"}.join("\n")
74
+ msg = "failed to execute invocation #{f._nydp_inspect}\n#{msg}"
75
+ msg += "\nsource was #{source._nydp_inspect}"
76
+ msg += "\nfunction name was #{source.car._nydp_inspect}"
32
77
  raise InvocationFailed.new msg
33
78
  end
34
79
  end
@@ -39,253 +84,10 @@ module Nydp
39
84
  @expr.map { |x| x.lexical_reach n}.max
40
85
  end
41
86
 
42
- def inspect ; @expr.map { |x| x.inspect }.join(' ') ; end
87
+ def inspect ; "(" + @expr.map { |x| x._nydp_inspect }.join(' ') + ")" ; end
43
88
  def source ; @source ; end
44
89
  def to_s ; source.to_s ; end
45
90
  end
46
-
47
- class Invocation_1 < Invocation::Base
48
- def execute vm
49
- # Invocation.sig @sig
50
- f = vm.args.pop
51
- f.invoke_1 vm
52
- rescue StandardError => e
53
- handle e, f, :invoke_1
54
- end
55
- end
56
-
57
- class Invocation_2 < Invocation::Base
58
- def execute vm
59
- # Invocation.sig @sig
60
- arg = vm.args.pop
61
- f = vm.args.pop
62
- f.invoke_2 vm, arg
63
- rescue StandardError => e
64
- handle e, f, :invoke_2, arg
65
- end
66
- end
67
-
68
- class Invocation_3 < Invocation::Base
69
- def execute vm
70
- # Invocation.sig @sig
71
- arg_1 = vm.args.pop
72
- arg_0 = vm.args.pop
73
- f = vm.args.pop
74
- f.invoke_3 vm, arg_0, arg_1
75
- rescue StandardError => e
76
- handle e, f, :invoke_3, arg_0, arg_1
77
- end
78
- end
79
-
80
- class Invocation_4 < Invocation::Base
81
- def execute vm
82
- # Invocation.sig @sig
83
- arg_2 = vm.args.pop
84
- arg_1 = vm.args.pop
85
- arg_0 = vm.args.pop
86
- f = vm.args.pop
87
- f.invoke_4 vm, arg_0, arg_1, arg_2
88
- rescue StandardError => e
89
- handle e, f, :invoke_4, arg_0, arg_1, arg_2
90
- end
91
- end
92
-
93
- class Invocation_N < Invocation::Base
94
- def initialize arg_count, expr, source
95
- super expr, source
96
- @arg_count = arg_count
97
- end
98
-
99
- def execute vm
100
- # Invocation.sig @sig
101
- args = vm.pop_args @arg_count
102
- args.car.invoke vm, args.cdr
103
- rescue StandardError => e
104
- handle e, args.car, :invoke, args.cdr
105
- end
106
- end
107
-
108
- # TODO generate various Invocation_XXX classes on-demand instead of hand_coding them all up front
109
- SIGS = Set.new
110
-
111
- class Invocation_LEX < Invocation::Base
112
- SIGS << self.name
113
- def initialize expr, src
114
- super expr, src
115
- @sym = expr.car
116
- end
117
-
118
- def execute vm
119
- # Invocation.sig self.class.name
120
- @sym.value(vm.current_context).invoke_1 vm
121
- rescue StandardError => e
122
- handle e, @sym.value(vm.current_context), :invoke_1
123
- end
124
- end
125
-
126
- class Invocation_SYM < Invocation::Base
127
- SIGS << self.name
128
- def initialize expr, src
129
- super expr, src
130
- @sym = expr.car
131
- end
132
-
133
- def execute vm
134
- # Invocation.sig self.class.name
135
- @sym.value.invoke_1 vm
136
- rescue StandardError => e
137
- handle e, @sym.value, :invoke_1
138
- end
139
- end
140
-
141
- class Invocation_LEX_LEX < Invocation::Base
142
- SIGS << self.name
143
- def initialize expr, src
144
- super expr, src
145
- @lex0 = expr.car
146
- @lex1 = expr.cdr.car
147
- end
148
-
149
- def execute vm
150
- # Invocation.sig self.class.name
151
- fn = @lex0.value(vm.current_context)
152
- a0 = @lex1.value(vm.current_context)
153
- fn.invoke_2 vm, a0
154
- rescue StandardError => e
155
- handle e, fn, :invoke_2, a0
156
- end
157
- end
158
-
159
- class Invocation_SYM_LEX < Invocation::Base
160
- SIGS << self.name
161
- def initialize expr, src
162
- super expr, src
163
- @sym = expr.car
164
- @lex = expr.cdr.car
165
- end
166
-
167
- def execute vm
168
- # Invocation.sig self.class.name
169
- a0 = @lex.value(vm.current_context)
170
- @sym.value.invoke_2 vm, a0
171
- rescue StandardError => e
172
- handle e, @sym.value, :invoke_2, a0
173
- end
174
- end
175
-
176
- class Invocation_SYM_LIT < Invocation::Base
177
- SIGS << self.name
178
- def initialize expr, src
179
- super expr, src
180
- @sym = expr.car
181
- @lit = expr.cdr.car.expression
182
- end
183
-
184
- def execute vm
185
- # Invocation.sig self.class.name
186
- @sym.value.invoke_2 vm, @lit
187
- rescue StandardError => e
188
- handle e, @sym.value, :invoke_2, @lit
189
- end
190
- end
191
-
192
- class Invocation_LEX_LEX_LEX < Invocation::Base
193
- SIGS << self.name
194
- def initialize expr, src
195
- super expr, src
196
- @lex_0 = expr.car
197
- @lex_1 = expr.cdr.car
198
- @lex_2 = expr.cdr.cdr.car
199
- end
200
-
201
- def execute vm
202
- # Invocation.sig self.class.name
203
- fn = @lex_0.value(vm.current_context)
204
- a0 = @lex_1.value(vm.current_context)
205
- a1 = @lex_2.value(vm.current_context)
206
- fn.invoke_3 vm, a0, a1
207
- rescue StandardError => e
208
- handle e, fn, :invoke_3, a0, a1
209
- end
210
- end
211
-
212
- class Invocation_SYM_LEX_LEX < Invocation::Base
213
- SIGS << self.name
214
- def initialize expr, src
215
- super expr, src
216
- @sym = expr.car
217
- @lex_0 = expr.cdr.car
218
- @lex_1 = expr.cdr.cdr.car
219
- end
220
-
221
- def execute vm
222
- # Invocation.sig self.class.name
223
- a0 = @lex_0.value(vm.current_context)
224
- a1 = @lex_1.value(vm.current_context)
225
- @sym.value.invoke_3 vm, a0, a1
226
- rescue StandardError => e
227
- handle e, @sym.value, :invoke_3, a0, a1
228
- end
229
- end
230
-
231
- class Invocation_SYM_LEX_LEX_LEX < Invocation::Base
232
- SIGS << self.name
233
- def initialize expr, src
234
- super expr, src
235
- @sym = expr.car
236
- @lex_0 = expr.cdr.car
237
- @lex_1 = expr.cdr.cdr.car
238
- @lex_2 = expr.cdr.cdr.cdr.car
239
- end
240
-
241
- def execute vm
242
- # Invocation.sig self.class.name
243
- a0 = @lex_0.value(vm.current_context)
244
- a1 = @lex_1.value(vm.current_context)
245
- a2 = @lex_2.value(vm.current_context)
246
- @sym.value.invoke_4 vm, a0, a1, a2
247
- rescue StandardError => e
248
- handle e, @sym.value, :invoke_4, a0, a1, a2
249
- end
250
- end
251
-
252
- class Invocation_SYM_LEX_LIT_LEX < Invocation::Base
253
- SIGS << self.name
254
- def initialize expr, src
255
- super expr, src
256
- @sym = expr.car
257
- @lex_0 = expr.cdr.car
258
- @lit_1 = expr.cdr.cdr.car.expression
259
- @lex_2 = expr.cdr.cdr.cdr.car
260
- end
261
-
262
- def execute vm
263
- # Invocation.sig self.class.name
264
- a0 = @lex_0.value(vm.current_context)
265
- a2 = @lex_2.value(vm.current_context)
266
- @sym.value.invoke_4 vm, a0, @lit_1, a2
267
- rescue StandardError => e
268
- handle e, @sym.value, :invoke_4, a0, @lit_1, a2
269
- end
270
- end
271
-
272
- class Invocation_SYM_LIT_LEX < Invocation::Base
273
- SIGS << self.name
274
- def initialize expr, src
275
- super expr, src
276
- @sym = expr.car
277
- @lit_0 = expr.cdr.car.expression
278
- @lex_1 = expr.cdr.cdr.car
279
- end
280
-
281
- def execute vm
282
- # Invocation.sig self.class.name
283
- a1 = @lex_1.value(vm.current_context)
284
- @sym.value.invoke_3 vm, @lit_0, a1
285
- rescue StandardError => e
286
- handle e, @sym.value, :invoke_3, @lit_0, a1
287
- end
288
- end
289
91
  end
290
92
 
291
93
  class FunctionInvocation
@@ -293,35 +95,26 @@ module Nydp
293
95
  attr_accessor :function_instruction, :argument_instructions
294
96
 
295
97
  def lexical_reach n
296
- function_instruction.car.lexical_reach(n)
98
+ function_instruction.lexical_reach(n)
297
99
  end
298
100
 
299
- def self.build expression, bindings
300
- compiled = Compiler.compile_each(expression, bindings)
301
- invocation_sig = compiled.map { |x| sig x }.join("_")
302
-
303
- cname = "Invocation_#{invocation_sig}"
101
+ def compile_to_ruby indent, srcs, opts=nil
102
+ ra = argument_instructions.map { |e| e.compile_to_ruby "#{indent} ", srcs, cando: true }.to_a
103
+ if ra.length == 1
104
+ "#{indent}#{ra.shift}._nydp_call()"
105
+ else
106
+ "#{indent}#{ra.shift}._nydp_call(
107
+ #{ra.join(",\n")}
108
+ #{indent})"
109
+ end
110
+ end
304
111
 
305
- # puts expression.inspect if cname == "Invocation_SYM_LEX_LIT_LEX"
112
+ @@seen = { }
306
113
 
307
- exists = Invocation::SIGS.include? "Nydp::Invocation::#{cname}"
308
- if exists
309
- return Nydp::Invocation.const_get(cname).new(compiled, expression)
310
- end
114
+ def self.build expression, bindings, ns
115
+ compiled = Compiler.compile_each(expression, bindings, ns)
311
116
 
312
- invocation = cons case expression.size
313
- when 1
314
- Invocation::Invocation_1.new(compiled, expression)
315
- when 2
316
- Invocation::Invocation_2.new(compiled, expression)
317
- when 3
318
- Invocation::Invocation_3.new(compiled, expression)
319
- when 4
320
- Invocation::Invocation_4.new(compiled, expression)
321
- else
322
- Invocation::Invocation_N.new(expression.size, compiled, expression)
323
- end
324
- new invocation, compiled, expression, cname
117
+ Invocation::Base.new(compiled, expression)
325
118
  end
326
119
 
327
120
  def initialize function_instruction, argument_instructions, source, sig=nil
@@ -329,13 +122,7 @@ module Nydp
329
122
  @sig = sig
330
123
  end
331
124
 
332
- def execute vm
333
- ## Invocation.sig @sig
334
- vm.push_ctx_instructions function_instruction
335
- vm.push_ctx_instructions argument_instructions
336
- end
337
-
338
- def inspect ; @function_instruction.inspect ; end
339
- def to_s ; @source.to_s ; end
125
+ def inspect ; @function_instruction._nydp_inspect ; end
126
+ def to_s ; @source.to_s ; end
340
127
  end
341
128
  end