10From [Joe Bryan](https://github.com/joemfb/nock.js/blob/master/nock.js):
12 (function (self, factory) {
15 if (typeof define === 'function' && define.amd) {
17 } else if (typeof module === 'object' && typeof module.exports === 'object') {
18 module.exports = factory()
26 * Nock is a combinator interpreter on nouns. A noun is an atom or a cell.
27 * An atom is an unsigned integer of any size; a cell is an ordered pair of nouns.
29 * @see http://urbit.org/docs/nock/definition/
30 * @see http://media.urbit.org/whitepaper.pdf
39 * `s` is a subject noun,
40 * `f` is a formula (or cell of formulas)
46 * wut (?): test for atom (1) or cell (0)
52 return typeof n === 'number' ? 1 : 0
56 * lus (+): increment an atom
62 if (wut(n) === 0) throw new Error('lus cell')
67 * tis (=): test equality
74 if (wut(n) === 1) throw new Error('tis atom')
75 return deepEqual(n[0], n[1]) ? 0 : 1
79 * fas (/): resolve a tree address
84 * /[(a + a) b] /[2 /[a b]]
85 * /[(a + a + 1) b] /[3 /[a b]]
88 function fas (addr, n) {
89 if (n === undefined) throw new Error('invalid fas noun')
90 if (addr === 0) throw new Error('invalid fas addr: 0')
92 if (addr === 1) return n
93 if (addr === 2) return n[0]
94 if (addr === 3) return n[1]
96 return fas(2 + (addr % 2), fas((addr / 2) | 0, n))
102 * slot (0): resolve a tree address
106 function slot (s, f) {
109 if (p === undefined) throw new Error('invalid fas addr: ' + f)
115 * constant (1): return the formula regardless of subject
119 function constant (s, f) {
124 * evaluate (2): evaluate the product of second formula against the product of the first
126 * *[a 2 b c] *[*[a b] *[a c]]
128 function evaluate (s, f) {
129 return nock(nock(s, f[0]), nock(s, f[1]))
133 * cell (3): test if the product is a cell
137 function cell (s, f) {
138 return wut(nock(s, f))
142 * incr (4): increment the product
146 function incr (s, f) {
147 return lus(nock(s, f))
151 * eq (5): test for equality between nouns in the product
156 return tis(nock(s, f))
162 * ife (6): if/then/else
164 * *[a 6 b c d] *[a 2 [0 1] 2 [1 c d] [1 0] 2 [1 2 3] [1 0] 4 4 b]
166 function macroIfe (s, f) {
167 return nock(s, [2, [[0, 1], [2, [[1, [f[1][0], f[1][1]]], [[1, 0], [2, [[1, [2, 3]], [[1, 0], [4, [4, f[0]]]]]]]]]]])
170 function ife (s, f) {
171 var cond = nock(s, f[0])
173 if (cond === 0) return nock(s, f[1][0])
174 if (cond === 1) return nock(s, f[1][1])
176 throw new Error('invalid ife conditional')
180 * compose (7): evaluate formulas composed left-to-right
182 * *[a 7 b c] *[a 2 b 1 c]
184 function macroCompose (s, f) {
185 return nock(s, [2, [f[0], [1, f[1]]]])
188 function compose (s, f) {
190 // return nock(nock(s, f[0]), constant(s, f[1]))
191 return nock(nock(s, f[0]), f[1])
195 * extend (8): evaluate the second formula against [product of first, subject]
197 * *[a 8 b c] *[a 7 [[7 [0 1] b] 0 1] c]
199 function macroExtend (s, f) {
200 return nock(s, [7, [[[7, [[0, 1], f[0]]], [0, 1]], f[1]]])
203 function extend (s, f) {
205 // return nock([compose(s, [[0, 1], f[0]]), s], f[1])
206 return nock([nock(s, f[0]), s], f[1])
210 * invoke (9): construct a core and evaluate one of it's arms against it
212 * *[a 9 b c] *[a 7 c 2 [0 1] 0 b]
214 function macroInvoke (s, f) {
215 return nock(s, [7, [f[1], [2, [[0, 1], [0, f[0]]]]]])
218 function invoke (s, f) {
219 var prod = nock(s, f[1])
220 return nock(prod, slot(prod, f[0]))
224 * hint (10): skip first formula, evaluate second
226 * *[a 10 [b c] d] *[a 8 c 7 [0 3] d]
229 function macroHint (s, f) {
230 if (wut(f[0]) === 0) return nock(s, [8, [f[0][1], [7, [[0, 3], f[1]]]]])
234 function hint (s, f) {
235 if (wut(f[0]) === 0) {
236 if (wut(f[0][1]) === 1) throw new Error('invalid hint')
242 /* indexed formula functions */
243 var macroFormulas = [slot, constant, evaluate, cell, incr, eq, macroIfe, macroCompose, macroExtend, macroInvoke, macroHint]
244 var formulas = [slot, constant, evaluate, cell, incr, eq, ife, compose, extend, invoke, hint]
251 * *[a [b c] d] [*[a b c] *[a d]]
254 function nock (s, f) {
255 if (wut(f[0]) === 0) return [nock(s, f[0]), nock(s, f[1])]
259 if (idx > 10) throw new Error('invalid formula: ' + idx)
261 if (useMacros) return macroFormulas[idx](s, f[1])
263 return formulas[idx](s, f[1])
266 /* construct a JS noun (group an array into pairs, associating right) */
268 if (!x.length) return x
270 if (x.length === 1) return assoc(x[0])
272 return [assoc(x[0]), assoc(x.slice(1))]
275 /* deep equality for arrays or primitives */
276 function deepEqual (a, b) {
277 if (a === b) return true
279 if (!(Array.isArray(a) && Array.isArray(b))) return false
280 if (a.length !== b.length) return false
282 for (var i = 0; i < a.length; i++) {
283 if (!deepEqual(a[i], b[i])) return false
289 /* parse a hoon-serialized nock formula and construct a JS noun */
290 function parseNoun (x) {
291 if (Array.isArray(x)) return assoc(x)
293 if (typeof x === 'string') {
294 var str = x.replace(/[\."']/g, '').split(' ').join(',')
295 return assoc(JSON.parse(str))
301 function nockInterface () {
302 var args = [].slice.call(arguments)
303 var subject, formula, noun
305 if (args.length === 1) {
306 formula = parseNoun(args[0])
307 } else if (args.length === 2) {
308 subject = parseNoun(args[0])
309 formula = parseNoun(args[1])
316 if (!formula) throw new Error('formula required')
323 return nock(subject, formula)
329 useMacros: function (arg) {
330 useMacros = arg === undefined || arg
335 parseNoun: parseNoun,
353 macroCompose: macroCompose,
355 macroExtend: macroExtend,
357 macroInvoke: macroInvoke,
359 macroHint: macroHint,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list