nydp 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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/compiler.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'nydp/cond'
2
+ require 'nydp/loop'
2
3
  require 'nydp/function_invocation'
3
4
  require 'nydp/interpreted_function'
4
5
  require 'nydp/literal'
@@ -7,48 +8,57 @@ module Nydp
7
8
  class Compiler
8
9
  extend Helper
9
10
 
10
- def self.compile expression, bindings
11
- compile_expr expression, bindings
11
+ def self.compile expression, bindings, ns
12
+ compile_expr expression, bindings, ns
12
13
  rescue StandardError => e
13
- raise Nydp::Error.new "failed to compile expression:\n#{expression.inspect}"
14
+ raise Nydp::Error.new "failed to compile expression:\n#{expression._nydp_inspect}\n#{e.message}"
14
15
  end
15
16
 
16
- def self.compile_expr expression, bindings
17
- if expression.is_a? Nydp::Symbol
18
- SymbolLookup.build expression, bindings
17
+ def self.compile_expr expression, bindings, ns
18
+ # if expression.is_a? Nydp::Symbol
19
+ if expression.is_a? ::Symbol
20
+ SymbolLookup.build expression, bindings, ns
19
21
  elsif literal? expression
20
- Literal.build expression, bindings
22
+ Literal.build expression, bindings, ns
21
23
  elsif expression.is_a? Nydp::Pair
22
- compile_pair expression, bindings
24
+ begin
25
+ compile_pair expression, bindings, ns
26
+ rescue => e
27
+ raise "failed to compile #{expression._nydp_inspect}"
28
+ end
29
+ else
30
+ raise Nydp::Error.new "failed to compile unrecognised expression:\n#{expression._nydp_inspect}\nwhich is a #{expression.class}"
23
31
  end
24
32
  end
25
33
 
26
34
  def self.maybe_cons a, b
27
- Nydp::NIL.is?(a) ? b : cons(a, b)
35
+ (a == nil) ? b : cons(a, b)
28
36
  end
29
37
 
30
- def self.compile_each expr, bindings
31
- if Nydp::NIL.is?(expr)
38
+ def self.compile_each expr, bindings, ns
39
+ if expr == nil
32
40
  expr
33
41
  elsif pair?(expr)
34
- maybe_cons compile(expr.car, bindings), compile_each(expr.cdr, bindings)
42
+ maybe_cons compile(expr.car, bindings, ns), compile_each(expr.cdr, bindings, ns)
35
43
  else
36
- compile(expr, bindings)
44
+ compile(expr, bindings, ns)
37
45
  end
38
46
  end
39
47
 
40
- def self.compile_pair expression, bindings
48
+ def self.compile_pair expression, bindings, ns
41
49
  key = expression.car
42
50
  if sym?(key, :cond)
43
- Cond.build expression.cdr, bindings # todo: replace with function? (cond x (fn () iftrue) (fn () iffalse)) -->> performance issues, creating two closures for every cond invocation
51
+ Cond.build expression.cdr, bindings, ns # todo: replace with function? (cond x (fn () iftrue) (fn () iffalse)) -->> performance issues, creating two closures for every cond invocation
52
+ elsif sym?(key, :loop)
53
+ Loop.build expression.cdr, bindings, ns
44
54
  elsif sym?(key, :quote)
45
- Literal.build expression.cadr, bindings
55
+ Literal.build expression.cadr, bindings, ns
46
56
  elsif sym?(key, :assign)
47
- Assignment.build expression.cdr, bindings
57
+ Assignment.build expression.cdr, bindings, ns
48
58
  elsif sym?(key, :fn)
49
- InterpretedFunction.build expression.cadr, expression.cddr, bindings
59
+ InterpretedFunction.build expression.cadr, expression.cddr, bindings, ns
50
60
  else
51
- FunctionInvocation.build expression, bindings
61
+ FunctionInvocation.build expression, bindings, ns
52
62
  end
53
63
  end
54
64
  end
data/lib/nydp/cond.rb CHANGED
@@ -1,73 +1,69 @@
1
1
  module Nydp
2
- class ExecuteConditionalInstruction
3
- extend Helper
4
-
5
- def initialize when_true, when_false
6
- @when_true, @when_false = when_true, when_false
7
- end
8
-
9
- def lexical_reach n
10
- [@when_true.lexical_reach(n), @when_false.lexical_reach(n)].max
11
- end
12
-
13
- def execute vm
14
- (Nydp::NIL.is?(vm.args.pop) ? @when_false : @when_true).execute vm
15
- end
16
-
17
- def inspect
18
- "when_true:#{@when_true.inspect}:when_false:#{@when_false.inspect}"
19
- end
20
-
21
- def to_s
22
- "#{@when_true.to_s} #{@when_false.to_s}"
23
- end
24
- end
25
-
26
2
  class Cond
27
3
  extend Helper
28
4
  include Helper
29
5
  attr_reader :condition, :conditional
30
6
 
31
7
  def initialize cond, when_true, when_false
32
- @condition, @conditional = cond, cons(ExecuteConditionalInstruction.new(when_true, when_false))
8
+ @condition, @when_true, @when_false = cond, when_true, when_false
33
9
  end
34
10
 
35
11
  def lexical_reach n
36
- [@condition.lexical_reach(n), @conditional.car.lexical_reach(n)].max
12
+ [@condition.lexical_reach(n), @when_true.lexical_reach(n), @when_false.lexical_reach(n)].max
37
13
  end
38
14
 
39
15
  def execute vm
40
- vm.push_ctx_instructions conditional
41
- condition.execute vm
16
+ if (@condition.execute(vm))
17
+ @when_true.execute(vm)
18
+ else
19
+ @when_false.execute(vm)
20
+ end
21
+ end
22
+
23
+ def compile_to_ruby indent, srcs, opts=nil
24
+ if (!@when_false) || (@when_false.is_a?(Nydp::Literal) && !@when_false.expression)
25
+ "#{indent}##> #{inspect.split(/\n/).join('\n')}
26
+ #{indent}if (#{@condition.compile_to_ruby "", srcs})
27
+ #{@when_true.compile_to_ruby(indent + " ", srcs, cando: true)}
28
+ #{indent}end"
29
+ else
30
+ "#{indent}##> #{inspect.split(/\n/).join('\n')}
31
+ #{indent}if (#{@condition.compile_to_ruby "", srcs})
32
+ #{@when_true.compile_to_ruby(indent + " ", srcs, cando: true)}
33
+ #{indent}else
34
+ #{@when_false.compile_to_ruby(indent + " ", srcs, cando: true)}
35
+ #{indent}end"
36
+ end
42
37
  end
43
38
 
44
39
  def inspect
45
- "cond:#{condition.inspect}:#{conditional.inspect}"
40
+ "(cond #{condition._nydp_inspect} #{@when_true._nydp_inspect} #{@when_false._nydp_inspect})"
46
41
  end
47
42
 
48
43
  def to_s
49
- "(cond #{condition.to_s} #{conditional.to_s})"
44
+ "(cond #{condition.to_s} #{@when_true.to_s} #{@when_false.to_s})"
50
45
  end
51
46
 
52
- def self.build expressions, bindings
47
+ def self.build expressions, bindings, ns
53
48
  if expressions.is_a? Nydp::Pair
54
- cond = Compiler.compile expressions.car, bindings
55
- when_true = Compiler.compile expressions.cdr.car, bindings
56
- when_false = Compiler.compile expressions.cdr.cdr.car, bindings
49
+ cond = Compiler.compile expressions.car, bindings, ns
50
+ when_true = Compiler.compile expressions.cdr.car, bindings, ns
51
+ when_false = Compiler.compile expressions.cdr.cdr.car, bindings, ns
57
52
  csig = sig(cond)
58
- # puts cond_sig
59
- # TODO : handle literal nil explicitly (if x y) -> #when_false is literal nil, we can hardcode that
60
- # todo : handle "OR" explicitly -> (if x x y) -> when #cond equals #when_true, hardcode this case
61
- case csig
62
- when "LEX"
63
- Cond_LEX.build(cond, when_true, when_false)
64
- when "SYM"
65
- Cond_SYM.new(cond, cons(when_true), cons(when_false))
66
- else
67
- new(cond, when_true, when_false)
68
- end
53
+ xsig = "#{sig(cond)}_#{sig(when_true)}_#{sig(when_false)}"
54
+
55
+ # case csig
56
+ # when "LEX"
57
+ # Cond_LEX.build(cond, when_true, when_false)
58
+ # when "SYM"
59
+ # Cond_SYM.new(cond, when_true, when_false)
60
+ # else
61
+ # new(cond, when_true, when_false)
62
+ # end
63
+
64
+ new(cond, when_true, when_false)
69
65
  else
70
- raise "can't compile Cond: #{expressions.inspect}"
66
+ raise "can't compile Cond: #{expressions._nydp_inspect}"
71
67
  end
72
68
  end
73
69
  end
@@ -84,20 +80,26 @@ module Nydp
84
80
  [@condition.lexical_reach(n), @when_true.lexical_reach(n), @when_false.lexical_reach(n)].max
85
81
  end
86
82
 
87
- def inspect ; "cond:#{@condition.inspect}:#{@when_true.inspect}:#{@when_false.inspect}" ; end
83
+ def inspect ; "cond:#{@condition._nydp_inspect}:#{@when_true._nydp_inspect}:#{@when_false._nydp_inspect}" ; end
88
84
  def to_s ; "(cond #{@condition.to_s} #{@when_true.to_s} #{@when_false.to_s})" ; end
85
+ # def compile_to_ruby indent
86
+ # "((#{@condition.compile_to_ruby "", srcs}) ? (#{@when_true.compile_to_ruby "", srcs}) : (#{@when_false.compile_to_ruby "", srcs}))"
87
+ # end
89
88
  end
90
89
 
91
90
  class Cond_LEX < CondBase
92
91
  def execute vm
93
- truth = !Nydp::NIL.is?(@condition.value vm.current_context)
94
- vm.push_ctx_instructions (truth ? @when_true : @when_false)
92
+ if @condition.value vm.current_context
93
+ @when_true.execute(vm)
94
+ else
95
+ @when_false.execute(vm)
96
+ end
95
97
  end
96
98
 
97
99
  def lexical_reach n
98
100
  cr = @condition.lexical_reach(n)
99
- ct = @when_true.car.lexical_reach(n)
100
- cf = @when_false.car.lexical_reach(n)
101
+ ct = @when_true.lexical_reach(n)
102
+ cf = @when_false.lexical_reach(n)
101
103
 
102
104
  [cr, ct, cf].max
103
105
  end
@@ -107,23 +109,27 @@ module Nydp
107
109
  fsig = sig(when_false)
108
110
  cond_sig = "#{tsig}_#{fsig}"
109
111
 
110
- # if (cond == when_true)
111
- # OR_LEX.build cond, when_false
112
-
113
112
  if (cond == when_true) && (fsig == "LIT")
114
- OR_LEX_LIT.new cond, nil, when_false
113
+ OR_LEX_LIT.new cond, nil, when_false.expression
115
114
  elsif (cond == when_true) && (fsig == "LEX")
116
115
  OR_LEX_LEX.new cond, nil, when_false
116
+ elsif (cond == when_true)
117
+ OR_LEX_XXX.new cond, nil, when_false
117
118
  else
118
119
  case cond_sig
119
120
  when "LIT_LIT"
121
+ # puts "building Cond_LEX_LIT_LIT #{[cond, when_true.expression, when_false.expression]}"
120
122
  Nydp::Cond_LEX_LIT_LIT.new(cond, when_true.expression, when_false.expression)
121
123
  when "LEX_LIT"
122
124
  Nydp::Cond_LEX_LEX_LIT.new(cond, when_true, when_false.expression)
123
125
  when "CND_LIT"
124
126
  Nydp::Cond_LEX_CND_LIT.new(cond, when_true, when_false.expression)
127
+ when "NVB_LEX"
128
+ Nydp::Cond_LEX_NVB_LEX.new(cond, when_true, when_false)
129
+ when "NVB_LIT"
130
+ Nydp::Cond_LEX_NVB_LIT.new(cond, when_true, when_false.expression)
125
131
  else
126
- Nydp::Cond_LEX.new(cond, cons(when_true), cons(when_false))
132
+ new(cond, when_true, when_false)
127
133
  end
128
134
  end
129
135
  end
@@ -131,67 +137,68 @@ module Nydp
131
137
 
132
138
  class OR_LEX_LIT < CondBase
133
139
  def execute vm
134
- value = @condition.value vm.current_context
135
- vm.push_arg(!Nydp::NIL.is?(value) ? value : @when_false)
140
+ @condition.value(vm.current_context) || @when_false
136
141
  end
137
142
  end
138
143
 
139
144
  class OR_LEX_LEX < CondBase
140
145
  def execute vm
141
- value = @condition.value vm.current_context
142
- vm.push_arg(!Nydp::NIL.is?(value) ? value : (@when_false.value vm.current_context))
146
+ @condition.value(vm.current_context) || (@when_false.value vm.current_context)
143
147
  end
144
148
  end
145
149
 
146
- # class OR_LEX < CondBase
147
- # def execute vm
148
- # value = @condition.value vm.current_context
149
- # if !Nydp::NIL.is?(value)
150
- # vm.push_arg value
151
- # else
152
- # @when_false.execute vm
153
- # end
154
- # end
155
-
156
- # def self.build cond, when_false
157
- # case sig(when_false)
158
- # when "LIT"
159
- # OR_LEX_LIT.new(cond, nil, when_false)
160
- # when "LEX"
161
- # OR_LEX_LEX.new(cond, nil, when_false)
162
- # else
163
- # OR_LEX.new(cond, nil, when_false)
164
- # end
165
- # end
166
- # end
150
+ class OR_LEX_XXX < CondBase
151
+ def execute vm
152
+ @condition.value(vm.current_context) || (@when_false.execute vm)
153
+ end
154
+ end
167
155
 
168
156
  class Cond_LEX_LIT_LIT < CondBase # (def no (arg) (cond arg nil t))
169
157
  def execute vm
170
- falsity = Nydp::NIL.is?(@condition.value vm.current_context)
171
- vm.push_arg(falsity ? @when_false : @when_true)
158
+ (@condition.value vm.current_context) ? @when_true : @when_false
172
159
  end
173
160
  end
174
161
 
175
162
  class Cond_LEX_LEX_LIT < CondBase
176
163
  def execute vm
177
- falsity = Nydp::NIL.is?(@condition.value vm.current_context)
178
- vm.push_arg(falsity ? @when_false : (@when_true.value vm.current_context))
164
+ (@condition.value vm.current_context) ? (@when_true.value vm.current_context) : @when_false
179
165
  end
180
166
  end
181
167
 
182
168
  class Cond_LEX_CND_LIT < CondBase
183
169
  def execute vm
184
- if Nydp::NIL.is?(@condition.value vm.current_context)
185
- vm.push_arg @when_false
170
+ if (@condition.value vm.current_context)
171
+ @when_true.execute vm
172
+ else
173
+ @when_false
174
+ end
175
+ end
176
+ end
177
+
178
+ class Cond_LEX_NVB_LEX < CondBase
179
+ def execute vm
180
+ if (@condition.value vm.current_context)
181
+ @when_true.execute vm
186
182
  else
183
+ @when_false.value(vm.current_context)
184
+ end
185
+ end
186
+ end
187
+
188
+ class Cond_LEX_NVB_LIT < CondBase
189
+ def execute vm
190
+ if (@condition.value vm.current_context)
187
191
  @when_true.execute vm
192
+ else
193
+ @when_false
188
194
  end
189
195
  end
190
196
  end
191
197
 
192
198
  class Cond_SYM < CondBase
193
199
  def execute vm
194
- vm.push_ctx_instructions (Nydp::NIL.is?(@condition.value) ? @when_false : @when_true)
200
+ # puts "Cond_SYM"
201
+ (@condition.value) ? @when_true.execute(vm) : @when_false.execute(vm)
195
202
  end
196
203
  end
197
204
  end
@@ -24,6 +24,7 @@ module Nydp
24
24
  "set_index(#{binding_index}, value)"
25
25
  end
26
26
 
27
+ line = __LINE__ + 2
27
28
  code = <<-KLASS
28
29
  def initialize name, lexical_depth
29
30
  @name, @lexical_depth = name, lexical_depth
@@ -33,10 +34,14 @@ module Nydp
33
34
  @lexical_depth + n
34
35
  end
35
36
 
37
+ def compile_to_ruby indent, srcs, opts=nil
38
+ "\#{indent}_arg_\#{@name.to_s._nydp_name_to_rb_name}"
39
+ end
40
+
36
41
  def value ctx
37
- ctx#{getctx}.#{at_index} || Nydp::NIL
42
+ ctx#{getctx}.#{at_index} || nil
38
43
  rescue
39
- raise "failed looking up \#{@name.inspect} (\#{@name.class.name})"
44
+ raise "failed looking up \#{@name._nydp_inspect} (\#{@name.class.name}) : lookup expression was ctx#{getctx}.#{at_index}"
40
45
  end
41
46
 
42
47
  def assign value, ctx
@@ -45,24 +50,21 @@ module Nydp
45
50
  raise "problem in \#{self.class.name}#assign, name is \#{@name}, depth is \#{depth}, index is #{binding_index}"
46
51
  end
47
52
 
48
- def execute vm
49
- vm.push_arg value vm.current_context
50
- end
51
-
52
53
  def depth ; #{depth} ; end
53
54
  def inspect ; to_s ; end
54
- def to_s ; "[#{depth}##{binding_index}#\#{@lexical_depth}]\#{@name}" ; end
55
+ # def to_s ; "[#{depth}##{binding_index}#\#{@lexical_depth}]\#{@name}" ; end
56
+ def to_s ; @name ; end
55
57
  KLASS
56
58
 
57
59
  const_set name, Class.new(Nydp::ContextSymbol) {
58
- eval code, binding, name.to_s, 0
60
+ eval code, binding, "#{name.to_s} : #{__FILE__}", line
59
61
  }
60
62
  end
61
63
 
62
64
  def self.build effective_depth, name, binding_index, lexical_depth
63
65
  const_get(:"ContextSymbol_#{effective_depth}_#{binding_index}").new(name, lexical_depth)
64
66
  rescue
65
- raise "building ContextSymbol #{[effective_depth, name, binding_index, lexical_depth].inspect}"
67
+ raise "building ContextSymbol #{[effective_depth, name, binding_index, lexical_depth]._nydp_inspect}"
66
68
  end
67
69
  end
68
70
  end
data/lib/nydp/core.rb CHANGED
@@ -2,11 +2,9 @@ require 'nydp/plugin'
2
2
 
3
3
  module Nydp
4
4
  class Core
5
- def name ; "Nydp Core" ; end
5
+ include Nydp::PluginHelper
6
6
 
7
- def relative_path name
8
- File.join File.expand_path(File.dirname(__FILE__)), name
9
- end
7
+ def name ; "Nydp Core" ; end
10
8
 
11
9
  def base_path
12
10
  relative_path "../lisp/"
@@ -17,81 +15,84 @@ module Nydp
17
15
  end
18
16
 
19
17
  def loadfiles
20
- Dir.glob(relative_path '../lisp/core-*.nydp').sort
18
+ file_readers Dir.glob(relative_path '../lisp/core-*.nydp').sort
21
19
  end
22
20
 
23
21
  def testfiles
24
- Dir.glob(relative_path '../lisp/tests/**/*.nydp')
22
+ file_readers Dir.glob(relative_path '../lisp/tests/**/*.nydp')
25
23
  end
26
24
 
27
25
  def setup ns
28
- Symbol.mk(:cons , ns).assign(Nydp::Builtin::RubyWrap::Cons.instance)
29
- Symbol.mk(:car , ns).assign(Nydp::Builtin::RubyWrap::Car.instance )
30
- Symbol.mk(:cdr , ns).assign(Nydp::Builtin::RubyWrap::Cdr.instance )
31
- Symbol.mk(:log , ns).assign(Nydp::Builtin::RubyWrap::Log.instance )
32
-
33
- Symbol.mk(:+, ns).assign(Nydp::Builtin::Plus.instance)
34
- Symbol.mk(:-, ns).assign(Nydp::Builtin::Minus.instance)
35
- Symbol.mk(:*, ns).assign(Nydp::Builtin::Times.instance)
36
- Symbol.mk(:/, ns).assign(Nydp::Builtin::Divide.instance)
37
- Symbol.mk(:>, ns).assign(Nydp::Builtin::GreaterThan.instance)
38
- Symbol.mk(:<, ns).assign(Nydp::Builtin::LessThan.instance)
39
- Symbol.mk(:mod, ns).assign(Nydp::Builtin::Modulo.instance)
40
- Symbol.mk(:eval, ns).assign(Nydp::Builtin::Eval.instance)
41
- Symbol.mk(:false, ns).assign(false)
42
- Symbol.mk(:hash, ns).assign(Nydp::Builtin::Hash.instance)
43
- Symbol.mk(:apply, ns).assign(Nydp::Builtin::Apply.instance)
44
- Symbol.mk(:date, ns).assign(Nydp::Builtin::Date.instance)
45
- Symbol.mk(:error, ns).assign(Nydp::Builtin::Error.instance)
46
- Symbol.mk(:parse, ns).assign(Nydp::Builtin::Parse.instance)
47
- Symbol.mk(:p, ns).assign(Nydp::Builtin::Puts.instance)
48
- Symbol.mk(:PI, ns).assign 3.1415
49
- Symbol.mk(:nil, ns).assign Nydp::NIL
50
- Symbol.mk(:rand, ns).assign Nydp::Builtin::Rand.instance
51
- Symbol.mk(:sort, ns).assign Nydp::Builtin::Sort.instance
52
- Symbol.mk(:abs, ns).assign Nydp::Builtin::Abs.instance
53
- Symbol.mk(:sqrt, ns).assign Nydp::Builtin::Sqrt.instance
54
- Symbol.mk(:t, ns).assign Nydp::T
55
- Symbol.mk(:sym, ns).assign Nydp::Builtin::Sym.instance
56
- Symbol.mk(:ensuring , ns).assign(Nydp::Builtin::Ensuring.instance)
57
- Symbol.mk(:inspect , ns).assign(Nydp::Builtin::Inspect.instance)
58
- Symbol.mk(:comment , ns).assign(Nydp::Builtin::Comment.instance)
59
- Symbol.mk("handle-error" , ns).assign(Nydp::Builtin::HandleError.instance)
60
- Symbol.mk("parse-in-string" , ns).assign(Nydp::Builtin::ParseInString.instance)
61
- Symbol.mk("random-string" , ns).assign(Nydp::Builtin::RandomString.instance)
62
- Symbol.mk("regexp" , ns).assign(Nydp::Builtin::Regexp.instance)
63
- Symbol.mk("to-string" , ns).assign(Nydp::Builtin::ToString.instance)
64
- Symbol.mk("to-integer" , ns).assign(Nydp::Builtin::ToInteger.instance)
65
- Symbol.mk("string-length" , ns).assign(Nydp::Builtin::StringLength.instance)
66
- Symbol.mk("string-replace" , ns).assign(Nydp::Builtin::StringReplace.instance)
67
- Symbol.mk("string-match" , ns).assign(Nydp::Builtin::StringMatch.instance)
68
- Symbol.mk("string-split" , ns).assign(Nydp::Builtin::StringSplit.instance)
69
- Symbol.mk("string/pad-left" , ns).assign(Nydp::Builtin::StringPadLeft.instance)
70
- Symbol.mk("string/pad-right" , ns).assign(Nydp::Builtin::StringPadRight.instance)
71
- Symbol.mk("time" , ns).assign(Nydp::Builtin::Time.instance)
72
- Symbol.mk("thread-locals" , ns).assign(Nydp::Builtin::ThreadLocals.instance)
73
- Symbol.mk("type-of" , ns).assign(Nydp::Builtin::TypeOf.instance)
74
- Symbol.mk(:"eq?" , ns).assign(Nydp::Builtin::IsEqual.instance)
75
- Symbol.mk(:"cdr-set" , ns).assign(Nydp::Builtin::CdrSet.instance)
76
- Symbol.mk(:"hash-get" , ns).assign(Nydp::Builtin::HashGet.instance)
77
- Symbol.mk(:"hash-set" , ns).assign(Nydp::Builtin::HashSet.instance)
78
- Symbol.mk(:"hash-keys" , ns).assign(Nydp::Builtin::HashKeys.instance)
79
- Symbol.mk(:"hash-key?" , ns).assign(Nydp::Builtin::HashKeyPresent.instance)
80
- Symbol.mk(:"hash-merge" , ns).assign(Nydp::Builtin::HashMerge.instance)
81
- Symbol.mk(:"hash-slice" , ns).assign(Nydp::Builtin::HashSlice.instance)
82
- Symbol.mk(:"vm-info" , ns).assign Nydp::Builtin::VmInfo.instance
83
- Symbol.mk(:"pre-compile" , ns).assign Nydp::Builtin::PreCompile.instance
84
- Symbol.mk(:"script-run" , ns).assign Nydp::Builtin::ScriptRun.instance
85
- Symbol.mk(:"**" , ns).assign Nydp::Builtin::MathPower.instance
86
- Symbol.mk(:"" , ns).assign Nydp::Builtin::MathFloor.instance
87
- Symbol.mk(:"math-floor" , ns).assign Nydp::Builtin::MathFloor.instance
88
- Symbol.mk(:"" , ns).assign Nydp::Builtin::MathCeiling.instance
89
- Symbol.mk(:"math-ceiling" , ns).assign Nydp::Builtin::MathCeiling.instance
90
- Symbol.mk(:"math-round" , ns).assign Nydp::Builtin::MathRound.instance
91
- Symbol.mk(:"" , ns).assign Nydp::Builtin::SetIntersection.instance
92
- Symbol.mk(:"set-intersection", ns).assign Nydp::Builtin::SetIntersection.instance
93
- Symbol.mk(:"" , ns).assign Nydp::Builtin::SetUnion.instance
94
- Symbol.mk(:"set-union" , ns).assign Nydp::Builtin::SetUnion.instance
26
+ ns.assign(:cons , Nydp::Builtin::RubyWrap::Cons.instance )
27
+ ns.assign(:car , Nydp::Builtin::RubyWrap::Car.instance )
28
+ ns.assign(:cdr , Nydp::Builtin::RubyWrap::Cdr.instance )
29
+ ns.assign(:log , Nydp::Builtin::Log.instance )
30
+ ns.assign(:ln , Nydp::Builtin::RubyWrap::Ln.instance )
31
+ ns.assign(:mod , Nydp::Builtin::RubyWrap::Modulo.instance )
32
+ ns.assign(:sqrt , Nydp::Builtin::RubyWrap::Sqrt.instance )
33
+ ns.assign(:regexp , Nydp::Builtin::RubyWrap::Regexp.instance )
34
+ ns.assign("string/pad-left" , Nydp::Builtin::RubyWrap::StringPadLeft.instance )
35
+ ns.assign("string/pad-right" , Nydp::Builtin::RubyWrap::StringPadRight.instance)
36
+ ns.assign("string/force-encoding", Nydp::Builtin::RubyWrap::StringForceEncoding.instance)
37
+ ns.assign("to-list" , Nydp::Builtin::RubyWrap::ToList.instance )
38
+ ns.assign(:+ , Nydp::Builtin::Plus.instance )
39
+ ns.assign(:- , Nydp::Builtin::Minus.instance )
40
+ ns.assign(:* , Nydp::Builtin::Times.instance )
41
+ ns.assign(:/ , Nydp::Builtin::Divide.instance )
42
+ ns.assign(:> , Nydp::Builtin::GreaterThan.instance )
43
+ ns.assign(:< , Nydp::Builtin::LessThan.instance )
44
+ ns.assign(:eval , Nydp::Builtin::Eval.new(ns) )
45
+ ns.assign(:false , false )
46
+ ns.assign(:hash , Nydp::Builtin::Hash.instance )
47
+ ns.assign(:apply , Nydp::Builtin::Apply.instance )
48
+ ns.assign(:date , Nydp::Builtin::Date.instance )
49
+ ns.assign(:error , Nydp::Builtin::Error.instance )
50
+ ns.assign(:parse , Nydp::Builtin::Parse.instance )
51
+ ns.assign(:p , Nydp::Builtin::Puts.instance )
52
+ ns.assign(:PI , Math::PI )
53
+ ns.assign(:"π" , Math::PI )
54
+ ns.assign(:rand , Nydp::Builtin::Rand.instance )
55
+ ns.assign(:rng , Nydp::Builtin::RNG.instance )
56
+ ns.assign(:sort , Nydp::Builtin::Sort.instance )
57
+ ns.assign(:abs , Nydp::Builtin::Abs.instance )
58
+ ns.assign(:sym , Nydp::Builtin::Sym.instance )
59
+ ns.assign(:ensuring , Nydp::Builtin::Ensuring.instance )
60
+ ns.assign(:inspect , Nydp::Builtin::Inspect.instance )
61
+ ns.assign(:comment , Nydp::Builtin::Comment.instance )
62
+ ns.assign("handle-error" , Nydp::Builtin::HandleError.instance )
63
+ ns.assign("parse-in-string" , Nydp::Builtin::ParseInString.instance )
64
+ ns.assign("random-string" , Nydp::Builtin::RandomString.instance )
65
+ ns.assign("to-string" , Nydp::Builtin::ToString.instance )
66
+ ns.assign("to-integer" , Nydp::Builtin::ToInteger.instance )
67
+ ns.assign("string-length" , Nydp::Builtin::StringLength.instance )
68
+ ns.assign("string-replace" , Nydp::Builtin::StringReplace.instance )
69
+ ns.assign("string-match" , Nydp::Builtin::StringMatch.instance )
70
+ ns.assign("string-split" , Nydp::Builtin::StringSplit.instance )
71
+ ns.assign("time" , Nydp::Builtin::Time.instance )
72
+ ns.assign("thread-locals" , Nydp::Builtin::ThreadLocals.instance )
73
+ ns.assign("type-of" , Nydp::Builtin::TypeOf.instance )
74
+ ns.assign(:"eq?" , Nydp::Builtin::IsEqual.instance )
75
+ ns.assign(:"cdr-set" , Nydp::Builtin::CdrSet.instance )
76
+ ns.assign(:"hash-get" , Nydp::Builtin::HashGet.instance )
77
+ ns.assign(:"hash-set" , Nydp::Builtin::HashSet.instance )
78
+ ns.assign(:"hash-keys" , Nydp::Builtin::HashKeys.instance )
79
+ ns.assign(:"hash-key?" , Nydp::Builtin::HashKeyPresent.instance )
80
+ ns.assign(:"hash-merge" , Nydp::Builtin::HashMerge.instance )
81
+ ns.assign(:"hash-slice" , Nydp::Builtin::HashSlice.instance )
82
+ ns.assign(:"hash-to-array" , Nydp::Builtin::HashToArray.instance )
83
+ ns.assign(:"vm-info" , Nydp::Builtin::VmInfo.instance )
84
+ ns.assign(:"pre-compile-new-expression", Nydp::Builtin::PreCompile.instance )
85
+ ns.assign(:"script-run" , Nydp::Builtin::ScriptRun.instance )
86
+ ns.assign(:"**" , Nydp::Builtin::MathPower.instance )
87
+ ns.assign(:"" , Nydp::Builtin::MathFloor.instance )
88
+ ns.assign(:"math-floor" , Nydp::Builtin::MathFloor.instance )
89
+ ns.assign(:"" , Nydp::Builtin::MathCeiling.instance )
90
+ ns.assign(:"math-ceiling" , Nydp::Builtin::MathCeiling.instance )
91
+ ns.assign(:"math-round" , Nydp::Builtin::MathRound.instance )
92
+ ns.assign(:"" , Nydp::Builtin::SetIntersection.instance )
93
+ ns.assign(:"set-intersection", Nydp::Builtin::SetIntersection.instance )
94
+ ns.assign(:"⋃" , Nydp::Builtin::SetUnion.instance )
95
+ ns.assign(:"set-union" , Nydp::Builtin::SetUnion.instance )
95
96
  end
96
97
  end
97
98
  end