urbit/j/5/jam.c

145 lines
2.7 KiB
C
Raw Normal View History

2014-09-05 23:55:16 +04:00
/* j/5/jam.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
2014-08-25 10:58:38 +04:00
2013-09-29 00:21:18 +04:00
/* functions
*/
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-04 04:02:55 +03:00
_jam_in(u3p(u3_ch_root) har_p, u3_atom, u3_atom, u3_noun);
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-04 04:02:55 +03:00
_jam_in_pair(u3p(u3_ch_root) har_p,
2014-09-06 00:13:24 +04:00
u3_atom h_a,
u3_atom t_a,
u3_atom b,
u3_noun l)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun w = u3nc(u3nc(2, 1), u3k(l));
u3_noun x = u3_cqa_add(2, b);
2014-11-04 04:02:55 +03:00
u3_noun d = _jam_in(har_p, h_a, x, w);
2014-09-06 00:13:24 +04:00
u3_noun p_d, q_d, r_d;
u3_noun r;
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3_cr_trel(d, &p_d, &q_d, &r_d);
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun y = u3_cqa_add(x, p_d);
2014-11-04 04:02:55 +03:00
u3_noun e = _jam_in(har_p, t_a, y, q_d);
2014-09-06 00:13:24 +04:00
u3_noun p_e, q_e, r_e;
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3_cr_trel(e, &p_e, &q_e, &r_e);
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun z = u3_cqa_add(p_d, p_e);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
r = u3nt(u3_cqa_add(2, z), u3k(q_e), 0);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(z);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(e);
u3z(y);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(d);
u3z(x);
u3z(w);
2013-09-29 00:21:18 +04:00
return r;
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-04 04:02:55 +03:00
_jam_in_flat(u3p(u3_ch_root) har_p,
2014-09-06 00:13:24 +04:00
u3_atom a,
u3_noun l)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun d = u3_cqe_mat(a);
u3_noun x = u3_cqa_add(1, u3h(d));
u3_noun y = u3nt
(u3k(x), u3nc(u3nc(x, u3_cqc_lsh(0, 1, u3t(d))), u3k(l)), 0);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(d);
2013-09-29 00:21:18 +04:00
return y;
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-04 04:02:55 +03:00
_jam_in_ptr(u3p(u3_ch_root) har_p,
2014-09-06 00:13:24 +04:00
u3_atom u_c,
u3_noun l)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun d = u3_cqe_mat(u_c);
u3_atom x = u3_cqc_lsh(0, 2, u3t(d));
u3_atom y = u3_cqa_add(2, u3h(d));
u3_noun z = u3nt
(u3k(y), u3nc(u3nc(y, u3_cqc_mix(3, x)), u3k(l)), 0);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(d);
u3z(x);
2013-09-29 00:21:18 +04:00
return z;
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-04 04:02:55 +03:00
_jam_in(u3p(u3_ch_root) har_p,
2014-09-06 00:13:24 +04:00
u3_noun a,
u3_atom b,
u3_noun l)
2013-09-29 00:21:18 +04:00
{
2014-11-04 04:02:55 +03:00
u3_noun c = u3_ch_get(har_p, a);
2014-09-06 00:13:24 +04:00
u3_noun x;
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_none == c ) {
2014-11-04 04:02:55 +03:00
u3_ch_put(har_p, a, u3k(b));
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3ud(a) ) {
2014-11-04 04:02:55 +03:00
x = _jam_in_flat(har_p, a, l);
2013-09-29 00:21:18 +04:00
} else {
2014-11-04 04:02:55 +03:00
x = _jam_in_pair(har_p, u3h(a), u3t(a), b, l);
2013-09-29 00:21:18 +04:00
}
}
else {
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3ud(a) && u3_cr_met(0, a) <= u3_cr_met(0, c) ) {
2014-11-04 04:02:55 +03:00
x = _jam_in_flat(har_p, a, l);
2013-09-29 00:21:18 +04:00
}
else {
2014-11-04 04:02:55 +03:00
x = _jam_in_ptr(har_p, c, l);
2013-09-29 00:21:18 +04:00
}
}
return x;
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cqe_jam(u3_atom a)
2013-09-29 00:21:18 +04:00
{
2014-11-04 04:02:55 +03:00
u3p(u3_ch_root) har_p = u3_ch_new();
2014-08-25 10:58:38 +04:00
2014-11-04 04:02:55 +03:00
u3_noun x = _jam_in(har_p, a, 0, u3_nul);
2014-09-06 00:13:24 +04:00
u3_noun q = u3_cqb_flop(u3h(u3t(x)));
u3_noun r = u3_cqc_can(0, q);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(x);
u3z(q);
2014-11-04 04:02:55 +03:00
u3_ch_free(har_p);
2013-09-29 00:21:18 +04:00
return r;
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwe_jam(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-09-06 00:13:24 +04:00
if ( (u3_none == (a = u3_cr_at(u3_cv_sam, cor))) ) {
return u3_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
return u3_cqe_jam(a);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_atom
u3_cke_jam(u3_noun a)
2014-09-05 23:55:16 +04:00
{
2014-09-06 00:13:24 +04:00
u3_atom b = u3_cqe_jam(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;
}