tightening up jam jet

This commit is contained in:
Paul Driver 2018-03-14 17:53:12 -07:00
parent 67d3460a91
commit 1d1593274c

View File

@ -61,101 +61,131 @@
return z; return z;
} }
#define JAM_NONE 0
#define JAM_HEAD 1
#define JAM_TAIL 2
typedef struct { typedef struct {
u3_noun a; c3_y sat_y;
u3_noun b; u3_noun nun;
u3_noun l; u3_noun len;
u3_noun* r; u3_noun lis;
u3_noun hed; u3_noun hed;
u3_noun tel;
} jamframe; } jamframe;
static inline void static inline jamframe*
_jam_push(u3_noun a, u3_noun b, u3_noun l, u3_noun *r) _jam_push(c3_ys mov, c3_ys off)
{ {
jamframe* fam = u3a_push(sizeof(jamframe)); u3R->cap_p += mov;
fam->a = a; return u3to(jamframe, u3R->cap_p + off);
fam->b = b;
fam->l = l;
fam->r = r;
fam->hed = u3_none;
fam->tel = u3_none;
}
static inline void
_jam_pop()
{
u3a_pop(sizeof(jamframe));
} }
static inline jamframe* static inline jamframe*
_jam_peek() _jam_pop(c3_ys mov, c3_ys off)
{ {
return (jamframe*) u3a_peek(sizeof(jamframe)); u3R->cap_p -= mov;
return u3to(jamframe, u3R->cap_p + off);
} }
static u3_noun
_jam_cap(u3_atom a)
{
u3p(u3h_root) har_p = u3h_new();
c3_o nor_o = u3a_is_north(u3R);
c3_y wis_y = c3_wiseof(jamframe);
c3_ys mov = ( c3y == nor_o ? -wis_y : wis_y );
c3_ys off = ( c3y == nor_o ? 0 : -wis_y );
jamframe* fam = _jam_push(mov, off);
fam->sat_y = JAM_NONE;
fam->nun = a;
fam->len = 0;
fam->lis = u3_nul;
u3_noun q, r = u3_none;
c3_w dep_w;
for ( dep_w = 1; dep_w > 0; ) {
switch ( fam->sat_y ) {
case JAM_NONE: {
u3_noun nun = fam->nun;
u3_noun len = fam->len;
u3_noun lis = fam->lis;
u3_weak got = u3h_get(har_p, nun);
if ( u3_none == got ) {
u3h_put(har_p, nun, u3k(len));
if ( c3n == u3du(nun) ) {
r = _jam_flat(nun, lis);
fam = _jam_pop(mov, off);
--dep_w;
u3z(len);
}
else {
fam->sat_y = JAM_HEAD;
fam = _jam_push(mov, off);
fam->sat_y = JAM_NONE;
fam->nun = u3h(nun);
fam->len = u3qa_add(2, len);
fam->lis = u3nc(u3nc(2, 1), lis);
++dep_w;
}
}
else {
if ( c3y == u3ud(nun) && (u3r_met(0, nun) <= u3r_met(0, got)) ) {
r = _jam_flat(nun, lis);
}
else {
r = _jam_ptr(got, lis);
}
fam = _jam_pop(mov, off);
--dep_w;
u3z(len);
}
break;
}
case JAM_HEAD: {
u3_noun p_r, q_r, r_r;
u3x_trel(r, &p_r, &q_r, &r_r);
u3_noun nun = fam->nun;
fam->sat_y = JAM_TAIL;
fam->hed = r;
u3_noun z = u3qa_add(2, fam->len);
fam = _jam_push(mov, off);
fam->sat_y = JAM_NONE;
fam->nun = u3t(nun);
fam->len = u3qa_add(z, p_r);
fam->lis = u3k(q_r);
u3z(z);
++dep_w;
break;
}
case JAM_TAIL: {
u3_noun len = fam->len;
r = _jam_pair(u3qa_add(2, len), fam->hed, r);
fam = _jam_pop(mov, off);
--dep_w;
u3z(len);
break;
}
default:
c3_assert(0);
return u3_none;
}
}
q = u3qb_flop(u3h(u3t(r)));
u3z(r);
r = u3qc_can(0, q);
u3z(q);
u3h_free(har_p);
return r;
}
u3_noun u3_noun
u3qe_jam(u3_atom a) u3qe_jam(u3_atom a)
{ {
u3p(u3h_root) har_p = u3h_new(); return _jam_cap(a);
u3p(jamframe) empty = u3R->cap_p;
jamframe* fam;
u3_noun out, c, x, q, r;
_jam_push(a, 0, u3_nul, &out);
while ( empty != u3R->cap_p ) {
fam = _jam_peek();
if ( u3_none != fam->tel ) {
u3_noun z = u3qa_add(2, fam->b);
x = _jam_pair(z, fam->hed, fam->tel);
}
else if ( u3_none != fam->hed ) {
u3_noun p_d, q_d, r_d;
u3x_trel(fam->hed, &p_d, &q_d, &r_d);
{
u3_noun z = u3qa_add(2, fam->b);
u3_noun y = u3qa_add(z, p_d);
_jam_push(u3t(fam->a), y, u3k(q_d), &(fam->tel));
u3z(z);
continue;
}
}
else {
a = fam->a;
c = u3h_get(har_p, a);
if ( u3_none != c ) {
if ( (c3y == u3ud(a)) && u3r_met(0, a) <= u3r_met(0, c) ) {
x = _jam_flat(a, fam->l);
}
else {
x = _jam_ptr(c, fam->l);
}
}
else {
u3h_put(har_p, a, u3k(fam->b));
if ( c3y == u3ud(a) ) {
x = _jam_flat(a, fam->l);
}
else {
u3_noun z = u3qa_add(2, fam->b);
u3_noun w = u3nc(u3nc(2, 1), fam->l);
_jam_push(u3h(a), z, w, &(fam->hed));
continue;
}
}
}
*(fam->r) = x;
u3z(fam->b);
_jam_pop();
}
q = u3qb_flop(u3h(u3t(out)));
r = u3qc_can(0, q);
u3z(out);
u3z(q);
u3h_free(har_p);
return r;
} }
u3_noun u3_noun
u3we_jam(u3_noun cor) u3we_jam(u3_noun cor)