nydp 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +1 -0
- data/bin/nydp +5 -0
- data/bin/nydp-tests +5 -0
- data/lib/lisp/boot.nydp +219 -0
- data/lib/lisp/test-runner.nydp +39 -0
- data/lib/lisp/tests/foundation-test.nydp +28 -0
- data/lib/nydp.rb +143 -0
- data/lib/nydp/assignment.rb +40 -0
- data/lib/nydp/builtin.rb +8 -0
- data/lib/nydp/builtin/apply.rb +16 -0
- data/lib/nydp/builtin/car.rb +5 -0
- data/lib/nydp/builtin/cdr.rb +5 -0
- data/lib/nydp/builtin/cdr_set.rb +8 -0
- data/lib/nydp/builtin/comment.rb +5 -0
- data/lib/nydp/builtin/cons.rb +16 -0
- data/lib/nydp/builtin/divide.rb +13 -0
- data/lib/nydp/builtin/error.rb +6 -0
- data/lib/nydp/builtin/eval.rb +14 -0
- data/lib/nydp/builtin/greater_than.rb +10 -0
- data/lib/nydp/builtin/hash.rb +30 -0
- data/lib/nydp/builtin/inspect.rb +5 -0
- data/lib/nydp/builtin/is_equal.rb +5 -0
- data/lib/nydp/builtin/less_than.rb +10 -0
- data/lib/nydp/builtin/millisecs.rb +7 -0
- data/lib/nydp/builtin/minus.rb +13 -0
- data/lib/nydp/builtin/plus.rb +28 -0
- data/lib/nydp/builtin/pre_compile.rb +5 -0
- data/lib/nydp/builtin/puts.rb +7 -0
- data/lib/nydp/builtin/quit.rb +5 -0
- data/lib/nydp/builtin/random_string.rb +11 -0
- data/lib/nydp/builtin/times.rb +13 -0
- data/lib/nydp/builtin/to_string.rb +12 -0
- data/lib/nydp/builtin/to_sym.rb +21 -0
- data/lib/nydp/builtin/vm_info.rb +13 -0
- data/lib/nydp/closure.rb +17 -0
- data/lib/nydp/compiler.rb +49 -0
- data/lib/nydp/cond.rb +56 -0
- data/lib/nydp/context_symbol.rb +22 -0
- data/lib/nydp/error.rb +4 -0
- data/lib/nydp/function_invocation.rb +52 -0
- data/lib/nydp/helper.rb +32 -0
- data/lib/nydp/interpreted_function.rb +79 -0
- data/lib/nydp/lexical_context.rb +38 -0
- data/lib/nydp/literal.rb +40 -0
- data/lib/nydp/pair.rb +112 -0
- data/lib/nydp/parser.rb +123 -0
- data/lib/nydp/string_atom.rb +24 -0
- data/lib/nydp/string_token.rb +21 -0
- data/lib/nydp/symbol.rb +47 -0
- data/lib/nydp/symbol_lookup.rb +43 -0
- data/lib/nydp/tokeniser.rb +80 -0
- data/lib/nydp/truth.rb +37 -0
- data/lib/nydp/version.rb +3 -0
- data/lib/nydp/vm.rb +76 -0
- data/nydp.gemspec +27 -0
- data/spec/boot_spec.rb +119 -0
- data/spec/embedded_spec.rb +106 -0
- data/spec/nypd_spec.rb +127 -0
- data/spec/pair_spec.rb +102 -0
- data/spec/parser_spec.rb +191 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/symbol_spec.rb +32 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 070df0ab7e597dbbcb8e80aca7f4c6dbc444d0e9
|
4
|
+
data.tar.gz: a772c25f02d36b78842698bd798704dda414490c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50400bf553e7bf65c4514f7922c0ab57f2a31ea7971f2626b45e55747bdc1994172dbae3a111350ce3a33d9933653845b19654099309991f164dae1e81f611a9
|
7
|
+
data.tar.gz: cf5d1475f6c57fd4dde96969ca0b469e3a1a3b9f8a338d1af237931b88d4e1eaab9ad49da6cc69872dbda56afe90f40f9468ba829b6290b10d42c7a1160a6684
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 conanite
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Nydp
|
2
|
+
|
3
|
+
NYDP is "Not Your Daddy's Parentheses", a reference to [Xkcd 297](http://xkcd.com/297/) (itself a reference
|
4
|
+
to Star Wars), as well as to the meme [Not Your Daddy's Q](http://tvtropes.org/pmwiki/pmwiki.php/Main/NotYourDaddysX), where Q is a modern,
|
5
|
+
improved Q unlike the Q your daddy used. "NYDP" also shamelessly piggypacks on the
|
6
|
+
catchiness and popularity of the [NYPD](https://en.wikipedia.org/wiki/NYPD_Blue) abbreviation ("New York Police Department",
|
7
|
+
for those who have no interest in popular US TV or authoritarian politics).
|
8
|
+
|
9
|
+
Macro-expansion is not built-in to the interpreter; however, the compiler will invoke 'pre-compile before compiling, passing
|
10
|
+
the expression to compile as an argument. You can override 'pre-compile to transform the expression in any way you wish. By default,
|
11
|
+
nydp provides an implementation of 'pre-compile that performs macro-expansion.
|
12
|
+
|
13
|
+
```
|
14
|
+
(def pre-compile (expr)
|
15
|
+
(map pre-compile
|
16
|
+
(if (mac-names (car expr))
|
17
|
+
(pre-compile (mac-expand (car expr) (cdr expr)))
|
18
|
+
expr)))
|
19
|
+
```
|
20
|
+
|
21
|
+
; blah blah
|
22
|
+
|
23
|
+
```
|
24
|
+
==> (comment "blah blah")
|
25
|
+
|
26
|
+
==> (mac comment (txt) nil)
|
27
|
+
```
|
28
|
+
|
29
|
+
We do not wish to suggest by "Not Your Daddy's Parentheses" that Common Lisp,
|
30
|
+
Scheme, Racket, Arc, Clojure or your favourite other lisp are somehow
|
31
|
+
old-fashioned, inferior, or in need of improvement in any way.
|
32
|
+
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
gem 'nydp'
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install nydp
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
TODO: Write usage instructions here
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/nydp
ADDED
data/bin/nydp-tests
ADDED
data/lib/lisp/boot.nydp
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
; -*- lisp -*-
|
2
|
+
|
3
|
+
(assign last-cons (fn (xs)
|
4
|
+
(cond (pair? (cdr xs))
|
5
|
+
(last-cons (cdr xs))
|
6
|
+
xs)))
|
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
|
+
|
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))
|
19
|
+
|
20
|
+
(assign mac-expand (fn (macfn name body)
|
21
|
+
(cond macfn
|
22
|
+
(pre-compile-do (apply macfn body))
|
23
|
+
(cons name body))))
|
24
|
+
|
25
|
+
(assign macs (hash))
|
26
|
+
|
27
|
+
(assign debug-pre-compile
|
28
|
+
(fn (arg)
|
29
|
+
(assign *debug-pre-compile* arg)))
|
30
|
+
|
31
|
+
(assign pre-compile-expr
|
32
|
+
(fn (name body)
|
33
|
+
(mac-expand (hash-get macs name) name body)))
|
34
|
+
|
35
|
+
(assign pre-compile-each
|
36
|
+
(fn (args)
|
37
|
+
(cond args
|
38
|
+
(cond (pair? args)
|
39
|
+
(cons (pre-compile-do (car args))
|
40
|
+
(pre-compile-each (cdr args)))
|
41
|
+
args))))
|
42
|
+
|
43
|
+
(assign pre-compile-msg
|
44
|
+
(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)))
|
54
|
+
|
55
|
+
(assign pre-compile
|
56
|
+
(fn (arg)
|
57
|
+
(pre-compile-msg "pre-compile" arg)
|
58
|
+
(pre-compile-msg (pre-compile-do arg))))
|
59
|
+
|
60
|
+
(hash-set macs 'def
|
61
|
+
(fn (name args . body)
|
62
|
+
(list 'assign
|
63
|
+
name
|
64
|
+
(+ (list 'fn args)
|
65
|
+
body))))
|
66
|
+
|
67
|
+
(def qq-unquote-recurse (arg rest)
|
68
|
+
(list 'cons
|
69
|
+
(qq-quasiquote arg)
|
70
|
+
(qq-quasiquote rest)))
|
71
|
+
|
72
|
+
(def qq-unquote? (arg rest)
|
73
|
+
(cond (pair? arg)
|
74
|
+
(cond (eq? (car arg) 'unquote)
|
75
|
+
(list 'cons (cadr arg) (qq-quasiquote rest))
|
76
|
+
(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)))
|
98
|
+
|
99
|
+
(hash-set macs 'mac (fn (name args . body)
|
100
|
+
`(hash-set macs ',name (fn ,args ,@body))))
|
101
|
+
|
102
|
+
(mac if args
|
103
|
+
(cond (no args) nil
|
104
|
+
(cond (cdr args)
|
105
|
+
(cond (cddr args)
|
106
|
+
`(cond ,(car args) ,(cadr args) (if ,@(cddr args)))
|
107
|
+
`(cond ,(car args) ,(cadr args)))
|
108
|
+
(car args))))
|
109
|
+
|
110
|
+
(mac and args
|
111
|
+
(if args
|
112
|
+
(if (cdr args)
|
113
|
+
`(if ,(car args) (and ,@(cdr args)))
|
114
|
+
(car args))
|
115
|
+
't))
|
116
|
+
|
117
|
+
(mac do args
|
118
|
+
`((fn nil ,@args)))
|
119
|
+
|
120
|
+
(mac when (condition . body)
|
121
|
+
`(cond ,condition (do ,@body)))
|
122
|
+
|
123
|
+
(def map (f things)
|
124
|
+
(if (no things)
|
125
|
+
nil
|
126
|
+
(pair? things)
|
127
|
+
(cons (f (car things)) (map f (cdr things)))
|
128
|
+
(map f (list things))))
|
129
|
+
|
130
|
+
(def pairs (things)
|
131
|
+
(if (no things) nil
|
132
|
+
(no (cdr things)) (list (list (car things)))
|
133
|
+
(cons (list (car things) (cadr things))
|
134
|
+
(pairs (cddr things)))))
|
135
|
+
|
136
|
+
(mac with (parms . body)
|
137
|
+
`((fn ,(map car (pairs parms))
|
138
|
+
,@body)
|
139
|
+
,@(map cadr (pairs parms))))
|
140
|
+
|
141
|
+
(mac let (var val . body)
|
142
|
+
`(with (,var ,val) ,@body))
|
143
|
+
|
144
|
+
;; (debug-pre-compile t)
|
145
|
+
;; (let a b c d e)
|
146
|
+
;; (debug-pre-compile nil)
|
147
|
+
|
148
|
+
(def each (f acc things)
|
149
|
+
(if things
|
150
|
+
(each f (f acc (car things)) (cdr things))
|
151
|
+
acc))
|
152
|
+
|
153
|
+
(def eachr (f things)
|
154
|
+
(when things
|
155
|
+
(eachr f (cdr things))
|
156
|
+
(f (car things))))
|
157
|
+
|
158
|
+
(def zip (a b)
|
159
|
+
(if a
|
160
|
+
(cons (list (car a) (car b))
|
161
|
+
(zip (cdr a) (cdr b)))))
|
162
|
+
|
163
|
+
(mac push (x things)
|
164
|
+
`(assign ,things (cons ,x ,things)))
|
165
|
+
|
166
|
+
(def flatten (things)
|
167
|
+
(let acc nil
|
168
|
+
(let flattenize nil
|
169
|
+
(assign flattenize (fn (x)
|
170
|
+
(if (pair? x)
|
171
|
+
(eachr flattenize x)
|
172
|
+
(push x acc))))
|
173
|
+
(flattenize things))
|
174
|
+
acc))
|
175
|
+
|
176
|
+
(def joinstr (txt things)
|
177
|
+
(apply +
|
178
|
+
(to-string (car things))
|
179
|
+
(flatten (zip (map (fn (_) txt) (cdr things))
|
180
|
+
(map to-string (cdr things))))))
|
181
|
+
|
182
|
+
(let uniq-counter 0
|
183
|
+
(def uniq (prefix)
|
184
|
+
(sym (joinstr "-"
|
185
|
+
(list prefix
|
186
|
+
(assign uniq-counter
|
187
|
+
(+ uniq-counter 1))))))
|
188
|
+
(def reset-uniq-counter ()
|
189
|
+
(assign uniq-counter 0)))
|
190
|
+
|
191
|
+
(mac w/uniq (vars . body)
|
192
|
+
(if (pair? vars)
|
193
|
+
`(with ,(apply + (map (fn (n) (list n '(uniq ',n))) vars))
|
194
|
+
,@body)
|
195
|
+
`(let ,vars (uniq ',vars) ,@body)))
|
196
|
+
|
197
|
+
(mac or args
|
198
|
+
(cond args
|
199
|
+
(w/uniq ora
|
200
|
+
`(let ,ora ,(car args)
|
201
|
+
(cond ,ora ,ora (or ,@(cdr args)))))))
|
202
|
+
|
203
|
+
(mac pop (xs)
|
204
|
+
(w/uniq gp
|
205
|
+
`(let ,gp (car ,xs)
|
206
|
+
(assign ,xs (cdr ,xs))
|
207
|
+
,gp)))
|
208
|
+
|
209
|
+
(def nth (n things)
|
210
|
+
(if (eq? n 0)
|
211
|
+
(car things)
|
212
|
+
(nth (- n 1) (cdr things))))
|
213
|
+
|
214
|
+
(def iso (x y)
|
215
|
+
(or (eq? x y)
|
216
|
+
(and (pair? x)
|
217
|
+
(pair? y)
|
218
|
+
(iso (car x) (car y))
|
219
|
+
(iso (cdr x) (cdr y)))))
|
@@ -0,0 +1,39 @@
|
|
1
|
+
(assign show-failed-only t)
|
2
|
+
(assign all-tests nil)
|
3
|
+
|
4
|
+
(def register-test (test)
|
5
|
+
"register a test to be run later by 'run-all-tests"
|
6
|
+
(push test all-tests))
|
7
|
+
|
8
|
+
(def run-all-tests ()
|
9
|
+
"runs all tests that have been registered with 'register-test"
|
10
|
+
(with (passed 0 failed 0)
|
11
|
+
(with (f-pass (fn nil (assign passed (+ 1 passed)))
|
12
|
+
f-fail (fn nil (assign failed (+ 1 failed))))
|
13
|
+
(run-tests `(suite "all tests" ,@all-tests) f-pass f-fail)
|
14
|
+
(p "passed: " passed)
|
15
|
+
(p "failed: " failed)
|
16
|
+
(/ passed (+ passed failed)))))
|
17
|
+
|
18
|
+
(def run-tests (tests passf failf)
|
19
|
+
(execute-test "" tests passf failf)
|
20
|
+
results)
|
21
|
+
|
22
|
+
(def execute-test (desc test passf failf)
|
23
|
+
(if (eq? 'suite (car test))
|
24
|
+
(execute-tests (+ desc " - " (cadr test)) (cddr test) passf failf)
|
25
|
+
(execute-single-test desc test passf failf)))
|
26
|
+
|
27
|
+
(def execute-single-test (desc test passf failf)
|
28
|
+
(with (expected (nth 2 test) result (eval (nth 1 test)))
|
29
|
+
(if (iso result expected)
|
30
|
+
(passf)
|
31
|
+
(do (p desc " - " (car test) " - FAILED:
|
32
|
+
expected " (inspect expected) ",
|
33
|
+
got " (inspect result) "
|
34
|
+
")
|
35
|
+
(failf)))))
|
36
|
+
|
37
|
+
(def execute-tests (desc tests passf failf)
|
38
|
+
(execute-test desc (car tests) passf failf)
|
39
|
+
(if (cdr tests) (execute-tests desc (cdr tests) passf failf)))
|
@@ -0,0 +1,28 @@
|
|
1
|
+
(register-test '(suite "Foundation Tests"
|
2
|
+
(suite "Lists"
|
3
|
+
(suite "cons"
|
4
|
+
("cons creates a list"
|
5
|
+
(cons 'a '(b c))
|
6
|
+
(a b c))
|
7
|
+
|
8
|
+
("cons conses two strings"
|
9
|
+
(cons "a" "b")
|
10
|
+
("a" . "b")))
|
11
|
+
|
12
|
+
(suite "car"
|
13
|
+
("car of nil is nil"
|
14
|
+
(car nil)
|
15
|
+
nil)
|
16
|
+
|
17
|
+
("car of empty list is nil"
|
18
|
+
(car '())
|
19
|
+
nil)
|
20
|
+
|
21
|
+
("car - no need to quote empty list"
|
22
|
+
(car ())
|
23
|
+
nil)
|
24
|
+
|
25
|
+
("car returns car of argument"
|
26
|
+
(car '(foo 12.34 "bar"))
|
27
|
+
foo)
|
28
|
+
))))
|
data/lib/nydp.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
module Nydp
|
2
|
+
def self.compile_and_eval vm, expr
|
3
|
+
vm.thread Pair.new(Compiler.compile(expr, Nydp.NIL), Nydp.NIL)
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.setup ns
|
7
|
+
Symbol.mk(:cons, ns).assign(Nydp::Builtin::Cons.new)
|
8
|
+
Symbol.mk(:car, ns).assign(Nydp::Builtin::Car.new)
|
9
|
+
Symbol.mk(:cdr, ns).assign(Nydp::Builtin::Cdr.new)
|
10
|
+
Symbol.mk(:+, ns).assign(Nydp::Builtin::Plus.new)
|
11
|
+
Symbol.mk(:-, ns).assign(Nydp::Builtin::Minus.new)
|
12
|
+
Symbol.mk(:*, ns).assign(Nydp::Builtin::Times.new)
|
13
|
+
Symbol.mk(:/, ns).assign(Nydp::Builtin::Divide.new)
|
14
|
+
Symbol.mk(:>, ns).assign(Nydp::Builtin::GreaterThan.new)
|
15
|
+
Symbol.mk(:<, ns).assign(Nydp::Builtin::LessThan.new)
|
16
|
+
Symbol.mk(:eval, ns).assign(Nydp::Builtin::Eval.new(ns))
|
17
|
+
Symbol.mk(:hash, ns).assign(Nydp::Builtin::Hash.new)
|
18
|
+
Symbol.mk(:apply, ns).assign(Nydp::Builtin::Apply.new)
|
19
|
+
Symbol.mk(:error, ns).assign(Nydp::Builtin::Error.new)
|
20
|
+
Symbol.mk(:quit, ns).assign(Nydp::Builtin::Quit.new)
|
21
|
+
Symbol.mk(:p, ns).assign(Nydp::Builtin::Puts.new)
|
22
|
+
Symbol.mk(:PI, ns).assign Literal.new(3.1415)
|
23
|
+
Symbol.mk(:nil, ns).assign Nydp.NIL
|
24
|
+
Symbol.mk(:t, ns).assign Nydp.T
|
25
|
+
Symbol.mk(:sym, ns).assign Nydp::Builtin::ToSym.new(ns)
|
26
|
+
Symbol.mk(:inspect, ns).assign(Nydp::Builtin::Inspect.new)
|
27
|
+
Symbol.mk(:comment, ns).assign(Nydp::Builtin::Comment.new)
|
28
|
+
Symbol.mk(:millisecs, ns).assign(Nydp::Builtin::Millisecs.new)
|
29
|
+
Symbol.mk("random-string",ns).assign(Nydp::Builtin::RandomString.new)
|
30
|
+
Symbol.mk("to-string", ns).assign(Nydp::Builtin::ToString.new)
|
31
|
+
Symbol.mk(:"eq?", ns).assign(Nydp::Builtin::IsEqual.new)
|
32
|
+
Symbol.mk(:"pair?", ns).assign(Nydp::Builtin::IsPair.new)
|
33
|
+
Symbol.mk(:"cdr-set", ns).assign(Nydp::Builtin::CdrSet.new)
|
34
|
+
Symbol.mk(:"hash-get", ns).assign(Nydp::Builtin::HashGet.new)
|
35
|
+
Symbol.mk(:"hash-set", ns).assign(Nydp::Builtin::HashSet.new)
|
36
|
+
Symbol.mk(:"hash-keys", ns).assign(Nydp::Builtin::HashKeys.new)
|
37
|
+
Symbol.mk(:"vm-info", ns).assign Nydp::Builtin::VmInfo.new
|
38
|
+
Symbol.mk(:"pre-compile", ns).assign Nydp::Builtin::PreCompile.new
|
39
|
+
end
|
40
|
+
|
41
|
+
class Runner
|
42
|
+
attr_accessor :vm, :ns
|
43
|
+
|
44
|
+
def initialize vm, ns
|
45
|
+
@vm = vm
|
46
|
+
@ns = ns
|
47
|
+
@precompile = Symbol.mk(:"pre-compile", ns)
|
48
|
+
@quote = Symbol.mk(:quote, ns)
|
49
|
+
end
|
50
|
+
|
51
|
+
def quote expr
|
52
|
+
Pair.from_list [@quote, expr]
|
53
|
+
end
|
54
|
+
|
55
|
+
def precompile expr
|
56
|
+
Pair.from_list [@precompile, quote(expr)]
|
57
|
+
end
|
58
|
+
|
59
|
+
def pre_compile expr
|
60
|
+
Nydp.compile_and_eval(vm, precompile(expr))
|
61
|
+
end
|
62
|
+
|
63
|
+
def evaluate expr
|
64
|
+
Nydp.compile_and_eval(vm, pre_compile(expr))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class StreamRunner < Runner
|
69
|
+
attr_accessor :stream, :parser
|
70
|
+
|
71
|
+
def initialize vm, ns, stream
|
72
|
+
super vm, ns
|
73
|
+
@stream = stream
|
74
|
+
@parser = Nydp::Parser.new(ns)
|
75
|
+
@tokens = Nydp::Tokeniser.new stream
|
76
|
+
end
|
77
|
+
|
78
|
+
def prompt *_
|
79
|
+
end
|
80
|
+
|
81
|
+
def run
|
82
|
+
res = Nydp.NIL
|
83
|
+
prompt
|
84
|
+
while !@tokens.finished
|
85
|
+
expr = parser.expression(@tokens)
|
86
|
+
unless expr.nil?
|
87
|
+
res = evaluate expr
|
88
|
+
prompt res
|
89
|
+
end
|
90
|
+
end
|
91
|
+
res
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class Repl < StreamRunner
|
96
|
+
def prompt val=nil
|
97
|
+
puts val if val
|
98
|
+
print "nydp > "
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.repl
|
103
|
+
puts "welcome to nydp"
|
104
|
+
root_ns = { }
|
105
|
+
setup(root_ns)
|
106
|
+
vm = VM.new
|
107
|
+
boot_path = File.join File.expand_path(File.dirname(__FILE__)), 'lisp/boot.nydp'
|
108
|
+
StreamRunner.new(vm, root_ns, File.new(boot_path)).run
|
109
|
+
Repl.new(vm, root_ns, $stdin).run
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.tests
|
113
|
+
puts "welcome to nydp : running tests"
|
114
|
+
root_ns = { }
|
115
|
+
setup(root_ns)
|
116
|
+
vm = VM.new
|
117
|
+
boot_path = File.join File.expand_path(File.dirname(__FILE__)), 'lisp/boot.nydp'
|
118
|
+
test_runner_path = File.join File.expand_path(File.dirname(__FILE__)), 'lisp/test-runner.nydp'
|
119
|
+
tests = Dir.glob(File.join File.expand_path(File.dirname(__FILE__)), 'lisp/tests/**/*.nydp')
|
120
|
+
StreamRunner.new(vm, root_ns, File.new(boot_path)).run
|
121
|
+
StreamRunner.new(vm, root_ns, File.new(test_runner_path)).run
|
122
|
+
tests.each do |tst|
|
123
|
+
StreamRunner.new(vm, root_ns, File.new(tst)).run
|
124
|
+
end
|
125
|
+
StreamRunner.new(vm, root_ns, "(run-all-tests)").run
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
require "nydp/error"
|
130
|
+
require "nydp/truth"
|
131
|
+
require "nydp/version"
|
132
|
+
require "nydp/helper"
|
133
|
+
require "nydp/symbol"
|
134
|
+
require "nydp/symbol_lookup"
|
135
|
+
require "nydp/pair"
|
136
|
+
require "nydp/assignment"
|
137
|
+
require "nydp/builtin"
|
138
|
+
require "nydp/string_atom"
|
139
|
+
require "nydp/string_token"
|
140
|
+
require "nydp/tokeniser"
|
141
|
+
require "nydp/parser"
|
142
|
+
require "nydp/compiler"
|
143
|
+
require "nydp/vm"
|