nydp 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 373997c8dcd5597d8742b347ef75b0b68ef312e7
4
- data.tar.gz: 69ab87494d38291aa35786f835489095ce1ccd71
3
+ metadata.gz: e96bb2970aa18753721c5faa2c5aeec53a73245f
4
+ data.tar.gz: 151802fddd1bcdb72ecddc6fb2e882b11c3d2ea4
5
5
  SHA512:
6
- metadata.gz: 801258418b77c34f8521e8dc5f6e6218156b690fa7d1fa3a584a8b77102df8af4021f56f19c27f33be944d88c8fff33443a888c25ccf6b3f20934b5cae17628e
7
- data.tar.gz: 3ddf5f9e89f7159a74ef6a2f163afcbc289ca6b4330b6b1307f3be5043f97e9ec023f2ce2fc2f3aae28fb37c05bc36a6410f6ddf8610673d558034f1c37ea65b
6
+ metadata.gz: 437799e27e763566d9c6febf8ea154aea63bf66a7a9db57f6e8e8c033086646d9966c687d276578840dc0a974e5fdf7fb722ce9366bfb70d840f8e1b3906936f
7
+ data.tar.gz: da65be98312e0b821e9c7fe3b39ae6a8944b87fc6a7d20e56aaffe6a8673f632bda2c7daa6bbefe488243d156e933e81c8f33ee29909b55aa264f2afaa570a83
data/lib/lisp/boot.nydp CHANGED
@@ -1,25 +1,27 @@
1
1
  ; -*- lisp -*-
2
2
 
3
3
  (assign last-cons (fn (xs)
4
- (cond (pair? (cdr xs))
5
- (last-cons (cdr xs))
6
- xs)))
4
+ (cond (pair? (cdr xs))
5
+ (last-cons (cdr xs))
6
+ xs)))
7
7
 
8
- (assign append-list! (fn (list-1 list-2)
9
- (cdr-set (last-cons list-1) list-2)
10
- list-1))
11
-
12
- (assign list (fn args args))
13
8
 
14
- (assign caar (fn (arg) (car (car arg))))
15
- (assign cadr (fn (arg) (car (cdr arg))))
16
- (assign cddr (fn (arg) (cdr (cdr arg))))
17
- (assign no (fn (arg) (eq? arg nil)))
18
- (assign just (fn (arg) arg))
9
+ (assign append-list! (fn (list-1 list-2)
10
+ (cdr-set (last-cons list-1) list-2)
11
+ list-1))
12
+
13
+ (assign list (fn args args))
14
+ (assign last (fn (arg) (car (last-cons arg))))
15
+ (assign caar (fn (arg) (car (car arg))))
16
+ (assign cadr (fn (arg) (car (cdr arg))))
17
+ (assign cddr (fn (arg) (cdr (cdr arg))))
18
+ (assign no (fn (arg) (eq? arg nil)))
19
+ (assign just (fn (arg) arg))
20
+ (assign pargs (fn args (apply p args) (last args)))
19
21
 
20
22
  (assign mac-expand (fn (macfn name body)
21
23
  (cond macfn
22
- (pre-compile-do (apply macfn body))
24
+ (pre-compile (apply macfn body))
23
25
  (cons name body))))
24
26
 
25
27
  (assign macs (hash))
@@ -36,26 +38,23 @@
36
38
  (fn (args)
37
39
  (cond args
38
40
  (cond (pair? args)
39
- (cons (pre-compile-do (car args))
41
+ (cons (pre-compile (car args))
40
42
  (pre-compile-each (cdr args)))
41
43
  args))))
42
44
 
43
45
  (assign pre-compile-msg
44
46
  (fn args
45
- (cond *debug-pre-compile* (apply p args) (car args))))
46
-
47
- (assign pre-compile-do
48
- (fn (arg)
49
- (cond (pair? arg)
50
- (cond (eq? (car arg) 'quote)
51
- arg
52
- (pre-compile-each (pre-compile-expr (car arg) (cdr arg))))
53
- arg)))
47
+ (cond *debug-pre-compile* (apply p args))
48
+ (last args)))
54
49
 
55
50
  (assign pre-compile
56
51
  (fn (arg)
57
- (pre-compile-msg "pre-compile" arg)
58
- (pre-compile-msg (pre-compile-do arg))))
52
+ (pre-compile-msg "pre-compile" arg "\n -> "
53
+ (cond (pair? arg)
54
+ (cond (eq? (car arg) 'quote)
55
+ arg
56
+ (pre-compile-each (pre-compile-expr (car arg) (cdr arg))))
57
+ arg))))
59
58
 
60
59
  (hash-set macs 'def
61
60
  (fn (name args . body)
@@ -64,37 +63,74 @@
64
63
  (+ (list 'fn args)
65
64
  body))))
66
65
 
67
- (def qq-unquote-recurse (arg rest)
66
+ (def qq-handle-unquote-splicing (arg rest level)
67
+ (cond (eq? level 0)
68
+ (qq-do-unquote-splicing arg rest level)
69
+ (qq-skip-unquote-splicing arg rest level)))
70
+
71
+ (def qq-do-unquote-splicing (arg rest level)
72
+ (cond (no rest)
73
+ arg
74
+ (list '+
75
+ (pre-compile arg)
76
+ (qq-quasiquote rest level))))
77
+
78
+ (def qq-skip-unquote-splicing (arg rest level)
79
+ (list 'cons
80
+ (list 'list ''unquote-splicing (qq-quasiquote arg (- level 1)))
81
+ (qq-quasiquote rest level)))
82
+
83
+ (def qq-handle-quasiquote (arg rest level)
84
+ (list 'cons
85
+ (list 'list ''quasiquote (qq-quasiquote arg (+ level 1)))
86
+ (qq-quasiquote rest level)))
87
+
88
+ (def qq-handle-unquote (arg rest level)
89
+ (list 'cons
90
+ (qq-maybe-unquote arg level)
91
+ (qq-quasiquote rest level)))
92
+
93
+ (def qq-unquote-recurse (arg rest level)
68
94
  (list 'cons
69
- (qq-quasiquote arg)
70
- (qq-quasiquote rest)))
95
+ (qq-quasiquote arg level)
96
+ (qq-quasiquote rest level)))
97
+
98
+ (def qq-handle-plain (arg rest level)
99
+ (list 'cons
100
+ (list 'quote arg)
101
+ (qq-quasiquote rest level)))
71
102
 
72
- (def qq-unquote? (arg rest)
103
+ (def qq-unquote? (arg rest level)
73
104
  (cond (pair? arg)
74
105
  (cond (eq? (car arg) 'unquote)
75
- (list 'cons (cadr arg) (qq-quasiquote rest))
106
+ (qq-handle-unquote (cadr arg) rest level)
76
107
  (cond (eq? (car arg) 'unquote-splicing)
77
- (cond (no rest)
78
- (cadr arg)
79
- (list '+
80
- (cadr arg)
81
- (qq-quasiquote rest)))
82
- (qq-unquote-recurse arg rest)))
83
- (list 'cons
84
- (list 'quote arg)
85
- (qq-quasiquote rest))))
86
-
87
- (def qq-quasiquote (xs)
88
- (cond (no xs)
89
- nil
90
- (cond (pair? xs)
91
- (cond (eq? (car xs) 'unquote)
92
- (cadr xs)
93
- (qq-unquote? (car xs) (cdr xs)))
94
- (list 'quote xs))))
95
-
96
- (hash-set macs 'quasiquote (fn (arg)
97
- (qq-quasiquote arg)))
108
+ (qq-handle-unquote-splicing (cadr arg) rest level)
109
+ (cond (eq? (car arg) 'quasiquote)
110
+ (qq-handle-quasiquote (cadr arg) rest level)
111
+ (qq-unquote-recurse arg rest level))))
112
+ (qq-handle-plain arg rest level)))
113
+
114
+ (def qq-maybe-unquote (xs level)
115
+ (cond (eq? level 0)
116
+ (pre-compile xs)
117
+ (list 'list ''unquote (qq-quasiquote xs (- level 1)))))
118
+
119
+ (def qq-quasiquote (xs level)
120
+ (cond (no xs)
121
+ nil
122
+ (cond (pair? xs)
123
+ (cond (eq? (car xs) 'unquote)
124
+ (qq-maybe-unquote (cadr xs) level)
125
+ (cond (eq? (car xs) 'unquote-splicing)
126
+ (qq-handle-unquote-splicing (cadr xs) nil level)
127
+ (cond (eq? (car xs) 'quasiquote)
128
+ (list 'list ''quasiquote (qq-quasiquote (cdr xs) (+ level 1)))
129
+ (qq-unquote? (car xs) (cdr xs) level))))
130
+ (list 'quote xs))))
131
+
132
+ (hash-set macs 'quasiquote
133
+ (fn (arg) (qq-quasiquote arg 0)))
98
134
 
99
135
  (hash-set macs 'mac (fn (name args . body)
100
136
  `(hash-set macs ',name (fn ,args ,@body))))
@@ -141,10 +177,6 @@
141
177
  (mac let (var val . body)
142
178
  `(with (,var ,val) ,@body))
143
179
 
144
- ;; (debug-pre-compile t)
145
- ;; (let a b c d e)
146
- ;; (debug-pre-compile nil)
147
-
148
180
  (def each (f acc things)
149
181
  (if things
150
182
  (each f (f acc (car things)) (cdr things))
@@ -228,3 +260,6 @@
228
260
  (if (pair? xs)
229
261
  (+ 1 (len (cdr xs)))
230
262
  0))
263
+
264
+ (def build-keyword-args (pairs)
265
+ (map (fn (ab) `(list (quote ,(car ab)) ,@(cdr ab))) pairs))
@@ -0,0 +1,54 @@
1
+ ;
2
+ ; absurd contrived exaggerated example to test that macros that generate
3
+ ; macros that generate macros that generate expressions are likely to work
4
+ ;
5
+ ; may you never need to do this in real life
6
+ ;
7
+ (mac make-make-op (opname op)
8
+ `(mac ,opname (name n . body)
9
+ `(mac ,name (x)
10
+ `(,',',op ,,n ,x))))
11
+
12
+ (make-make-op make-mult *)
13
+ (make-make-op make-plus +)
14
+
15
+ (make-mult *five 5)
16
+ (make-mult *seven 7)
17
+ (make-mult *eleven 11)
18
+
19
+ (make-plus +five 5)
20
+ (make-plus +seven 7)
21
+ (make-plus +eleven 11)
22
+
23
+ (register-test
24
+ '(suite "Boot Tests"
25
+
26
+ (suite "quasiquote"
27
+ ("peeks inside nested quotes"
28
+ `(a b '(c ,(+ 1 2)))
29
+ (a b '(c 3)))
30
+ ("handles nested unquote-splicing"
31
+ ``(a ,,@(list '+ 1 2) b)
32
+ `((a ,(+ 1 2) b)))
33
+ ("returns nested quasiquotes"
34
+ `(a b `(c d ,(+ 1 2) ,,(+ 3 4)))
35
+ (a b `(c d ,(+ 1 2) ,7))))
36
+
37
+ (suite "build-keyword-args"
38
+ ("takes a list of lists and returns the list with the first item of each sublist quoted"
39
+ (build-keyword-args '( (a 1) (b c) (d e "f" 22) ))
40
+ ((list 'a 1) (list 'b c) (list 'd e "f" 22))))
41
+
42
+ (suite "make-macros can create macros"
43
+ ("make-plus example generates a plus-seven expression"
44
+ (+seven 6)
45
+ 13)
46
+ ("make-mult example generates a multiply-by-seven expression"
47
+ (*seven 6)
48
+ 42)
49
+ ("make-mult example generates a multiply-by-eleven expression"
50
+ (*eleven 20)
51
+ 220)
52
+ ("make-make example expressions can be nested"
53
+ (*eleven (*five (+seven 2)))
54
+ 495))))
@@ -0,0 +1,9 @@
1
+ class Nydp::Builtin::LoadTests
2
+ def initialize ns
3
+ @ns = ns
4
+ end
5
+ def invoke vm, args
6
+ Nydp.loadall vm, @ns, Nydp.testfiles
7
+ vm.push_arg Nydp.NIL
8
+ end
9
+ end
@@ -13,9 +13,3 @@ class Nydp::Builtin::ToSym
13
13
  vm.push_arg val
14
14
  end
15
15
  end
16
-
17
-
18
-
19
-
20
-
21
-
data/lib/nydp/core.rb CHANGED
@@ -45,6 +45,7 @@ module Nydp
45
45
  Symbol.mk(:inspect, ns).assign(Nydp::Builtin::Inspect.new)
46
46
  Symbol.mk(:comment, ns).assign(Nydp::Builtin::Comment.new)
47
47
  Symbol.mk(:millisecs, ns).assign(Nydp::Builtin::Millisecs.new)
48
+ Symbol.mk("load-tests", ns).assign(Nydp::Builtin::LoadTests.new(ns))
48
49
  Symbol.mk("random-string",ns).assign(Nydp::Builtin::RandomString.new)
49
50
  Symbol.mk("to-string", ns).assign(Nydp::Builtin::ToString.new)
50
51
  Symbol.mk("string-pieces",ns).assign(Nydp::Builtin::StringPieces.new)
data/lib/nydp/literal.rb CHANGED
@@ -18,8 +18,9 @@ module Nydp
18
18
  vm.push_arg expression
19
19
  end
20
20
 
21
- def inspect; @expression.inspect; end
22
- def to_s ; @expression.to_s ; end
21
+ def nydp_type ; :literal ; end
22
+ def inspect ; @expression.inspect ; end
23
+ def to_s ; @expression.to_s ; end
23
24
 
24
25
  def coerce _
25
26
  [_, expression]
data/lib/nydp/pair.rb CHANGED
@@ -66,6 +66,24 @@ class Nydp::Pair
66
66
  else
67
67
  "'#{cdr.to_s}"
68
68
  end
69
+ elsif car.is_a?(Nydp::Symbol) && car.is?(:quasiquote)
70
+ if Nydp.NIL.is? cdr.cdr
71
+ "`#{cdr.car.to_s}"
72
+ else
73
+ "`#{cdr.to_s}"
74
+ end
75
+ elsif car.is_a?(Nydp::Symbol) && car.is?(:unquote)
76
+ if Nydp.NIL.is? cdr.cdr
77
+ ",#{cdr.car.to_s}"
78
+ else
79
+ ",#{cdr.to_s}"
80
+ end
81
+ elsif car.is_a?(Nydp::Symbol) && car.is?(:"unquote-splicing")
82
+ if Nydp.NIL.is? cdr.cdr
83
+ ",@#{cdr.car.to_s}"
84
+ else
85
+ ",@#{cdr.to_s}"
86
+ end
69
87
  else
70
88
  "(#{to_s_rest})"
71
89
  end
@@ -95,13 +113,13 @@ class Nydp::Pair
95
113
  [car.inspect, cdr_s].compact.join " "
96
114
  end
97
115
 
98
- def append pair
116
+ def append thing
99
117
  if Nydp.NIL.is? self.cdr
100
- self.cdr = pair
118
+ self.cdr = thing
101
119
  elsif pair? self.cdr
102
- self.cdr.append pair
120
+ self.cdr.append thing
103
121
  else
104
- raise "can't append #{pair} to list #{self} : cdr is #{self.cdr.inspect}"
122
+ raise "can't append #{thing} to list #{self} : cdr is #{self.cdr.inspect}"
105
123
  end
106
124
  self
107
125
  end
data/lib/nydp/parser.rb CHANGED
@@ -20,15 +20,16 @@ module Nydp
20
20
  end
21
21
 
22
22
  def prefix_list prefix, list
23
+ return list if prefix == ''
23
24
  case prefix
24
- when "'"
25
- Pair.from_list [sym(:quote), list]
26
- when "`"
27
- Pair.from_list [sym(:quasiquote), list]
28
- when ","
29
- Pair.from_list [sym(:unquote), list]
30
- when ",@"
31
- Pair.from_list [sym(:"unquote-splicing"), list]
25
+ when /^(.*)'$/
26
+ prefix_list $1, Pair.from_list([sym(:quote), list])
27
+ when /^(.*)`$/
28
+ prefix_list $1, Pair.from_list([sym(:quasiquote), list])
29
+ when /^(.*),$/
30
+ prefix_list $1, Pair.from_list([sym(:unquote), list])
31
+ when /^(.*),@$/
32
+ prefix_list $1, Pair.from_list([sym(:"unquote-splicing"), list])
32
33
  else
33
34
  list
34
35
  end
@@ -40,6 +41,10 @@ module Nydp
40
41
 
41
42
  def parse_symbol txt
42
43
  case txt
44
+ when /^[-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?$/
45
+ txt.to_f
46
+ when /^[-+]?[0-9]+$/
47
+ txt.to_i
43
48
  when /^'(.+)$/
44
49
  Pair.from_list [sym(:quote), parse_symbol($1)]
45
50
  when /^`(.+)$/
data/lib/nydp/symbol.rb CHANGED
@@ -29,8 +29,9 @@ class Nydp::Symbol
29
29
  ns[name.to_sym]
30
30
  end
31
31
 
32
- def inspect ; to_s ; end
33
- def to_s ; name.to_s ; end
32
+ def nydp_type ; :symbol ; end
33
+ def inspect ; to_s ; end
34
+ def to_s ; name.to_s ; end
34
35
 
35
36
  def == other
36
37
  other.is_a?(Nydp::Symbol) && (self.name == other.name)
data/lib/nydp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nydp
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/nydp/vm.rb CHANGED
@@ -54,7 +54,7 @@ module Nydp
54
54
  msg << "\ncontext stack"
55
55
  msg << "\n================="
56
56
  contexts.each_with_index do |ctx, ix|
57
- msg << "\ncontext##{ix} : #{ctx}"
57
+ msg << "\ncontext##{ix} :\n#{ctx}"
58
58
  end
59
59
  msg << "\n"
60
60
  msg << "\n"
data/spec/parser_spec.rb CHANGED
@@ -124,6 +124,18 @@ describe Nydp::Parser do
124
124
  expect(parse "foo.bar").to eq pair_list([dotsyn, foo, bar])
125
125
  end
126
126
 
127
+ it "should spot numbers hiding in special syntax" do
128
+ parsed = parse("foo.2:3:4")
129
+ expect(parsed.inspect).to eq "(dot-syntax foo (colon-syntax 2 3 4))"
130
+
131
+ numbers = parsed.cdr.cdr.car.cdr
132
+ two = numbers.car
133
+ three = numbers.cdr.car
134
+ four = numbers.cdr.cdr.car
135
+
136
+ expect([two, three, four].map &:class).to eq [Fixnum, Fixnum, Fixnum]
137
+ end
138
+
127
139
  it "should parse a dotted symbol" do
128
140
  expect(parse "(list a b foo.bar c)").to eq pair_list([sym(:list), a, b, pair_list([dotsyn, foo, bar]), c])
129
141
  end
@@ -140,10 +152,32 @@ describe Nydp::Parser do
140
152
  expect(parse "'foo").to eq pair_list([quote, foo])
141
153
  end
142
154
 
155
+ it "should not let quote decorations trick it into thinking numbers are symbols" do
156
+ two = parse("'2").cdr.car
157
+ expect(two.class).to eq 2.class
158
+ expect(two).to eq 2
159
+ end
160
+
161
+ it "should not let unquote decorations trick it into thinking numbers are symbols" do
162
+ two = parse(",2").cdr.car
163
+ expect(two.class).to eq 2.class
164
+ expect(two).to eq 2
165
+ end
166
+
167
+ it "should not let unquote-splicing decorations trick it into thinking numbers are symbols" do
168
+ two = parse(",@2").cdr.car
169
+ expect(two.class).to eq 2.class
170
+ expect(two).to eq 2
171
+ end
172
+
143
173
  it "should quote-unquote symbols" do
144
174
  expect(parse "',foo").to eq pair_list([quote, pair_list([unquote, foo])])
145
175
  end
146
176
 
177
+ it "should unquote-unquote_splicing symbols" do
178
+ expect(parse(",,@foo").inspect).to eq "(unquote (unquote-splicing foo))"
179
+ end
180
+
147
181
  it "should quote lists" do
148
182
  expect(parse "'(foo)").to eq pair_list([quote, pair_list([foo])])
149
183
  end
@@ -156,6 +190,24 @@ describe Nydp::Parser do
156
190
  expect(parse ",(bar)").to eq pair_list([unquote, pair_list([bar])])
157
191
  end
158
192
 
193
+ it "should do some complicated unquote stuff with lists" do
194
+ expect(parse("`(a b `(c d ,(+ 1 2) ,,(+ 3 4)))").inspect).to eq "(quasiquote (a b (quasiquote (c d (unquote (+ 1 2)) (unquote (unquote (+ 3 4)))))))"
195
+ end
196
+
197
+ it "should do some complicated unquote stuff with mixed lists and symbols" do
198
+ expect(parse("`(a b `(c d ,,@foo e f))").inspect).to eq "(quasiquote (a b (quasiquote (c d (unquote (unquote-splicing foo)) e f))))"
199
+ expect(parse("`(a b `(c d ,@,foo e f))").inspect).to eq "(quasiquote (a b (quasiquote (c d (unquote-splicing (unquote foo)) e f))))"
200
+ expect(parse("`(a b `(c d ,',foo e f))").inspect).to eq "(quasiquote (a b (quasiquote (c d (unquote (quote (unquote foo))) e f))))"
201
+ end
202
+
203
+ it "should do some complicated unquote stuff with lists" do
204
+ expect(parse("`(a b `(c d ,(+ 1 2) ,,@(list 3 4)))").inspect).to eq "(quasiquote (a b (quasiquote (c d (unquote (+ 1 2)) (unquote (unquote-splicing (list 3 4)))))))"
205
+ end
206
+
207
+ it "should do some complicated unquote stuff with symbols" do
208
+ expect(parse("`(a b `(c d ,(+ 1 2) ,,x))").inspect).to eq "(quasiquote (a b (quasiquote (c d (unquote (+ 1 2)) (unquote (unquote x))))))"
209
+ end
210
+
159
211
  it "should unquote-splicing atoms" do
160
212
  expect(parse ",@foo").to eq pair_list([unquote_splicing, foo])
161
213
  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.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-17 00:00:00.000000000 Z
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,7 @@ files:
85
85
  - bin/nydp-tests
86
86
  - lib/lisp/boot.nydp
87
87
  - lib/lisp/test-runner.nydp
88
+ - lib/lisp/tests/boot-tests.nydp
88
89
  - lib/lisp/tests/foundation-test.nydp
89
90
  - lib/nydp.rb
90
91
  - lib/nydp/assignment.rb
@@ -103,6 +104,7 @@ files:
103
104
  - lib/nydp/builtin/inspect.rb
104
105
  - lib/nydp/builtin/is_equal.rb
105
106
  - lib/nydp/builtin/less_than.rb
107
+ - lib/nydp/builtin/load_tests.rb
106
108
  - lib/nydp/builtin/millisecs.rb
107
109
  - lib/nydp/builtin/minus.rb
108
110
  - lib/nydp/builtin/plus.rb