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

Nock interpreter in Groovy

commit583eb9627b4b1b7cc23a0ccec8fca6c18e843593 authorGalen Wolfe-Pauly <galen@office-for.com> dateMon 30 May 2016, 12:52 (10 years ago) parentdbd1faf8348c1e
2 files changed+154−1
docs/nock/implementations/groovy.md+153 docs/nock/implementations/hoon.md+1−1
docs/nock/implementations/groovy.mdnew file+153
@@ -0,0 +1,153 @@
1+---
2+navhome: /docs
3+title: Groovy
4+sort: 7
5+next: true
6+---
7+
8+# Groovy
9+
10+From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
11+
12+```
13+def i(def a) {
14+ a.class in [
15+ byte, Byte,
16+ char, Character,
17+ short, Short,
18+ int, Integer,
19+ long, Long,
20+ BigInteger
21+ ] && a >= 0
22+}
23+
24+def n(def a) {
25+ if (a in List) {
26+ 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())]
32+ } else {
33+ throw new IllegalStateException()
34+ }
35+ } else if (i(a)) {
36+ (BigInteger) a
37+ } else {
38+ throw new IllegalStateException()
39+ }
40+}
41+
42+def wut(def a) {
43+ i(a) ? 1 : 0
44+}
45+
46+def lus(def a) {
47+ if (wut(a) == 0) {
48+ throw new IllegalStateException()
49+ }
50+ 1 + a
51+}
52+
53+def tis(def a) {
54+ if (wut(a) == 1) {
55+ throw new IllegalStateException()
56+ }
57+ a[0] == a[1] ? 0 : 1
58+}
59+
60+def fas(def a) {
61+ if (wut(a) == 1) {
62+ throw new IllegalStateException()
63+ }
64+ def h = a[0]
65+ if (!i(h)) {
66+ throw new IllegalStateException()
67+ }
68+ def t = a[1]
69+ if (h == 0) {
70+ throw new IllegalStateException()
71+ } else if (h == 1) {
72+ t
73+ } else {
74+ if (i(t)) {
75+ throw new IllegalStateException()
76+ }
77+ if (h == 2) {
78+ t[0]
79+ } else if (h == 3) {
80+ t[1]
81+ } 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+ }
88+ }
89+ }
90+}
91+
92+def tar(def a) {
93+ if (wut(a) == 1) {
94+ throw new IllegalStateException()
95+ }
96+ def s = a[0]
97+ def f = a[1]
98+ if (wut(f) == 1) {
99+ throw new IllegalStateException()
100+ }
101+ def o = f[0]
102+ def v = f[1]
103+ if (wut(o) == 0) {
104+ [tar([s, o]), tar([s, v])]
105+ } else {
106+ if (o == 0) {
107+ fas([v, s])
108+ } else if (o == 1) {
109+ v
110+ } else if (o == 3) {
111+ wut(tar([s, v]))
112+ } else if (o == 4) {
113+ lus(tar([s, v]))
114+ } else if (o == 5) {
115+ tis(tar([s, v]))
116+ } else {
117+ if (wut(v) == 1) {
118+ throw new IllegalStateException()
119+ }
120+ def x = v[0]
121+ def y = v[1]
122+ if (o == 2) {
123+ tar([tar([s, x]), tar([s, y])])
124+ } else if (o == 7) {
125+ tar(n([s, 2, x, 1, y]))
126+ } else if (o == 8) {
127+ tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]))
128+ } else if (o == 9) {
129+ tar(n([s, 7, y, 2, [0, 1], 0, x]))
130+ } else if (o == 10) {
131+ if (wut(x) == 1) {
132+ tar([s, y])
133+ } else {
134+ def r = x[0]
135+ def t = x[1]
136+ tar(n([s, 8, t, 7, [0, 3], y]))
137+ }
138+ } else {
139+ if (wut(y) == 1) {
140+ throw new IllegalStateException()
141+ }
142+ def p = y[0]
143+ def q = y[1]
144+ 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]))
146+ } else {
147+ throw new IllegalStateException()
148+ }
149+ }
150+ }
151+ }
152+}
153+```
docs/nock/implementations/hoon.md+1−1
@@ -1,7 +1,7 @@
11---
22navhome: /docs
33title: Hoon
4−sort: 7
4+sort: 8
55---
66
77# Hoon