urbit/gen164/5/jam.c

136 lines
2.6 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/3/jam.c
**
** 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
*/
static u2_noun
_jam_in(u2_ch_root* har_u, u2_atom, u2_atom, u2_noun);
2013-09-29 00:21:18 +04:00
2014-09-04 01:33:18 +04:00
static u2_noun
_jam_in_pair(u2_ch_root* har_u,
2014-09-04 01:33:18 +04:00
u2_atom h_a,
u2_atom t_a,
u2_atom b,
u2_noun l)
2013-09-29 00:21:18 +04:00
{
2014-08-20 10:22:41 +04:00
u2_noun w = u2nc(u2nc(2, 1), u2k(l));
2014-09-03 07:36:29 +04:00
u2_noun x = u2_cqa_add(2, b);
2014-08-25 10:58:38 +04:00
u2_noun d = _jam_in(har_u, h_a, x, w);
2013-09-29 00:21:18 +04:00
u2_noun p_d, q_d, r_d;
u2_noun r;
2014-08-20 10:22:41 +04:00
u2_cr_trel(d, &p_d, &q_d, &r_d);
2013-09-29 00:21:18 +04:00
{
2014-09-03 07:36:29 +04:00
u2_noun y = u2_cqa_add(x, p_d);
2014-08-25 10:58:38 +04:00
u2_noun e = _jam_in(har_u, t_a, y, q_d);
2013-09-29 00:21:18 +04:00
u2_noun p_e, q_e, r_e;
2014-08-20 10:22:41 +04:00
u2_cr_trel(e, &p_e, &q_e, &r_e);
2013-09-29 00:21:18 +04:00
{
2014-09-03 07:36:29 +04:00
u2_noun z = u2_cqa_add(p_d, p_e);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
r = u2nt(u2_cqa_add(2, z), u2k(q_e), 0);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(z);
2013-09-29 00:21:18 +04:00
}
2014-08-20 10:22:41 +04:00
u2z(e);
u2z(y);
2013-09-29 00:21:18 +04:00
}
2014-08-20 10:22:41 +04:00
u2z(d);
u2z(x);
u2z(w);
2013-09-29 00:21:18 +04:00
return r;
}
2014-09-04 01:33:18 +04:00
static u2_noun
_jam_in_flat(u2_ch_root* har_u,
2014-09-04 01:33:18 +04:00
u2_atom a,
u2_noun l)
2013-09-29 00:21:18 +04:00
{
2014-09-03 07:36:29 +04:00
u2_noun d = u2_cqe_mat(a);
u2_noun x = u2_cqa_add(1, u2h(d));
2014-08-20 10:22:41 +04:00
u2_noun y = u2nt
2014-09-03 07:36:29 +04:00
(u2k(x), u2nc(u2nc(x, u2_cqc_lsh(0, 1, u2t(d))), u2k(l)), 0);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(d);
2013-09-29 00:21:18 +04:00
return y;
}
2014-09-04 01:33:18 +04:00
static u2_noun
_jam_in_ptr(u2_ch_root* har_u,
2014-09-04 01:33:18 +04:00
u2_atom u_c,
u2_noun l)
2013-09-29 00:21:18 +04:00
{
2014-09-03 07:36:29 +04:00
u2_noun d = u2_cqe_mat(u_c);
u2_atom x = u2_cqc_lsh(0, 2, u2t(d));
u2_atom y = u2_cqa_add(2, u2h(d));
2014-08-20 10:22:41 +04:00
u2_noun z = u2nt
2014-09-03 07:36:29 +04:00
(u2k(y), u2nc(u2nc(y, u2_cqc_mix(3, x)), u2k(l)), 0);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(d);
u2z(x);
2013-09-29 00:21:18 +04:00
return z;
}
2014-09-04 01:33:18 +04:00
static u2_noun
_jam_in(u2_ch_root* har_u,
2014-09-04 01:33:18 +04:00
u2_noun a,
u2_atom b,
u2_noun l)
2013-09-29 00:21:18 +04:00
{
u2_noun c = u2_ch_get(har_u, u2k(a));
2013-09-29 00:21:18 +04:00
u2_noun x;
2014-05-07 22:21:16 +04:00
if ( u2_none == c ) {
u2_ch_put(har_u, u2k(a), u2k(b));
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
if ( u2_yes == u2ud(a) ) {
2014-08-25 10:58:38 +04:00
x = _jam_in_flat(har_u, a, l);
2013-09-29 00:21:18 +04:00
} else {
2014-08-25 10:58:38 +04:00
x = _jam_in_pair(har_u, u2h(a), u2t(a), b, l);
2013-09-29 00:21:18 +04:00
}
}
else {
2014-08-20 10:22:41 +04:00
if ( u2_yes == u2ud(a) && u2_cr_met(0, a) <= u2_cr_met(0, c) ) {
2014-08-25 10:58:38 +04:00
x = _jam_in_flat(har_u, a, l);
2013-09-29 00:21:18 +04:00
}
else {
2014-08-25 10:58:38 +04:00
x = _jam_in_ptr(har_u, c, l);
2013-09-29 00:21:18 +04:00
}
}
return x;
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cqe_jam(u2_atom a)
2013-09-29 00:21:18 +04:00
{
u2_ch_root* har_u = u2_ch_new();
2014-08-25 10:58:38 +04:00
u2_noun x = _jam_in(har_u, a, 0, u2_nul);
2014-09-03 07:36:29 +04:00
u2_noun q = u2_cqb_flop(u2h(u2t(x)));
u2_noun r = u2_cqc_can(0, q);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(x);
u2z(q);
u2_ch_free(har_u);
2013-09-29 00:21:18 +04:00
return r;
}
2014-09-04 01:33:18 +04:00
u2_noun
u2_cwe_jam(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_jam(a);
2013-09-29 00:21:18 +04:00
}
}