on master help sign in
Merge pull request #12 from kohanyirobert/patch-1

Nock interpreter in Scheme

commiteac6ea5715d3d9a34493d0b9f9e51f4c385825be authorGalen Wolfe-Pauly <galen@office-for.com> dateThu 2 Jun 2016, 14:58 (10 years ago) parent091776bcede971
3 files changed+214−33
docs/nock/implementations/groovy.md+85−32 docs/nock/implementations/hoon.md+1−1 docs/nock/implementations/scheme.md+128
docs/nock/implementations/groovy.md+85−32
@@ -10,6 +10,7 @@ next: true
1010From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
1111
1212```
13+@Memoized
1314def i(def a) {
1415 a.class in [
1516 byte, Byte,
@@ -21,28 +22,40 @@ def i(def a) {
2122 ] && a >= 0
2223}
2324
25+@Memoized
2426def n(def a) {
27+ def r
28+ n(a, { r = it })
29+ r
30+}
31+
32+@TailRecursive
33+def n(def a, def r) {
2534 if (a in List) {
2635 if (a.size() == 1) {
27− n(a[0])
28− } else if (a.size() == 2) {
29− [n(a[0]), n(a[1])]
30− } else if (a.size() > 2) {
31− [n(a[0]), n(a.tail())]
36+ r(a[0])
37+ } else if (a.size() >= 2) {
38+ n(a[0], { t ->
39+ n(a.size() == 2 ? a[1] : a.tail(), { h ->
40+ r([t, h])
41+ })
42+ })
3243 } else {
3344 throw new IllegalStateException()
3445 }
3546 } else if (i(a)) {
36− (BigInteger) a
47+ r((BigInteger) a)
3748 } else {
3849 throw new IllegalStateException()
3950 }
4051}
4152
53+@Memoized
4254def wut(def a) {
4355 i(a) ? 1 : 0
4456}
4557
58+@Memoized
4659def lus(def a) {
4760 if (wut(a) == 0) {
4861 throw new IllegalStateException()
@@ -50,6 +63,7 @@ def lus(def a) {
5063 1 + a
5164}
5265
66+@Memoized
5367def tis(def a) {
5468 if (wut(a) == 1) {
5569 throw new IllegalStateException()
@@ -57,7 +71,15 @@ def tis(def a) {
5771 a[0] == a[1] ? 0 : 1
5872}
5973
74+@Memoized
6075def fas(def a) {
76+ def r
77+ fas(a, { r = it })
78+ r
79+}
80+
81+@TailRecursive
82+def fas(def a, def r) {
6183 if (wut(a) == 1) {
6284 throw new IllegalStateException()
6385 }
@@ -69,27 +91,34 @@ def fas(def a) {
6991 if (h == 0) {
7092 throw new IllegalStateException()
7193 } else if (h == 1) {
72− t
94+ r(t)
7395 } else {
7496 if (i(t)) {
7597 throw new IllegalStateException()
7698 }
7799 if (h == 2) {
78− t[0]
100+ r(t[0])
79101 } else if (h == 3) {
80− t[1]
102+ r(t[1])
81103 } else {
82− def x = h.intdiv(2)
83− if (h.mod(2) == 0) {
84− fas([2, fas([x, t])])
85− } else {
86− fas([3, fas([x, t])])
87− }
104+ fas([h.intdiv(2), t], { p ->
105+ fas([2 + h.mod(2), p], { q ->
106+ r(q)
107+ })
108+ })
88109 }
89110 }
90111}
91112
113+@Memoized
92114def tar(def a) {
115+ def r
116+ tar(a, { r = it})
117+ r
118+}
119+
120+@TailRecursive
121+def tar(def a, def r) {
93122 if (wut(a) == 1) {
94123 throw new IllegalStateException()
95124 }
@@ -101,18 +130,28 @@ def tar(def a) {
101130 def o = f[0]
102131 def v = f[1]
103132 if (wut(o) == 0) {
104− [tar([s, o]), tar([s, v])]
133+ tar([s, o], { p ->
134+ tar([s, v], { q ->
135+ r([p, q])
136+ })
137+ })
105138 } else {
106139 if (o == 0) {
107− fas([v, s])
140+ r(fas([v, s]))
108141 } else if (o == 1) {
109− v
142+ r(v)
110143 } else if (o == 3) {
111− wut(tar([s, v]))
144+ tar([s, v], {
145+ r(wut(it))
146+ })
112147 } else if (o == 4) {
113− lus(tar([s, v]))
148+ tar([s, v], {
149+ r(lus(it))
150+ })
114151 } else if (o == 5) {
115− tis(tar([s, v]))
152+ tar([s, v], {
153+ r(tis(it))
154+ })
116155 } else {
117156 if (wut(v) == 1) {
118157 throw new IllegalStateException()
@@ -120,29 +159,43 @@ def tar(def a) {
120159 def x = v[0]
121160 def y = v[1]
122161 if (o == 2) {
123− tar([tar([s, x]), tar([s, y])])
162+ tar([s, x], { p ->
163+ tar([s, y], { q ->
164+ tar([p, q], {
165+ r(it)
166+ })
167+ })
168+ })
124169 } else if (o == 7) {
125− tar(n([s, 2, x, 1, y]))
170+ tar(n([s, 2, x, 1, y]), {
171+ r(it)
172+ })
126173 } else if (o == 8) {
127− tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]))
174+ tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]), {
175+ r(it)
176+ })
128177 } else if (o == 9) {
129− tar(n([s, 7, y, 2, [0, 1], 0, x]))
178+ tar(n([s, 7, y, 2, [0, 1], 0, x]), {
179+ r(it)
180+ })
130181 } else if (o == 10) {
131182 if (wut(x) == 1) {
132− tar([s, y])
183+ tar([s, y], {
184+ r(it)
185+ })
133186 } else {
134− def r = x[0]
135− def t = x[1]
136− tar(n([s, 8, t, 7, [0, 3], y]))
187+ tar(n([s, 8, x[1], 7, [0, 3], y]), {
188+ r(it)
189+ })
137190 }
138191 } else {
139192 if (wut(y) == 1) {
140193 throw new IllegalStateException()
141194 }
142− def p = y[0]
143− def q = y[1]
144195 if (o == 6) {
145− tar(n([s, 2, [0, 1], 2, [1, p, q], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]))
196+ tar(n([s, 2, [0, 1], 2, [1, y[0], y[1]], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]), {
197+ r(it)
198+ })
146199 } else {
147200 throw new IllegalStateException()
148201 }
docs/nock/implementations/hoon.md+1−1
@@ -1,7 +1,7 @@
11---
22navhome: /docs
33title: Hoon
4−sort: 8
4+sort: 9
55---
66
77# Hoon
docs/nock/implementations/scheme.mdnew file+128
@@ -0,0 +1,128 @@
1+---
2+navhome: /docs
3+title: Scheme
4+sort: 8
5+next: true
6+---
7+
8+# Scheme
9+
10+From [Kohányi Róbert](https://github.com/kohanyirobert/snock/blob/master/snock.ss):
11+
12+```
13+(import (rnrs (6)))
14+
15+(define (i a) a)
16+
17+(define (n a r)
18+ (cond
19+ ((list? a)
20+ (let ((l (length a)))
21+ (cond
22+ ((equal? l 0) (raise 1))
23+ ((equal? l 1) (r (car a)))
24+ (else
25+ (let ((t (cond
26+ ((equal? l 2) (cadr a))
27+ (else (cdr a)))))
28+ (n (car a)
29+ (lambda (p) (n t
30+ (lambda (q) (r (cons p q)))))))))))
31+ ((fixnum? a) (r a))
32+ (else (raise 2))))
33+
34+(define (wut a)
35+ (cond
36+ ((fixnum? a) 1)
37+ (else 0)))
38+
39+(define (lus a)
40+ (cond
41+ ((equal? (wut a) 0) (raise 3))
42+ (else (+ 1 a))))
43+
44+(define (tis a)
45+ (cond
46+ ((equal? (wut a) 1) (raise 4))
47+ ((equal? (car a) (cdr a)) 0)
48+ (else 1)))
49+
50+(define (fas a r)
51+ (cond
52+ ((equal? (wut a) 1) (raise 5))
53+ (else
54+ (let ((h (car a))
55+ (t (cdr a)))
56+ (cond
57+ ((not (fixnum? h)) (raise 6))
58+ ((equal? h 0) (raise 7))
59+ ((equal? h 1) (r t))
60+ ((fixnum? t) (raise 8))
61+ ((equal? h 2) (r (car t)))
62+ ((equal? h 3) (r (cdr t)))
63+ (else
64+ (fas (cons (div h 2) t)
65+ (lambda (p) (fas (cons (+ 2 (mod h 2)) p)
66+ (lambda (q) (r q)))))))))))
67+
68+(define (tar a r)
69+ (cond
70+ ((equal? (wut a) 1) (raise 9))
71+ (else
72+ (let ((s (car a))
73+ (f (cdr a)))
74+ (cond
75+ ((equal? (wut f) 1) (raise 10))
76+ (else
77+ (let ((o (car f))
78+ (v (cdr f)))
79+ (cond
80+ ((equal? (wut o) 0) (tar (cons s o)
81+ (lambda (p) (tar (cons s v)
82+ (lambda (q) (r (cons p q)))))))
83+ ((equal? o 0) (r (fas (cons v s) i)))
84+ ((equal? o 1) (r v))
85+ ((equal? o 3) (tar (cons s v)
86+ (lambda (p) (r (wut p)))))
87+ ((equal? o 4) (tar (cons s v)
88+ (lambda (p) (r (lus p)))))
89+ ((equal? o 5) (tar (cons s v)
90+ (lambda (p) (r (tis p)))))
91+ ((equal? (wut v) 1) (raise 11))
92+ (else
93+ (let ((x (car v))
94+ (y (cdr v)))
95+ (cond
96+ ((equal? o 2) (tar (cons s x)
97+ (lambda (p) (tar (cons s y)
98+ (lambda (q) (tar (cons p q)
99+ (lambda (u) (r u))))))))
100+ ((equal? o 7) (tar (n (list (list s) 2 (list x) 1 (list y)) i)
101+ (lambda (p) (r p))))
102+ ((equal? o 8) (tar (n (list (list s) 7 (list (list 7 (list 0 1) (list x)) 0 1) (list y)) i)
103+ (lambda (p) (r p))))
104+ ((equal? o 9) (tar (n (list (list s) 7 (list y) 2 (list 0 1) 0 (list x)) i)
105+ (lambda (p) (r p))))
106+ ((equal? o 10) (cond
107+ ((equal? (wut x) 1) (tar (cons s y)
108+ (lambda (p) (r p))))
109+ (else (tar (n (list (list s) 8 (list (cdr x)) 7 (list 0 3) (list y)) i)
110+ (lambda (p) (r p))))))
111+ ((equal? (wut y) 1) (raise 12))
112+ ((equal? o 6) (tar (n (list
113+ (list s)
114+ 2
115+ (list 0 1)
116+ 2
117+ (list 1 (list (car y)) (list (cdr y)))
118+ (list 1 0)
119+ 2
120+ (list 1 2 3)
121+ (list 1 0)
122+ 4
123+ 4
124+ (list x))
125+ i)
126+ (lambda (p) (r p))))
127+ (else (raise 13)))))))))))))
128+```