urbit/j/e/jam.c

145 lines
2.6 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-06 03:20:01 +03:00
_jam_in(u3p(u3h_root) har_p, u3_atom, u3_atom, u3_noun);
2014-09-06 00:13:24 +04:00
static u3_noun
2014-11-06 03:20:01 +03:00
_jam_in_pair(u3p(u3h_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));
2014-11-06 22:13:57 +03:00
u3_noun x = u3qa_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-11-06 03:20:01 +03:00
u3r_trel(d, &p_d, &q_d, &r_d);
2013-09-29 00:21:18 +04:00
{
2014-11-06 22:13:57 +03:00
u3_noun y = u3qa_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-11-06 03:20:01 +03:00
u3r_trel(e, &p_e, &q_e, &r_e);
2013-09-29 00:21:18 +04:00
{
2014-11-06 22:13:57 +03:00
u3_noun z = u3qa_add(p_d, p_e);
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
r = u3nt(u3qa_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-06 03:20:01 +03:00
_jam_in_flat(u3p(u3h_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-11-06 22:13:57 +03:00
u3_noun d = u3qe_mat(a);
u3_noun x = u3qa_add(1, u3h(d));
2014-09-06 00:13:24 +04:00
u3_noun y = u3nt
2014-11-06 22:13:57 +03:00
(u3k(x), u3nc(u3nc(x, u3qc_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-06 03:20:01 +03:00
_jam_in_ptr(u3p(u3h_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-11-06 22:13:57 +03:00
u3_noun d = u3qe_mat(u_c);
u3_atom x = u3qc_lsh(0, 2, u3t(d));
u3_atom y = u3qa_add(2, u3h(d));
2014-09-06 00:13:24 +04:00
u3_noun z = u3nt
2014-11-06 22:13:57 +03:00
(u3k(y), u3nc(u3nc(y, u3qc_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-06 03:20:01 +03:00
_jam_in(u3p(u3h_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-06 03:20:01 +03:00
u3_noun c = u3h_get(har_p, a);
2014-09-06 00:13:24 +04:00
u3_noun x;
2013-09-29 00:21:18 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == c ) {
2014-11-06 03:20:01 +03:00
u3h_put(har_p, a, u3k(b));
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( c3y == 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-11-06 03:20:01 +03:00
if ( c3y == u3ud(a) && u3r_met(0, a) <= u3r_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
2014-11-06 22:13:57 +03:00
u3qe_jam(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-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-11-06 22:13:57 +03:00
u3_noun q = u3qb_flop(u3h(u3t(x)));
u3_noun r = u3qc_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-06 03:20:01 +03:00
u3h_free(har_p);
2013-09-29 00:21:18 +04:00
return r;
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3we_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-11-06 03:20:01 +03:00
if ( (u3_none == (a = u3r_at(u3v_sam, cor))) ) {
return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-11-06 22:13:57 +03:00
return u3qe_jam(a);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_atom
2014-11-06 22:13:57 +03:00
u3ke_jam(u3_noun a)
2014-09-05 23:55:16 +04:00
{
2014-11-06 22:13:57 +03:00
u3_atom b = u3qe_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;
}