nydp 0.1.8 → 0.1.9
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.
- checksums.yaml +4 -4
- data/lib/lisp/core-017-builtin-dox.nydp +9 -0
- data/lib/lisp/core-040-utils.nydp +3 -1
- data/lib/lisp/core-080-pretty-print.nydp +8 -0
- data/lib/lisp/core-100-utils.nydp +12 -0
- data/lib/lisp/tests/best-examples.nydp +17 -1
- data/lib/lisp/tests/sort-examples.nydp +28 -0
- data/lib/nydp/builtin/sort.rb +15 -0
- data/lib/nydp/core.rb +1 -0
- data/lib/nydp/string_atom.rb +7 -1
- data/lib/nydp/symbol.rb +2 -0
- data/lib/nydp/version.rb +1 -1
- data/spec/embedded_spec.rb +4 -4
- data/spec/nydp_spec.rb +2 -2
- data/spec/parser_spec.rb +6 -6
- data/spec/string_atom_spec.rb +52 -0
- data/spec/symbol_spec.rb +37 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a04241f46cc86cfb401435e98feac3315b758bb
|
4
|
+
data.tar.gz: a2885ed15e20a545b3c73ada1cfe815f74fa163e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cb48a423ba6e63f894eb6731cde8c1006e142288f0c25b14be25a3b7877d5736fa4d94142d5fd9f53002c2ca76ab4e19a72b89b06b411afd06d565b5315a016
|
7
|
+
data.tar.gz: 3c0e631c2c920c73f00feaf98237e4548907c441097cabd037db3849cf4570b6d2969c15ead3c5408d3cdd0be6759d8bb4b656319196fa0ac841b1d0636b4e51
|
@@ -0,0 +1,9 @@
|
|
1
|
+
(dox-add-doc 'cons 'def '("with args a and b, returns a new cons cell, (a . b)") '(a b) nil)
|
2
|
+
(dox-add-doc 'car 'def '("with args a, where a is a cons cell (x . y), return x." "Commonly used to get the first element of a list") '(a) nil)
|
3
|
+
(dox-add-doc 'cdr 'def '("with args a, where a is a cons cell (x . y), return y." "Commonly used to get contents of a list, excluding the first element") '(a) nil)
|
4
|
+
(dox-add-doc '+ 'def '("with rest-args things, return the sum of the elements of things." "Will also increment dates and concatenate strings and lists") 'things nil)
|
5
|
+
(dox-add-doc '- 'def '("return the result of subtracting all other args from the first arg." "(- a b c d) is equivalent to (- a (+ b c d))") 'things nil)
|
6
|
+
(dox-add-doc '* 'def '("with rest-args things, return the product of the elements of things.") 'things nil)
|
7
|
+
(dox-add-doc '/ 'def '("return the result of dividing all other args into the first arg." "(/ a b c d) is equivalent to (/ a (* b c d))") 'things nil)
|
8
|
+
(dox-add-doc '> 'def '("true if each arg is greater than the next arg") 'things nil)
|
9
|
+
(dox-add-doc '< 'def '("true if each arg is less than the next arg") 'things nil)
|
@@ -17,11 +17,19 @@
|
|
17
17
|
|
18
18
|
(def pp/string-pieces (things) "\"~(joinstr "" (map pp/string-piece things))\"")
|
19
19
|
|
20
|
+
(def pp/kv (hsh)
|
21
|
+
(map λk(joinstr " "
|
22
|
+
(pp k)
|
23
|
+
(pp hsh.,k))
|
24
|
+
(hash-keys hsh)))
|
25
|
+
|
20
26
|
(def pp/literal (thing)
|
21
27
|
(if (eq? thing '||)
|
22
28
|
""
|
23
29
|
(isa 'string thing)
|
24
30
|
"\"~(pp/escape-string-literal thing)\""
|
31
|
+
(isa 'hash thing)
|
32
|
+
"{ ~(joinstr " " (pp/kv thing)) }"
|
25
33
|
(inspect thing)))
|
26
34
|
|
27
35
|
(mac pp/def (name args . body)
|
@@ -2,3 +2,15 @@
|
|
2
2
|
; alias for 'detect
|
3
3
|
; true if thing is in things, nil otherwise
|
4
4
|
(detect thing things))
|
5
|
+
|
6
|
+
(def sort-by (f things)
|
7
|
+
; sort 'things according to the value
|
8
|
+
; returned by 'f for each thing in 'things
|
9
|
+
(let tmp (hash)
|
10
|
+
(each thing things
|
11
|
+
(hash-cons tmp
|
12
|
+
(f thing)
|
13
|
+
thing))
|
14
|
+
(apply joinlists
|
15
|
+
(map λx(hash-get tmp x)
|
16
|
+
(sort:hash-keys tmp)))))
|
@@ -3,7 +3,23 @@
|
|
3
3
|
(best < '(3 5 4 7 8 2))
|
4
4
|
2)
|
5
5
|
|
6
|
-
("finds
|
6
|
+
("finds minimum of list of syms"
|
7
|
+
(best < '(c g d o p b m e g z m))
|
8
|
+
b)
|
9
|
+
|
10
|
+
("finds minimum of list of strings"
|
11
|
+
(best < '("c" "g" "d" "o" "p" "b" "z" "m"))
|
12
|
+
"b")
|
13
|
+
|
14
|
+
("finds maximum of list of strings"
|
15
|
+
(best > '("c" "g" "d" "o" "p" "b" "y" "m"))
|
16
|
+
"y")
|
17
|
+
|
18
|
+
("finds maximum of list of syms"
|
19
|
+
(best > '(c g d o a p b m e g a z m))
|
20
|
+
z)
|
21
|
+
|
22
|
+
("finds maximum of list of numbers"
|
7
23
|
(best > '(3 5 4 7 8 2))
|
8
24
|
8))
|
9
25
|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
(examples-for sort
|
2
|
+
("sorts a list of numbers"
|
3
|
+
(sort '(3 5 4 7 8 2))
|
4
|
+
(2 3 4 5 7 8))
|
5
|
+
|
6
|
+
("sorts a list of syms"
|
7
|
+
(sort '(c g d o a p b m e g a z m))
|
8
|
+
(a a b c d e g g m m o p z))
|
9
|
+
|
10
|
+
("sorts a list of strings"
|
11
|
+
(sort '("c" "g" "d" "o" "a" "p" "b" "m" "e" "g" "a" "z" "m"))
|
12
|
+
("a" "a" "b" "c" "d" "e" "g" "g" "m" "m" "o" "p" "z")))
|
13
|
+
|
14
|
+
(examples-for sort-by
|
15
|
+
("sorts a list of hashes by a specified key"
|
16
|
+
(let hh (list { a 1 b 2 } { a 7 b 9 } { a 3 b 1 } { a 2 b 8 })
|
17
|
+
(pp (sort-by &a hh)))
|
18
|
+
"({ a 1 b 2 } { a 2 b 8 } { a 3 b 1 } { a 7 b 9 })")
|
19
|
+
|
20
|
+
("sorts a list of strings according to their length"
|
21
|
+
(sort-by len
|
22
|
+
'("short" "very very very long" "somewhat long" "more medium" "min" "medium"))
|
23
|
+
("min" "short" "medium" "more medium" "somewhat long" "very very very long"))
|
24
|
+
|
25
|
+
("sort a list of strings according to their length even when some strings have the same length"
|
26
|
+
(sort-by len
|
27
|
+
'("short" "very long" "sport" "very song" "min" "max"))
|
28
|
+
("max" "min" "sport" "short" "very song" "very long")))
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Nydp
|
2
|
+
class Builtin::Sort
|
3
|
+
include Builtin::Base
|
4
|
+
|
5
|
+
def builtin_invoke vm, args
|
6
|
+
vm.push_arg Pair.from_list to_array(args.car, []).sort
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def to_array pair, list
|
12
|
+
pair.is_a?(Pair) ? to_array(pair.cdr, (list << pair.car)) : list
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/nydp/core.rb
CHANGED
@@ -40,6 +40,7 @@ module Nydp
|
|
40
40
|
Symbol.mk(:p, ns).assign(Nydp::Builtin::Puts.new)
|
41
41
|
Symbol.mk(:PI, ns).assign Literal.new(3.1415)
|
42
42
|
Symbol.mk(:nil, ns).assign Nydp.NIL
|
43
|
+
Symbol.mk(:sort, ns).assign Nydp::Builtin::Sort.new
|
43
44
|
Symbol.mk(:sqrt, ns).assign Nydp::Builtin::Sqrt.new
|
44
45
|
Symbol.mk(:t, ns).assign Nydp.T
|
45
46
|
Symbol.mk(:sym, ns).assign Nydp::Builtin::Sym.new(ns)
|
data/lib/nydp/string_atom.rb
CHANGED
@@ -13,9 +13,15 @@ module Nydp
|
|
13
13
|
def inspect ; string.inspect ; end
|
14
14
|
def hash ; string.hash ; end
|
15
15
|
def length ; string.length ; end
|
16
|
+
def > other ; self.string > other.string ; end
|
17
|
+
def < other ; self.string < other.string ; end
|
18
|
+
|
19
|
+
def <=> other
|
20
|
+
self < other ? -1 : (self == other ? 0 : 1)
|
21
|
+
end
|
16
22
|
|
17
23
|
def == other
|
18
|
-
other.to_s == self.to_s
|
24
|
+
other.is_a?(Nydp::StringAtom) && (other.to_s == self.to_s)
|
19
25
|
end
|
20
26
|
|
21
27
|
def + other
|
data/lib/nydp/symbol.rb
CHANGED
@@ -40,6 +40,8 @@ class Nydp::Symbol
|
|
40
40
|
def hash ; name.hash ; end
|
41
41
|
def eql? other ; self == other ; end
|
42
42
|
def is? nm ; self.name == nm.to_sym ; end
|
43
|
+
def > other ; self.name > other.name ; end
|
44
|
+
def < other ; self.name < other.name ; end
|
43
45
|
def <=> other ; self.name <=> other.name ; end
|
44
46
|
|
45
47
|
def == other
|
data/lib/nydp/version.rb
CHANGED
data/spec/embedded_spec.rb
CHANGED
@@ -27,19 +27,19 @@ describe Nydp::Parser do
|
|
27
27
|
it "should parse empty string" do
|
28
28
|
expected = pair_list([sym('string-pieces'), Nydp::StringFragmentCloseToken.new('','$%')])
|
29
29
|
actual = parse_string "%", '$', /%/
|
30
|
-
expect(actual).to eq ''
|
30
|
+
expect(actual).to eq Nydp::StringAtom.new ''
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should parse external text" do
|
34
34
|
actual = parse_string "a fluffy bunny!", 'EAT ', /!/
|
35
|
-
expect(actual) .to eq "a fluffy bunny"
|
35
|
+
expect(actual) .to eq Nydp::StringAtom.new "a fluffy bunny"
|
36
36
|
expect(actual.inspect).to eq '"a fluffy bunny"'
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should parse a string delimited by eof" do
|
40
40
|
expected = pair_list([sym('string-pieces'), Nydp::StringFragmentCloseToken.new('a fluffy bunny!','a fluffy bunny!')])
|
41
41
|
actual = parse_string "a fluffy bunny!", '', :eof
|
42
|
-
expect(actual) .to eq "a fluffy bunny!"
|
42
|
+
expect(actual) .to eq Nydp::StringAtom.new "a fluffy bunny!"
|
43
43
|
expect(actual.inspect).to eq '"a fluffy bunny!"'
|
44
44
|
end
|
45
45
|
|
@@ -59,7 +59,7 @@ describe Nydp::Parser do
|
|
59
59
|
it "should parse a string with embedded code containing a nested string, delimited by eof" do
|
60
60
|
n1 = sym(:foo)
|
61
61
|
n2 = sym(:bar)
|
62
|
-
n3 = 'an embedded bunny :)'
|
62
|
+
n3 = Nydp::StringAtom.new 'an embedded bunny :)'
|
63
63
|
n4 = sym(:zop)
|
64
64
|
|
65
65
|
x1 = sym('string-pieces')
|
data/spec/nydp_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe Nydp do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should add strings" do
|
21
|
-
expect(run '(+ "hello" " " "world")').to eq "hello world"
|
21
|
+
expect(run '(+ "hello" " " "world")').to eq Nydp::StringAtom.new("hello world")
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should add Pairs" do
|
@@ -42,7 +42,7 @@ describe Nydp do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should convert items to strings" do
|
45
|
-
expect(run "(to-string 3.1415)").to eq "3.1415"
|
45
|
+
expect(run "(to-string 3.1415)").to eq Nydp::StringAtom.new("3.1415")
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should compare integers" do
|
data/spec/parser_spec.rb
CHANGED
@@ -80,7 +80,7 @@ describe Nydp::Parser do
|
|
80
80
|
s2 = Nydp::StringFragmentCloseToken.new "hello there", '"hello there"'
|
81
81
|
|
82
82
|
x1 = 1
|
83
|
-
x2 = "hello there"
|
83
|
+
x2 = Nydp::StringAtom.new "hello there"
|
84
84
|
x3 = 3
|
85
85
|
|
86
86
|
expected = pair_list [x1, x2, x3]
|
@@ -90,7 +90,7 @@ describe Nydp::Parser do
|
|
90
90
|
|
91
91
|
it "should parse a string" do
|
92
92
|
x1 = sym 'join'
|
93
|
-
x2 = " - "
|
93
|
+
x2 = Nydp::StringAtom.new " - "
|
94
94
|
x3 = 1
|
95
95
|
x4 = 2
|
96
96
|
x5 = 3
|
@@ -105,7 +105,7 @@ describe Nydp::Parser do
|
|
105
105
|
s2 = Nydp::StringFragmentCloseToken.new "hello (1 2 3) there", '"hello (1 2 3) there"'
|
106
106
|
|
107
107
|
x1 = 1
|
108
|
-
x2 = "hello (1 2 3) there"
|
108
|
+
x2 = Nydp::StringAtom.new "hello (1 2 3) there"
|
109
109
|
x3 = 3
|
110
110
|
|
111
111
|
expected = pair_list [x1, x2, x3]
|
@@ -118,7 +118,7 @@ describe Nydp::Parser do
|
|
118
118
|
s2 = Nydp::StringFragmentCloseToken.new "hello there \"jimmy\"", '"hello there \"jimmy\""'
|
119
119
|
|
120
120
|
x1 = 1
|
121
|
-
x2 = "hello there \"jimmy\""
|
121
|
+
x2 = Nydp::StringAtom.new "hello there \"jimmy\""
|
122
122
|
x3 = 3
|
123
123
|
|
124
124
|
expected = pair_list [x1, x2, x3]
|
@@ -216,7 +216,7 @@ describe Nydp::Parser do
|
|
216
216
|
end
|
217
217
|
|
218
218
|
it "retains otherwise unidentified list prefixes" do
|
219
|
-
expect(parse "%wong(bar)").to eq pair_list([prefix_list, "%wong", pair_list([bar])])
|
219
|
+
expect(parse "%wong(bar)").to eq pair_list([prefix_list, Nydp::StringAtom.new("%wong"), pair_list([bar])])
|
220
220
|
end
|
221
221
|
|
222
222
|
it "should do some complicated unquote stuff with lists" do
|
@@ -262,7 +262,7 @@ describe Nydp::Parser do
|
|
262
262
|
; here's a comment
|
263
263
|
(zab))
|
264
264
|
"
|
265
|
-
c1 = pair_list([comment, "here's a comment"])
|
265
|
+
c1 = pair_list([comment, Nydp::StringAtom.new("here's a comment")])
|
266
266
|
fbar = pair_list([bar])
|
267
267
|
fzab = pair_list([Nydp::Symbol.mk(:zab, ns)])
|
268
268
|
fdef = Nydp::Symbol.mk(:def, ns)
|
data/spec/string_atom_spec.rb
CHANGED
@@ -1,9 +1,61 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Nydp::StringAtom do
|
4
|
+
let(:bar) { Nydp::StringAtom.new "BAR" }
|
5
|
+
let(:foo) { Nydp::StringAtom.new "FOO" }
|
6
|
+
let(:vm) { Nydp::VM.new }
|
7
|
+
|
8
|
+
it "is not equal to a symbol with the same represenation" do
|
9
|
+
string = Nydp::StringAtom.new "harrypotter"
|
10
|
+
symbol = Nydp::Symbol.mk "harrypotter", ns
|
11
|
+
compare = Nydp::StringAtom.new symbol.to_s
|
12
|
+
expect(string == compare).to eq true
|
13
|
+
expect(string == symbol) .to eq false
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is not equal to a list with the same represenation" do
|
17
|
+
string = Nydp::StringAtom.new "(FOO BAR)"
|
18
|
+
list = Nydp::Pair.from_list [foo, bar]
|
19
|
+
compare = Nydp::StringAtom.new list.to_s
|
20
|
+
expect(string == compare).to eq true
|
21
|
+
expect(string == list) .to eq false
|
22
|
+
end
|
23
|
+
|
4
24
|
it "returns its string in #to_ruby" do
|
5
25
|
s = Nydp::StringAtom.new "harrypotter"
|
6
26
|
expect(s.to_ruby).to eq "harrypotter"
|
7
27
|
expect(s.to_ruby.class).to eq String
|
8
28
|
end
|
29
|
+
|
30
|
+
it "works with builtin greater-than when true" do
|
31
|
+
f = Nydp::Builtin::GreaterThan.new
|
32
|
+
|
33
|
+
f.invoke vm, pair_list([foo, bar])
|
34
|
+
|
35
|
+
expect(vm.pop_arg).to eq Nydp.T
|
36
|
+
end
|
37
|
+
|
38
|
+
it "works with builtin greater-than when false" do
|
39
|
+
f = Nydp::Builtin::GreaterThan.new
|
40
|
+
|
41
|
+
f.invoke vm, pair_list([bar, foo])
|
42
|
+
|
43
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
44
|
+
end
|
45
|
+
|
46
|
+
it "works with builtin less-than when true" do
|
47
|
+
f = Nydp::Builtin::LessThan.new
|
48
|
+
|
49
|
+
f.invoke vm, pair_list([bar, foo])
|
50
|
+
|
51
|
+
expect(vm.pop_arg).to eq Nydp.T
|
52
|
+
end
|
53
|
+
|
54
|
+
it "works with builtin less-than when false" do
|
55
|
+
f = Nydp::Builtin::LessThan.new
|
56
|
+
|
57
|
+
f.invoke vm, pair_list([foo, bar])
|
58
|
+
|
59
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
60
|
+
end
|
9
61
|
end
|
data/spec/symbol_spec.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Nydp::Symbol do
|
4
|
+
let(:bar) { Nydp::Symbol.mk :BAR, ns }
|
5
|
+
let(:foo) { Nydp::Symbol.mk :FOO, ns }
|
6
|
+
let(:vm) { Nydp::VM.new }
|
7
|
+
|
4
8
|
it "returns a ruby symbol in #to_ruby" do
|
5
9
|
sym = Nydp::Symbol.mk :foo, ns
|
6
10
|
expect(sym.to_ruby).to eq :foo
|
@@ -37,4 +41,37 @@ describe Nydp::Symbol do
|
|
37
41
|
expect(sym1.eql? sym2).to eq true
|
38
42
|
expect(sym1.equal? sym2).to eq false
|
39
43
|
end
|
44
|
+
|
45
|
+
it "works with builtin greater-than when true" do
|
46
|
+
f = Nydp::Builtin::GreaterThan.new
|
47
|
+
|
48
|
+
f.invoke vm, pair_list([foo, bar])
|
49
|
+
|
50
|
+
expect(vm.pop_arg).to eq Nydp.T
|
51
|
+
end
|
52
|
+
|
53
|
+
it "works with builtin greater-than when false" do
|
54
|
+
f = Nydp::Builtin::GreaterThan.new
|
55
|
+
|
56
|
+
f.invoke vm, pair_list([bar, foo])
|
57
|
+
|
58
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
59
|
+
end
|
60
|
+
|
61
|
+
it "works with builtin less-than when true" do
|
62
|
+
f = Nydp::Builtin::LessThan.new
|
63
|
+
|
64
|
+
f.invoke vm, pair_list([bar, foo])
|
65
|
+
|
66
|
+
expect(vm.pop_arg).to eq Nydp.T
|
67
|
+
end
|
68
|
+
|
69
|
+
it "works with builtin less-than when false" do
|
70
|
+
f = Nydp::Builtin::LessThan.new
|
71
|
+
|
72
|
+
f.invoke vm, pair_list([foo, bar])
|
73
|
+
|
74
|
+
expect(vm.pop_arg).to eq Nydp.NIL
|
75
|
+
end
|
76
|
+
|
40
77
|
end
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- lib/lisp/core-000.nydp
|
88
88
|
- lib/lisp/core-010-precompile.nydp
|
89
89
|
- lib/lisp/core-015-documentation.nydp
|
90
|
+
- lib/lisp/core-017-builtin-dox.nydp
|
90
91
|
- lib/lisp/core-020-utils.nydp
|
91
92
|
- lib/lisp/core-030-syntax.nydp
|
92
93
|
- lib/lisp/core-040-utils.nydp
|
@@ -128,6 +129,7 @@ files:
|
|
128
129
|
- lib/lisp/tests/quasiquote-examples.nydp
|
129
130
|
- lib/lisp/tests/range-examples.nydp
|
130
131
|
- lib/lisp/tests/rfnwith-tests.nydp
|
132
|
+
- lib/lisp/tests/sort-examples.nydp
|
131
133
|
- lib/lisp/tests/string-tests.nydp
|
132
134
|
- lib/lisp/tests/syntax-tests.nydp
|
133
135
|
- lib/lisp/tests/tuples-examples.nydp
|
@@ -162,6 +164,7 @@ files:
|
|
162
164
|
- lib/nydp/builtin/puts.rb
|
163
165
|
- lib/nydp/builtin/quit.rb
|
164
166
|
- lib/nydp/builtin/random_string.rb
|
167
|
+
- lib/nydp/builtin/sort.rb
|
165
168
|
- lib/nydp/builtin/sqrt.rb
|
166
169
|
- lib/nydp/builtin/string_match.rb
|
167
170
|
- lib/nydp/builtin/string_replace.rb
|