urbit/jets/f/ut_fuse.c

216 lines
4.9 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/6/fuse.c
**
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
2013-09-29 00:21:18 +04:00
/* logic
*/
2014-09-06 00:13:24 +04:00
static u3_noun
_fuse_in(u3_noun, u3_noun, u3_noun, u3_noun);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
static u3_noun
2015-08-28 19:34:36 +03:00
_fuse_repo(u3_noun van,
2014-09-06 00:13:24 +04:00
u3_noun sut,
u3_noun ref,
u3_noun bix)
2013-09-29 00:21:18 +04:00
{
2014-11-06 22:13:57 +03:00
u3_noun rep = u3qfu_repo(van, sut);
2014-09-06 00:13:24 +04:00
u3_noun ret = _fuse_in(van, rep, ref, bix);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(ret, rep) ) {
2013-09-29 00:21:18 +04:00
if ( c3__void == rep ) {
return c3__void;
} else {
2014-09-06 00:13:24 +04:00
u3z(rep);
u3z(ret);
return u3k(sut);
2013-09-29 00:21:18 +04:00
}
} else {
2014-09-06 00:13:24 +04:00
u3z(rep);
2013-09-29 00:21:18 +04:00
return ret;
}
}
static u3_noun
2016-01-14 12:52:50 +03:00
_fuse_in_fork(u3_noun van, u3_noun p_sut, u3_noun ref, u3_noun bix)
{
if ( u3_nul == p_sut ) {
return u3_nul;
}
else {
return u3nc(_fuse_in(van, u3h(p_sut), ref, bix),
2016-01-14 12:52:50 +03:00
_fuse_in_fork(van, u3t(p_sut), ref, bix));
}
}
2014-09-06 00:13:24 +04:00
static u3_noun
2015-08-28 19:34:36 +03:00
_fuse_in(u3_noun van,
2014-09-06 00:13:24 +04:00
u3_noun sut,
u3_noun ref,
u3_noun bix)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun p_sut, q_sut;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(sut, ref) || (c3__noun == ref) ) {
2014-09-06 00:13:24 +04:00
return u3k(sut);
2013-09-29 00:21:18 +04:00
}
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(sut) ) switch ( sut ) {
2014-11-06 03:20:01 +03:00
default: return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
case c3__noun:
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3k(ref);
2013-09-29 00:21:18 +04:00
}
case c3__void:
{
return c3__void;
}
}
2014-09-06 00:13:24 +04:00
else switch ( u3h(sut) ) {
2014-11-06 03:20:01 +03:00
default: return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
case c3__atom: u3x_cell(u3t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
if ( c3y == u3du(ref) ) {
2014-09-06 00:13:24 +04:00
if ( c3__atom == u3h(ref) ) {
u3_noun p_ref, q_ref;
u3x_cell(u3t(ref), &p_ref, &q_ref);
{
u3_noun foc = (c3y == u3qf_fitz(p_ref, p_sut))
? u3k(p_sut)
: u3k(p_ref);
if ( c3y == u3du(q_sut) ) {
if ( c3y == u3du(q_ref) ) {
if ( c3y == u3r_sing(q_ref, q_sut) ) {
return u3nt(c3__atom, foc, u3k(q_sut));
}
else {
u3z(foc);
return c3__void;
}
}
else {
return u3nt(c3__atom, foc, u3k(q_sut));
}
} else {
return u3nt(c3__atom, foc, u3k(q_ref));
}
}
}
2014-09-06 00:13:24 +04:00
else if ( c3__cell == u3h(ref) ) {
2013-09-29 00:21:18 +04:00
return c3__void;
}
}
2014-08-21 01:18:34 +04:00
return _fuse_in(van, ref, sut, bix);
2013-09-29 00:21:18 +04:00
}
2014-11-06 03:20:01 +03:00
case c3__cell: u3x_cell(u3t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun p_ref, q_ref;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_pq(ref, c3__cell, &p_ref, &q_ref) ) {
2014-09-06 00:13:24 +04:00
u3_noun hed = _fuse_in(van, p_sut, p_ref, bix);
u3_noun tal = _fuse_in(van, q_sut, q_ref, bix);
2014-11-06 22:13:57 +03:00
u3_noun ret = u3qf_cell(hed, tal);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(hed);
u3z(tal);
2013-09-29 00:21:18 +04:00
return ret;
}
2014-08-21 01:18:34 +04:00
else return _fuse_in(van, ref, sut, bix);
2013-09-29 00:21:18 +04:00
}
case c3__core:
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
return _fuse_repo(van, sut, ref, bix);
2013-09-29 00:21:18 +04:00
}
2018-04-25 08:29:31 +03:00
case c3__hint: u3x_cell(u3t(sut), &p_sut, &q_sut);
2017-09-28 03:40:12 +03:00
{
u3_noun vot = _fuse_in(van, q_sut, ref, bix);
2018-04-25 08:29:31 +03:00
u3_noun ret = u3qf_hint(p_sut, vot);
2017-09-28 03:40:12 +03:00
u3z(vot);
return ret;
}
2014-11-06 03:20:01 +03:00
case c3__face: u3x_cell(u3t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun vot = _fuse_in(van, q_sut, ref, bix);
2014-11-06 22:13:57 +03:00
u3_noun ret = u3qf_face(p_sut, vot);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(vot);
2013-09-29 00:21:18 +04:00
return ret;
}
case c3__fork: p_sut = u3t(sut);
{
2017-09-28 03:40:12 +03:00
u3_noun yed = u3qdi_tap(p_sut);
2016-01-14 12:52:50 +03:00
u3_noun ret = u3kf_fork(_fuse_in_fork(van, yed, ref, bix));
2016-01-13 02:31:02 +03:00
u3z(yed);
return ret;
}
2016-01-11 09:37:59 +03:00
case c3__hold:
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun hud = u3nc(u3k(sut), u3k(ref));
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
if ( c3y == u3qdi_has(bix, hud) ) {
// u3_noun dun = u3qfu_dunq(van, "type", sut);
// u3_noun niz = u3qfu_dunq(van, "over", ref);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
// u3t_push(u3nc(c3__mean, dun));
// u3t_push(u3nc(c3__mean, niz));
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
return u3m_error("fuse-loop");
2013-09-29 00:21:18 +04:00
} else {
2014-11-06 22:13:57 +03:00
u3_noun bux = u3qdi_put(bix, hud);
2014-09-06 00:13:24 +04:00
u3_noun ret = _fuse_repo(van, sut, ref, bux);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(hud);
u3z(bux);
2013-09-29 00:21:18 +04:00
return ret;
}
}
}
}
2017-09-28 03:40:12 +03:00
static u3_noun
2015-08-28 19:34:36 +03:00
_cqfu_fuse(u3_noun van,
u3_noun sut,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return _fuse_in(van, sut, ref, u3_nul);
2013-09-29 00:21:18 +04:00
}
/* boilerplate
*/
2014-09-06 00:13:24 +04:00
u3_noun
2015-08-28 19:34:36 +03:00
u3wfu_fuse(u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun sut, ref, van;
2013-09-29 00:21:18 +04:00
2014-11-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam, &ref, u3x_con, &van, 0)) ||
(u3_none == (sut = u3r_at(u3x_sam, van))) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-09-03 09:46:55 +04:00
return _cqfu_fuse(van, sut, ref);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qfu_fuse(u3_noun van,
2015-08-28 19:34:36 +03:00
u3_noun sut,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2018-05-25 01:40:28 +03:00
c3_m fun_m = 141 + c3__fuse + ((!!u3r_at(u3qfu_van_vet, van)) << 8);
2014-11-06 03:20:01 +03:00
u3_noun pro = u3z_find_2(fun_m, sut, ref);
2013-09-29 00:21:18 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none != pro ) {
2014-08-28 23:40:40 +04:00
return pro;
2013-09-29 00:21:18 +04:00
}
else {
2014-09-03 09:46:55 +04:00
pro = _cqfu_fuse(van, sut, ref);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
return u3z_save_2(fun_m, sut, ref, pro);
2013-09-29 00:21:18 +04:00
}
}