urbit/gen164/6/ut_crop.c

346 lines
9.4 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/6/crop.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
_crop_dext(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
_crop_sint(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
_crop_repo(
2013-09-29 00:21:18 +04:00
u2_noun van,
u2_noun sut,
u2_noun ref,
u2_noun bix)
{
2014-08-21 01:18:34 +04:00
u2_noun rep = j2_mcy(Pt6, ut, repo)(van, sut);
u2_noun ret = _crop_dext(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 // produce
2014-08-21 01:18:34 +04:00
_crop_dext(
2013-09-29 00:21:18 +04:00
u2_noun van, // retain
u2_noun sut, // retain
u2_noun ref, // retain
u2_noun bix) // retain
{
2013-12-26 21:12:07 +04:00
u2_noun p_sut, q_sut, p_ref, q_ref;
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
if ( u2_no == u2du(ref) ) {
2013-09-29 00:21:18 +04:00
switch ( ref ) {
2014-08-21 01:18:34 +04:00
case c3__void: return u2k(sut);
2013-09-29 00:21:18 +04:00
case c3__noun: return c3__void;
2014-08-21 01:18:34 +04:00
default: return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
}
}
2014-08-21 01:18:34 +04:00
if ( u2_yes == u2_cr_sing(sut, ref) ) {
2013-09-29 00:21:18 +04:00
return c3__void;
}
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
2014-08-21 01:18:34 +04:00
case c3__noun: return _crop_repo(van, sut, ref, bix);
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 ( c3__atom == u2h(ref) ) {
2013-09-29 00:21:18 +04:00
return c3__void;
}
2014-08-21 01:18:34 +04:00
else if ( c3__cell == u2h(ref) ) {
return u2k(sut);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
else return _crop_sint(van, sut, ref, 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 foz = _crop_dext(van, q_sut, ref, bix);
u2_noun ret = j2_mby(Pt6, bull)(p_sut, foz);
2013-12-26 21:12:07 +04:00
2014-08-21 01:18:34 +04:00
u2z(foz);
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
{
2014-08-21 01:18:34 +04:00
if ( c3__atom == u2h(ref) ) {
return u2k(sut);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
else if ( c3__cell == u2h(ref) ) {
u2_cx_cell(u2t(ref), &p_ref, &q_ref);
2013-09-29 00:21:18 +04:00
if ( u2_yes == j2_mcy(Pt6, ut, nest)
2014-08-21 01:18:34 +04:00
(van, p_ref, u2_no, p_sut) )
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun foz = _crop_dext(van, q_sut, q_ref, bix);
u2_noun ret = j2_mby(Pt6, cell)(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
else return u2k(sut);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
else return _crop_sint(van, sut, ref, 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
if ( (c3__atom == u2h(ref)) || (c3__cell == u2h(ref)) ) {
return u2k(sut);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
else return _crop_sint(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
if ( (c3__cube == u2h(ref)) &&
(u2_yes == u2_cr_sing(p_sut, u2h(u2t(ref)))) )
2013-09-29 00:21:18 +04:00
{
return c3__void;
}
2014-08-21 01:18:34 +04:00
else if ( (c3__atom == u2h(ref)) || (c3__cell == u2h(ref)) ) {
u2_noun foz = _crop_dext(van, q_sut, ref, bix);
2013-09-29 00:21:18 +04:00
u2_noun ret;
2014-08-21 01:18:34 +04:00
if ( u2_yes == j2_mcy(Pt6, ut, firm)(van, foz, p_sut) ) {
ret = j2_mby(Pt6, cube)(p_sut, foz);
2013-09-29 00:21:18 +04:00
}
else ret = c3__void;
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
else return _crop_sint(van, sut, ref, bix);
2013-09-29 00:21:18 +04:00
}
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 foz = _crop_dext(van, q_sut, ref, bix);
u2_noun ret = j2_mby(Pt6, face)(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__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 = _crop_dext(van, p_sut, ref, bix);
u2_noun dat = _crop_dext(van, q_sut, ref, bix);
u2_noun ret = j2_mby(Pt6, 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-08-21 01:18:34 +04:00
if ( u2_yes == j2_mcc(Pt4, in, has)(bix, hud) ) {
2013-09-29 00:21:18 +04:00
# if 0
2014-08-21 01:18:34 +04:00
u2_noun dun = j2_mcy(Pt6, ut, dunq)(van, "type", sut);
u2_noun niz = j2_mcy(Pt6, ut, 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
# endif
2014-08-21 01:18:34 +04:00
return u2_cm_error("crop-loop");
2013-09-29 00:21:18 +04:00
} else {
2014-08-21 01:18:34 +04:00
u2_noun bux = j2_mcc(Pt4, in, put)(bix, hud);
u2_noun ret = _crop_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;
}
}
}
}
static u2_noun // produce
2014-08-21 01:18:34 +04:00
_crop_sint(
2013-09-29 00:21:18 +04:00
u2_noun van, // retain
u2_noun sut, // retain
u2_noun ref, // retain
u2_noun bix) // retain
{
u2_noun p_ref, q_ref;
2014-08-21 01:18:34 +04:00
switch ( u2h(ref) ) {
default: return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
case c3__bull: u2_cx_cell(u2t(ref), &p_ref, &q_ref);
2013-12-26 21:12:07 +04:00
{
2014-08-21 01:18:34 +04:00
return _crop_dext(van, sut, q_ref, bix);
2013-12-26 21:12:07 +04:00
}
2013-09-29 00:21:18 +04:00
case c3__core:
2013-12-26 21:12:07 +04:00
case c3__cube: {
2014-08-21 01:18:34 +04:00
return u2k(sut);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
case c3__face: u2_cx_cell(u2t(ref), &p_ref, &q_ref);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
return _crop_dext(van, sut, q_ref, bix);
2013-09-29 00:21:18 +04:00
}
2014-08-21 01:18:34 +04:00
case c3__fork: u2_cx_cell(u2t(ref), &p_ref, &q_ref);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun hin = _crop_dext(van, sut, p_ref, bix);
u2_noun ret = _crop_dext(van, hin, q_ref, bix);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(hin);
2013-09-29 00:21:18 +04:00
return ret;
}
2014-08-21 01:18:34 +04:00
case c3__hold: p_ref = u2t(ref);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
u2_noun rep = j2_mcy(Pt6, ut, repo)(van, ref);
u2_noun ret = _crop_dext(van, sut, rep, bix);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(rep);
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, crop)(
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 _crop_dext(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, crop)[];
u2_noun // transfer
2014-08-21 01:18:34 +04:00
j2_mc(Pt6, ut, crop)(
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, crop)(van, sut, ref);
2013-09-29 00:21:18 +04:00
}
}
u2_weak // transfer
2014-08-21 01:18:34 +04:00
j2_mci(Pt6, ut, crop)(
2013-09-29 00:21:18 +04:00
u2_noun van, // retain
u2_noun sut, // retain
2013-09-29 00:21:18 +04:00
u2_noun ref) // retain
{
2014-08-21 01:24:29 +04:00
u2_weak hoc = u2_cj_look(u2k(van), "crop");
2013-09-29 00:21:18 +04:00
if ( u2_none == hoc ) {
c3_assert(!"register crop");
return u2_none;
} else {
2014-08-21 01:18:34 +04:00
u2_weak von = u2_ci_molt(u2k(van), u2_cv_sam, u2k(sut), 0);
u2_weak gat = u2_cn_nock_on(von, hoc);
u2_weak cor = u2_ci_molt(u2k(gat), u2_cv_sam, u2k(ref), 0);
2013-09-29 00:21:18 +04:00
if ( (u2_none == j2_mcj(Pt6, ut, crop)[0].xip) ) {
2014-08-21 01:24:29 +04:00
u2_noun xip = u2_cj_find(u2k(cor));
2013-09-29 00:21:18 +04:00
c3_assert(u2_none != xip);
j2_mcj(Pt6, ut, crop)[0].xip = xip;
}
2014-08-21 01:18:34 +04:00
u2z(gat);
2013-09-29 00:21:18 +04:00
return cor;
}
}
u2_noun // transfer
2014-08-21 01:18:34 +04:00
j2_mcy(Pt6, ut, crop)(
2013-09-29 00:21:18 +04:00
u2_noun van, // retain
u2_noun sut, // retain
u2_noun ref) // retain
{
u2_ho_jet *jet_j = &j2_mcj(Pt6, ut, crop)[0];
if ( (jet_j->sat_s & u2_jet_live) && !(jet_j->sat_s & u2_jet_test) ) {
if ( !(jet_j->sat_s & u2_jet_memo) ) {
2014-08-21 01:18:34 +04:00
return j2_mcx(Pt6, ut, crop)(van, sut, ref);
2013-09-29 00:21:18 +04:00
}
else {
c3_m fun_m = c3__crop;
2014-08-26 20:12:45 +04:00
u2_noun pro = u2_cz_find_2(fun_m, sut, ref);
2013-09-29 00:21:18 +04:00
if ( u2_none != pro ) {
return pro;
}
else {
2014-08-21 01:18:34 +04:00
pro = j2_mcx(Pt6, ut, crop)(van, sut, ref);
2013-09-29 00:21:18 +04:00
2014-08-26 20:12:45 +04:00
return u2_cz_save_2(fun_m, sut, ref, pro);
2013-09-29 00:21:18 +04:00
}
}
}
else {
u2_noun cor, fol, pro;
2014-08-21 01:18:34 +04:00
cor = j2_mci(Pt6, ut, crop)(van, sut, ref);
fol = u2h(cor);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
pro = u2_ho_use(jet_j, cor, fol);
if ( u2_none == pro ) return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
u2z(cor);
u2z(fol);
2013-09-29 00:21:18 +04:00
return pro;
}
}
u2_weak
2014-08-21 01:18:34 +04:00
j2_mck(Pt6, ut, crop)(
2013-09-29 00:21:18 +04:00
u2_noun cor)
{
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, crop)[] = {
{ ".2", c3__hevy,
j2_mc(Pt6, ut, crop),
2013-09-29 00:21:18 +04:00
Tier6_b_memo,
u2_none, u2_none,
j2_mck(Pt6, ut, crop), c3__crop,
},
{ }
};