Merge pull request #948 from frodwith/charm

Charm
This commit is contained in:
Elliot Glaysher 2018-03-19 14:57:50 -07:00 committed by GitHub
commit a85369fa1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 411 additions and 273 deletions

View File

@ -291,6 +291,22 @@
void*
u3a_wealloc(void* lag_v, c3_w len_w);
/* u3a_push(): allocate space on the road stack
*/
void*
u3a_push(c3_w len_w);
/* u3a_pop(): deallocate space on the road stack
*/
void
u3a_pop(c3_w len_w);
/* u3a_peek(): examine the top of the road stack
*/
void*
u3a_peek(c3_w len_w);
/* C-style aligned allocation - *not* compatible with above.
*/
/* u3a_malloc(): aligned storage measured in bytes.

View File

@ -3,32 +3,19 @@
*/
#include "all.h"
/* functions
*/
static u3_noun
_jam_in(u3p(u3h_root) har_p, u3_atom, u3_atom, u3_noun);
static u3_noun
_jam_in_pair(u3p(u3h_root) har_p,
u3_atom h_a,
u3_atom t_a,
u3_atom b,
u3_noun l)
_jam_pair(u3_noun x, u3_noun d, u3_noun e)
{
u3_noun w = u3nc(u3nc(2, 1), u3k(l));
u3_noun x = u3qa_add(2, b);
u3_noun d = _jam_in(har_p, h_a, x, w);
u3_noun p_d, q_d, r_d;
u3_noun r;
u3r_trel(d, &p_d, &q_d, &r_d);
u3_noun r, p_d, q_d, r_d;
u3x_trel(d, &p_d, &q_d, &r_d);
{
u3_noun y = u3qa_add(x, p_d);
u3_noun e = _jam_in(har_p, t_a, y, q_d);
u3_noun p_e, q_e, r_e;
u3r_trel(e, &p_e, &q_e, &r_e);
u3x_trel(e, &p_e, &q_e, &r_e);
{
u3_noun z = u3qa_add(p_d, p_e);
@ -36,20 +23,16 @@
u3z(z);
}
u3z(e);
u3z(y);
}
u3z(d);
u3z(x);
u3z(w);
u3z(d);
u3z(e);
return r;
}
static u3_noun
_jam_in_flat(u3p(u3h_root) har_p,
u3_atom a,
u3_noun l)
_jam_flat(u3_atom a, u3_noun l)
{
u3_noun d = u3qe_mat(a);
u3_noun x = u3qa_add(1, u3h(d));
@ -57,14 +40,13 @@
(u3k(x), u3nc(u3nc(x, u3qc_lsh(0, 1, u3t(d))), u3k(l)), 0);
u3z(d);
u3z(l);
return y;
}
static u3_noun
_jam_in_ptr(u3p(u3h_root) har_p,
u3_atom u_c,
u3_noun l)
_jam_ptr(u3_atom u_c, u3_noun l)
{
u3_noun d = u3qe_mat(u_c);
u3_atom x = u3qc_lsh(0, 2, u3t(d));
@ -74,52 +56,132 @@
u3z(d);
u3z(x);
u3z(l);
return z;
}
static u3_noun
_jam_in(u3p(u3h_root) har_p,
u3_noun a,
u3_atom b,
u3_noun l)
#define JAM_NONE 0
#define JAM_HEAD 1
#define JAM_TAIL 2
typedef struct {
c3_y sat_y;
u3_noun nun;
u3_noun len;
u3_noun lis;
u3_noun hed;
} jamframe;
static inline jamframe*
_jam_push(c3_ys mov, c3_ys off)
{
u3_noun c = u3h_get(har_p, a);
u3_noun x;
u3R->cap_p += mov;
return u3to(jamframe, u3R->cap_p + off);
}
if ( u3_none == c ) {
u3h_put(har_p, a, u3k(b));
static inline jamframe*
_jam_pop(c3_ys mov, c3_ys off)
{
u3R->cap_p -= mov;
return u3to(jamframe, u3R->cap_p + off);
}
if ( c3y == u3ud(a) ) {
x = _jam_in_flat(har_p, a, l);
} else {
x = _jam_in_pair(har_p, u3h(a), u3t(a), b, l);
static u3_noun
_jam_cap(u3_atom a)
{
u3p(jamframe) empty = u3R->cap_p;
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);
jamframe* don = u3to(jamframe, empty + off);
fam->sat_y = JAM_NONE;
fam->nun = a;
fam->len = 0;
fam->lis = u3_nul;
u3_noun q, r = u3_none;
while ( don != fam ) {
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);
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);
}
}
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);
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);
break;
}
case JAM_TAIL: {
u3_noun len = fam->len;
r = _jam_pair(u3qa_add(2, len), fam->hed, r);
fam = _jam_pop(mov, off);
u3z(len);
break;
}
default:
c3_assert(0);
return u3_none;
}
}
else {
if ( c3y == u3ud(a) && u3r_met(0, a) <= u3r_met(0, c) ) {
x = _jam_in_flat(har_p, a, l);
}
else {
x = _jam_in_ptr(har_p, c, l);
}
}
return x;
q = u3qb_flop(u3h(u3t(r)));
u3z(r);
r = u3qc_can(0, q);
u3z(q);
u3h_free(har_p);
return r;
}
u3_noun
u3qe_jam(u3_atom a)
{
u3p(u3h_root) har_p = u3h_new();
u3_noun x = _jam_in(har_p, a, 0, u3_nul);
u3_noun q = u3qb_flop(u3h(u3t(x)));
u3_noun r = u3qc_can(0, q);
u3z(x);
u3z(q);
u3h_free(har_p);
return r;
return _jam_cap(a);
}
u3_noun
u3we_jam(u3_noun cor)

View File

@ -576,6 +576,58 @@ u3a_wealloc(void* lag_v, c3_w len_w)
}
}
}
/* u3a_push(): allocate space on the road stack
*/
void*
u3a_push(c3_w len_w)
{
void *cur, *top = u3to(void, u3R->cap_p);
if ( c3y == u3a_is_north(u3R) ) {
top -= len_w;
cur = top;
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p < u3R->mat_p);
c3_assert(cap_p > u3R->hat_p);
return cur;
}
else {
cur = top;
top += len_w;
u3R->cap_p = u3of(void, top);
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p > u3R->mat_p);
c3_assert(cap_p < u3R->hat_p);
return cur;
}
}
/* u3a_pop(): deallocate space on the road stack
*/
void
u3a_pop(c3_w len_w)
{
void* top = u3to(void, u3R->cap_p);
if ( c3y == u3a_is_north(u3R) ) {
top += len_w;
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p <= u3R->mat_p);
c3_assert(cap_p > u3R->hat_p);
}
else {
top -= len_w;
u3p(void) cap_p = u3R->cap_p = u3of(void, top);
c3_assert(cap_p >= u3R->mat_p);
c3_assert(cap_p < u3R->hat_p);
}
}
/* u3a_peek(): examine the top of the road stack
*/
void*
u3a_peek(c3_w len_w)
{
return u3to(void, u3R->cap_p) - (c3y == u3a_is_north(u3R) ? 0 : len_w);
}
/* u3a_wfree(): free storage.
*/

View File

@ -502,87 +502,40 @@ _sang_one(u3_noun* a, u3_noun* b)
}
}
/* _sang_x(): yes if a and b are the same noun, unifying but leaking.
*/
static c3_o
_sang_x(u3_noun a, u3_noun b)
#define SONG_NONE 0
#define SONG_HEAD 1
#define SONG_TAIL 2
typedef struct {
c3_y sat_y;
u3_noun a;
u3_noun b;
} eqframe;
static inline eqframe*
_eq_push(c3_ys mov, c3_ys off, u3_noun a, u3_noun b)
{
if ( a == b ) {
return c3y;
}
else {
if ( _(u3a_is_atom(a)) ) {
u3a_atom* a_u = u3a_to_ptr(a);
u3R->cap_p += mov;
eqframe* cur = u3to(eqframe, u3R->cap_p + off);
cur->sat_y = SONG_NONE;
cur->a = a;
cur->b = b;
return cur;
}
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
u3a_atom* b_u = u3a_to_ptr(b);
static inline eqframe*
_eq_pop(c3_ys mov, c3_ys off)
{
u3R->cap_p -= mov;
return u3to(eqframe, u3R->cap_p + off);
}
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
return c3n;
}
else {
c3_w i_w;
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
}
return c3y;
}
}
}
}
else {
if ( _(u3a_is_atom(b)) ) {
return c3n;
}
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
if ( c3n == _sang_x(a_u->hed, b_u->hed) ) {
return c3n;
}
else {
_sang_one(&a_u->hed, &b_u->hed);
if ( c3n == _sang_x(a_u->tel, b_u->tel) ) {
return c3n;
}
else {
_sang_one(&a_u->tel, &b_u->tel);
return c3y;
}
}
}
}
}
}
/* _sing_one(): do not pick a unified pointer for identical (a) and (b).
*/
static void
_sing_one(u3_noun* a, u3_noun* b)
{
// this space left intentionally blank
}
/* _sung_one(): pick a unified pointer for identical (a) and (b).
@ -669,168 +622,221 @@ _sung_one(u3_noun* a, u3_noun* b)
}
}
/* _sung_x(): yes if a and b are the same noun, unifying.
*/
static c3_o
_sung_x(u3_noun a, u3_noun b)
static inline c3_o
_song_atom(u3_atom a, u3_atom b)
{
if ( a == b ) {
return c3y;
u3a_atom* a_u = u3a_to_ptr(a);
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
if ( _(u3a_is_atom(a)) ) {
u3a_atom* a_u = u3a_to_ptr(a);
u3a_atom* b_u = u3a_to_ptr(b);
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
u3a_atom* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
return c3n;
}
else {
c3_w i_w;
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
}
return c3y;
}
}
}
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
if ( _(u3a_is_atom(b)) ) {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
return c3n;
}
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
c3_w i_w;
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
}
else {
if ( c3n == _sung_x(a_u->hed, b_u->hed) ) {
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
else {
_sung_one(&a_u->hed, &b_u->hed);
if ( c3n == _sung_x(a_u->tel, b_u->tel) ) {
return c3n;
}
else {
_sung_one(&a_u->tel, &b_u->tel);
return c3y;
}
}
}
}
}
}
return c3y;
}
/* _sing_x():
**
** Yes iff (a) and (b) are the same noun.
*/
/* _song_x_cape(): unifying equality with comparison deduplication
* (tightly coupled to _song_x)
*/
static c3_o
_sing_x(u3_noun a,
u3_noun b)
_song_x_cape(c3_ys mov, c3_ys off,
eqframe* fam, eqframe* don,
u3p(u3h_root) har_p,
void (*uni)(u3_noun*, u3_noun*))
{
c3_assert(u3_none != a);
c3_assert(u3_none != b);
u3_noun a, b, key;
u3_weak got;
u3a_cell* a_u;
u3a_cell* b_u;
if ( a == b ) {
return c3y;
}
else {
if ( _(u3a_is_atom(a)) ) {
u3a_atom* a_u = u3a_to_ptr(a);
if ( !_(u3a_is_atom(b)) ||
_(u3a_is_cat(a)) ||
_(u3a_is_cat(b)) )
{
return c3n;
}
else {
u3a_atom* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
return c3n;
while ( don != fam ) {
a = fam->a;
b = fam->b;
switch ( fam->sat_y ) {
case SONG_NONE:
if ( a == b ) {
break;
}
else {
c3_w w_rez = a_u->len_w;
c3_w w_mox = b_u->len_w;
if ( w_rez != w_mox ) {
else if ( c3y == u3a_is_atom(a) ) {
if ( c3n == _song_atom(a, b) ) {
return c3n;
}
else {
c3_w i_w;
for ( i_w = 0; i_w < w_rez; i_w++ ) {
if ( a_u->buf_w[i_w] != b_u->buf_w[i_w] ) {
return c3n;
}
}
return c3y;
break;
}
}
}
}
else {
if ( _(u3a_is_atom(b)) ) {
return c3n;
}
else {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) )
{
else if ( c3y == u3a_is_atom(b) ) {
return c3n;
}
else {
if ( c3n == _sing_x(u3a_h(a), u3a_h(b)) ) {
u3a_cell* a_u = u3a_to_ptr(a);
u3a_cell* b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) ) {
return c3n;
}
else if ( c3n == _sing_x(u3a_t(a), u3a_t(b)) ) {
return c3n;
else {
key = u3nc(u3a_to_off(a), u3a_to_off(b));
u3t_off(euq_o);
got = u3h_get(har_p, key);
u3t_on(euq_o);
u3z(key);
if ( u3_none != got ) {
fam = _eq_pop(mov, off);
continue;
}
fam->sat_y = SONG_HEAD;
fam = _eq_push(mov, off, a_u->hed, b_u->hed);
continue;
}
return c3y;
}
}
case SONG_HEAD:
a_u = u3a_to_ptr(a);
b_u = u3a_to_ptr(b);
uni(&(a_u->hed), &(b_u->hed));
fam->sat_y = SONG_TAIL;
fam = _eq_push(mov, off, a_u->tel, b_u->tel);
continue;
case SONG_TAIL:
a_u = u3a_to_ptr(a);
b_u = u3a_to_ptr(b);
uni(&(a_u->tel), &(b_u->tel));
break;
default:
c3_assert(0);
break;
}
key = u3nc(u3a_to_off(a), u3a_to_off(b));
u3t_off(euq_o);
u3h_put(har_p, key, c3y);
u3t_on(euq_o);
u3z(key);
fam = _eq_pop(mov, off);
}
return c3y;
}
/* _song_x(): yes if a and b are the same noun, use uni to unify
*/
static c3_o
_song_x(u3_noun a, u3_noun b, void (*uni)(u3_noun*, u3_noun*))
{
u3p(eqframe) empty = u3R->cap_p;
c3_y wis_y = c3_wiseof(eqframe);
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_s ovr_s = 0;
eqframe* fam = _eq_push(mov, off, a, b);
eqframe* don = u3to(eqframe, empty + off);
u3a_cell* a_u;
u3a_cell* b_u;
while ( don != fam ) {
a = fam->a;
b = fam->b;
switch ( fam->sat_y ) {
case SONG_NONE:
if ( a == b ) {
break;
}
else if ( c3y == u3a_is_atom(a) ) {
if ( c3n == _song_atom(a, b) ) {
u3R->cap_p = empty;
return c3n;
}
else {
break;
}
}
else if ( c3y == u3a_is_atom(b) ) {
u3R->cap_p = empty;
return c3n;
}
else {
a_u = u3a_to_ptr(a);
b_u = u3a_to_ptr(b);
if ( a_u->mug_w &&
b_u->mug_w &&
(a_u->mug_w != b_u->mug_w) ) {
u3R->cap_p = empty;
return c3n;
}
else {
fam->sat_y = SONG_HEAD;
fam = _eq_push(mov, off, a_u->hed, b_u->hed);
continue;
}
}
case SONG_HEAD:
a_u = u3a_to_ptr(a);
b_u = u3a_to_ptr(b);
uni(&(a_u->hed), &(b_u->hed));
fam->sat_y = SONG_TAIL;
fam = _eq_push(mov, off, a_u->tel, b_u->tel);
continue;
case SONG_TAIL:
a_u = u3a_to_ptr(a);
b_u = u3a_to_ptr(b);
uni(&(a_u->tel), &(b_u->tel));
break;
default:
c3_assert(0);
break;
}
if ( 0 == ++ovr_s ) {
u3p(u3h_root) har_p = u3h_new();
c3_o ret_o = _song_x_cape(mov, off, fam, don, har_p, uni);
u3h_free(har_p);
u3R->cap_p = empty;
return ret_o;
}
fam = _eq_pop(mov, off);
}
return c3y;
}
/* u3r_sang(): yes iff (a) and (b) are the same noun, unifying equals.
@ -838,7 +844,11 @@ _sing_x(u3_noun a,
c3_o
u3r_sang(u3_noun a, u3_noun b)
{
return _sang_x(a, b);
c3_o ret_o;
u3t_on(euq_o);
ret_o = _song_x(a, b, &_sang_one);
u3t_off(euq_o);
return ret_o;
}
/* u3r_sing():
@ -850,20 +860,14 @@ u3r_sing(u3_noun a, u3_noun b)
{
#ifndef U3_MEMORY_DEBUG
if ( u3R->par_p ) {
c3_o ret_o;
u3t_on(euq_o);
ret_o = u3r_sang(a, b);
u3t_off(euq_o);
return ret_o;
return u3r_sang(a, b);
}
#endif
{
c3_o ret_o;
u3t_on(euq_o);
ret_o = _sing_x(a, b);
ret_o = _song_x(a, b, &_sing_one);
u3t_off(euq_o);
return ret_o;
@ -875,7 +879,11 @@ u3r_sing(u3_noun a, u3_noun b)
c3_o
u3r_sung(u3_noun a, u3_noun b)
{
return _sung_x(a, b);
c3_o ret_o;
u3t_on(euq_o);
ret_o = _song_x(a, b, &_sung_one);
u3t_off(euq_o);
return ret_o;
}
c3_o