urbit/j/5/cue.c

98 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
**
** This file is in the public domain.
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
2013-09-29 00:21:18 +04:00
2014-09-04 01:33:18 +04:00
static u2_noun
_cue_in(u2_ch_root* har_u,
2014-09-04 01:33:18 +04:00
u2_atom a,
u2_atom b)
2013-09-29 00:21:18 +04:00
{
u2_noun p, q;
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
if ( 0 == u2_cqc_cut(0, b, 1, a) ) {
u2_noun x = u2_cqa_inc(b);
u2_noun c = u2_cqe_rub(x, a);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
p = u2_cqa_inc(u2k(u2h(c)));
2014-08-20 10:22:41 +04:00
q = u2k(u2t(c));
2014-08-25 10:58:38 +04:00
u2_ch_put(har_u, u2k(b), u2k(q));
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(c);
u2z(x);
2013-09-29 00:21:18 +04:00
}
else {
2014-09-03 07:36:29 +04:00
u2_noun c = u2_cqa_add(2, b);
u2_noun l = u2_cqa_inc(b);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
if ( 0 == u2_cqc_cut(0, l, 1, a) ) {
2013-09-29 00:21:18 +04:00
u2_noun u, v, w;
u2_noun x, y;
2014-08-25 10:58:38 +04:00
u = _cue_in(har_u, a, c);
2014-09-03 07:36:29 +04:00
x = u2_cqa_add(u2h(u), c);
2014-08-25 10:58:38 +04:00
v = _cue_in(har_u, a, x);
w = u2nc(u2k(u2h(u2t(u))), u2k(u2h(u2t(v))));
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
y = u2_cqa_add(u2h(u), u2h(v));
p = u2_cqa_add(2, y);
2014-08-25 10:58:38 +04:00
2013-09-29 00:21:18 +04:00
q = w;
u2_ch_put(har_u, u2k(b), u2k(q));
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(u); u2z(v); u2z(x); u2z(y);
2013-09-29 00:21:18 +04:00
}
else {
2014-09-03 07:36:29 +04:00
u2_noun d = u2_cqe_rub(c, a);
u2_noun x = u2_ch_get(har_u, u2k(u2t(d)));
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
p = u2_cqa_add(2, u2h(d));
2014-05-07 22:21:16 +04:00
if ( u2_none == x ) {
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
2013-09-29 00:21:18 +04:00
}
2014-08-25 10:58:38 +04:00
q = x;
2014-08-20 10:22:41 +04:00
u2z(d);
2013-09-29 00:21:18 +04:00
}
2014-08-20 10:22:41 +04:00
u2z(l);
u2z(c);
2013-09-29 00:21:18 +04:00
}
2014-08-20 10:22:41 +04:00
return u2nt(p, q, 0);
2013-09-29 00:21:18 +04:00
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cqe_cue(u2_atom a)
2013-09-29 00:21:18 +04:00
{
u2_ch_root* har_u = u2_ch_new();
2014-04-29 04:13:57 +04:00
2014-08-25 10:58:38 +04:00
u2_noun x = _cue_in(har_u, a, 0);
2014-08-20 10:22:41 +04:00
u2_noun y = u2k(u2h(u2t(x)));
u2_ch_free(har_u);
2014-08-20 10:22:41 +04:00
u2z(x);
return y;
2013-09-29 00:21:18 +04:00
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cwe_cue(u2_noun cor)
2013-09-29 00:21:18 +04:00
{
u2_noun a;
2014-08-20 10:22:41 +04:00
if ( (u2_none == (a = u2_cr_at(u2_cv_sam, cor))) ) {
return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-09-03 07:36:29 +04:00
return u2_cqe_cue(a);
2013-09-29 00:21:18 +04:00
}
}
2014-09-05 23:55:16 +04:00
u2_noun
u2_cke_cue(u2_atom a)
{
u2_noun b = u2_cqe_cue(a);
u2z(a);
return b;
}