urbit/gen164/6/ut_crop.c

294 lines
7.8 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-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 = _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);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_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
2014-09-03 07:36:29 +04:00
if ( u2_yes == u2_cqfu_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);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_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-09-03 07:36:29 +04:00
if ( u2_yes == u2_cqfu_firm(van, foz, p_sut) ) {
ret = u2_cqf_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);
2014-09-03 07:36:29 +04:00
u2_noun ret = u2_cqf_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);
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) ) {
2013-09-29 00:21:18 +04:00
# if 0
2014-09-03 07:36:29 +04:00
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
# 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-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 = _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-09-03 07:36:29 +04:00
u2_noun rep = u2_cqfu_repo(van, ref);
2014-08-21 01:18:34 +04:00
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_noun // transfer
2014-09-03 07:36:29 +04:00
u2_cqfu_crop(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__crop;
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, crop)(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, crop)(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, 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,
},
{ }
};