urbit/gen164/6/ut_fuse.c

233 lines
5.7 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/6/fuse.c
**
** This file is in the public domain.
*/
#include "all.h"
#include "../pit.h"
/* logic
*/
static u2_noun
2014-08-21 01:18:34 +04:00
_fuse_in(u2_noun, u2_noun, u2_noun, u2_noun);
2013-09-29 00:21:18 +04:00
static u2_noun
2014-08-21 01:18:34 +04:00
_fuse_repo(
2013-09-29 00:21:18 +04:00
u2_noun van,
u2_noun sut,
u2_noun ref,
u2_noun bix)
{
2014-09-03 07:36:29 +04:00
u2_noun rep = u2_cqfu_repo(van, sut);
2014-08-21 01:18:34 +04:00
u2_noun ret = _fuse_in(van, rep, ref, bix);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
if ( u2_yes == u2_cr_sing(ret, rep) ) {
2013-09-29 00:21:18 +04:00
if ( c3__void == rep ) {
return c3__void;
} else {
2014-08-21 01:18:34 +04:00
u2z(rep);
u2z(ret);
return u2k(sut);
2013-09-29 00:21:18 +04:00
}
} else {
2014-08-21 01:18:34 +04:00
u2z(rep);
2013-09-29 00:21:18 +04:00
return ret;
}
}
static u2_noun
2014-08-21 01:18:34 +04:00
_fuse_in(
2013-09-29 00:21:18 +04:00
u2_noun van,
u2_noun sut,
u2_noun ref,
u2_noun bix)
{
2013-12-26 21:12:07 +04:00
u2_noun p_sut, q_sut;
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
if ( u2_yes == u2_cr_sing(sut, ref) || (c3__noun == ref) ) {
return u2k(sut);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
if ( u2_no == u2du(sut) ) switch ( sut ) {
default: return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
case c3__noun:
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
return u2k(ref);
2013-09-29 00:21:18 +04:00
}
case c3__void:
{
return c3__void;
}
}
2014-08-21 01:18:34 +04:00
else switch ( u2h(sut) ) {
default: return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
case c3__atom:
{
2014-08-21 01:18:34 +04:00
if ( u2_yes == u2du(ref) ) {
if ( c3__atom == u2h(ref) ) {
2014-09-03 07:36:29 +04:00
if ( u2_yes == u2_cqf_fitz(u2t(ref), u2t(sut)) ) {
2014-08-21 01:18:34 +04:00
return u2k(sut);
} else return u2k(ref);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
else if ( c3__cell == u2h(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-08-21 01:18:34 +04:00
case c3__bull: u2_cx_cell(u2t(sut), &p_sut, &q_sut);
2013-12-26 21:12:07 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun vot = _fuse_in(van, q_sut, ref, bix);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_bull(p_sut, vot);
2013-12-26 21:12:07 +04:00
2014-08-21 01:18:34 +04:00
u2z(vot);
2013-12-26 21:12:07 +04:00
return ret;
}
2014-08-21 01:18:34 +04:00
case c3__cell: u2_cx_cell(u2t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
u2_noun p_ref, q_ref;
2014-08-21 01:18:34 +04:00
if ( u2_yes == u2_cr_pq(ref, c3__cell, &p_ref, &q_ref) ) {
u2_noun hed = _fuse_in(van, p_sut, p_ref, bix);
u2_noun tal = _fuse_in(van, q_sut, q_ref, bix);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_cell(hed, tal);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(hed);
u2z(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
}
2014-08-21 01:18:34 +04:00
case c3__cube: u2_cx_cell(u2t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun foz = _fuse_in(van, q_sut, ref, bix);
2013-09-29 00:21:18 +04:00
u2_noun ret;
2014-09-03 07:36:29 +04:00
if ( u2_no == u2_cqfu_firm(van, foz, p_sut) ) {
2013-09-29 00:21:18 +04:00
ret = c3__void;
} else {
2014-09-03 07:36:29 +04:00
ret = u2_cqf_cube(p_sut, foz);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
u2z(foz);
2013-09-29 00:21:18 +04:00
return ret;
}
2014-08-21 01:18:34 +04:00
case c3__face: u2_cx_cell(u2t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun vot = _fuse_in(van, q_sut, ref, bix);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_face(p_sut, vot);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(vot);
2013-09-29 00:21:18 +04:00
return ret;
}
2014-08-21 01:18:34 +04:00
case c3__fork: u2_cx_cell(u2t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun dis = _fuse_in(van, p_sut, ref, bix);
u2_noun dat = _fuse_in(van, q_sut, ref, bix);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_fork(dis, dat);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(dis);
u2z(dat);
2013-09-29 00:21:18 +04:00
return ret;
}
2014-08-21 01:18:34 +04:00
case c3__hold: p_sut = u2t(sut);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun hud = u2nc(u2k(sut), u2k(ref));
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
if ( u2_yes == u2_cqdi_has(bix, hud) ) {
// u2_noun dun = u2_cqfu_dunq(van, "type", sut);
// u2_noun niz = u2_cqfu_dunq(van, "over", ref);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
// u2_ct_push(u2nc(c3__mean, dun));
// u2_ct_push(u2nc(c3__mean, niz));
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
return u2_cm_error("fuse-loop");
2013-09-29 00:21:18 +04:00
} else {
2014-09-03 07:36:29 +04:00
u2_noun bux = u2_cqdi_put(bix, hud);
2014-08-21 01:18:34 +04:00
u2_noun ret = _fuse_repo(van, sut, ref, bux);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(hud);
u2z(bux);
2013-09-29 00:21:18 +04:00
return ret;
}
}
}
}
u2_noun // transfer
2014-08-21 01:18:34 +04:00
j2_mcx(Pt6, ut, fuse)(
2013-09-29 00:21:18 +04:00
u2_noun van, // retain
u2_noun sut, // retain
u2_noun ref) // retain
{
2014-08-21 01:18:34 +04:00
return _fuse_in(van, sut, ref, u2_nul);
2013-09-29 00:21:18 +04:00
}
/* boilerplate
*/
u2_ho_jet
2013-09-29 00:21:18 +04:00
j2_mcj(Pt6, ut, fuse)[];
u2_noun // transfer
2014-08-21 01:18:34 +04:00
j2_mc(Pt6, ut, fuse)(
2013-09-29 00:21:18 +04:00
u2_noun cor) // retain
{
u2_noun sut, ref, van;
2014-08-21 01:18:34 +04:00
if ( (u2_no == u2_cr_mean(cor, u2_cv_sam, &ref, u2_cv_con, &van, 0)) ||
(u2_none == (sut = u2_cr_at(u2_cv_sam, van))) )
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-08-21 01:18:34 +04:00
return j2_mcx(Pt6, ut, fuse)(van, sut, ref);
2013-09-29 00:21:18 +04:00
}
}
u2_noun // transfer
2014-09-03 07:36:29 +04:00
u2_cqfu_fuse(u2_noun van, // retain
2013-09-29 00:21:18 +04:00
u2_noun sut, // retain
u2_noun ref) // retain
{
2014-08-28 23:40:40 +04:00
c3_m fun_m = c3__fuse;
u2_noun pro = u2_cz_find_2(fun_m, sut, ref);
2013-09-29 00:21:18 +04:00
2014-08-28 23:40:40 +04:00
if ( u2_none != pro ) {
return pro;
2013-09-29 00:21:18 +04:00
}
else {
2014-08-28 23:40:40 +04:00
pro = j2_mcx(Pt6, ut, fuse)(van, sut, ref);
2013-09-29 00:21:18 +04:00
2014-08-28 23:40:40 +04:00
return u2_cz_save_2(fun_m, sut, ref, pro);
2013-09-29 00:21:18 +04:00
}
}
u2_weak
2014-08-28 23:40:40 +04:00
j2_mck(Pt6, ut, fuse)(u2_noun cor)
2013-09-29 00:21:18 +04:00
{
u2_noun sut, ref, van;
2014-08-21 01:18:34 +04:00
if ( (u2_no == u2_cr_mean(cor, u2_cv_sam, &ref, u2_cv_con, &van, 0)) ||
(u2_none == (sut = u2_cr_at(u2_cv_sam, van))) )
2013-09-29 00:21:18 +04:00
{
return u2_none;
} else {
2014-08-21 01:18:34 +04:00
return u2nc(u2k(sut), u2k(ref));
2013-09-29 00:21:18 +04:00
}
}
/* structures
*/
u2_ho_jet
2013-09-29 00:21:18 +04:00
j2_mcj(Pt6, ut, fuse)[] = {
{ ".2", c3__hevy,
j2_mc(Pt6, ut, fuse),
2013-09-29 00:21:18 +04:00
Tier6_b_memo,
u2_none, u2_none,
j2_mck(Pt6, ut, fuse), c3__fuse,
},
{ }
};