cleans up cue, correcting comments and refcounting

This commit is contained in:
Joe Bryan 2019-05-13 14:03:36 -07:00
parent 365239e1de
commit 7c4b1bf105

View File

@ -38,111 +38,116 @@ _cue_push(c3_ys mov,
fam_u->hed = hed;
}
static inline cueframe*
static inline cueframe
_cue_pop(c3_ys mov, c3_ys off)
{
cueframe* fam_u = u3to(cueframe, u3R->cap_p + off);
u3R->cap_p -= mov;
return fam_u;
return *fam_u;
}
/* _cue_rub(): rub, retaining target (b)
*/
static inline u3_atom
_cue_rub(u3_atom a, u3_atom b)
{
u3_atom pro = u3qe_rub(a, b);
u3z(a);
return pro;
}
#define CUE_ROOT 0
#define CUE_HEAD 1
#define CUE_TAIL 2
u3_noun
u3qe_cue(u3_atom a)
{
c3_y wis_y = c3_wiseof(cueframe);
c3_o nor_o = u3a_is_north(u3R);
c3_ys mov = ( c3y == nor_o ? -wis_y : wis_y );
c3_ys off = ( c3y == nor_o ? 0 : -wis_y );
c3_ys mov, off;
# define CUE_ROOT 0
# define CUE_HEAD 1
# define CUE_TAIL 2
{
c3_y wis_y = c3_wiseof(cueframe);
c3_o nor_o = u3a_is_north(u3R);
mov = ( c3y == nor_o ? -wis_y : wis_y );
off = ( c3y == nor_o ? 0 : -wis_y );
}
u3p(u3h_root) har_p = u3h_new();
// stash the current stack post
//
u3p(cueframe) cap_p = u3R->cap_p;
_cue_push(mov, off, CUE_ROOT, 0, 0, 0);
u3p(u3h_root) har_p = u3h_new();
u3_atom cur = 0;
u3_atom wid = 0;
u3_noun pro = u3_none;
u3_atom wid, pro;
// read from .a at .cur
// read from atom at at cursor
// push on to the stack and continue (cell-head recursion)
// or set .wid and .pro and goto give
// TRANSFER .cur
//
pass: {
while ( 1 ) {
// read tag bit at .cur
//
c3_y tag_y = u3qc_cut(0, cur, 1, a);
// read tag bit at cur
//
c3_y tag_y = u3qc_cut(0, cur, 1, a);
// low bit unset, cursor points to an atom
//
if ( 0 == tag_y ) {
u3_noun bur = _cue_rub(u3qa_inc(cur), a);
pro = u3k(u3t(bur));
u3h_put(har_p, u3k(cur), u3k(pro));
wid = u3qa_inc(u3h(bur));
u3z(bur);
goto give;
// low bit unset, cursor points to an atom
//
if ( 0 == tag_y ) {
u3_noun bur;
{
u3_noun x = u3qa_inc(cur);
bur = u3qe_rub(x, a);
u3z(x);
}
else {
// read tag bit at (1 + cur)
//
{
u3_noun l = u3qa_inc(cur);
tag_y = u3qc_cut(0, l, 1, a);
u3z(l);
}
// next bit unset, cursor points to a cell
//
if ( 0 == tag_y ) {
_cue_push(mov, off, CUE_HEAD, cur, 0, 0);
cur = u3ka_add(2, cur);
continue;
}
// next bit set, cursor points to a backreference
//
else {
u3_noun bur;
bur = _cue_rub(u3qa_add(2, cur), a);
pro = u3h_get(har_p, u3k(u3t(bur)));
pro = u3k(u3t(bur));
u3h_put(har_p, cur, u3k(pro));
wid = u3qa_inc(u3h(bur));
if ( u3_none == pro ) {
return u3m_bail(c3__exit);
}
u3z(bur);
goto give;
}
wid = u3qa_add(2, u3h(bur));
// read tag bit at (1 + cur)
//
{
u3_noun x = u3qa_inc(cur);
tag_y = u3qc_cut(0, x, 1, a);
u3z(x);
}
u3z(bur);
goto give;
}
// next bit set, cursor points to a backreference
//
if ( 1 == tag_y ) {
u3_noun bur;
{
u3_noun x = u3ka_add(2, cur);
bur = u3qe_rub(x, a);
u3z(x);
}
pro = u3h_get(har_p, u3k(u3t(bur)));
if ( u3_none == pro ) {
return u3m_bail(c3__exit);
}
wid = u3qa_add(2, u3h(bur));
u3z(bur);
goto give;
}
// next bit unset, cursor points to a cell
//
{
_cue_push(mov, off, CUE_HEAD, cur, 0, 0);
cur = u3qa_add(2, cur);
goto pass;
}
}
// read from .wid, .pro, and top of stack
// pop off the stack and read from .wid and .pro
// push on to the stack and goto pass (cell-tail recursion)
// or pop the stack and continue
// or pop the stack and continue (goto give)
// TRANSFER .wid, .pro, and contents of .fam_u
//
give: {
cueframe fam_u = *(_cue_pop(mov, off));
cueframe fam_u = _cue_pop(mov, off);
switch ( fam_u.tag_y ) {
default: {
@ -154,8 +159,9 @@ u3qe_cue(u3_atom a)
}
case CUE_HEAD: {
cur = u3ka_add(2, u3qa_add(wid, fam_u.cur));
_cue_push(mov, off, CUE_TAIL, fam_u.cur, wid, pro);
cur = u3ka_add(2, u3qa_add(wid, fam_u.cur));
goto pass;
}
@ -168,11 +174,12 @@ u3qe_cue(u3_atom a)
}
}
u3z(cur);
u3z(wid);
u3h_free(har_p);
u3R->cap_p = cap_p;
// sanity check
//
c3_assert( u3R->cap_p == cap_p );
return pro;
}