nydp 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lisp/core-012-utils.nydp +19 -5
  3. data/lib/lisp/core-015-documentation.nydp +6 -3
  4. data/lib/lisp/core-017-builtin-dox.nydp +35 -30
  5. data/lib/lisp/core-030-syntax.nydp +24 -23
  6. data/lib/lisp/core-035-flow-control.nydp +3 -3
  7. data/lib/lisp/core-037-list-utils.nydp +24 -14
  8. data/lib/lisp/core-040-utils.nydp +22 -14
  9. data/lib/lisp/core-041-string-utils.nydp +12 -8
  10. data/lib/lisp/core-043-list-utils.nydp +3 -9
  11. data/lib/lisp/core-045-dox-utils.nydp +5 -0
  12. data/lib/lisp/core-100-utils.nydp +27 -12
  13. data/lib/lisp/core-110-hash-utils.nydp +7 -0
  14. data/lib/lisp/core-120-settings.nydp +35 -0
  15. data/lib/lisp/{core-060-benchmarking.nydp → core-900-benchmarking.nydp} +47 -17
  16. data/lib/lisp/tests/cdr-set-examples.nydp +6 -0
  17. data/lib/lisp/tests/date-examples.nydp +2 -0
  18. data/lib/lisp/tests/foundation-test.nydp +12 -0
  19. data/lib/lisp/tests/hash-examples.nydp +1 -1
  20. data/lib/lisp/tests/set-intersection-examples.nydp +16 -0
  21. data/lib/lisp/tests/set-union-examples.nydp +8 -0
  22. data/lib/lisp/tests/settings-examples.nydp +24 -0
  23. data/lib/lisp/tests/sort-examples.nydp +8 -0
  24. data/lib/lisp/tests/string-tests.nydp +9 -0
  25. data/lib/lisp/tests/zap-examples.nydp +12 -0
  26. data/lib/nydp.rb +3 -4
  27. data/lib/nydp/builtin/cdr_set.rb +1 -6
  28. data/lib/nydp/builtin/handle_error.rb +1 -1
  29. data/lib/nydp/builtin/hash.rb +3 -44
  30. data/lib/nydp/core_ext.rb +41 -0
  31. data/lib/nydp/date.rb +18 -20
  32. data/lib/nydp/hash.rb +5 -6
  33. data/lib/nydp/helper.rb +9 -25
  34. data/lib/nydp/string_atom.rb +14 -19
  35. data/lib/nydp/symbol.rb +40 -27
  36. data/lib/nydp/truth.rb +9 -2
  37. data/lib/nydp/version.rb +1 -1
  38. data/lib/nydp/vm.rb +0 -2
  39. data/spec/date_spec.rb +22 -22
  40. data/spec/hash_non_hash_behaviour_spec.rb +2 -2
  41. data/spec/spec_helper.rb +4 -1
  42. data/spec/symbol_spec.rb +31 -0
  43. data/spec/time_spec.rb +1 -1
  44. metadata +9 -3
@@ -1,10 +1,9 @@
1
1
  class Nydp::Hash < ::Hash
2
2
  include Nydp::Helper
3
3
 
4
- def nydp_type ; :hash ; end
5
- def to_ruby
6
- h = Hash.new
7
- self.each { |k,v| h[n2r k] = n2r v }
8
- h
9
- end
4
+ def nydp_type ; :hash ; end
5
+ def to_ruby ; each_with_object({}) {|(k,v),h| h[n2r k] = n2r v} ; end
6
+ def _nydp_get a ; self[a] ; end
7
+ def _nydp_set a, v ; self[a] = v ; end
8
+ def _nydp_keys ; keys ; end
10
9
  end
@@ -1,34 +1,18 @@
1
1
  module Nydp
2
- R2NHELPERS = {
3
- ::Symbol => ->(obj, ns) { Nydp::Symbol.mk(obj, ns) },
4
- Array => ->(obj, ns) { Nydp::Pair.from_list obj.map { |o| Nydp.r2n o, ns } },
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 },
9
- ::Date => ->(obj, ns) { Nydp::Date.new obj },
10
- }
11
-
12
- def self.n2r nydp
13
- nydp.respond_to?(:to_ruby) ? nydp.to_ruby : nydp
2
+ module AutoWrap
3
+ def _nydp_wrapper ; self ; end
4
+ def to_ruby ; self ; end
14
5
  end
15
6
 
16
- def self.r2n ruby_obj, ns
17
- return ruby_obj._nydp_wrapper(ns) if ruby_obj.respond_to? :_nydp_wrapper
18
-
19
- rklass = ruby_obj.class
20
- R2NHELPERS.each do |hklass, proc|
21
- if rklass <= hklass
22
- return proc.call ruby_obj, ns
23
- end
24
- end
25
-
26
- ruby_obj
7
+ module Converter
8
+ def n2r o ; o.respond_to?(:to_ruby) ? o.to_ruby : o ; end
9
+ def r2n o, ns=nil ; o._nydp_wrapper ; end
27
10
  end
28
11
 
12
+ extend Converter
13
+
29
14
  module Helper
30
- def n2r obj ; Nydp.n2r obj ; end
31
- def r2n obj, ns ; Nydp.r2n obj, ns ; end
15
+ include Converter
32
16
 
33
17
  def sig klass
34
18
  case klass
@@ -5,28 +5,23 @@ module Nydp
5
5
  @string, @token = string, token
6
6
  end
7
7
 
8
- def nydp_type ; :string ; end
9
- def to_s ; string ; end
10
- def to_ruby ; string ; end
11
- def to_sym ; string.to_sym ; end
12
- def eql? other ; self == other ; end
13
- def inspect ; string.inspect ; end
14
- def hash ; string.hash ; end
15
- def length ; string.length ; end
16
- def > other ; self.string > other.string ; end
17
- def < other ; self.string < other.string ; end
18
- def * other ; StringAtom.new(string * other) ; end
19
-
20
- def <=> other
21
- self < other ? -1 : (self == other ? 0 : 1)
22
- end
8
+ def nydp_type ; :string ; end
9
+ def to_s ; string ; end
10
+ def to_ruby ; string ; end
11
+ def to_sym ; string.to_sym ; end
12
+ def to_date ; ::Date.parse(@string) ; end
13
+ def eql? other ; self == other ; end
14
+ def inspect ; string.inspect ; end
15
+ def hash ; string.hash ; end
16
+ def length ; string.length ; end
17
+ def > other ; self.string > other.string ; end
18
+ def < other ; self.string < other.string ; end
19
+ def * other ; StringAtom.new(string * other) ; end
20
+ def <=> other ; self < other ? -1 : (self == other ? 0 : 1) ; end
21
+ def + other ; StringAtom.new "#{@string}#{other}" ; end
23
22
 
24
23
  def == other
25
24
  other.is_a?(Nydp::StringAtom) && (other.to_s == self.to_s)
26
25
  end
27
-
28
- def + other
29
- StringAtom.new "#{@string}#{other}"
30
- end
31
26
  end
32
27
  end
@@ -1,19 +1,22 @@
1
1
  class Nydp::Symbol
2
2
  class Unbound < StandardError ; end
3
-
4
3
  EMPTY = :""
5
4
  attr_accessor :name
6
- attr_reader :hash
5
+
6
+ def self.new name
7
+ special(name.to_s.to_sym) || super
8
+ end
7
9
 
8
10
  def initialize name
9
11
  name = name.to_s
10
12
  @name = name.to_sym
11
- @inspection = "|#{name}|" if untidy(name)
12
- @hash = name.hash
13
+ @inspection = "|#{name.gsub(/\|/, '\|')}|" if untidy(name)
13
14
  end
14
15
 
16
+ def hash ; name.hash ; end # can't cache this, it seems to break when unmarshalling
17
+
15
18
  def untidy str
16
- (str == "") || (str == nil) || (str =~ /\s/)
19
+ (str == "") || (str == nil) || (str =~ /[\s\|,\(\)"]/)
17
20
  end
18
21
 
19
22
  def value context=nil
@@ -21,41 +24,51 @@ class Nydp::Symbol
21
24
  @value
22
25
  end
23
26
 
24
- def self.mk name, ns
25
- name = name.to_sym
27
+ def self.special name
26
28
  return Nydp::NIL if name == :nil
27
29
  return Nydp::T if name == :t
28
- sym = ns[name]
29
- unless sym
30
- sym = new(name)
31
- ns[name] = sym
32
- end
33
- sym
30
+ nil
34
31
  end
35
32
 
33
+ def self.mk name, ns
34
+ name = name.to_s.to_sym
35
+ ns[name] ||= new(name)
36
+ end
36
37
 
37
38
  def self.find name, ns ; ns[name.to_sym] ; end
38
39
 
39
- def nydp_type ; :symbol ; end
40
- def inspect ; @inspection || name.to_s ; end
41
- def to_s ; name.to_s ; end
42
- def to_sym ; name ; end
43
- def to_ruby ; to_sym ; end
44
- def eql? other ; self == other ; end
45
- def is? nm ; self.name == nm.to_sym ; end
46
- def > other ; self.name > other.name ; end
47
- def < other ; self.name < other.name ; end
48
- def <=> other ; self.name <=> other.name ; end
40
+ def nydp_type ; :symbol ; end
41
+ def inspect ; @inspection || name.to_s ; end
42
+ def to_s ; name.to_s ; end
43
+ def to_sym ; name ; end
44
+ def to_ruby ; to_sym ; end
45
+ def is? nm ; self.name == nm.to_sym ; end
46
+ def > other ; self.name > other.name ; end
47
+ def < other ; self.name < other.name ; end
48
+ def <=> other ; self.name <=> other.name ; end
49
+ def assign value, _=nil ; @value = value ; end
50
+ def execute vm ; vm.push_arg self.value ; end
49
51
 
50
52
  def == other
51
53
  other.is_a?(Nydp::Symbol) && (self.name == other.name)
52
54
  end
53
55
 
54
- def execute vm
55
- vm.push_arg self.value
56
+ alias eql? ==
57
+ end
58
+
59
+ class Nydp::FrozenSymbol < Nydp::Symbol
60
+ @@frozen = { }
61
+
62
+ def self.mk name
63
+ name = name.to_s.to_sym
64
+ @@frozen[name] ||= new(name)
65
+ end
66
+
67
+ def value _=nil
68
+ raise Unbound.new("frozen symbol: #{self.inspect}")
56
69
  end
57
70
 
58
- def assign value, _=nil
59
- @value = value
71
+ def assign v, _=nil
72
+ raise "can't assign to frozen: #{self.inspect}"
60
73
  end
61
74
  end
@@ -9,10 +9,12 @@ module Nydp
9
9
  def assign *_ ; self ; end
10
10
  def nydp_type ; :truth ; end
11
11
  def to_ruby ; true ; end
12
+ def _nydp_get a ; Nydp::T ; end
13
+ def _nydp_set a, v ; Nydp::T ; end
12
14
  end
13
15
 
14
16
  class Nil
15
- include Singleton
17
+ include Singleton, Enumerable
16
18
  def init_with * ; Nydp::NIL ; end
17
19
  def car ; self ; end
18
20
  def cdr ; self ; end
@@ -26,7 +28,12 @@ module Nydp
26
28
  def inspect ; "nil" ; end
27
29
  def nydp_type ; :nil ; end
28
30
  def to_ruby ; nil ; end
29
- def execute vm ; vm.push_arg self ; end
31
+ def execute vm ; vm.push_arg self ; end
32
+ def _nydp_get a ; Nydp::NIL ; end
33
+ def _nydp_set a, v ; Nydp::NIL ; end
34
+ def each ; ; end # nil behaves like an empty list
35
+ def & other ; self ; end
36
+ def | other ; other ; end
30
37
  end
31
38
 
32
39
  NIL = Nil.instance
@@ -1,3 +1,3 @@
1
1
  module Nydp
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -15,8 +15,6 @@ module Nydp
15
15
  @ns = ns
16
16
  end
17
17
 
18
- def r2n obj ; super obj, @ns ; end
19
-
20
18
  def push_instructions ii, ctx
21
19
  if @current_instructions && NIL != @current_instructions
22
20
  @instructions.push @current_instructions
@@ -7,7 +7,7 @@ describe Nydp::Date do
7
7
 
8
8
  it "converts ruby Date to Nydp::Date" do
9
9
  rd = Date.parse "2015-06-08"
10
- nd = Nydp.r2n rd, ns
10
+ nd = Nydp.r2n rd
11
11
 
12
12
  expect(nd). to be_a Nydp::Date
13
13
  expect(nd.to_s). to eq "2015-06-08"
@@ -33,16 +33,16 @@ describe Nydp::Date do
33
33
 
34
34
  it "returns date components" do
35
35
  rd = Date.parse "2015-06-08"
36
- nd = Nydp.r2n rd, ns
36
+ nd = Nydp.r2n rd
37
37
 
38
- expect(nd[:year]). to eq 2015
39
- expect(nd[:month]).to eq 6
40
- expect(nd[:day]). to eq 8
38
+ expect(nd._nydp_get(:year) ).to eq 2015
39
+ expect(nd._nydp_get(:month)).to eq 6
40
+ expect(nd._nydp_get(:day) ).to eq 8
41
41
  end
42
42
 
43
43
  describe "date maths" do
44
- let(:d0) { Nydp.r2n Date.today, ns }
45
- let(:d1) { Nydp.r2n (Date.today + 6), ns }
44
+ let(:d0) { Nydp.r2n Date.today }
45
+ let(:d1) { Nydp.r2n (Date.today + 6) }
46
46
 
47
47
  it "works with builtin minus" do
48
48
  minus = Nydp::Builtin::Minus.instance
@@ -120,31 +120,31 @@ describe Nydp::Date do
120
120
 
121
121
  it "returns relative dates by year" do
122
122
  rd = Date.parse "2015-06-08"
123
- nd = Nydp.r2n rd, ns
123
+ nd = Nydp.r2n rd
124
124
 
125
- expect(nd[:"last-year"].to_s). to eq "2014-06-08"
126
- expect(nd[:"next-year"].to_s). to eq "2016-06-08"
127
- expect(nd[:"beginning-of-year"].to_s). to eq "2015-01-01"
128
- expect(nd[:"end-of-year"].to_s). to eq "2015-12-31"
125
+ expect(nd._nydp_get(:"last-year").to_s). to eq "2014-06-08"
126
+ expect(nd._nydp_get(:"next-year").to_s). to eq "2016-06-08"
127
+ expect(nd._nydp_get(:"beginning-of-year").to_s). to eq "2015-01-01"
128
+ expect(nd._nydp_get(:"end-of-year").to_s). to eq "2015-12-31"
129
129
  end
130
130
 
131
131
  it "returns relative dates by month" do
132
132
  rd = Date.parse "2015-06-08"
133
- nd = Nydp.r2n rd, ns
133
+ nd = Nydp.r2n rd
134
134
 
135
- expect(nd[:"last-month"].to_s). to eq "2015-05-08"
136
- expect(nd[:"next-month"].to_s). to eq "2015-07-08"
137
- expect(nd[:"beginning-of-month"].to_s). to eq "2015-06-01"
138
- expect(nd[:"end-of-month"].to_s). to eq "2015-06-30"
135
+ expect(nd._nydp_get(:"last-month").to_s). to eq "2015-05-08"
136
+ expect(nd._nydp_get(:"next-month").to_s). to eq "2015-07-08"
137
+ expect(nd._nydp_get(:"beginning-of-month").to_s). to eq "2015-06-01"
138
+ expect(nd._nydp_get(:"end-of-month").to_s). to eq "2015-06-30"
139
139
  end
140
140
 
141
141
  it "returns relative dates by week" do
142
142
  rd = Date.parse "2015-03-12"
143
- nd = Nydp.r2n rd, ns
143
+ nd = Nydp.r2n rd
144
144
 
145
- expect(nd[:"last-week"].to_s). to eq "2015-03-05"
146
- expect(nd[:"next-week"].to_s). to eq "2015-03-19"
147
- expect(nd[:"beginning-of-week"].to_s). to eq "2015-03-09"
148
- expect(nd[:"end-of-week"].to_s). to eq "2015-03-15"
145
+ expect(nd._nydp_get(:"last-week").to_s). to eq "2015-03-05"
146
+ expect(nd._nydp_get(:"next-week").to_s). to eq "2015-03-19"
147
+ expect(nd._nydp_get(:"beginning-of-week").to_s). to eq "2015-03-09"
148
+ expect(nd._nydp_get(:"end-of-week").to_s). to eq "2015-03-15"
149
149
  end
150
150
  end
@@ -66,7 +66,7 @@ with args
66
66
  keysym
67
67
  \"foobar\""
68
68
 
69
- expect(cleanup_err_msg error.cause.message).to eq "hash-set: Not a hash: Nydp::StringAtom"
69
+ expect(cleanup_err_msg error.cause.message).to eq "_nydp_get : not settable: keysym on Nydp::StringAtom"
70
70
  end
71
71
  end
72
72
 
@@ -86,7 +86,7 @@ with args
86
86
  \"this here ain't no hash, hombre\"
87
87
  keysym"
88
88
 
89
- expect(cleanup_err_msg error.cause.message).to eq "hash-get: Not a hash: Nydp::StringAtom"
89
+ expect(cleanup_err_msg error.cause.message).to eq "_nydp_get : not gettable: keysym on Nydp::StringAtom"
90
90
  end
91
91
  end
92
92
  end
@@ -37,5 +37,8 @@ class TestThing
37
37
  "(TestThing #{a.inspect} #{b.inspect})"
38
38
  end
39
39
 
40
- def _nydp_safe_methods ; %i{ a b } ; end
40
+ def _nydp_get name
41
+ name = name.to_s.to_sym
42
+ send(name) if name == :a || name == :b
43
+ end
41
44
  end
@@ -5,6 +5,37 @@ describe Nydp::Symbol do
5
5
  let(:foo) { Nydp::Symbol.mk :FOO, ns }
6
6
  let(:vm) { Nydp::VM.new(ns) }
7
7
 
8
+ describe "#inspect" do
9
+ it "wraps itself in pipe chars if empty" do
10
+ sym = Nydp::Symbol.mk "", ns
11
+ expect(sym.inspect).to eq "||"
12
+ end
13
+ it "wraps itself in pipe chars if nil" do
14
+ sym = Nydp::Symbol.mk nil, ns
15
+ expect(sym.inspect).to eq "||"
16
+ end
17
+ it "wraps itself in pipe chars if it has spaces" do
18
+ sym = Nydp::Symbol.mk "hello world", ns
19
+ expect(sym.inspect).to eq "|hello world|"
20
+ end
21
+ it "wraps itself in pipe chars if it has pipe chars" do
22
+ sym = Nydp::Symbol.mk "hello|world", ns
23
+ expect(sym.inspect).to eq '|hello\|world|'
24
+ end
25
+ it "wraps itself in pipe chars if it contains quote chars" do
26
+ sym = Nydp::Symbol.mk "hello 'world'", ns
27
+ expect(sym.inspect).to eq "|hello 'world'|"
28
+ end
29
+ it "wraps itself in pipe chars if it contains doublequote chars" do
30
+ sym = Nydp::Symbol.mk 'hello "world"', ns
31
+ expect(sym.inspect).to eq '|hello "world"|'
32
+ end
33
+ it "wraps itself in pipe chars if it has other punctuation" do
34
+ sym = Nydp::Symbol.mk 'hello,(world)', ns
35
+ expect(sym.inspect).to eq '|hello,(world)|'
36
+ end
37
+ end
38
+
8
39
  it "returns a ruby symbol in #to_ruby" do
9
40
  sym = Nydp::Symbol.mk :foo, ns
10
41
  expect(sym.to_ruby).to eq :foo
@@ -8,7 +8,7 @@ describe Nydp::Date do
8
8
 
9
9
  it "r2n does not convert ruby Time" do
10
10
  rd = Time.now
11
- nd = Nydp.r2n rd, ns
11
+ nd = Nydp.r2n rd
12
12
 
13
13
  expect(nd).to be_a Time
14
14
  expect(nd).to eq rd
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nydp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-14 00:00:00.000000000 Z
11
+ date: 2018-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,11 +101,13 @@ files:
101
101
  - lib/lisp/core-043-list-utils.nydp
102
102
  - lib/lisp/core-045-dox-utils.nydp
103
103
  - lib/lisp/core-050-test-runner.nydp
104
- - lib/lisp/core-060-benchmarking.nydp
105
104
  - lib/lisp/core-070-prefix-list.nydp
106
105
  - lib/lisp/core-080-pretty-print.nydp
107
106
  - lib/lisp/core-090-hook.nydp
108
107
  - lib/lisp/core-100-utils.nydp
108
+ - lib/lisp/core-110-hash-utils.nydp
109
+ - lib/lisp/core-120-settings.nydp
110
+ - lib/lisp/core-900-benchmarking.nydp
109
111
  - lib/lisp/tests/accum-examples.nydp
110
112
  - lib/lisp/tests/add-hook-examples.nydp
111
113
  - lib/lisp/tests/aif-examples.nydp
@@ -117,6 +119,7 @@ files:
117
119
  - lib/lisp/tests/boot-tests.nydp
118
120
  - lib/lisp/tests/builtin-tests.nydp
119
121
  - lib/lisp/tests/car-examples.nydp
122
+ - lib/lisp/tests/cdr-set-examples.nydp
120
123
  - lib/lisp/tests/collect-tests.nydp
121
124
  - lib/lisp/tests/cons-examples.nydp
122
125
  - lib/lisp/tests/curry-tests.nydp
@@ -165,6 +168,7 @@ files:
165
168
  - lib/lisp/tests/seqf-examples.nydp
166
169
  - lib/lisp/tests/set-intersection-examples.nydp
167
170
  - lib/lisp/tests/set-union-examples.nydp
171
+ - lib/lisp/tests/settings-examples.nydp
168
172
  - lib/lisp/tests/sort-examples.nydp
169
173
  - lib/lisp/tests/string-tests.nydp
170
174
  - lib/lisp/tests/syntax-tests.nydp
@@ -172,6 +176,7 @@ files:
172
176
  - lib/lisp/tests/tuples-examples.nydp
173
177
  - lib/lisp/tests/type-of-examples.nydp
174
178
  - lib/lisp/tests/unparse-tests.nydp
179
+ - lib/lisp/tests/zap-examples.nydp
175
180
  - lib/lisp/tests/zip-examples.nydp
176
181
  - lib/nydp.rb
177
182
  - lib/nydp/assignment.rb
@@ -227,6 +232,7 @@ files:
227
232
  - lib/nydp/cond.rb
228
233
  - lib/nydp/context_symbol.rb
229
234
  - lib/nydp/core.rb
235
+ - lib/nydp/core_ext.rb
230
236
  - lib/nydp/date.rb
231
237
  - lib/nydp/error.rb
232
238
  - lib/nydp/function_invocation.rb