on master help sign in
deps: extract nocksjs to external package
commit9475894a2c654f1eb85025e798c63c3e19d6bb46 authortomholford <tomholford@users.noreply.github.com> dateThu 13 Oct 2022, 15:53 (4 years ago) parent2305dbd
17 files changed+44−2107
package-lock.json+42 package.json+1 src/lib/keys.js+1−2 src/nockjs/TODO−42 src/nockjs/bits.js−332 src/nockjs/compiler.js−717 src/nockjs/hamt.js−135 src/nockjs/list.js−39 src/nockjs/noun.js−367 src/nockjs/serial.js−162 src/nockjs/test/ack.js−75 src/nockjs/test/add.js−57 src/nockjs/test/bits.js−15 src/nockjs/test/decrement.js−38 src/nockjs/test/hamt.js−16 src/nockjs/test/noun.js−88 src/nockjs/test/serial.js−22
package-lock.json+42
@@ -27,6 +27,7 @@
2727 "@types/styled-components": "^5.1.10",
2828 "@types/styled-system": "^5.1.11",
2929 "@types/styled-system__css": "^5.0.15",
30+ "@urbit/nockjs": "^0.0.1",
3031 "@urbit/roller-api": "^0.1.15",
3132 "@walletconnect/client": "^1.8.0",
3233 "@walletconnect/qrcode-modal": "^1.8.0",
@@ -115,6 +116,19 @@
115116 "node": "16.18.0"
116117 }
117118 },
119+ "../nockjs": {
120+ "name": "@urbit/nockjs",
121+ "version": "0.0.1",
122+ "extraneous": true,
123+ "license": "MIT",
124+ "dependencies": {
125+ "jsbn": "^1.1.0"
126+ },
127+ "devDependencies": {
128+ "tape": "^5.6.1",
129+ "tape-check": "^1.0.0-rc.0"
130+ }
131+ },
118132 "node_modules/@ampproject/remapping": {
119133 "version": "2.2.0",
120134 "dev": true,
@@ -7311,6 +7325,19 @@
73117325 "license": "MIT",
73127326 "optional": true
73137327 },
7328+ "node_modules/@urbit/nockjs": {
7329+ "version": "0.0.1",
7330+ "resolved": "https://registry.npmjs.org/@urbit/nockjs/-/nockjs-0.0.1.tgz",
7331+ "integrity": "sha512-0QM7LwA8OZCs/p03woWwKEqieClAfovulfQb2PfLmHOmmzwWwZN9tF3SXnfDWRcWnijpyfnfaQgli+U6XYWmgg==",
7332+ "dependencies": {
7333+ "jsbn": "^1.1.0"
7334+ }
7335+ },
7336+ "node_modules/@urbit/nockjs/node_modules/jsbn": {
7337+ "version": "1.1.0",
7338+ "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz",
7339+ "integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A=="
7340+ },
73147341 "node_modules/@urbit/roller-api": {
73157342 "version": "0.1.15",
73167343 "license": "MIT",
@@ -32611,6 +32638,21 @@
3261132638 "dev": true,
3261232639 "optional": true
3261332640 },
32641+ "@urbit/nockjs": {
32642+ "version": "0.0.1",
32643+ "resolved": "https://registry.npmjs.org/@urbit/nockjs/-/nockjs-0.0.1.tgz",
32644+ "integrity": "sha512-0QM7LwA8OZCs/p03woWwKEqieClAfovulfQb2PfLmHOmmzwWwZN9tF3SXnfDWRcWnijpyfnfaQgli+U6XYWmgg==",
32645+ "requires": {
32646+ "jsbn": "^1.1.0"
32647+ },
32648+ "dependencies": {
32649+ "jsbn": {
32650+ "version": "1.1.0",
32651+ "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz",
32652+ "integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A=="
32653+ }
32654+ }
32655+ },
3261432656 "@urbit/roller-api": {
3261532657 "version": "0.1.15",
3261632658 "requires": {
package.json+1
@@ -33,6 +33,7 @@
3333 "@types/styled-components": "^5.1.10",
3434 "@types/styled-system": "^5.1.11",
3535 "@types/styled-system__css": "^5.0.15",
36+ "@urbit/nockjs": "^0.0.1",
3637 "@urbit/roller-api": "^0.1.15",
3738 "@walletconnect/client": "^1.8.0",
3839 "@walletconnect/qrcode-modal": "^1.8.0",
src/lib/keys.js+1−2
@@ -1,8 +1,7 @@
11import BN from 'bn.js';
22import { Just, Nothing } from 'folktale/maybe';
33
4−import * as noun from '../nockjs/noun';
5−import * as serial from '../nockjs/serial';
4+import { noun, serial } from '@urbit/nockjs';
65import * as kg from 'urbit-key-generation';
76
87import { addHexPrefix, eqAddr } from './utils/address';
src/nockjs/TODOdeleted−42
@@ -1,42 +0,0 @@
1−* a noun is an atom or a cell. DONE
2−* mugs. DONE
3−* unifying equals. DONE
4−* dorky embedded DSL for nouns. DONE
5−* some kind of real hashmap (needed for memoization at least). DONE
6−* nock->js compiler (THE REALLY INTERESTING PART). DONE (at least, v1)
7−* decrement. DONE
8−* port random-noun tests to tape-check. DONE
9−* jam/cue for pill reading (makes testing easier). DONE
10−* add. DONE
11−* jets (basic binding). DONE
12−* ackerman. DONE
13−* atom-gates.pill
14−* ALL jets (ref: jaque)
15−* boot ivory pill
16−^--- call urbit library functions from js! great success!
17−* boot fakezod (brass pill) to dojo
18−* prevalence (on-disk in node, IndexedDB in brower)
19−* expose basic vere-slave interface as a library
20− i.e. send me pokes, i handle all persistence and send you io
21−^--- candidate vere backend
22− urbit in the browser
23−* urbit over webrtc?
24−
25−At this point, we have a working vere backend, as well as a full
26−"urbit in the browser". Performance is unlikely to be as good as jaque,
27−but will likely be quite accepatable (especially for in-browser things).
28−
29−At the very least, using an ivory pill to compile and run hoon from the
30−browser will ease client-side programming tasks.
31−
32−--- future implementation ideas
33−
34−it would be feasible to add profiling code to the compiled javascript,
35−specifically in callsites (9), so that the 9 would run say 100 times and
36−then recompile itself with a check and a direct call to the most called
37−battery. this would also enable loop finding in tail position where the
38−most called battery is the one being compiled. (important here not to
39−use the 2 compiler cache, but to have a generated call site per battery)
40−
41−it would be cool to profile that single optimization, all other things
42−being equal. it should be the most important one.
src/nockjs/bits.jsdeleted−332
@@ -1,332 +0,0 @@
1−import noun from './noun.js';
2−import { BigInteger } from 'jsbn';
3−var zero = noun.Atom.yes, one = noun.Atom.no;
4−
5−// a is native, returns native
6−function met(a, b) {
7− var bits = b.number.bitLength(),
8− full = bits >>> a,
9− part = (full << a) !== bits;
10−
11− return part ? full + 1 : full;
12−}
13−
14−function gth(a, b) {
15− return a.number.compareTo(b.number) > 0;
16−}
17−
18−function lth(a, b) {
19− return a.number.compareTo(b.number) < 0;
20−}
21−
22−function gte(a, b) {
23− return a.number.compareTo(b.number) >= 0;
24−}
25−
26−function lte(a, b) {
27− return a.number.compareTo(b.number) <= 0;
28−}
29−
30−function add(a, b) {
31− return new noun.Atom.Atom(a.number.add(b.number));
32−}
33−
34−function dec(a) {
35− return sub(a, one);
36−}
37−
38−function bex(a) {
39− return new noun.Atom.Atom(BigInteger.ONE.shiftLeft(a.number.intValue()));
40−}
41−
42−function sub(a, b) {
43− var r = a.number.subtract(b.number);
44− if ( r.signum < 0 ) {
45− throw new Error("subtract underflow");
46− }
47− else {
48− return new noun.Atom.Atom(r);
49− }
50−}
51−
52−function lsh(a, b, c) {
53− var bits = b.number.shiftLeft(a.number.intValue()).intValue();
54− return new noun.Atom.Atom(c.number.shiftLeft(bits));
55−}
56−
57−function rsh(a, b, c) {
58− var bits = b.number.shiftLeft(a.number.intValue()).intValue();
59− return new noun.Atom.Atom(c.number.shiftRight(bits));
60−}
61−
62−// to/from little-endian 32-bit word array, as used in vere
63−// TODO: efficiency is horrible here, but can be improved using internals
64−function bytesToWords(bytes) {
65− var len = bytes.length,
66− trim = len % 4;
67− var i, b, w;
68−
69− if ( trim > 0 ) {
70− len += (4-trim);
71− for ( i = 0; i < trim; ++i ) {
72− bytes.push(0);
73− }
74− }
75−
76− var size = len >> 2;
77− var words = new Array(size);
78− for ( i = 0, b = 0; i < size; ++i ) {
79− w = (bytes[b++] << 0) & 0x000000FF;
80− w ^= (bytes[b++] << 8) & 0x0000FF00;
81− w ^= (bytes[b++] << 16) & 0x00FF0000;
82− w ^= (bytes[b++] << 24) & 0xFF000000;
83− words[i] = w;
84− }
85− return words;
86−}
87−
88−function wordsToBytes(words) {
89− var buf = [];
90− var w, i, b;
91− for ( i = 0, b = 0; i < words.length; ++i ) {
92− w = words[i];
93− buf[b++] = 0xff & (w & 0x000000FF);
94− buf[b++] = 0xff & ((w & 0x0000FF00) >>> 8);
95− buf[b++] = 0xff & ((w & 0x00FF0000) >>> 16);
96− buf[b++] = 0xff & ((w & 0xFF000000) >>> 24);
97− }
98− // or here. one of the 'get rid of extra zeros' functions.
99− while ( buf[--b] === 0 ) {
100− buf.pop();
101− }
102− return buf;
103−}
104−
105−function bytesToAtom(bytes) {
106− var byt, parts = [];
107− for ( var i = bytes.length - 1; i >= 0; --i ) {
108− byt = bytes[i] & 0xff;
109− parts.push(byt < 16 ? ("0" + byt.toString(16)) : byt.toString(16));
110− }
111− return new noun.Atom.Atom(new BigInteger(parts.join(''), 16));
112−}
113−
114−function atomToBytes(atom) {
115− return atom.bytes();
116−}
117−
118−function atomToWords(atom) {
119− return bytesToWords(atomToBytes(atom));
120−}
121−
122−function wordsToAtom(words) {
123− return bytesToAtom(wordsToBytes(words));
124−}
125−
126−var malt = wordsToAtom;
127−
128−// XX: INTERNAL
129−function slaq(bloq, len) {
130− return new Array(((len << bloq) + 31) >>> 5);
131−}
132−
133−// src is atom, all others native
134−function chop(met, fum, wid, tou, dst, src) {
135− var buf = atomToWords(src),
136− len = buf.length,
137− i, j, mek, baf, bat, hut, san,
138− wuf, wut, waf, raf, wat, rat, hop;
139−
140− if ( met < 5 ) {
141− san = 1 << met;
142− mek = ((1 << san) - 1);
143− baf = fum << met;
144− bat = tou << met;
145−
146− for ( i = 0; i < wid; ++i ) {
147− waf = baf >>> 5;
148− raf = baf & 31;
149− wat = bat >>> 5;
150− rat = bat & 31;
151− hop = ((waf >= len) ? 0 : buf[waf]);
152− hop = ((hop >>> raf) & mek);
153− dst[wat] ^= hop << rat;
154− baf += san;
155− bat += san;
156− }
157− }
158− else {
159− hut = met - 5;
160− san = 1 << hut;
161−
162− for ( i = 0; i < wid; ++i ) {
163− wuf = (fum + i) << hut;
164− wut = (tou + i) << hut;
165−
166− for ( j = 0; j < san; ++j ) {
167− dst[wut + j] ^= ((wuf + j) >= len)
168− ? 0
169− : buf[wuf + j];
170− }
171− }
172− }
173−}
174−
175−function cut(a, b, c, d) {
176− var ai = a.number.intValue(),
177− bi = b.number.intValue(),
178− ci = c.number.intValue();
179−
180− var len = met(ai, d);
181− if ( zero.equals(c) || bi >= len ) {
182− return zero;
183− }
184− if ( bi + ci > len ) {
185− ci = len - b;
186− }
187− if ( 0 === bi && ci === len ) {
188− return d;
189− }
190− else {
191− var sal = slaq(ai, ci);
192− chop(ai, bi, ci, 0, sal, d);
193− return malt(sal);
194− }
195−}
196−
197−var maxCat = noun.Atom.fromInt(0xffffffff);
198−var catBits = noun.Atom.fromInt(32);
199−
200−function end(a, b, c) {
201− if ( gth(a, catBits) ) {
202− throw new Error("Fail");
203− }
204− else if ( gth(b, maxCat) ) {
205− return c;
206− }
207− else {
208− var ai = a.number.intValue(),
209− bi = b.number.intValue(),
210− len = met(ai, c);
211−
212− if ( 0 === bi ) {
213− return zero;
214− }
215− else if ( bi >= len ) {
216− return c;
217− }
218− else {
219− var sal = slaq(ai, bi);
220− chop(ai, 0, bi, 0, sal, c);
221− return malt(sal);
222− }
223− }
224−}
225−
226−function mix(a, b) {
227− return new noun.Atom.Atom(a.number.xor(b.number));
228−}
229−
230−function cat(a, b, c) {
231− if ( gth(a, catBits) ) {
232− throw new Error("Fail");
233− }
234− else {
235− var ai = a.number.intValue(),
236− lew = met(ai, b),
237− ler = met(ai, c),
238− all = lew + ler;
239−
240− if ( 0 === all ) {
241− return zero;
242− }
243− else {
244− var sal = slaq(ai, all);
245− chop(ai, 0, lew, 0, sal, b);
246− chop(ai, 0, ler, lew, sal, c);
247− return malt(sal);
248− }
249− }
250−}
251−
252−function can(a, b) {
253− if ( gth(a, catBits) ) {
254− throw new Error("Fail");
255− }
256− else {
257− var ai = a.number.intValue(),
258− tot = 0,
259− cab = b,
260− pos, i_cab, pi_cab, qi_cab;
261−
262− // measure
263− while ( true ) {
264− if ( zero.equals(cab) ) {
265− break;
266− }
267− if ( !cab.deep ) {
268− throw new Error("Fail");
269− }
270− i_cab = cab.head;
271− if ( !i_cab.deep ) {
272− throw new Error("Fail");
273− }
274− pi_cab = i_cab.head;
275− qi_cab = i_cab.tail;
276− if ( gth(pi_cab, maxCat) ) {
277− throw new Error("Fail");
278− }
279− if ( qi_cab.deep ) {
280− throw new Error("Fail");
281− }
282− if ( (tot + pi_cab) < tot ) {
283− throw new Error("Fail");
284− }
285− tot += pi_cab;
286− cab = cab.tail;
287− }
288− if ( 0 === tot ) {
289− return zero;
290− }
291− var sal = slaq(ai, tot);
292−
293− // chop the list atoms in
294− cab = b;
295− pos = 0;
296− while ( !zero.equals(cab) ) {
297− i_cab = cab.head;
298− pi_cab = i_cab.head.number.intValue();
299− qi_cab = i_cab.tail;
300−
301− chop(ai, 0, pi_cab, pos, sal, qi_cab);
302− pos += pi_cab;
303− cab = cab.tail;
304− }
305− return malt(sal);
306− }
307−}
308−
309−export default {
310− met: met,
311− cut: cut,
312− add: add,
313− sub: sub,
314− dec: dec,
315− gth: gth,
316− lth: lth,
317− gte: gte,
318− lte: lte,
319− bex: bex,
320− lsh: lsh,
321− rsh: rsh,
322− end: end,
323− mix: mix,
324− cat: cat,
325− can: can,
326− bytesToWords: bytesToWords,
327− wordsToBytes: wordsToBytes,
328− bytesToAtom: bytesToAtom,
329− atomToBytes: atomToBytes,
330− atomToWords: atomToWords,
331− wordsToAtom: wordsToAtom,
332−};
src/nockjs/compiler.jsdeleted−717
@@ -1,717 +0,0 @@
1−import noun from './noun.js';
2−import { NounMap } from './hamt.js';
3−import list from './list.js';
4−var Noun = noun.Noun, Atom = noun.Atom.Atom, Cell = noun.Cell, MEMO = noun.dwim("memo"), SLOG = noun.dwim("slog"), FAST = noun.dwim("fast"), SPOT = noun.dwim("spot"), MEAN = noun.dwim("mean"), HUNK = noun.dwim("hunk"), LOSE = noun.dwim("lose");
5−
6−function Statement() {
7−}
8−
9−function Block() {
10− Statement.call(this);
11− this.statements = [];
12−}
13−Block.prototype = Object.create(Statement.prototype);
14−Block.prototype.constructor = Block;
15−
16−Block.prototype.append = function(st) {
17− this.statements.push(st);
18−}
19−
20−Block.prototype.toJs = function() {
21− var sts = this.statements;
22− var parts = new Array(sts.length);
23− for ( var i = 0; i < sts.length; ++i) {
24− parts[i] = sts[i].toJs();
25− }
26− return parts.join('');
27−};
28−
29−function Assignment(name, expr) {
30− Statement.call(this);
31− this.name = name;
32− this.expr = expr;
33−}
34−Assignment.prototype = Object.create(Statement.prototype);
35−Assignment.prototype.constructor = Assignment;
36−
37−Assignment.prototype.toJs = function() {
38− return "var " + this.name + " = " + this.expr.toJs() + ";";
39−}
40−
41−function Expression() {
42−}
43−
44−function Cons(head, tail) {
45− Expression.call(this);
46− this.head = head;
47− this.tail = tail;
48−}
49−Cons.prototype = Object.create(Expression.prototype);
50−Cons.prototype.constructor = Cons;
51−
52−Cons.prototype.toJs = function() {
53− return "context.cons(" + this.head + ", " + this.tail + ")";
54−};
55−
56−function Frag(axis, name) {
57− Expression.call(this);
58− this.axis = axis;
59− this.name = name;
60−}
61−Frag.prototype = Object.create(Expression.prototype);
62−Frag.prototype.constructor = Frag;
63−
64−Frag.prototype.toJs = function() {
65− var parts = [this.name];
66− for ( var ax = this.axis; ax > 1; ax = ax.mas() ) {
67− parts.push( ( 2 === ax.cap().valueOf() ) ? "head" : "tail" );
68− }
69− return parts.join(".");
70−};
71−
72−function Bail() {
73− Statement.call(this);
74−}
75−Bail.prototype = Object.create(Statement.prototype);
76−Bail.prototype.constructor = Bail;
77−
78−Bail.prototype.toJs = function() {
79− return "throw new Error(\"Bail\")";
80−};
81−
82−function Identity(name) {
83− Expression.call(this);
84− this.name = name;
85−}
86−Identity.prototype = Object.create(Expression.prototype);
87−Identity.prototype.constructor = Identity;
88−
89−Identity.prototype.toJs = function() {
90− return this.name;
91−};
92−
93−function Constant(index) {
94− Expression.call(this);
95− this.index = index;
96−}
97−Constant.prototype = Object.create(Expression.prototype);
98−Constant.prototype.constructor = Constant;
99−
100−Constant.prototype.toJs = function() {
101− return "constants[" + this.index + "]";
102−};
103−
104−function Nock(subject, formula, tail) {
105− Expression.call(this);
106− this.subject = subject;
107− this.formula = formula;
108− this.tail = tail;
109−}
110−Nock.prototype = Object.create(Expression.prototype);
111−Nock.prototype.constructor = Nock;
112−
113−Nock.prototype.toJs = function() {
114− var f = this.formula;
115− var targetCode = "(" + f + ".hasOwnProperty('target') ? " + f +
116− ".target : (" + f + ".target = context.compile(" + this.formula + ")))";
117− return this.tail ?
118− "context.trampoline(" + targetCode + ", " + this.subject + ")" :
119− targetCode + "(" + this.subject + ")";
120−};
121−
122−function Deep(name) {
123− Expression.call(this);
124− this.name = name;
125−}
126−Deep.prototype = Object.create(Expression.prototype);
127−Deep.prototype.constructor = Deep;
128−
129−Deep.prototype.toJs = function() {
130− return this.name +".deep ? context.yes : context.no";
131−};
132−
133−function Bump(name) {
134− Expression.call(this);
135− this.name = name;
136−}
137−Bump.prototype = Object.create(Expression.prototype);
138−Bump.prototype.constructor = Bump;
139−
140−Bump.prototype.toJs = function() {
141− return this.name + ".bump()";
142−};
143−
144−function Same(one, two) {
145− Expression.call(this);
146− this.one = one;
147− this.two = two;
148−}
149−Same.prototype = Object.create(Expression.prototype);
150−Same.prototype.constructor = Same;
151−
152−Same.prototype.toJs = function() {
153− return "(" + this.one + ".equals(" + this.two + ")" + " ? context.yes : context.no)";
154−};
155−
156−function If(test, yes, no) {
157− Statement.call(this);
158− this.test = test;
159− this.yes = yes;
160− this.no = no;
161−}
162−If.prototype = Object.create(Statement.prototype);
163−If.prototype.constructor = If;
164−
165−If.prototype.toJs = function() {
166− return "if(" + this.test + ".loob()){" +
167− this.yes.toJs() + "}else{" + this.no.toJs() + "}";
168−};
169−
170−function Kick(axis, core, tail) {
171− Expression.call(this)
172− this.axis = axis;
173− this.core = core;
174− this.tail = tail;
175−}
176−Kick.prototype = Object.create(Expression.prototype);
177−Kick.prototype.constructor = Kick;
178−
179−Kick.prototype.toJs = function() {
180− var axis = this.axis.shortCode();
181−
182− return "(function (cor) {" +
183− "var pro, tgt, bus, arms, bat = cor.head, has = false;" +
184− "if ( bat.hasOwnProperty('loc') && (tgt = bat.loc.jets[" + axis + "]) && bat.loc.fine(cor) ) {" +
185− "return tgt(cor);" +
186− "}" +
187− "if ( bat.hasOwnProperty('arms') ) {" +
188− "arms = bat.arms;" +
189− "has = arms.hasOwnProperty('" + axis + "');" +
190− "}" +
191− "else arms = bat.arms = {};" +
192− "tgt = (has ? arms['" + axis + "'] : (arms['" + axis + "'] = context.compile(" +
193− new Frag(this.axis, "bat").toJs() + ")));" +
194− "bus = cor;" +
195− (this.tail ? "pro = context.trampoline(tgt, bus);" :
196− "while (true) {" +
197− "pro = tgt(bus);" +
198− "if ( context.isTrampoline(pro) ) {" +
199− "tgt = pro.target;" +
200− "bus = pro.subject;" +
201− "}" +
202− "else break;" +
203− "}") +
204− "return pro;" +
205− "})(" + this.core + ")";
206−};
207−
208−function GetMemo(name) {
209− Expression.call(this);
210− this.name = name;
211−}
212−GetMemo.prototype = Object.create(Expression.prototype);
213−GetMemo.prototype.constructor = GetMemo;
214−
215−GetMemo.prototype.toJs = function() {
216− return "context.getMemo(" + this.name + ")";
217−};
218−
219−function PutMemo(key, val) {
220− Statement.call(this);
221− this.key = key;
222− this.val = val;
223−}
224−PutMemo.prototype = Object.create(Statement.prototype);
225−PutMemo.prototype.constructor = PutMemo;
226−
227−function Push(name) {
228− Statement.call(this);
229− this.name = name;
230−}
231−Push.prototype = Object.create(Statement.prototype);
232−Push.prototype.constructor = Push;
233−
234−Push.prototype.toJs = function() {
235− return "context.stackPush(" + this.name + ");";
236−};
237−
238−function Pop() {
239− Statement.call(this);
240−}
241−Pop.prototype = Object.create(Statement.prototype);
242−Pop.prototype.constructor = Pop;
243−
244−Pop.prototype.toJs = function() {
245− return "context.stackPop()";
246−};
247−
248−function Fast(clue, core) {
249− Statement.call(this);
250− this.clue = clue;
251− this.core = core;
252−}
253−
254−Fast.prototype.toJs = function() {
255− return "context.register(" + this.core + ", " + this.clue + ");";
256−}
257−
258−function Slog(name) {
259− Statement.call(this);
260− this.name = name;
261−}
262−
263−Slog.prototype.toJs = function() {
264− return "context.slog(" + this.name + ")";
265−};
266−
267−function compile(formula, subject, product, fresh, constants, block, tail) {
268− var op, arg, one, two, odd;
269− if ( !(formula instanceof Cell )) {
270− throw new Error("invalid formula");
271− }
272− op = formula.head;
273− arg = formula.tail;
274− if ( op instanceof Cell ) {
275− one = fresh();
276− two = fresh();
277− compile(op, subject, one, fresh, constants, block, false);
278− compile(arg, subject, two, fresh, constants, block, false);
279− block.append(new Assignment(product, new Cons(one, two)));
280− }
281− else switch ( op.valueOf() ) {
282− case 0:
283− if ( 0 === arg ) {
284− block.append(new Bail());
285− }
286− else if ( 1 === arg ) {
287− block.append(new Identity(subject));
288− }
289− else {
290− block.append(new Assignment(product, new Frag(arg, subject)));
291− }
292− break;
293− case 1:
294− constants.push(arg);
295− block.append(new Assignment(product, new Constant(constants.length - 1)));
296− break;
297− case 2:
298− one = fresh();
299− two = fresh();
300− compile(arg.head, subject, one, fresh, constants, block, false);
301− compile(arg.tail, subject, two, fresh, constants, block, false);
302− block.append(new Assignment(product, new Nock(one, two, tail)));
303− break;
304− case 3:
305− one = fresh();
306− compile(arg, subject, one, fresh, constants, block, false);
307− block.append(new Assignment(product, new Deep(one)));
308− break;
309− case 4:
310− one = fresh();
311− compile(arg, subject, one, fresh, constants, block, false);
312− block.append(new Assignment(product, new Bump(one)));
313− break;
314− case 5:
315− one = fresh();
316− two = fresh();
317− compile(arg.head, subject, one, fresh, constants, block, false);
318− compile(arg.tail, subject, two, fresh, constants, block, false);
319− block.append(new Assignment(product, new Same(one, two)));
320− break;
321− case 6:
322− odd = fresh();
323− one = new Block();
324− two = new Block();
325− compile(arg.head, subject, odd, fresh, constants, block, false);
326− compile(arg.tail.head, subject, product, fresh, constants, one, tail);
327− compile(arg.tail.tail, subject, product, fresh, constants, two, tail);
328− block.append(new If(odd, one, two));
329− break;
330− case 7:
331− one = fresh();
332− compile(arg.head, subject, one, fresh, constants, block, false);
333− compile(arg.tail, one, product, fresh, constants, block, tail);
334− break;
335− case 8:
336− one = fresh();
337− two = fresh();
338− compile(arg.head, subject, one, fresh, constants, block, false);
339− block.append(new Assignment(two, new Cons(one, subject)));
340− compile(arg.tail, two, product, fresh, constants, block, tail);
341− break;
342− case 9:
343− odd = arg.head;
344− if ( 2 === odd.cap().valueOf() ) {
345− one = fresh();
346− two = odd.mas();
347− compile(arg.tail, subject, one, fresh, constants, block, false);
348− block.append(new Assignment(product, new Kick(two, one, tail)));
349− }
350− else {
351− compile(noun.dwim([7, arg.tail, 2, [0, 1], 0, odd]),
352− subject, product, fresh, constants, block, tail);
353− }
354− break;
355− case 10:
356− var hint = arg.head;
357− if ( !(arg.head instanceof Cell) ) {
358− // no recognized static hints
359− compile(arg.tail, subject, product, fresh, constants, block, tail);
360− }
361− else {
362− var zep = hint.head;
363− var clu = fresh();
364− compile(hint.tail, subject, clu, fresh, constants, block, false);
365− if ( zep.equals(MEMO) ) {
366− var key = fresh();
367− var got = fresh();
368− odd = fresh();
369− one = new Block();
370− two = new Block();
371− var konst = fresh();
372− block.append(new Assignment(konst, new Constant(hint.tail)));
373− block.append(new Assignment(key, new Cons(subject, konst)));
374− block.append(new Assignment(got, new GetMemo(two)));
375− block.append(new Assignment(odd, new Deep(got)));
376− one.append(new Assignment(product, new Frag(noun.dwim(3), got)));
377− compile(arg.tail, subject, product, fresh, two, false);
378− two.append(new PutMemo(key, product));
379− block.append(new If(odd, one, two));
380− }
381− else if ( zep.equals(SLOG) ) {
382− block.append(new Slog(clu));
383− compile(arg.tail, subject, product, fresh, constants, block, tail);
384− }
385− else if ( zep.equals(FAST) ) {
386− compile(arg.tail, subject, product, fresh, constants, block, false);
387− block.append(new Fast(clu, product));
388− }
389− else if ( zep.equals(SPOT) ||
390− zep.equals(MEAN) ||
391− zep.equals(HUNK) ||
392− zep.equals(LOSE) ) {
393− one = fresh();
394− two = fresh();
395− block.append(new Assignment(one, new Constant(zep)));
396− block.append(new Assignment(two, new Cons(one, clu)));
397− block.append(new Push(two));
398− compile(arg.tail, subject, product, fresh, constants, block, false);
399− block.append(new Pop());
400− }
401− else {
402− // unrecognized
403− compile(arg.tail, subject, product, fresh, constants, block, tail);
404− }
405− }
406− break;
407− case 11:
408− one = fresh();
409− two = fresh();
410− compile(arg.head, subject, one, fresh, constants, block, false);
411− compile(arg.tail, subject, two, fresh, constants, block, false);
412− block.append(new Assignment(product, new Esc(one, two)));
413− break;
414− default:
415− throw new Error("invalid opcode");
416− }
417−}
418−
419−function Trampoline(target, subject) {
420− this.target = target;
421− this.subject = subject;
422−}
423−
424−function genFine(loc) {
425− var constants = [], out = [], i;
426− for ( i = 0; !loc.isStatic; ++i ) {
427− out.push("if(!constants[" + i + "].equals(a.head)){return false;}");
428− constants.push(loc.noun);
429− out.push("a=" + new Frag(loc.axisToParent, "a").toJs() + ";");
430− loc = loc.parentLoc;
431− }
432− out.push("return constants[" + i + "].equals(a);");
433− constants.push(loc.noun);
434− var body = 'return function(a){' + out.join('') + 'return true;};';
435− var builder = new Function('constants', body);
436− return builder(constants);
437−}
438−
439−var three = noun.dwim(3);
440−function Location(context, name, label, axisToParent, hooks, noun, parentLoc) {
441− this.name = name;
442− this.label = label;
443− this.parentLoc = parentLoc;
444− this.axisToParent = axisToParent;
445− this.fragToParent = Noun.fragmenter(axisToParent);
446− this.nameToAxis = hooks;
447− this.axisToName = {};
448− this.isStatic = ( null === parentLoc || (three.equals(axisToParent) && parentLoc.isStatic) );
449− if ( this.isStatic ) {
450− this.noun = noun;
451− }
452− else {
453− this.noun = noun.head;
454− this.noun.mug();
455− }
456− for ( var k in hooks ) {
457− if ( hooks.hasOwnProperty(k) ) {
458− this.axisToName[hooks[k].shortCode()] = k;
459− }
460− }
461− this.jets = {};
462− var drivers = context.drivers[label];
463− if ( drivers && drivers.length > 0 ) {
464− for ( var i = 0; i < drivers.length; ++i ) {
465− var d = drivers[i];
466− if ( d instanceof AxisArm ) {
467− this.jets[d.axis.mas().shortCode()] = d.fn;
468− }
469− else {
470− this.jets[nameToAxis[d.name].mas().shortCode()] = d.fn;
471− }
472− }
473− }
474− this.fine = genFine(this);
475−}
476−
477−function Clue(name, parentAxis, hooks) {
478− this.name = name;
479− this.parentAxis = parentAxis;
480− this.hooks = hooks;
481−}
482−
483−function JetDriver(label, fn) {
484− this.label = label;
485− this.fn = fn;
486−}
487−
488−function AxisArm(label, axis, fn) {
489− JetDriver.call(this, label, fn);
490− this.axis = axis;
491−}
492−AxisArm.prototype = Object.create(JetDriver.prototype);
493−AxisArm.prototype.constructor = AxisArm;
494−
495−function NamedArm(label, name, fn) {
496− JetDriver.call(this, label, fn);
497− this.name = name;
498−}
499−NamedArm.prototype = Object.create(JetDriver.prototype);
500−NamedArm.prototype.constructor = NamedArm;
501−
502−var two = noun.dwim(2);
503−function collectFromCore(prefix, spec, out) {
504− var name = spec[0], arms = spec[1], children = spec[2],
505− labl = prefix + "/" + name;
506− if ( arms instanceof Function ) {
507− out[labl] = [new AxisArm(labl, two, arms)];
508− }
509− else {
510− var all = [];
511− for ( var k in arms ) {
512− if ( arms.hasOwnProperty(k) ) {
513− all.push(( 'number' === typeof(k) ) ?
514− new AxisArm(labl, noun.dwim(k), arms[k]) :
515− new NamedArm(labl, k, arms[k]));
516− }
517− }
518− out[labl] = all;
519− }
520− if ( children ) {
521− for ( var i = 0; i < children.length; ++i ) {
522− collectFromCore(labl, children[i], out);
523− }
524− }
525−}
526−
527−function Context(drivers) {
528− this.memo = new NounMap();
529− this.clues = new NounMap();
530− this.dash = new NounMap();
531− this.tax = noun.Atom.yes;
532− this.drivers = {};
533− if ( drivers ) {
534− collectFromCore('', drivers, this.drivers);
535− }
536−}
537−
538−Context.prototype.yes = noun.Atom.yes;
539−Context.prototype.no = noun.Atom.no;
540−Context.prototype.cons = function (h, t) {
541− return new Cell(h, t);
542−};
543−
544−Context.prototype.trampoline = function(tgt, bus) {
545− return new Trampoline(tgt, bus);
546−};
547−
548−Context.prototype.isTrampoline = function(a) {
549− return (a instanceof Trampoline);
550−};
551−
552−Context.prototype.compile = function(cell) {
553− var i = 0;
554− var fresh = function() {
555− return "v" + ++i;
556− };
557− var body = new Block();
558− var constants = [];
559− compile(cell, "subject", "product", fresh, constants, body, true);
560− var text = "return function(subject){" + body.toJs() + "return product;}";
561− var builder = new Function("context", "constants", text);
562− return cell.target = builder(this, constants);
563−};
564−
565−Context.prototype.nock = function(subject, formula) {
566− var product, target;
567− if ( !formula.hasOwnProperty("target") ) {
568− this.compile(formula);
569− }
570− target = formula.target;
571− while ( true ) {
572− product = target(subject);
573− if ( product instanceof Trampoline ) {
574− subject = product.subject;
575− target = product.target;
576− }
577− else {
578− return product;
579− }
580− }
581−};
582−
583−Context.prototype.getMemo = function(key) {
584− return this.memo.get(key);
585−}
586−
587−Context.prototype.putMemo = function(key, val) {
588− this.memo.insert(key, val);
589−};
590−
591−Context.prototype.stackPush = function(item) {
592− this.tax = new Cell(item, this.tax);
593−};
594−
595−Context.prototype.stackPop = function() {
596− this.tax = this.tax.tail;
597−}
598−
599−Context.prototype.slog = function(item) {
600− // TODO: don't rewrite ++wash again, just call the kernel
601− console.log(item);
602−};
603−
604−function chum(n) {
605− if ( n.deep ) {
606− return Atom.cordToString(n.head) + n.tail.number.shortValue().toString(10);
607− }
608− else {
609− return Atom.cordToString(n);
610− }
611−}
612−
613−var ten = noun.dwim(10);
614−function skipHints(formula) {
615− while ( true ) {
616− if ( formula.deep ) {
617− if ( ten.equals(formula.head) ) {
618− formula = formula.tail.tail;
619− continue;
620− }
621− }
622− return formula;
623− }
624−}
625−
626−var zero = noun.dwim(0), constant_zero = noun.dwim(1,0);
627−function parseParentAxis(noun) {
628− var f = skipHints(noun);
629− if ( constant_zero.equals(f) ) {
630− return zero;
631− }
632− else if ( !zero.equals(f.head) ) {
633− throw new Error("weird formula head");
634− }
635− else if ( 3 != f.tail.cap().valueOf() ) {
636− throw new Error("weird parent axis");
637− }
638− return f.tail;
639−}
640−
641−var nine = noun.dwim(9), constant_frag = noun.dwim(0,1);
642−function parseHookAxis(nock) {
643− var f = skipHints(nock),
644− op = f.head;
645− if ( !op.deep ) {
646− if ( zero.equals(op) ) {
647− if ( !f.tail.deep ) {
648− return f.tail;
649− }
650− }
651− else if ( nine.equals(op) ) {
652− var rest = f.tail;
653− if ( !rest.head.deep && constant_frag.equals(rest.tail) ) {
654− return rest.head;
655− }
656− }
657− }
658− return null;
659−}
660−
661−function parseHooks(noun) {
662− var o = {};
663− list.forEach(noun, function(c) {
664− var term = Atom.cordToString(c.head),
665− axis = parseHookAxis(c.tail);
666− if ( null != axis ) {
667− o[term] = axis;
668− }
669− });
670− return o;
671−}
672−
673−Context.prototype.parseClue = function(raw) {
674− var clue = this.clues.get(raw);
675− if ( clue === undefined ) {
676− var name = chum(raw.head),
677− parentAxis = parseParentAxis(raw.tail.head),
678− hooks = parseHooks(raw.tail.tail);
679− clue = new Clue(name, parentAxis, hooks);
680− this.clues.insert(raw, clue);
681− }
682− return clue;
683−}
684−
685−Context.prototype.register = function(core, raw) {
686− var bat = core.head;
687− var loc = this.dash.get(bat);
688− if ( undefined === loc ) {
689− try {
690− var clue = this.parseClue(raw);
691− if ( zero.equals(clue.parentAxis) ) {
692− loc = new Location(this, clue.name, '/' + clue.name, zero, clue.hooks, core, null);
693− }
694− else {
695− var parentCore = core.at(clue.parentAxis),
696− parentBattery = parentCore.head,
697− parentLoc = this.dash.get(parentBattery);
698− if ( undefined === parentLoc ) {
699− console.log('register: invalid parent for ' + clue.name);
700− }
701− else {
702− var label = parentLoc.label + "/" + clue.name;
703− loc = new Location(this, clue.name, label, clue.parentAxis, clue.hooks, core, parentLoc);
704− }
705− }
706− bat.loc = loc;
707− this.dash.insert(bat, loc);
708− }
709− catch (e) {
710− console.log(e);
711− }
712− }
713−};
714−
715−export default {
716− Context: Context,
717−};
src/nockjs/hamt.jsdeleted−135
@@ -1,135 +0,0 @@
1−/* keys can be any noun, values aren't constrained */
2−
3−function Slot() {
4−}
5−
6−function Node() {
7− Slot.call(this);
8− this.slots = new Array(32);
9−}
10−Node.prototype = Object.create(Slot.prototype);
11−Node.prototype.constructor = Node;
12−
13−Node.prototype.insert = function(key, val, lef, rem) {
14− var inx;
15− lef -= 5;
16− inx = rem >>> lef;
17− rem &= ((1 << lef) - 1);
18−
19− this.slots[inx] = ( undefined === this.slots[inx] )
20− ? new Single(key, val)
21− : this.slots[inx].insert(key, val, lef, rem);
22− return this;
23−};
24−
25−Node.prototype.get = function(key, lef, rem) {
26− var inx, sot;
27− lef -= 5;
28− inx = rem >>> lef;
29− rem &= ((1 << lef) - 1);
30− sot = this.slots[inx];
31−
32− return ( undefined === sot ) ? undefined : sot.get(key, lef, rem);
33−};
34−
35−function Bucket() {
36− this.singles = [];
37−}
38−Bucket.prototype = Object.create(Slot.prototype);
39−Bucket.prototype.constructor = Bucket;
40−
41−Bucket.prototype.insert = function(key, val, lef, rem) {
42− var s, a = this.singles;
43−
44− for ( var i = 0; i < a.length; ++i ) {
45− s = a[i];
46− if ( s.key.equals(key) ) {
47− s.val = val;
48− return this;
49− }
50− }
51− a.push(new Single(key, val));
52− return this;
53−};
54−
55−Bucket.prototype.get = function(key, lef, rem) {
56− var s, a = this.singles;
57−
58− for ( var i = 0; i < a.length; ++i ) {
59− s = a[i];
60− if ( s.key.equals(key) ) {
61− return s.val;
62− }
63− }
64−
65− return undefined;
66−};
67−
68−function Single(key, val) {
69− Slot.call(this);
70− this.key = key;
71− this.val = val;
72−}
73−Single.prototype = Object.create(Slot.prototype);
74−Single.prototype.constructor = Single;
75−
76−Single.prototype.insert = function(key, val, lef, rem) {
77− if ( this.key.equals(key) ) {
78− this.val = val;
79− return this;
80− }
81− else {
82− var n, rom = this.key.mug() & ((1 << lef) - 1);
83−
84− if ( lef > 0 ) {
85− n = new Node();
86− }
87− else {
88− n = new Bucket();
89− }
90− n.insert(this.key, this.val, lef, rom);
91− n.insert(key, val, lef, rem);
92− return n;
93− }
94−};
95−
96−Single.prototype.get = function(key, lef, rem) {
97− if ( this.key.equals(key) ) {
98− return this.val;
99− }
100− else {
101− return undefined;
102− }
103−};
104−
105−function NounMap() {
106− this.slots = new Array(64);
107−}
108−
109−NounMap.prototype.insert = function(key, val) {
110− var m = key.mug();
111− var inx = m >>> 25;
112− var sot = this.slots;
113− if ( undefined === sot[inx] ) {
114− sot[inx] = new Single(key, val);
115− }
116− else {
117− var rem = m & ((1 << 25) - 1);
118− sot[inx] = sot[inx].insert(key, val, 25, rem);
119− }
120−};
121−
122−NounMap.prototype.get = function(key) {
123− var m = key.mug();
124− var inx = m >>> 25;
125− var sot = this.slots[inx];
126− if ( undefined === sot ) {
127− return undefined;
128− }
129− else {
130− var rem = m & ((1 << 25) - 1);
131− return sot.get(key, 25, rem);
132− }
133−};
134−
135−export default NounMap;
src/nockjs/list.jsdeleted−39
@@ -1,39 +0,0 @@
1−import noun from './noun.js';
2−var Cell = noun.Cell, zero = noun.Atom.yes;
3−
4−function flop(a) {
5− var b = zero;
6−
7− while ( true ) {
8− if ( zero.equals(a) ) {
9− return b;
10− }
11− else if ( !a.deep ) {
12− throw new Error("Bail");
13− }
14− else {
15− b = new Cell(a.head, b);
16− a = a.tail;
17− }
18− }
19−}
20−
21−function forEach(n, f) {
22− while ( true ) {
23− if ( zero.equals(n) ) {
24− return;
25− }
26− else if ( !n.deep ) {
27− throw new Error("Bail");
28− }
29− else {
30− f(n.head);
31− n = n.tail;
32− }
33− }
34−}
35−
36−export default {
37− flop: flop,
38− forEach: forEach,
39−};
src/nockjs/noun.jsdeleted−367
@@ -1,367 +0,0 @@
1−import { BigInteger } from 'jsbn';
2−
3−function Noun() {
4− this._mug = 0;
5−}
6−
7−Noun.prototype.loob = function () {
8− throw new Error("Bail");
9−};
10−
11−Noun.prototype.toString = function() {
12− var parts = [];
13− this.pretty(parts, false);
14− return parts.join('');
15−};
16−
17−Noun.prototype.mug = function () {
18− if ( 0 === this._mug ) {
19− this._mug = this.calculateMug();
20− }
21− return this._mug;
22−};
23−
24−Noun.prototype.mugged = function () {
25− return 0 !== this._mug;
26−};
27−
28−Noun.prototype.deep = false;
29−Noun.prototype.bump = function () {
30− throw new Error("Bail");
31−}
32−
33−Noun.prototype.equals = function(o) {
34− if ( this === o ) {
35− return true;
36− }
37−
38− if ( this instanceof Cell ) {
39− if ( o instanceof Cell) {
40− return this.unify(o);
41− }
42− else {
43− return false;
44− }
45− }
46− else {
47− if ( o instanceof Cell ) {
48− return false;
49− }
50− else if (0 === this.number.compareTo(o.number)) {
51− o.number = this.number;
52− return true;
53− }
54− else {
55− return false;
56− }
57− }
58−};
59−
60−
61−function _mug_fnv(has_w) {
62− return Math.imul(has_w, 16777619);
63−}
64−
65−function _mug_out(has_w) {
66− return (has_w >>> 31) ^ (has_w & 0x7fffffff);
67−}
68−
69−function _mug_both(lef_w, rit_w) {
70− var bot_w = _mug_fnv(lef_w ^ _mug_fnv(rit_w));
71− var out_w = _mug_out(bot_w);
72−
73− if ( 0 !== out_w ) {
74− return out_w;
75− }
76− else {
77− return _mug_both(lef_w, ++rit_w);
78− }
79−}
80−
81−function Cell(head, tail) {
82− Noun.call(this);
83− this.head = head;
84− this.tail = tail;
85−}
86−Cell.prototype = Object.create(Noun.prototype);
87−Cell.prototype.constructor = Cell;
88−Cell.prototype.deep = true;
89−
90−Cell.prototype.pretty = function(out, tail) {
91− if ( !tail ) {
92− out.push('[');
93− }
94− this.head.pretty(out, false);
95− out.push(' ');
96− this.tail.pretty(out, true);
97− if ( !tail ) {
98− out.push(']');
99− }
100−};
101−
102−Cell.prototype.calculateMug = function() {
103− return _mug_both(this.head.mug(), this.tail.mug());
104−};
105−
106−Cell.prototype.unify = function(o) {
107− if ( this === o ) {
108− return true;
109− }
110−
111− if ( o.mugged() ) {
112− if ( this.mugged() ) {
113− if ( this.mug() !== o.mug() ) {
114− return false;
115− }
116− }
117− else {
118− return o.unify(this);
119− }
120− }
121−
122− if ( this.head.equals(o.head) ) {
123− o.head = this.head;
124− if ( this.tail.equals(o.tail) ) {
125− o._mug = this._mug;
126− o.tail = this.tail;
127− return true;
128− }
129− }
130−
131− return false;
132−};
133−
134−function Atom(number) {
135− Noun.call(this);
136− this.number = number;
137−}
138−Atom.prototype = Object.create(Noun.prototype);
139−Atom.prototype.constructor = Atom;
140−
141−var small = new Array(256);
142−(function() {
143− var i, bi;
144− for ( i = 0; i < 256; ++i ) {
145− bi = new BigInteger();
146− bi.fromInt(i);
147− small[i] = new Atom(bi);
148− }
149−})();
150−
151−var fragCache = {
152− 0: function(a) {
153− throw new Error("Bail");
154− },
155− 1: function(a) {
156− return a;
157− },
158−};
159−var one = small[1];
160−Noun.fragmenter = function(a) {
161− var s = a.shortCode();
162− if ( fragCache.hasOwnProperty(s) ) {
163− return fragCache[s];
164− }
165− else {
166− for ( var parts = ['a']; !one.equals(a); a = a.mas() ) {
167− parts.push( ( 2 === a.cap().valueOf() ) ? 'head' : 'tail' );
168− }
169− return fragCache[s] =
170− function(a) {
171− return parts.join('.');
172− }
173− }
174−}
175−
176−Noun.prototype.at = function(a) {
177− return Noun.fragmenter(a)(this);
178−};
179−
180−var shortBi = new BigInteger();
181−shortBi.fromInt(65536);
182−
183−Atom.prototype.bytes = function() {
184− var bytes = this.number.toByteArray();
185− var r = [];
186− for ( var i = bytes.length-1; i >= 0; --i ) {
187− r.push(bytes[i]&0xff);
188− }
189− return r;
190−}
191−
192−Atom.cordToString = function(c) {
193− var bytes = c.bytes();
194− var chars = [];
195−
196− for ( var i = 0; i < bytes.length; ++i ) {
197− chars.push(String.fromCharCode(bytes[i]));
198− }
199− return chars.join('');
200−};
201−
202−Atom.prototype.pretty = function(out, tail) {
203− if ( this.number.compareTo(shortBi) < 0 ) {
204− return out.push(this.number.toString(10));
205− }
206− else {
207− var tap = [], isTa = true, isTas = true, bytes = this.number.toByteArray();
208− for ( var i = bytes.length - 1; i >= 0; --i) {
209− var c = bytes[i];
210− if ( isTa && ((c < 32) || (c > 127)) ) {
211− isTa = false;
212− isTas = false;
213− break;
214− }
215− else if ( isTas && !((c > 47 && c < 58) || // digits
216− (c > 96 && c < 123) || // lowercase letters
217− c === 45) ) { // -
218− isTas = false;
219− }
220− tap.push(String.fromCharCode(c));
221− }
222− if ( isTas ) {
223− out.push('%');
224− out.push.apply(out, tap);
225− }
226− else if ( isTa ) {
227− out.push("'");
228− out.push.apply(out, tap);
229− out.push("'");
230− }
231− else {
232− out.push("0x");
233− out.push(this.number.toString(16));
234− }
235− }
236−};
237−
238−Atom.prototype.loob = function() {
239− switch ( this.number.intValue() ) {
240− case 0:
241− return true;
242− case 1:
243− return false;
244− default:
245− throw new Error("Bail");
246− }
247−};
248−
249−Atom.prototype.bump = function() {
250− return new Atom(this.number.add(BigInteger.ONE));
251−};
252−
253−var ida = i(1);
254−var heda = i(2);
255−var tala = i(3);
256−
257−Atom.prototype.cap = function() {
258− switch (this.number.intValue()) {
259− case 0:
260− case 1:
261− throw new Error("Bail");
262− default:
263− return this.number.testBit(this.number.bitLength() - 2) ? tala : heda;
264− }
265−};
266−
267−Atom.prototype.mas = function() {
268− switch (this.number.intValue()) {
269− case 0:
270− case 1:
271− throw new Error("Bail");
272− case 2:
273− case 3:
274− return ida;
275− default:
276− var n = this.number;
277− var l = n.bitLength() - 2;
278− var addTop = new BigInteger();
279− addTop.fromInt(1 << l);
280− var mask = new BigInteger();
281− mask.fromInt((1 << l)-1);
282− return new Atom(n.and(mask).xor(addTop));
283− }
284−};
285−
286−Atom.prototype.calculateMug = function() {
287− var a = this.number.toByteArray();
288− var b, c, d, e, f, bot;
289− for ( e = a.length - 1, b = (2166136261|0); ; ++b ) {
290− c = b;
291− bot = ( 0 === a[0] ) ? 1 : 0;
292− for ( d = e; d >= bot; --d ) {
293− c = _mug_fnv(c ^ (0xff & a[d]));
294− }
295− f = _mug_out(c);
296− if ( 0 !== f ) {
297− return f;
298− }
299− }
300−};
301−
302−Atom.prototype.shortCode = function() {
303− return this.number.toString(36); // max supported by BigInteger
304−};
305−
306−function s(str, radix) {
307− return new Atom(new BigInteger(str, radix));
308−}
309−
310−function i(num) {
311− if ( num < 256 ) {
312− return small[num];
313− }
314− else {
315− var bi = new BigInteger();
316− bi.fromInt(num);
317− return new Atom(bi);
318− }
319−}
320−
321−function m(str) {
322− var i, j, octs = new Array(str.length);
323− for ( i = 0, j = octs.length - 1; i < octs.length; ++i, --j ) {
324− octs[j] = (str.charCodeAt(i) & 0xff).toString(16);
325− }
326− return new Atom(new BigInteger(octs.join(''), 16))
327−}
328−
329−function dwim(a) {
330− var n = (arguments.length === 1 ? a : Array.apply(null, arguments));
331− if ( n instanceof Noun ) {
332− return n;
333− }
334− if ( typeof n === "number" ) {
335− return i(n);
336− }
337− else if ( Array.isArray(n) ) {
338− var cel = new Cell(dwim(n[n.length-2]), dwim(n[n.length-1]));
339− for ( var j = n.length-3; j >= 0; --j ) {
340− cel = new Cell(dwim(n[j]), cel);
341− }
342− return cel;
343− }
344− else if ( typeof n === "string" ) {
345− return m(n);
346− }
347−}
348−
349−Atom.prototype.valueOf = function() {
350− return this.number.bitLength() <= 32
351− ? this.number.intValue()
352− : this.number.toString();
353−};
354−
355−export default {
356− dwim: dwim,
357− Noun: Noun,
358− Cell: Cell,
359− Atom: {
360− Atom: Atom,
361− yes: i(0),
362− no: i(1),
363− fromMote: m,
364− fromInt: i,
365− fromString: s,
366− },
367−};
src/nockjs/serial.jsdeleted−162
@@ -1,162 +0,0 @@
1−import noun from './noun.js';
2−import list from './list.js';
3−import bits from './bits.js';
4−import NounMap from './hamt.js';
5−var Cell = noun.Cell, zero = noun.Atom.yes, one = noun.Atom.no, i = noun.Atom.fromInt, two = i(2), three = i(3);
6−
7−function rub(a, b) {
8− var c, d, e, w, x, y, z, p, q, m;
9−
10− m = bits.add(a, i(bits.met(0, b)));
11− x = a;
12−
13− while ( zero.equals(bits.cut(zero, x, one, b)) ) {
14− y = bits.add(one, x);
15−
16− // Sanity check: crash if decoding more bits than available
17− if ( bits.gth(x, m) ) {
18− throw new Error("Bail");
19− }
20−
21− x = y;
22− }
23−
24− if ( a.equals(x) ) {
25− return new Cell(one, zero);
26− }
27−
28− c = bits.sub(x, a);
29− d = bits.add(x, one);
30−
31− x = bits.dec(c);
32− y = bits.bex(x);
33− z = bits.cut(zero, d, x, b);
34−
35− e = bits.add(y, z);
36− w = bits.add(c, c);
37− y = bits.add(w, e);
38− z = bits.add(d, x);
39−
40− p = bits.add(w, e);
41− q = bits.cut(zero, z, e, b);
42−
43− return new Cell(p, q);
44−}
45−
46−function cue_in(m, a, b) {
47− var c,l,u,v,w,y,p,q,d,x;
48−
49− if ( zero.equals(bits.cut(zero, b, one, a)) ) {
50− x = bits.add(b, one);
51− c = rub(x, a);
52− p = bits.add(c.head, one);
53− q = c.tail;
54− m.insert(b, q);
55− }
56− else {
57− c = bits.add(two, b);
58− l = bits.add(one, b);
59−
60− if ( zero.equals(bits.cut(zero, l, one, a)) ) {
61− u = cue_in(m, a, c);
62− x = bits.add(u.head, c);
63− v = cue_in(m, a, x);
64− w = new Cell(u.tail.head, v.tail.head);
65− y = bits.add(u.head, v.head);
66− p = bits.add(two, y);
67− q = w;
68− m.insert(b, q);
69− }
70− else {
71− d = rub(c, a);
72− x = m.get(d.tail);
73−
74− if ( undefined === x ) {
75− throw new Error("Bail");
76− }
77−
78− p = bits.add(two, d.head);
79− q = x;
80− }
81− }
82− return new Cell(p, new Cell(q, zero));
83−}
84−
85−function cue(a) {
86− return cue_in(new NounMap(), a, zero).tail.head;
87−}
88−
89−function mat(a) {
90− if ( zero.equals(a) ) {
91− return noun.dwim(1, 1);
92− }
93− else {
94− var b = noun.dwim(bits.met(0, a)),
95− c = noun.dwim(bits.met(0, b)),
96− u = bits.dec(c),
97− v = bits.add(c, c),
98− x = bits.end(zero, u, b),
99− w = bits.bex(c),
100− y = bits.lsh(zero, u, a),
101− z = bits.mix(x, y),
102− p = bits.add(v, b),
103− q = bits.cat(zero, w, z);
104− return noun.dwim(p, q);
105− }
106−}
107−
108−function _jam_in_pair(m, h_a, t_a, b, l) {
109− var w = noun.dwim([2, 1], l),
110− x = bits.add(two, b),
111− d = _jam_in(m, h_a, x, w),
112− y = bits.add(x, d.head),
113− e = _jam_in(m, t_a, y, d.tail.head),
114− z = bits.add(d.head, e.head);
115−
116− return noun.dwim(bits.add(two, z), e.tail.head, zero);
117−}
118−
119−function _jam_in_ptr(m, u_c, l) {
120− var d = mat(u_c),
121− x = bits.lsh(zero, two, d.tail),
122− y = bits.add(two, d.head);
123−
124− return noun.dwim(y, [[y, bits.mix(three, x)], l], zero);
125−}
126−
127−function _jam_in_flat(m, a, l) {
128− var d = mat(a),
129− x = bits.add(one, d.head);
130−
131− return noun.dwim(x, [[x, bits.lsh(zero, one, d.tail)], l], zero);
132−}
133−
134−function _jam_in(m, a, b, l) {
135− var c = m.get(a);
136−
137− if ( undefined === c ) {
138− m.insert(a, b);
139− return a.deep ?
140− _jam_in_pair(m, a.head, a.tail, b, l) :
141− _jam_in_flat(m, a, l);
142− }
143− else if ( !a.deep && bits.met(0, a) <= bits.met(0, c) ) {
144− return _jam_in_flat(m, a, l);
145− }
146− else {
147− return _jam_in_ptr(m, c, l);
148− }
149−}
150−
151−function jam(n) {
152− var x = _jam_in(new NounMap(), n, zero, zero),
153− q = list.flop(x.tail.head);
154−
155− return bits.can(zero, q);
156−}
157−
158−export default {
159− cue: cue,
160− mat: mat,
161− jam: jam
162−};
src/nockjs/test/ack.jsdeleted−75
@@ -1,75 +0,0 @@
1−/*
2−^- @ux
3−%- jam
4−=> [2 2]
5−!=
6−=- (ack +< +>)
7−=> %kack
8−~% %kack ~ ~
9−|%
10−++ dec
11− ~/ %dec
12− |= a=@
13− =| i=@
14− |-
15− =+ n=+(i)
16− ?: =(n a)
17− i
18− $(i n)
19−++ ack
20− ~/ %ack
21− |= [m=@ n=@]
22− ?~ m +(n)
23− ?~ n $(m (dec m), n 1)
24− $(m (dec m), n $(n (dec n)))
25−--
26−*/
27−import compiler from '../compiler.js';
28−
29−import serial from '../serial.js';
30−import noun from '../noun.js';
31−import bits from '../bits.js';
32−import test from 'tape';
33−/*
34−^- @ux
35−%- jam
36−=> [2 2]
37−!=
38−=- (ack +< +>)
39−=> %kack
40−~% %kack ~ ~
41−|%
42−++ dec
43− ~/ %dec
44− |= a=@
45− =| i=@
46− |-
47− =+ n=+(i)
48− ?: =(n a)
49− i
50− $(i n)
51−++ ack
52− ~/ %ack
53− |= [m=@ n=@]
54− ?~ m +(n)
55− ?~ n $(m (dec m), n 1)
56− $(m (dec m), n $(n (dec n)))
57−--
58−*/
59−var n = noun.dwim, pill = noun.Atom.fromString("6eca1c00a1bac286c86483dc21dc324164dbf18777e361a371d5186b441bf187fe30f5b1b0bc071d5186b441bf1870028700287bb287d612eb0a1b21920e2aa1b26ce8a1af7086c8648384a86bdc21b21920c00a1e6364278c2598964324180143986482c9abdc21b21920b47a87df8db85d87484cc2e109efc6dc2ec38b4e2c9afc37e30ef8971b2b23c071e8e6c2cdf0168837e30f2643241f213dc21b9421b21920f1097ea13225b85d876266164dc5937bf1b26e2d8b26bf0b8b26eb63616bf818bf0b041", 16), formula = serial.cue(pill), context = new compiler.Context();
60−
61−var getSample = noun.Noun.fragmenter(n(6)),
62− jetCalled = false;
63−function decJet(core) {
64− jetCalled = true;
65− return bits.dec(getSample(core));
66−}
67−
68−test("nock ackermann function", function(t) {
69− t.plan(3);
70− t.equal(context.nock(n(2,2), formula).valueOf(), 7, "unjetted 2,2");
71− context = new compiler.Context(["kack", null, [["dec", decJet]]]);
72− formula = serial.cue(pill); // old formula will remember there is no jet for dec
73− t.equal(context.nock(n(3,9), formula).valueOf(), 4093, "jetted 3,9");
74− t.ok(jetCalled, 'jet was called');
75−});
src/nockjs/test/add.jsdeleted−57
@@ -1,57 +0,0 @@
1−/*
2−^- @ux
3−%- jam
4−=> [40 2]
5−!=
6−=- (add +< +>)
7−|%
8−++ add
9− |= [a=@ b=@]
10− ?~ a
11− b
12− $(a (dec a), b +(b))
13−++ dec
14− |= a=@
15− =| i=@
16− |-
17− =+ n=+(i)
18− ?: =(n a)
19− i
20− $(i n)
21−--
22−*/
23−import compiler from '../compiler.js';
24−
25−import serial from '../serial.js';
26−import noun from '../noun.js';
27−import test from 'tape';
28−import tchk from 'tape-check';
29−import { BigInteger } from 'jsbn';
30−/*
31−^- @ux
32−%- jam
33−=> [40 2]
34−!=
35−=- (add +< +>)
36−|%
37−++ add
38− |= [a=@ b=@]
39− ?~ a
40− b
41− $(a (dec a), b +(b))
42−++ dec
43− |= a=@
44− =| i=@
45− |-
46− =+ n=+(i)
47− ?: =(n a)
48− i
49− $(i n)
50−--
51−*/
52−var check = tchk.check, gen = tchk.gen, n = noun.dwim, pill = noun.Atom.fromString("829878621bce21b21920c888730c9059367e61cfcc39f98721920f9099110dd6986c86483c425fa84c8886dc2ec3b1330b26e2c9b478d937168f1b26e4e1887ab8e61b213c612cc4b21920fc4dc324164d5912c86483a425c21362dc2ec38b4e2c9ae2b041", 16), formula = serial.cue(pill), small = gen.intWithin(0,128), context = new compiler.Context();
53−
54−test("nock add", check(small, small, function(t, a, b) {
55− t.plan(1);
56− t.equal(context.nock(n(a,b), formula).valueOf(), a + b, "a+b");
57−}));
src/nockjs/test/bits.jsdeleted−15
@@ -1,15 +0,0 @@
1−import nounT from './noun.js';
2−import bits from '../bits.js';
3−import test from 'tape';
4−import tchk from 'tape-check';
5−var check = tchk.check;
6−
7−test('bytes', check({times: 10000}, nounT.genAtom, function (t, a) {
8− t.plan(1);
9− nounT.equals(t, bits.bytesToAtom(bits.atomToBytes(a)), a, 'round trip');
10−}));
11−
12−test('words', check({times: 10000}, nounT.genAtom, function (t, a) {
13− t.plan(1);
14− nounT.equals(t, bits.wordsToAtom(bits.atomToWords(a)), a, 'round trip');
15−}));
src/nockjs/test/decrement.jsdeleted−38
@@ -1,38 +0,0 @@
1−/*
2− * =< (dec 43)
3− * |%
4− * ++ dec
5− * |= a/@
6− * =| i/@
7− * |-
8− * =/ n/@ +(i)
9− * ?: =(a n)
10− * i
11− * $(i n)
12− * --
13− * [8 [1 8 [1 0] [1 8 [1 0] 8 [1 8 [4 0 6]
14− 6 [5 [0 62] 0 2]
15− [0 14]
16− 9 2 [0 6] [0 2] 0 15]
17− 9 2 0 1] 0 1]
18− 8 [9 2 0 1] 9 2 [0 4] [7 [0 3] 1 43] 0 11]
19− */
20−import noun from '../noun.js';
21−
22−nounT = require('./noun.js'),
23−n = noun.dwim,
24−compiler = require('../compiler.js'),
25−context = new compiler.Context(),
26−formula = n(8, [1, 8, [1, 0], [1, 8, [1, 0], 8, [1, 8, [4, 0, 6],
27− 6, [5, [0, 62], 0, 2],
28− [0, 14],
29− 9, 2, [0, 6], [0, 2], 0, 15],
30− 9, 2, 0, 1], 0, 1],
31− 8, [9, 2, 0, 1], 9, 2, [0, 4], [7, [0, 3], 1, 43], 0, 11),
32−product = context.nock(n(0), formula);
33−test = require('tape');
34−
35−test('decrement', function (t) {
36− t.plan(1);
37− nounT.equals(t, product, n(42), 'dec(43) == 42');
38−});
src/nockjs/test/hamt.jsdeleted−16
@@ -1,16 +0,0 @@
1−import nounT from './noun.js';
2−import test from 'tape';
3−import tchk from 'tape-check';
4−import { NounMap } from '../hamt.js';
5−var check = tchk.check;
6−
7−var g = nounT.genNoun,
8− m = new NounMap();
9−
10−test('maps work like maps', check({ times: 1000 }, g, g, g, function (t, k, v1, v2) {
11− t.plan(2);
12− m.insert(k, v1);
13− nounT.equals(t, m.get(k), v1, "first insert");
14− m.insert(k, v2);
15− nounT.equals(t, m.get(k), v2, "second insert");
16−}));
src/nockjs/test/noun.jsdeleted−88
@@ -1,88 +0,0 @@
1−import noun from '../noun.js';
2−import test from 'tape';
3−import tchk from 'testcheck';
4−import { BigInteger } from 'jsbn';
5−var gen = tchk.gen;
6−
7−function equals(t, got, expected, msg) {
8− if ( got.equals(expected) ) {
9− t.pass(msg);
10− return true;
11− }
12− else {
13− t.fail(msg);
14− t.comment("got: " + got.toString());
15− t.comment("expected: " + expected.toString());
16− return false;
17− }
18−}
19−
20−var genSmall = gen.posInt.then(noun.Atom.fromInt),
21− genBig = gen.array(gen.intWithin(0, 16), { minSize: 10 }).then(function (digits) {
22− for ( var i = 0; i < digits.length; ++i ) {
23− digits[i] = digits[i].toString(16);
24− }
25− return new noun.Atom.Atom(new BigInteger(digits.join(''), 16));
26− }),
27− genAtom = gen.oneOf([genSmall, genBig]);
28−
29−function mkCellGen(g1) {
30− return gen.array(g1, {size: 2}).then(function(a) {
31− return new noun.Cell(a[0], a[1]);
32− });
33−}
34−
35−var genCell = gen.nested(mkCellGen, genAtom),
36− genNoun = gen.oneOf([genAtom, genCell]);
37−
38−export default {
39− genAtom: genAtom,
40− genCell: genCell,
41− genNoun: genNoun,
42−};
43−
44−/*
45−const { check, gen } = require('tape-check')
46−
47−test('maps work like maps', check(
48−
49−test('addition is commutative', check(gen.int, gen.int, (t, numA, numB) => {
50− t.plan(1)
51− t.equal(numA + numB, numB + numA)
52−}));
53−*/
54−
55−/*
56−function randomAtom() {
57− var c, i, bytes = Math.floor(Math.random() * 4) + 1;
58− var c = new BigInteger();
59− var d = new BigInteger();
60− c.fromInt(0);
61− for ( i = 0; i < bytes; ++i ) {
62− d.fromInt(Math.floor(Math.random() * 0xff));
63− c = c.shiftLeft(8);
64− c = c.xor(d);
65− }
66− return new noun.Atom.Atom(c);
67−}
68−
69−function randomCell(depth) {
70− return new noun.Cell(randomNoun(depth+1), randomNoun(depth+1));
71−}
72−
73−function randomNoun(depth) {
74− if ( depth > 10 || Math.random() > 0.5) {
75− return randomAtom();
76− }
77− else {
78− return randomCell(depth);
79− }
80−}
81−*/
82−
83−export default {
84− equals: equals,
85− genAtom: genAtom,
86− genCell: genCell,
87− genNoun: genNoun,
88−};
src/nockjs/test/serial.jsdeleted−22
@@ -1,22 +0,0 @@
1−import serial from '../serial.js';
2−import noun from '../noun.js';
3−import test from 'tape';
4−import { check } from 'tape-check';
5−import nounT from './noun.js';
6−import { BigInteger } from 'jsbn';
7−var n = noun.dwim;
8−
9−test('examples', function(t) {
10− t.plan(5);
11− nounT.equals(t, serial.jam(n(42)), n(5456), 'jam 1');
12− nounT.equals(t, serial.cue(n(5456)), n(42), 'cue 1');
13− nounT.equals(t, serial.jam(n('foo', 'bar')), noun.Atom.fromString('1054973063816666730241'), 'jam 2');
14− nounT.equals(t, serial.cue(noun.Atom.fromString('1054973063816666730241')), n('foo', 'bar'), 'cue 2');
15− var addPill = new noun.Atom.Atom(new BigInteger("829878621bce21b21920c888730c9059367e61cfcc39f98721920f9099110dd6986c86483c425fa84c8886dc2ec3b1330b26e2c9b478d937168f1b26e4e1887ab8e61b213c612cc4b21920fc4dc324164d5912c86483a425c21362dc2ec38b4e2c9ae2b041", 16));
16− nounT.equals(t, serial.jam(serial.cue(addPill)), addPill, "add pill");
17−});
18−
19−test('generative', check({times: 1000}, nounT.genNoun, function(t, n) {
20− t.plan(1);
21− nounT.equals(t, serial.cue(serial.jam(n)), n, 'round trip');
22−}));