nydp 0.1.15 → 0.2.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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lisp/core-015-documentation.nydp +11 -4
  3. data/lib/lisp/core-040-utils.nydp +3 -0
  4. data/lib/lisp/tests/builtin-tests.nydp +1 -1
  5. data/lib/lisp/tests/foundation-test.nydp +40 -0
  6. data/lib/lisp/tests/time-examples.nydp +21 -0
  7. data/lib/nydp.rb +10 -8
  8. data/lib/nydp/builtin.rb +3 -2
  9. data/lib/nydp/builtin/apply.rb +2 -2
  10. data/lib/nydp/builtin/car.rb +1 -1
  11. data/lib/nydp/builtin/cdr.rb +1 -1
  12. data/lib/nydp/builtin/cdr_set.rb +1 -1
  13. data/lib/nydp/builtin/comment.rb +2 -2
  14. data/lib/nydp/builtin/cons.rb +1 -1
  15. data/lib/nydp/builtin/date.rb +20 -0
  16. data/lib/nydp/builtin/divide.rb +2 -2
  17. data/lib/nydp/builtin/ensuring.rb +3 -3
  18. data/lib/nydp/builtin/error.rb +1 -1
  19. data/lib/nydp/builtin/eval.rb +2 -6
  20. data/lib/nydp/builtin/greater_than.rb +3 -3
  21. data/lib/nydp/builtin/handle_error.rb +2 -2
  22. data/lib/nydp/builtin/hash.rb +14 -20
  23. data/lib/nydp/builtin/inspect.rb +1 -1
  24. data/lib/nydp/builtin/is_equal.rb +10 -5
  25. data/lib/nydp/builtin/less_than.rb +3 -3
  26. data/lib/nydp/builtin/load_tests.rb +3 -7
  27. data/lib/nydp/builtin/millisecs.rb +1 -1
  28. data/lib/nydp/builtin/minus.rb +2 -2
  29. data/lib/nydp/builtin/modulo.rb +1 -1
  30. data/lib/nydp/builtin/parse.rb +3 -6
  31. data/lib/nydp/builtin/parse_in_string.rb +3 -6
  32. data/lib/nydp/builtin/plus.rb +3 -3
  33. data/lib/nydp/builtin/pre_compile.rb +2 -2
  34. data/lib/nydp/builtin/puts.rb +2 -2
  35. data/lib/nydp/builtin/random_string.rb +4 -4
  36. data/lib/nydp/builtin/sort.rb +1 -1
  37. data/lib/nydp/builtin/sqrt.rb +1 -1
  38. data/lib/nydp/builtin/string_match.rb +7 -10
  39. data/lib/nydp/builtin/string_replace.rb +1 -1
  40. data/lib/nydp/builtin/string_split.rb +1 -1
  41. data/lib/nydp/builtin/sym.rb +2 -6
  42. data/lib/nydp/builtin/thread_locals.rb +1 -1
  43. data/lib/nydp/builtin/time.rb +52 -0
  44. data/lib/nydp/builtin/times.rb +2 -2
  45. data/lib/nydp/builtin/to_string.rb +2 -2
  46. data/lib/nydp/builtin/type_of.rb +3 -7
  47. data/lib/nydp/builtin/vm_info.rb +1 -1
  48. data/lib/nydp/compiler.rb +2 -2
  49. data/lib/nydp/cond.rb +1 -1
  50. data/lib/nydp/core.rb +48 -48
  51. data/lib/nydp/function_invocation.rb +1 -1
  52. data/lib/nydp/helper.rb +5 -5
  53. data/lib/nydp/interpreted_function.rb +3 -3
  54. data/lib/nydp/lexical_context.rb +4 -4
  55. data/lib/nydp/lexical_context_builder.rb +2 -2
  56. data/lib/nydp/pair.rb +11 -11
  57. data/lib/nydp/runner.rb +2 -2
  58. data/lib/nydp/symbol.rb +2 -2
  59. data/lib/nydp/symbol_lookup.rb +1 -1
  60. data/lib/nydp/truth.rb +8 -11
  61. data/lib/nydp/version.rb +1 -1
  62. data/lib/nydp/vm.rb +4 -3
  63. data/spec/date_spec.rb +28 -20
  64. data/spec/error_spec.rb +1 -1
  65. data/spec/foreign_hash_spec.rb +12 -12
  66. data/spec/hash_non_hash_behaviour_spec.rb +8 -8
  67. data/spec/hash_spec.rb +14 -14
  68. data/spec/literal_spec.rb +2 -2
  69. data/spec/nydp_spec.rb +6 -6
  70. data/spec/pair_spec.rb +8 -8
  71. data/spec/parser_spec.rb +1 -1
  72. data/spec/spec_helper.rb +1 -1
  73. data/spec/string_atom_spec.rb +9 -9
  74. data/spec/string_token_spec.rb +1 -1
  75. data/spec/symbol_spec.rb +9 -9
  76. data/spec/thread_local_spec.rb +2 -6
  77. data/spec/time_spec.rb +48 -0
  78. metadata +7 -3
  79. data/lib/nydp/builtin/today.rb +0 -18
@@ -33,7 +33,7 @@ module Nydp
33
33
  f = vm.args.pop
34
34
  f.invoke_1 vm
35
35
  rescue Exception => e
36
- handle e, f, Nydp.NIL
36
+ handle e, f, Nydp::NIL
37
37
  end
38
38
  end
39
39
 
data/lib/nydp/helper.rb CHANGED
@@ -3,9 +3,9 @@ module Nydp
3
3
  ::Symbol => ->(obj, ns) { Nydp::Symbol.mk(obj, ns) },
4
4
  Array => ->(obj, ns) { Nydp::Pair.from_list obj.map { |o| Nydp.r2n o, ns } },
5
5
  String => ->(obj, ns) { Nydp::StringAtom.new obj.to_s },
6
- NilClass => ->(obj, ns) { Nydp.NIL },
7
- FalseClass => ->(obj, ns) { Nydp.NIL },
8
- TrueClass => ->(obj, ns) { Nydp.T },
6
+ NilClass => ->(obj, ns) { Nydp::NIL },
7
+ FalseClass => ->(obj, ns) { Nydp::NIL },
8
+ TrueClass => ->(obj, ns) { Nydp::T },
9
9
  ::Date => ->(obj, ns) { Nydp::Date.new obj },
10
10
  }
11
11
 
@@ -38,7 +38,7 @@ module Nydp
38
38
  expr.is_a?(Nydp::Pair)
39
39
  end
40
40
 
41
- def cons a, b=Nydp.NIL
41
+ def cons a, b=Nydp::NIL
42
42
  Nydp::Pair.new a, b
43
43
  end
44
44
 
@@ -52,7 +52,7 @@ module Nydp
52
52
 
53
53
  def literal? expr
54
54
  case expr
55
- when String, Float, Integer, Fixnum, Nydp.NIL, Nydp::Symbol, Nydp::StringAtom, Nydp::Truth, Nydp::Nil
55
+ when String, Float, Integer, Fixnum, Nydp::Symbol, Nydp::StringAtom, Nydp::Truth, Nydp::Nil
56
56
  true
57
57
  else
58
58
  false
@@ -52,7 +52,7 @@ module Nydp
52
52
  if pair? names
53
53
  context.set names.car, values.car
54
54
  setup_context context, names.cdr, values.cdr
55
- elsif Nydp.NIL.isnt? names
55
+ elsif Nydp::NIL.isnt? names
56
56
  context.set names, values
57
57
  end
58
58
  end
@@ -72,7 +72,7 @@ module Nydp
72
72
  instructions << Nydp::Compiler.compile(body_forms.car, bindings)
73
73
 
74
74
  rest = body_forms.cdr
75
- if Nydp.NIL.is? rest
75
+ if Nydp::NIL.is? rest
76
76
  return Pair.from_list(instructions)
77
77
  else
78
78
  instructions << PopArg
@@ -84,7 +84,7 @@ module Nydp
84
84
  if pair? arg_list
85
85
  index_parameters arg_list.car, hsh
86
86
  index_parameters arg_list.cdr, hsh
87
- elsif Nydp.NIL.isnt? arg_list
87
+ elsif Nydp::NIL.isnt? arg_list
88
88
  hsh[arg_list] = hsh.size
89
89
  end
90
90
  end
@@ -20,7 +20,7 @@ class Nydp::LexicalContext
20
20
  end
21
21
 
22
22
  def at_index index
23
- values[index] || Nydp.NIL
23
+ values[index] || Nydp::NIL
24
24
  end
25
25
 
26
26
  def set name, value
@@ -31,7 +31,7 @@ class Nydp::LexicalContext
31
31
  def set_args_1 names, arg
32
32
  if pair? names
33
33
  set names.car, arg
34
- elsif Nydp.NIL.isnt? names
34
+ elsif Nydp::NIL.isnt? names
35
35
  set names, cons(arg)
36
36
  end
37
37
  end
@@ -40,7 +40,7 @@ class Nydp::LexicalContext
40
40
  if pair? names
41
41
  set names.car, arg_0
42
42
  set_args_1 names.cdr, arg_1
43
- elsif Nydp.NIL.isnt? names
43
+ elsif Nydp::NIL.isnt? names
44
44
  set names, cons(arg_0, cons(arg_1))
45
45
  end
46
46
  end
@@ -49,7 +49,7 @@ class Nydp::LexicalContext
49
49
  if pair? names
50
50
  set names.car, arg_0
51
51
  set_args_2 names.cdr, arg_1, arg_2
52
- elsif Nydp.NIL.isnt? names
52
+ elsif Nydp::NIL.isnt? names
53
53
  set names, cons(arg_0, cons(arg_1, cons(arg_2)))
54
54
  end
55
55
  end
@@ -167,7 +167,7 @@ module Nydp::LexicalContextBuilder
167
167
  _set_args lc, @param_names, args
168
168
  end
169
169
  def _set_args lc, names, args
170
- unless Nydp.NIL.is? names
170
+ unless Nydp::NIL.is? names
171
171
  lc.set names.car, args.car
172
172
  _set_args lc, names.cdr, args.cdr
173
173
  end
@@ -185,7 +185,7 @@ module Nydp::LexicalContextBuilder
185
185
  if pair? names
186
186
  lc.set names.car, args.car
187
187
  _set_args lc, names.cdr, args.cdr
188
- elsif Nydp.NIL.isnt? names
188
+ elsif Nydp::NIL.isnt? names
189
189
  lc.set names, args
190
190
  end
191
191
  end
data/lib/nydp/pair.rb CHANGED
@@ -27,7 +27,7 @@ class Nydp::Pair
27
27
  end
28
28
  end
29
29
 
30
- def self.from_list list, last=Nydp.NIL, n=0
30
+ def self.from_list list, last=Nydp::NIL, n=0
31
31
  if n >= list.size
32
32
  last
33
33
  else
@@ -52,12 +52,12 @@ class Nydp::Pair
52
52
  end
53
53
 
54
54
  def proper?
55
- Nydp.NIL.is?(cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?)
55
+ Nydp::NIL.is?(cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?)
56
56
  end
57
57
 
58
58
  def each &block
59
59
  yield car
60
- cdr.each(&block) unless Nydp.NIL.is?(cdr)
60
+ cdr.each(&block) unless Nydp::NIL.is?(cdr)
61
61
  end
62
62
 
63
63
  def inspect
@@ -66,31 +66,31 @@ class Nydp::Pair
66
66
 
67
67
  def to_s
68
68
  if car.is_a?(Nydp::Symbol) && car.is?(:quote)
69
- if Nydp.NIL.is? cdr.cdr
69
+ if Nydp::NIL.is? cdr.cdr
70
70
  "'#{cdr.car.to_s}"
71
71
  else
72
72
  "'#{cdr.to_s}"
73
73
  end
74
74
  elsif car.is_a?(Nydp::Symbol) && car.is?(:"brace-list")
75
- if Nydp.NIL.is? cdr
75
+ if Nydp::NIL.is? cdr
76
76
  "{}"
77
77
  else
78
78
  "{ #{cdr.to_s_rest} }"
79
79
  end
80
80
  elsif car.is_a?(Nydp::Symbol) && car.is?(:quasiquote)
81
- if Nydp.NIL.is? cdr.cdr
81
+ if Nydp::NIL.is? cdr.cdr
82
82
  "`#{cdr.car.to_s}"
83
83
  else
84
84
  "`#{cdr.to_s}"
85
85
  end
86
86
  elsif car.is_a?(Nydp::Symbol) && car.is?(:unquote)
87
- if Nydp.NIL.is? cdr.cdr
87
+ if Nydp::NIL.is? cdr.cdr
88
88
  ",#{cdr.car.to_s}"
89
89
  else
90
90
  ",#{cdr.to_s}"
91
91
  end
92
92
  elsif car.is_a?(Nydp::Symbol) && car.is?(:"unquote-splicing")
93
- if Nydp.NIL.is? cdr.cdr
93
+ if Nydp::NIL.is? cdr.cdr
94
94
  ",@#{cdr.car.to_s}"
95
95
  else
96
96
  ",@#{cdr.to_s}"
@@ -103,7 +103,7 @@ class Nydp::Pair
103
103
  def to_s_rest
104
104
  cdr_s = if cdr.is_a?(self.class)
105
105
  cdr.to_s_rest
106
- elsif Nydp.NIL.is? cdr
106
+ elsif Nydp::NIL.is? cdr
107
107
  nil
108
108
  else
109
109
  ". #{cdr.to_s}"
@@ -115,7 +115,7 @@ class Nydp::Pair
115
115
  def inspect_rest
116
116
  cdr_s = if cdr.is_a?(self.class)
117
117
  cdr.inspect_rest
118
- elsif cdr == Nydp.NIL
118
+ elsif cdr == Nydp::NIL
119
119
  nil
120
120
  else
121
121
  ". #{cdr.inspect}"
@@ -125,7 +125,7 @@ class Nydp::Pair
125
125
  end
126
126
 
127
127
  def append thing
128
- if Nydp.NIL.is? self.cdr
128
+ if Nydp::NIL.is? self.cdr
129
129
  self.cdr = thing
130
130
  elsif pair? self.cdr
131
131
  self.cdr.append thing
data/lib/nydp/runner.rb CHANGED
@@ -48,7 +48,7 @@ module Nydp
48
48
 
49
49
  def compile_and_eval expr
50
50
  begin
51
- vm.thread Pair.new(Compiler.compile(expr, Nydp.NIL), Nydp.NIL)
51
+ vm.thread Pair.new(Compiler.compile(expr, Nydp::NIL), Nydp::NIL)
52
52
  rescue Exception => e
53
53
  new_msg = "failed to eval #{expr.inspect}\nerror was #{Nydp.indent_text e.message}"
54
54
  raise e.class, new_msg, e.backtrace
@@ -92,7 +92,7 @@ module Nydp
92
92
  end
93
93
 
94
94
  def run
95
- res = Nydp.NIL
95
+ res = Nydp::NIL
96
96
  while !@tokens.finished
97
97
  expr = @parser.expression(@tokens)
98
98
  unless expr.nil?
data/lib/nydp/symbol.rb CHANGED
@@ -21,8 +21,8 @@ class Nydp::Symbol
21
21
 
22
22
  def self.mk name, ns
23
23
  name = name.to_sym
24
- return Nydp.NIL if name == :nil
25
- return Nydp.T if name == :t
24
+ return Nydp::NIL if name == :nil
25
+ return Nydp::T if name == :t
26
26
  sym = ns[name]
27
27
  unless sym
28
28
  sym = new(name)
@@ -20,7 +20,7 @@ module Nydp
20
20
 
21
21
  def self.build name, bindings
22
22
  depth = 0
23
- while Nydp.NIL.isnt? bindings
23
+ while Nydp::NIL.isnt? bindings
24
24
  here = bindings.car
25
25
  if here.key? name
26
26
  binding_index = here[name]
data/lib/nydp/truth.rb CHANGED
@@ -1,6 +1,9 @@
1
+ require 'singleton'
2
+
1
3
  module Nydp
2
4
  class Truth
3
- def init_with *; Nydp.T ; end
5
+ include Singleton
6
+ def init_with *; Nydp::T ; end
4
7
  def to_s ; 't' ; end
5
8
  def inspect ; 't[nydp::Truth]' ; end
6
9
  def assign *_ ; self ; end
@@ -9,7 +12,8 @@ module Nydp
9
12
  end
10
13
 
11
14
  class Nil
12
- def init_with * ; Nydp.NIL ; end
15
+ include Singleton
16
+ def init_with * ; Nydp::NIL ; end
13
17
  def car ; self ; end
14
18
  def cdr ; self ; end
15
19
  def size ; 0 ; end
@@ -25,13 +29,6 @@ module Nydp
25
29
  def execute vm ; vm.push_arg self ; end
26
30
  end
27
31
 
28
- @@nil = Nil.new
29
- @@t = Truth.new
30
-
31
- class Nil
32
- def self.new ; raise "no" ; end
33
- end
34
-
35
- def self.NIL; @@nil; end
36
- def self.T; @@t; end
32
+ NIL = Nil.instance
33
+ T = Truth.instance
37
34
  end
data/lib/nydp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nydp
2
- VERSION = "0.1.15"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/nydp/vm.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  module Nydp
2
2
  class VM
3
3
  include Helper
4
- attr_accessor :instructions, :args, :contexts, :current_context, :locals, :unhandled_error
4
+ attr_accessor :instructions, :args, :contexts, :current_context, :locals, :unhandled_error, :ns
5
5
 
6
6
  module Finally ; end
7
7
  module HandleError ; end
8
8
 
9
- def initialize
9
+ def initialize ns
10
10
  @instructions = []
11
11
  @args = []
12
12
  @contexts = []
13
13
  @locals = Nydp::Hash.new
14
+ @ns = ns
14
15
  end
15
16
 
16
17
  def thread expr=nil
@@ -67,7 +68,7 @@ module Nydp
67
68
  def args! ; args.empty? ? (raise "illegal operation: no args") : args ; end
68
69
  def peek_arg ; args!.last ; end
69
70
 
70
- def pop_args count, tail=Nydp.NIL
71
+ def pop_args count, tail=Nydp::NIL
71
72
  case count
72
73
  when 0
73
74
  tail
data/spec/date_spec.rb CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe Nydp::Date do
4
4
 
5
- let(:ns) { { } }
6
- let(:vm) { Nydp::VM.new }
5
+ let(:ns) { { } }
6
+ let(:vm) { Nydp::VM.new(ns) }
7
7
 
8
8
  it "converts ruby Date to Nydp::Date" do
9
9
  rd = Date.parse "2015-06-08"
@@ -16,13 +16,21 @@ describe Nydp::Date do
16
16
  end
17
17
 
18
18
  it "creates a new date" do
19
- df = Nydp::Builtin::Date.new
20
- df.invoke vm, pair_list([2015, 11, 18])
19
+ df = Nydp::Builtin::Date.instance
20
+ df.invoke_4 vm, 2015, 11, 18
21
21
  nd = vm.args.pop
22
22
  expect(nd).to be_a Nydp::Date
23
23
  expect(nd.ruby_date).to eq Date.parse("2015-11-18")
24
24
  end
25
25
 
26
+ it "returns today" do
27
+ df = Nydp::Builtin::Date.instance
28
+ df.invoke_1 vm
29
+ nd = vm.args.pop
30
+ expect(nd).to be_a Nydp::Date
31
+ expect(nd.ruby_date).to eq Date.today
32
+ end
33
+
26
34
  it "returns date components" do
27
35
  rd = Date.parse "2015-06-08"
28
36
  nd = Nydp.r2n rd, ns
@@ -37,7 +45,7 @@ describe Nydp::Date do
37
45
  let(:d1) { Nydp.r2n (Date.today + 6), ns }
38
46
 
39
47
  it "works with builtin minus" do
40
- minus = Nydp::Builtin::Minus.new
48
+ minus = Nydp::Builtin::Minus.instance
41
49
 
42
50
  minus.invoke vm, pair_list([d1, d0])
43
51
  diff = vm.args.pop
@@ -48,58 +56,58 @@ describe Nydp::Date do
48
56
 
49
57
  describe "'>" do
50
58
  it "works with builtin greater-than when true" do
51
- f = Nydp::Builtin::GreaterThan.new
59
+ f = Nydp::Builtin::GreaterThan.instance
52
60
 
53
61
  f.invoke vm, pair_list([d1, d0])
54
62
 
55
- expect(vm.args.pop).to eq Nydp.T
63
+ expect(vm.args.pop).to eq Nydp::T
56
64
  end
57
65
 
58
66
  it "compares with nil" do
59
- f = Nydp::Builtin::GreaterThan.new
67
+ f = Nydp::Builtin::GreaterThan.instance
60
68
 
61
- f.invoke vm, pair_list([d1, Nydp.NIL])
69
+ f.invoke vm, pair_list([d1, Nydp::NIL])
62
70
 
63
- expect(vm.args.pop).to eq Nydp.NIL
71
+ expect(vm.args.pop).to eq Nydp::NIL
64
72
  end
65
73
 
66
74
  it "works with builtin greater-than when false" do
67
- f = Nydp::Builtin::GreaterThan.new
75
+ f = Nydp::Builtin::GreaterThan.instance
68
76
 
69
77
  f.invoke vm, pair_list([d0, d1])
70
78
 
71
- expect(vm.args.pop).to eq Nydp.NIL
79
+ expect(vm.args.pop).to eq Nydp::NIL
72
80
  end
73
81
  end
74
82
 
75
83
  describe "'<" do
76
84
  it "works with builtin less-than when true" do
77
- f = Nydp::Builtin::LessThan.new
85
+ f = Nydp::Builtin::LessThan.instance
78
86
 
79
87
  f.invoke vm, pair_list([d0, d1])
80
88
 
81
- expect(vm.args.pop).to eq Nydp.T
89
+ expect(vm.args.pop).to eq Nydp::T
82
90
  end
83
91
 
84
92
  it "works with builtin less-than when false" do
85
- f = Nydp::Builtin::LessThan.new
93
+ f = Nydp::Builtin::LessThan.instance
86
94
 
87
95
  f.invoke vm, pair_list([d1, d0])
88
96
 
89
- expect(vm.args.pop).to eq Nydp.NIL
97
+ expect(vm.args.pop).to eq Nydp::NIL
90
98
  end
91
99
 
92
100
  it "compares with nil" do
93
- f = Nydp::Builtin::LessThan.new
101
+ f = Nydp::Builtin::LessThan.instance
94
102
 
95
- f.invoke vm, pair_list([d1, Nydp.NIL])
103
+ f.invoke vm, pair_list([d1, Nydp::NIL])
96
104
 
97
- expect(vm.args.pop).to eq Nydp.NIL
105
+ expect(vm.args.pop).to eq Nydp::NIL
98
106
  end
99
107
  end
100
108
 
101
109
  it "works with builtin plus" do
102
- plus = Nydp::Builtin::Plus.new
110
+ plus = Nydp::Builtin::Plus.instance
103
111
 
104
112
  plus.invoke vm, pair_list([d0, 5])
105
113
  sum = vm.args.pop
data/spec/error_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Nydp::VM do
4
4
  let(:ns) { { } }
5
- let(:vm) { Nydp::VM.new }
5
+ let(:vm) { Nydp::VM.new(ns) }
6
6
 
7
7
  def run txt
8
8
  Nydp.setup ns