nydp 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lisp/core-015-documentation.nydp +8 -2
- data/lib/lisp/core-030-syntax.nydp +5 -2
- data/lib/lisp/core-040-utils.nydp +19 -6
- data/lib/lisp/core-100-utils.nydp +21 -0
- data/lib/lisp/tests/auto-hash-examples.nydp +5 -0
- data/lib/lisp/tests/boot-tests.nydp +5 -1
- data/lib/lisp/tests/builtin-tests.nydp +3 -0
- data/lib/lisp/tests/date-examples.nydp +5 -0
- data/lib/lisp/tests/group-by-examples.nydp +8 -0
- data/lib/lisp/tests/relative-months-examples.nydp +4 -0
- data/lib/lisp/tests/returning-examples.nydp +6 -0
- data/lib/lisp/tests/string-tests.nydp +8 -0
- data/lib/nydp/builtin/modulo.rb +11 -0
- data/lib/nydp/core.rb +1 -0
- data/lib/nydp/date.rb +6 -12
- data/lib/nydp/parser.rb +2 -2
- data/lib/nydp/truth.rb +7 -5
- data/lib/nydp/version.rb +1 -1
- data/spec/date_spec.rb +38 -18
- data/spec/foreign_hash_spec.rb +88 -0
- data/spec/hash_non_hash_behaviour_spec.rb +86 -0
- data/spec/hash_spec.rb +0 -164
- data/spec/parser_spec.rb +8 -10
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39e2357b8393c54fc0eea68f0edd2a8d6728ad3a
|
4
|
+
data.tar.gz: 27f90d034ee3a3fa44fe7c24e3a08638ac5b6781
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18a8c415c0a4c6418a17122fd619cc840120e9a5d3fc0444185399e22d52fe4852c966f60cc67c304e7c2b38e7b449459d5cbac498caa67fe405de8eb56809a1
|
7
|
+
data.tar.gz: b959fde0c9b6e51957687eccdd7252cacea90e51a82d8d166242aaaa41835035ecec79987a3cbe4ee741b2097f131401f094739e5f1b4cb5e26789992cb31c95
|
@@ -1,10 +1,16 @@
|
|
1
|
-
((fn (dox examples)
|
1
|
+
((fn (dox examples chapters)
|
2
2
|
(def dox-add-doc (name what texts args src)
|
3
3
|
(hash-set dox
|
4
4
|
name
|
5
5
|
(cons (list name what texts args src)
|
6
6
|
(hash-get dox sym))))
|
7
7
|
|
8
|
+
(def dox-add-to-chapter (chapter item)
|
9
|
+
(hash-set chapters
|
10
|
+
item
|
11
|
+
(cons item
|
12
|
+
(hash-get chapters item))))
|
13
|
+
|
8
14
|
(def dox-add-examples (name example-exprs)
|
9
15
|
(hash-set examples
|
10
16
|
name
|
@@ -33,7 +39,7 @@
|
|
33
39
|
(def dox-arg-names (name)
|
34
40
|
(cond (dox? name)
|
35
41
|
(cadddr (car (dox-lookup name))))))
|
36
|
-
(hash) (hash))
|
42
|
+
(hash) (hash) (hash))
|
37
43
|
|
38
44
|
(def isa-comment? (thing)
|
39
45
|
(cond (pair? thing)
|
@@ -218,7 +218,10 @@
|
|
218
218
|
`(brace-list-mono ,(car args))
|
219
219
|
(brace-list-build-hash args)))
|
220
220
|
|
221
|
+
(mac returnlet (var val . body)
|
222
|
+
; stores ,val in ,var, executes ,@body, returns ,var
|
223
|
+
`(let ,var ,val ,@body ,var))
|
224
|
+
|
221
225
|
(mac returning (val . body)
|
222
226
|
; stores ,val, executes ,@body, and returns ,val.
|
223
|
-
(w/uniq retval
|
224
|
-
`(let ,retval ,val ,@body ,retval)))
|
227
|
+
(w/uniq retval `(returnlet ,retval ,val ,@body)))
|
@@ -29,14 +29,27 @@
|
|
29
29
|
txt)))
|
30
30
|
|
31
31
|
(def joinstr (txt . things)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
; flatten 'things into a single list (ie unnest lists)
|
33
|
+
; convert each item to a string
|
34
|
+
; return a single string which is the concatenation of each
|
35
|
+
; stringified item, with given 'txt inserted in between
|
36
|
+
; each item
|
37
|
+
(let joinables (flatten things)
|
38
|
+
(apply +
|
39
|
+
(to-string (car joinables))
|
40
|
+
(flatten (zip (map (fn (_) txt) (cdr joinables))
|
41
|
+
(map to-string (cdr joinables)))))))
|
42
|
+
|
43
|
+
(def j items
|
44
|
+
; delegate to 'joinstr with an empty join string
|
45
|
+
(joinstr "" items))
|
37
46
|
|
38
47
|
(def string-pieces pieces
|
39
|
-
|
48
|
+
; string-interpolation syntax emits this form. Default implementation
|
49
|
+
; is to delegate to 'j , but containing forms may use macros that
|
50
|
+
; override this in order to provide specific interpolation behaviour
|
51
|
+
; (for example, formatting numbers or stripping HTML tags)
|
52
|
+
(j pieces))
|
40
53
|
|
41
54
|
(def collect (f things)
|
42
55
|
; if 'things is a list, return all the items in the list for which 'f returns non-nil
|
@@ -36,3 +36,24 @@
|
|
36
36
|
(def uniqify (things)
|
37
37
|
; return a list containing all the elements of 'things, but with no duplicates
|
38
38
|
(reject (seen?) things))
|
39
|
+
|
40
|
+
(def group-by (f things)
|
41
|
+
; return a hash of 'things keyed by (f thing) for
|
42
|
+
; each thing in 'things
|
43
|
+
(returnlet hsh {}
|
44
|
+
(each thing things
|
45
|
+
(hash-cons hsh (f thing) thing))))
|
46
|
+
|
47
|
+
(with (m2i λd(+ (* 12 d.year) (- d.month 1))
|
48
|
+
i2m λi(date (/ i 12) (+ 1 (mod i 12)) 1))
|
49
|
+
(def relative-months (anchor . mm)
|
50
|
+
; 'anchor is a date
|
51
|
+
; 'mm is a list of integers
|
52
|
+
; for each m in 'mm, return the date at the beginning of
|
53
|
+
; the month given by adding m months to 'anchor
|
54
|
+
(let mi (m2i anchor)
|
55
|
+
(map λm(i2m (+ mi m)) mm))))
|
56
|
+
|
57
|
+
(mac auto-hash names
|
58
|
+
; (auto-hash a b c) same as { a a b b c c }
|
59
|
+
`(brace-list ,@(flatten:map λn(list n n) names)))
|
@@ -85,7 +85,11 @@
|
|
85
85
|
|
86
86
|
("special combination with bang"
|
87
87
|
(pre-compile '(!x:y a b))
|
88
|
-
((fn args (no (x (apply y args)))) a b))
|
88
|
+
((fn args (no (x (apply y args)))) a b))
|
89
|
+
|
90
|
+
("precedence over ampersand-syntax"
|
91
|
+
(pre-compile '(&attr:func a b))
|
92
|
+
((fn args ((fn (obj) (hash-get obj (quote attr))) (apply func args))) a b)))
|
89
93
|
|
90
94
|
(examples-for let
|
91
95
|
("expands 'let"
|
@@ -24,6 +24,11 @@
|
|
24
24
|
(to-string (hash-get h (date 2015 11 8))))
|
25
25
|
"on this day")
|
26
26
|
|
27
|
+
("equals itself" (eq? (date 2004 3 12) (date 2004 3 12)) t )
|
28
|
+
("does not equal another" (eq? (date 2004 3 12) (date 2006 6 21)) nil)
|
29
|
+
("does not equal nil" (eq? (date 2004 3 12) nil ) nil)
|
30
|
+
("nil does not equal a date" (eq? nil (date 2004 3 12) ) nil)
|
31
|
+
|
27
32
|
("returns its year" (let d (date 1999 12 31) d.year) 1999)
|
28
33
|
("returns its month" (let d (date 1999 12 31) d.month) 12 )
|
29
34
|
("returns its day" (let d (date 1999 12 31) d.day) 31 )
|
@@ -0,0 +1,8 @@
|
|
1
|
+
(examples-for group-by
|
2
|
+
("create a hash keyed by value of given function"
|
3
|
+
(let h (group-by car
|
4
|
+
(list '(a b c) '(a x y) '(b c d) '(z y x) '(z b a) '(1 2 3)))
|
5
|
+
(joinstr ", "
|
6
|
+
(map λk(j k " -> " (inspect:hash-get h k))
|
7
|
+
(hash-keys h))))
|
8
|
+
"a -> ((a x y) (a b c)), b -> ((b c d)), z -> ((z b a) (z y x)), 1 -> ((1 2 3))"))
|
@@ -4,3 +4,9 @@
|
|
4
4
|
(returning x
|
5
5
|
(= x 3)))
|
6
6
|
2))
|
7
|
+
|
8
|
+
(examples-for returnlet
|
9
|
+
("it stores the given value in the given variable, executes body, returns the value assigned to the variable"
|
10
|
+
(let result (returnlet hsh (hash) (= hsh.a 1) (= hsh.b 2))
|
11
|
+
(list (hash-keys result) (hash-values result)))
|
12
|
+
((a b) (1 2))))
|
@@ -62,6 +62,14 @@
|
|
62
62
|
(joinstr " - " '(1 2 3))
|
63
63
|
"1 - 2 - 3"))
|
64
64
|
|
65
|
+
(examples-for j
|
66
|
+
("joins elements with empty string"
|
67
|
+
(j 1 2 3)
|
68
|
+
"123")
|
69
|
+
("joins elements of a list with empty string"
|
70
|
+
(j '(1 2 3))
|
71
|
+
"123"))
|
72
|
+
|
65
73
|
(examples-for string-strip
|
66
74
|
("removes leading whitespace" (string-strip " hello!") "hello!" )
|
67
75
|
("removes trailing whitespace" (string-strip "(world) ") "(world)" )
|
data/lib/nydp/core.rb
CHANGED
@@ -30,6 +30,7 @@ module Nydp
|
|
30
30
|
Symbol.mk(:/, ns).assign(Nydp::Builtin::Divide.new)
|
31
31
|
Symbol.mk(:>, ns).assign(Nydp::Builtin::GreaterThan.new)
|
32
32
|
Symbol.mk(:<, ns).assign(Nydp::Builtin::LessThan.new)
|
33
|
+
Symbol.mk(:mod, ns).assign(Nydp::Builtin::Modulo.new)
|
33
34
|
Symbol.mk(:eval, ns).assign(Nydp::Builtin::Eval.new(ns))
|
34
35
|
Symbol.mk(:false, ns).assign(false)
|
35
36
|
Symbol.mk(:hash, ns).assign(Nydp::Builtin::Hash.new)
|
data/lib/nydp/date.rb
CHANGED
@@ -25,20 +25,14 @@ module Nydp
|
|
25
25
|
def inspect ; ruby_date.inspect ; end
|
26
26
|
def nydp_type ; :date ; end
|
27
27
|
def + int ; r2n(ruby_date + int , nil) ; end
|
28
|
-
def > other ; ruby_date > other.ruby_date ; end
|
29
|
-
def < other ; ruby_date < other.ruby_date ; end
|
30
|
-
def == other ; ruby_date == other.ruby_date ; end
|
31
|
-
def <=> 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 == other ; is_date?(other) && ruby_date == other.ruby_date ; end
|
31
|
+
def <=> other ; is_date?(other) && ruby_date <=> other.ruby_date ; end
|
32
32
|
def eql? d ; self == d ; end
|
33
33
|
def hash ; ruby_date.hash ; end
|
34
|
-
|
35
|
-
def - other
|
36
|
-
if other.is_a? Nydp::Date
|
37
|
-
r2n(ruby_date - other.ruby_date, nil)
|
38
|
-
else
|
39
|
-
r2n(ruby_date - other, nil)
|
40
|
-
end
|
41
|
-
end
|
34
|
+
def is_date? other ; other.is_a? Nydp::Date ; end
|
35
|
+
def - other ; r2n(ruby_date - (is_date?(other) ? other.ruby_date : other), nil) ; end
|
42
36
|
|
43
37
|
@@pass_through = %i{ monday? tuesday? wednesday? thursday? friday? saturday? sunday? }
|
44
38
|
@@keys = Set.new %i{
|
data/lib/nydp/parser.rb
CHANGED
@@ -44,11 +44,11 @@ module Nydp
|
|
44
44
|
[
|
45
45
|
[ /%/, "percent-syntax" ],
|
46
46
|
[ /\!/, "bang-syntax" ],
|
47
|
+
[ /::/, "colon-colon-syntax"],
|
48
|
+
[ /:/, "colon-syntax" ],
|
47
49
|
[ /&/, "ampersand-syntax" ],
|
48
50
|
[ /\./, "dot-syntax" ],
|
49
51
|
[ /\$/, "dollar-syntax" ],
|
50
|
-
[ /::/, "colon-colon-syntax"],
|
51
|
-
[ /:/, "colon-syntax" ],
|
52
52
|
[ /->/, "arrow-syntax" ],
|
53
53
|
[ /[=][>]/, "rocket-syntax" ],
|
54
54
|
]
|
data/lib/nydp/truth.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
module Nydp
|
2
2
|
class Truth
|
3
|
-
def
|
4
|
-
def
|
5
|
-
def
|
6
|
-
def
|
7
|
-
def
|
3
|
+
def init_with *; Nydp.T ; end
|
4
|
+
def to_s ; 't' ; end
|
5
|
+
def inspect ; 't[nydp::Truth]' ; end
|
6
|
+
def assign *_ ; self ; end
|
7
|
+
def nydp_type ; :truth ; end
|
8
|
+
def to_ruby ; true ; end
|
8
9
|
end
|
9
10
|
|
10
11
|
class Nil
|
12
|
+
def init_with * ; Nydp.NIL ; end
|
11
13
|
def car ; self ; end
|
12
14
|
def cdr ; self ; end
|
13
15
|
def size ; 0 ; end
|
data/lib/nydp/version.rb
CHANGED
data/spec/date_spec.rb
CHANGED
@@ -46,36 +46,56 @@ describe Nydp::Date do
|
|
46
46
|
expect(diff).to eq 6
|
47
47
|
end
|
48
48
|
|
49
|
-
|
50
|
-
|
49
|
+
describe "'>" do
|
50
|
+
it "works with builtin greater-than when true" do
|
51
|
+
f = Nydp::Builtin::GreaterThan.new
|
51
52
|
|
52
|
-
|
53
|
+
f.invoke vm, pair_list([d1, d0])
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
expect(vm.pop_arg).to eq Nydp.T
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
-
|
58
|
+
it "compares with nil" do
|
59
|
+
f = Nydp::Builtin::GreaterThan.new
|
59
60
|
|
60
|
-
|
61
|
+
f.invoke vm, pair_list([d1, Nydp.NIL])
|
61
62
|
|
62
|
-
|
63
|
-
|
63
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
64
|
+
end
|
64
65
|
|
65
|
-
|
66
|
-
|
66
|
+
it "works with builtin greater-than when false" do
|
67
|
+
f = Nydp::Builtin::GreaterThan.new
|
67
68
|
|
68
|
-
|
69
|
+
f.invoke vm, pair_list([d0, d1])
|
69
70
|
|
70
|
-
|
71
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
72
|
+
end
|
71
73
|
end
|
72
74
|
|
73
|
-
|
74
|
-
|
75
|
+
describe "'<" do
|
76
|
+
it "works with builtin less-than when true" do
|
77
|
+
f = Nydp::Builtin::LessThan.new
|
78
|
+
|
79
|
+
f.invoke vm, pair_list([d0, d1])
|
80
|
+
|
81
|
+
expect(vm.pop_arg).to eq Nydp.T
|
82
|
+
end
|
83
|
+
|
84
|
+
it "works with builtin less-than when false" do
|
85
|
+
f = Nydp::Builtin::LessThan.new
|
86
|
+
|
87
|
+
f.invoke vm, pair_list([d1, d0])
|
88
|
+
|
89
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
90
|
+
end
|
91
|
+
|
92
|
+
it "compares with nil" do
|
93
|
+
f = Nydp::Builtin::LessThan.new
|
75
94
|
|
76
|
-
|
95
|
+
f.invoke vm, pair_list([d1, Nydp.NIL])
|
77
96
|
|
78
|
-
|
97
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
98
|
+
end
|
79
99
|
end
|
80
100
|
|
81
101
|
it "works with builtin plus" do
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Nydp::Hash do
|
4
|
+
let(:vm) { Nydp::VM.new }
|
5
|
+
|
6
|
+
describe "foreign hashes" do
|
7
|
+
let(:ahash) { Hash.new }
|
8
|
+
|
9
|
+
describe "hash set" do
|
10
|
+
it "returns a new Nydp hash" do
|
11
|
+
k = Nydp::Symbol.mk "keysym", ns
|
12
|
+
v = Nydp::StringAtom.new "foobar"
|
13
|
+
args = pair_list [ahash, k, v]
|
14
|
+
Nydp::Builtin::HashSet.new.invoke vm, args
|
15
|
+
|
16
|
+
expect(ahash[:keysym]). to eq "foobar"
|
17
|
+
expect(ahash[:keysym].class).to eq String
|
18
|
+
expect(ahash.keys). to eq [:keysym]
|
19
|
+
|
20
|
+
expect(vm.pop_arg).to eq v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "hash get" do
|
25
|
+
it "converts ruby value to nydp value" do
|
26
|
+
ahash[:keysym] = "avalue"
|
27
|
+
k = sym("keysym")
|
28
|
+
args = [ ahash, k ]
|
29
|
+
|
30
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
31
|
+
|
32
|
+
expect(vm.pop_arg).to eq Nydp::StringAtom.new("avalue")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "converts ruby nil to nydp value" do
|
36
|
+
k = sym("keysym")
|
37
|
+
args = [ ahash, k ]
|
38
|
+
|
39
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
40
|
+
|
41
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
42
|
+
end
|
43
|
+
|
44
|
+
it "converts ruby true to nydp value" do
|
45
|
+
ahash[:keysym] = true
|
46
|
+
k = sym("keysym")
|
47
|
+
args = [ ahash, k ]
|
48
|
+
|
49
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
50
|
+
|
51
|
+
expect(vm.pop_arg).to eq Nydp.T
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "key?" do
|
56
|
+
it "returns t when key is present" do
|
57
|
+
ahash[:simon] = 24
|
58
|
+
k = sym("simon")
|
59
|
+
args = [ ahash, k ]
|
60
|
+
|
61
|
+
Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list(args)
|
62
|
+
|
63
|
+
expect(vm.pop_arg).to eq Nydp.T
|
64
|
+
end
|
65
|
+
|
66
|
+
it "returns nil when key is absent" do
|
67
|
+
k = sym("simon")
|
68
|
+
args = [ ahash, k ]
|
69
|
+
|
70
|
+
Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list(args)
|
71
|
+
|
72
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "hash keys" do
|
77
|
+
it "returns a list of keys" do
|
78
|
+
ahash[:k0] = 42
|
79
|
+
ahash[:k1] = 84
|
80
|
+
args = [ahash]
|
81
|
+
|
82
|
+
Nydp::Builtin::HashKeys.new(ns).invoke vm, pair_list(args)
|
83
|
+
|
84
|
+
expect(vm.pop_arg).to eq pair_list [sym("k0"), sym("k1")]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Nydp::Hash do
|
4
|
+
let(:vm) { Nydp::VM.new }
|
5
|
+
|
6
|
+
describe "friendly non-hashes" do
|
7
|
+
let(:ahash) { TestThing.new 123, "hello there", "private" }
|
8
|
+
|
9
|
+
describe "hash get" do
|
10
|
+
it "returns a plain number" do
|
11
|
+
k = Nydp::Symbol.mk "a", ns
|
12
|
+
args = [ ahash, k ]
|
13
|
+
|
14
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
15
|
+
expect(vm.pop_arg).to eq 123
|
16
|
+
end
|
17
|
+
|
18
|
+
it "converts ruby value to nydp value" do
|
19
|
+
k = Nydp::Symbol.mk "b", ns
|
20
|
+
args = [ ahash, k ]
|
21
|
+
|
22
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
23
|
+
expect(vm.pop_arg).to eq Nydp::StringAtom.new("hello there")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "converts string keys to method names" do
|
27
|
+
k = Nydp::StringAtom.new "b"
|
28
|
+
args = [ ahash, k ]
|
29
|
+
|
30
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
31
|
+
expect(vm.pop_arg).to eq Nydp::StringAtom.new("hello there")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns nil for unavailable methods" do
|
35
|
+
k = Nydp::Symbol.mk "c", ns
|
36
|
+
args = [ ahash, k ]
|
37
|
+
|
38
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
39
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "unfriendly non-hash" do
|
45
|
+
let(:ahash) { Nydp::StringAtom.new "this here ain't no hash, hombre" }
|
46
|
+
|
47
|
+
describe "hash set" do
|
48
|
+
it "does nothing, returns its value" do
|
49
|
+
k = Nydp::Symbol.mk "keysym", ns
|
50
|
+
v = Nydp::StringAtom.new "foobar"
|
51
|
+
args = pair_list [ahash, k, v]
|
52
|
+
|
53
|
+
begin
|
54
|
+
Nydp::Builtin::HashSet.new.invoke vm, args
|
55
|
+
rescue Exception => e
|
56
|
+
error = e
|
57
|
+
end
|
58
|
+
|
59
|
+
expect(error.message.gsub(/at \/.*:in `builtin_invoke'/, '<error info>')).to eq "Called builtin/hash-set
|
60
|
+
with args (\"this here ain't no hash, hombre\" keysym \"foobar\")
|
61
|
+
raised
|
62
|
+
hash-set: Not a hash: Nydp::StringAtom
|
63
|
+
<error info>"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "hash get" do
|
68
|
+
it "converts ruby value to nydp value" do
|
69
|
+
k = Nydp::Symbol.mk "keysym", ns
|
70
|
+
args = [ ahash, k ]
|
71
|
+
|
72
|
+
begin
|
73
|
+
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
74
|
+
rescue Exception => e
|
75
|
+
error = e
|
76
|
+
end
|
77
|
+
|
78
|
+
expect(error.message.gsub(/at \/.*:in `ruby_call'/, '<error info>')).to eq "Called builtin/hash-get
|
79
|
+
with args (\"this here ain't no hash, hombre\" keysym)
|
80
|
+
raised
|
81
|
+
hash-get: Not a hash: Nydp::StringAtom
|
82
|
+
<error info>"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/spec/hash_spec.rb
CHANGED
@@ -130,89 +130,6 @@ describe Nydp::Hash do
|
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
|
-
describe "foreign hashes" do
|
134
|
-
let(:ahash) { Hash.new }
|
135
|
-
|
136
|
-
describe "hash set" do
|
137
|
-
it "returns a new Nydp hash" do
|
138
|
-
k = Nydp::Symbol.mk "keysym", ns
|
139
|
-
v = Nydp::StringAtom.new "foobar"
|
140
|
-
args = pair_list [ahash, k, v]
|
141
|
-
Nydp::Builtin::HashSet.new.invoke vm, args
|
142
|
-
|
143
|
-
expect(ahash[:keysym]). to eq "foobar"
|
144
|
-
expect(ahash[:keysym].class).to eq String
|
145
|
-
expect(ahash.keys). to eq [:keysym]
|
146
|
-
|
147
|
-
expect(vm.pop_arg).to eq v
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
describe "hash get" do
|
152
|
-
it "converts ruby value to nydp value" do
|
153
|
-
ahash[:keysym] = "avalue"
|
154
|
-
k = sym("keysym")
|
155
|
-
args = [ ahash, k ]
|
156
|
-
|
157
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
158
|
-
|
159
|
-
expect(vm.pop_arg).to eq Nydp::StringAtom.new("avalue")
|
160
|
-
end
|
161
|
-
|
162
|
-
it "converts ruby nil to nydp value" do
|
163
|
-
k = sym("keysym")
|
164
|
-
args = [ ahash, k ]
|
165
|
-
|
166
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
167
|
-
|
168
|
-
expect(vm.pop_arg).to eq Nydp.NIL
|
169
|
-
end
|
170
|
-
|
171
|
-
it "converts ruby true to nydp value" do
|
172
|
-
ahash[:keysym] = true
|
173
|
-
k = sym("keysym")
|
174
|
-
args = [ ahash, k ]
|
175
|
-
|
176
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
177
|
-
|
178
|
-
expect(vm.pop_arg).to eq Nydp.T
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
describe "key?" do
|
183
|
-
it "returns t when key is present" do
|
184
|
-
ahash[:simon] = 24
|
185
|
-
k = sym("simon")
|
186
|
-
args = [ ahash, k ]
|
187
|
-
|
188
|
-
Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list(args)
|
189
|
-
|
190
|
-
expect(vm.pop_arg).to eq Nydp.T
|
191
|
-
end
|
192
|
-
|
193
|
-
it "returns nil when key is absent" do
|
194
|
-
k = sym("simon")
|
195
|
-
args = [ ahash, k ]
|
196
|
-
|
197
|
-
Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list(args)
|
198
|
-
|
199
|
-
expect(vm.pop_arg).to eq Nydp.NIL
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
describe "hash keys" do
|
204
|
-
it "returns a list of keys" do
|
205
|
-
ahash[:k0] = 42
|
206
|
-
ahash[:k1] = 84
|
207
|
-
args = [ahash]
|
208
|
-
|
209
|
-
Nydp::Builtin::HashKeys.new(ns).invoke vm, pair_list(args)
|
210
|
-
|
211
|
-
expect(vm.pop_arg).to eq pair_list [sym("k0"), sym("k1")]
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
133
|
describe "get/set nil" do
|
217
134
|
let(:ahash) { Nydp.NIL }
|
218
135
|
|
@@ -239,85 +156,4 @@ describe Nydp::Hash do
|
|
239
156
|
end
|
240
157
|
end
|
241
158
|
end
|
242
|
-
|
243
|
-
describe "friendly non-hashes" do
|
244
|
-
let(:ahash) { TestThing.new 123, "hello there", "private" }
|
245
|
-
|
246
|
-
describe "hash get" do
|
247
|
-
it "returns a plain number" do
|
248
|
-
k = Nydp::Symbol.mk "a", ns
|
249
|
-
args = [ ahash, k ]
|
250
|
-
|
251
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
252
|
-
expect(vm.pop_arg).to eq 123
|
253
|
-
end
|
254
|
-
|
255
|
-
it "converts ruby value to nydp value" do
|
256
|
-
k = Nydp::Symbol.mk "b", ns
|
257
|
-
args = [ ahash, k ]
|
258
|
-
|
259
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
260
|
-
expect(vm.pop_arg).to eq Nydp::StringAtom.new("hello there")
|
261
|
-
end
|
262
|
-
|
263
|
-
it "converts string keys to method names" do
|
264
|
-
k = Nydp::StringAtom.new "b"
|
265
|
-
args = [ ahash, k ]
|
266
|
-
|
267
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
268
|
-
expect(vm.pop_arg).to eq Nydp::StringAtom.new("hello there")
|
269
|
-
end
|
270
|
-
|
271
|
-
it "returns nil for unavailable methods" do
|
272
|
-
k = Nydp::Symbol.mk "c", ns
|
273
|
-
args = [ ahash, k ]
|
274
|
-
|
275
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
276
|
-
expect(vm.pop_arg).to eq Nydp.NIL
|
277
|
-
end
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
describe "non-hash" do
|
282
|
-
let(:ahash) { Nydp::StringAtom.new "this here ain't no hash, hombre" }
|
283
|
-
|
284
|
-
describe "hash set" do
|
285
|
-
it "does nothing, returns its value" do
|
286
|
-
k = Nydp::Symbol.mk "keysym", ns
|
287
|
-
v = Nydp::StringAtom.new "foobar"
|
288
|
-
args = pair_list [ahash, k, v]
|
289
|
-
|
290
|
-
begin
|
291
|
-
Nydp::Builtin::HashSet.new.invoke vm, args
|
292
|
-
rescue Exception => e
|
293
|
-
error = e
|
294
|
-
end
|
295
|
-
|
296
|
-
expect(error.message.gsub(/at \/.*:in `builtin_invoke'/, '<error info>')).to eq "Called builtin/hash-set
|
297
|
-
with args (\"this here ain't no hash, hombre\" keysym \"foobar\")
|
298
|
-
raised
|
299
|
-
hash-set: Not a hash: Nydp::StringAtom
|
300
|
-
<error info>"
|
301
|
-
end
|
302
|
-
end
|
303
|
-
|
304
|
-
describe "hash get" do
|
305
|
-
it "converts ruby value to nydp value" do
|
306
|
-
k = Nydp::Symbol.mk "keysym", ns
|
307
|
-
args = [ ahash, k ]
|
308
|
-
|
309
|
-
begin
|
310
|
-
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
|
311
|
-
rescue Exception => e
|
312
|
-
error = e
|
313
|
-
end
|
314
|
-
|
315
|
-
expect(error.message.gsub(/at \/.*:in `ruby_call'/, '<error info>')).to eq "Called builtin/hash-get
|
316
|
-
with args (\"this here ain't no hash, hombre\" keysym)
|
317
|
-
raised
|
318
|
-
hash-get: Not a hash: Nydp::StringAtom
|
319
|
-
<error info>"
|
320
|
-
end
|
321
|
-
end
|
322
|
-
end
|
323
159
|
end
|
data/spec/parser_spec.rb
CHANGED
@@ -146,23 +146,20 @@ describe Nydp::Parser do
|
|
146
146
|
|
147
147
|
it "should spot numbers hiding in special syntax" do
|
148
148
|
parsed = parse("foo.2:3:4")
|
149
|
-
expect(parsed.inspect).to eq "(
|
149
|
+
expect(parsed.inspect).to eq "(colon-syntax (dot-syntax foo 2) 3 4)"
|
150
150
|
|
151
|
-
|
152
|
-
|
153
|
-
three = numbers.cdr.car
|
154
|
-
four = numbers.cdr.cdr.car
|
155
|
-
|
156
|
-
expect([two, three, four].map &:class).to eq [Fixnum, Fixnum, Fixnum]
|
151
|
+
expect(parsed.map &:class).to eq [Nydp::Symbol, Nydp::Pair, Fixnum, Fixnum]
|
152
|
+
expect(parsed.cdr.car.map &:class).to eq [Nydp::Symbol, Nydp::Symbol, Fixnum]
|
157
153
|
end
|
158
154
|
|
159
155
|
it "should handle prefix and postfix syntax also" do
|
160
156
|
parsed = parse(".foo123:")
|
161
|
-
expect(parsed.inspect).to eq "(
|
157
|
+
expect(parsed.inspect).to eq "(colon-syntax (dot-syntax || foo123) ||)"
|
162
158
|
end
|
163
159
|
|
164
160
|
it "should parse a dotted symbol" do
|
165
|
-
|
161
|
+
expected = parse "(list a b (dot-syntax foo bar) c)"
|
162
|
+
expect(parse "(list a b foo.bar c)").to eq expected
|
166
163
|
end
|
167
164
|
|
168
165
|
it "should parse a colon-colon symbol" do
|
@@ -170,7 +167,8 @@ describe Nydp::Parser do
|
|
170
167
|
end
|
171
168
|
|
172
169
|
it "should parse a colon-symbol within a colon-colon within a dotted symbol" do
|
173
|
-
|
170
|
+
expected = parse "(colon-colon-syntax (colon-syntax (dot-syntax aa foo) foo) (colon-syntax bar (dot-syntax bar zz)))"
|
171
|
+
expect(parse "aa.foo:foo::bar:bar.zz").to eq expected
|
174
172
|
end
|
175
173
|
|
176
174
|
it "should quote symbols" do
|
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.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/lisp/core-090-hook.nydp
|
100
100
|
- lib/lisp/core-100-utils.nydp
|
101
101
|
- lib/lisp/tests/add-hook-examples.nydp
|
102
|
+
- lib/lisp/tests/auto-hash-examples.nydp
|
102
103
|
- lib/lisp/tests/best-examples.nydp
|
103
104
|
- lib/lisp/tests/boot-tests.nydp
|
104
105
|
- lib/lisp/tests/builtin-tests.nydp
|
@@ -117,6 +118,7 @@ files:
|
|
117
118
|
- lib/lisp/tests/error-tests.nydp
|
118
119
|
- lib/lisp/tests/explain-mac-examples.nydp
|
119
120
|
- lib/lisp/tests/foundation-test.nydp
|
121
|
+
- lib/lisp/tests/group-by-examples.nydp
|
120
122
|
- lib/lisp/tests/hash-examples.nydp
|
121
123
|
- lib/lisp/tests/include-examples.nydp
|
122
124
|
- lib/lisp/tests/invocation-tests.nydp
|
@@ -129,6 +131,7 @@ files:
|
|
129
131
|
- lib/lisp/tests/pretty-print-tests.nydp
|
130
132
|
- lib/lisp/tests/quasiquote-examples.nydp
|
131
133
|
- lib/lisp/tests/range-examples.nydp
|
134
|
+
- lib/lisp/tests/relative-months-examples.nydp
|
132
135
|
- lib/lisp/tests/returning-examples.nydp
|
133
136
|
- lib/lisp/tests/rfnwith-tests.nydp
|
134
137
|
- lib/lisp/tests/sort-examples.nydp
|
@@ -159,6 +162,7 @@ files:
|
|
159
162
|
- lib/nydp/builtin/load_tests.rb
|
160
163
|
- lib/nydp/builtin/millisecs.rb
|
161
164
|
- lib/nydp/builtin/minus.rb
|
165
|
+
- lib/nydp/builtin/modulo.rb
|
162
166
|
- lib/nydp/builtin/parse.rb
|
163
167
|
- lib/nydp/builtin/parse_in_string.rb
|
164
168
|
- lib/nydp/builtin/plus.rb
|
@@ -209,6 +213,8 @@ files:
|
|
209
213
|
- spec/date_spec.rb
|
210
214
|
- spec/embedded_spec.rb
|
211
215
|
- spec/error_spec.rb
|
216
|
+
- spec/foreign_hash_spec.rb
|
217
|
+
- spec/hash_non_hash_behaviour_spec.rb
|
212
218
|
- spec/hash_spec.rb
|
213
219
|
- spec/literal_spec.rb
|
214
220
|
- spec/nydp_spec.rb
|
@@ -247,6 +253,8 @@ test_files:
|
|
247
253
|
- spec/date_spec.rb
|
248
254
|
- spec/embedded_spec.rb
|
249
255
|
- spec/error_spec.rb
|
256
|
+
- spec/foreign_hash_spec.rb
|
257
|
+
- spec/hash_non_hash_behaviour_spec.rb
|
250
258
|
- spec/hash_spec.rb
|
251
259
|
- spec/literal_spec.rb
|
252
260
|
- spec/nydp_spec.rb
|