urbit/j/e/cue.c

97 lines
1.7 KiB
C
Raw Normal View History

2014-09-05 23:55:16 +04:00
/* j/5/cue.c
2013-09-29 00:21:18 +04:00
**
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-06 03:20:01 +03:00
_cue_in(u3p(u3h_root) har_p,
2014-11-04 21:39:56 +03:00
u3_atom a,
u3_atom b)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun p, q;
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
if ( 0 == u3qc_cut(0, b, 1, a) ) {
u3_noun x = u3qa_inc(b);
u3_noun c = u3qe_rub(x, a);
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
p = u3qa_inc(u3k(u3h(c)));
2014-09-06 00:13:24 +04:00
q = u3k(u3t(c));
2014-08-25 10:58:38 +04:00
2014-11-06 03:20:01 +03:00
u3h_put(har_p, u3k(b), u3k(q));
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(c);
u3z(x);
2013-09-29 00:21:18 +04:00
}
else {
2014-11-06 22:13:57 +03:00
u3_noun c = u3qa_add(2, b);
u3_noun l = u3qa_inc(b);
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
if ( 0 == u3qc_cut(0, l, 1, a) ) {
2014-09-06 00:13:24 +04:00
u3_noun u, v, w;
u3_noun x, y;
2013-09-29 00:21:18 +04:00
2014-11-04 21:39:56 +03:00
u = _cue_in(har_p, a, c);
2014-11-06 22:13:57 +03:00
x = u3qa_add(u3h(u), c);
2014-11-04 21:39:56 +03:00
v = _cue_in(har_p, a, x);
2014-09-06 00:13:24 +04:00
w = u3nc(u3k(u3h(u3t(u))), u3k(u3h(u3t(v))));
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
y = u3qa_add(u3h(u), u3h(v));
p = u3qa_add(2, y);
2014-08-25 10:58:38 +04:00
2013-09-29 00:21:18 +04:00
q = w;
2014-11-06 03:20:01 +03:00
u3h_put(har_p, u3k(b), u3k(q));
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(u); u3z(v); u3z(x); u3z(y);
2013-09-29 00:21:18 +04:00
}
else {
2014-11-06 22:13:57 +03:00
u3_noun d = u3qe_rub(c, a);
2014-11-06 03:20:01 +03:00
u3_noun x = u3h_get(har_p, u3k(u3t(d)));
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
p = u3qa_add(2, u3h(d));
2014-11-06 02:36:30 +03:00
if ( u3_none == x ) {
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
2013-09-29 00:21:18 +04:00
}
2014-08-25 10:58:38 +04:00
q = x;
2014-09-06 00:13:24 +04:00
u3z(d);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(l);
u3z(c);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
return u3nt(p, q, 0);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qe_cue(u3_atom a)
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
u3p(u3h_root) har_p = u3h_new();
2014-04-29 04:13:57 +04:00
2014-11-04 21:39:56 +03:00
u3_noun x = _cue_in(har_p, a, 0);
2014-09-06 00:13:24 +04:00
u3_noun y = u3k(u3h(u3t(x)));
2014-11-06 03:20:01 +03:00
u3h_free(har_p);
2014-09-06 00:13:24 +04:00
u3z(x);
return y;
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3we_cue(u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun a;
2013-09-29 00:21:18 +04:00
2014-11-18 00:56:51 +03:00
if ( (u3_none == (a = u3r_at(u3x_sam, cor))) ) {
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-11-06 22:13:57 +03:00
return u3qe_cue(a);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3ke_cue(u3_atom a)
2014-09-05 23:55:16 +04:00
{
2014-11-06 22:13:57 +03:00
u3_noun b = u3qe_cue(a);
2014-09-05 23:55:16 +04:00
2014-09-06 00:13:24 +04:00
u3z(a);
2014-09-05 23:55:16 +04:00
return b;
}