urbit/j/f/ut_cull.c

211 lines
4.7 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/6/cull.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
u3_noun
2014-11-06 06:10:22 +03:00
_cqfu_cull(u3_noun, u3_noun, u3_noun, u3_atom, u3_noun);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
static u3_noun
2014-08-21 01:18:34 +04:00
_cull_in(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun sut,
2014-11-06 06:10:22 +03:00
u3_noun pol,
2014-09-06 00:13:24 +04:00
u3_atom axe,
u3_noun ref,
u3_noun now,
u3_noun lat,
u3_noun vil)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun ret, p_sut, q_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: {
2014-11-06 22:13:57 +03:00
u3_noun pyr = u3qfu_repo(van, sut);
2014-09-06 00:13:24 +04:00
u3_noun yot = _cqfu_cull(van, pyr, pol, axe, ref);
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(pyr, yot) ) {
2014-09-06 00:13:24 +04:00
ret = u3k(sut);
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
ret = u3k(yot);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(pyr);
u3z(yot);
2013-09-29 00:21:18 +04:00
break;
}
case c3__void: {
ret = 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: {
ret = c3__void;
2013-09-29 00:21:18 +04:00
break;
}
2014-11-06 03:20:01 +03:00
case c3__bull: u3x_cell(u3t(sut), &p_sut, &q_sut);
2013-12-26 21:12:07 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun hic = _cqfu_cull
2014-08-21 01:18:34 +04:00
(van, q_sut, pol, axe, ref);
2013-12-26 21:12:07 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(hic, q_sut) ) {
2014-09-06 00:13:24 +04:00
ret = u3k(sut);
2013-12-26 21:12:07 +04:00
} else {
2014-11-06 22:13:57 +03:00
ret = u3qf_bull(p_sut, hic);
2013-12-26 21:12:07 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(hic);
2013-12-26 21:12:07 +04:00
break;
}
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 hub;
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
if ( 2 == now ) {
2014-09-03 09:46:55 +04:00
hub = _cqfu_cull(van, p_sut, pol, lat, ref);
2014-11-06 22:13:57 +03:00
ret = u3qf_cell(hub, q_sut);
2013-09-29 00:21:18 +04:00
} else {
2014-09-03 09:46:55 +04:00
hub = _cqfu_cull(van, q_sut, pol, lat, ref);
2014-11-06 22:13:57 +03:00
ret = u3qf_cell(p_sut, hub);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(hub);
2013-09-29 00:21:18 +04:00
break;
}
2014-11-06 03:20:01 +03:00
case c3__core: u3x_cell(u3t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
if ( 3 != now ) {
2014-09-06 00:13:24 +04:00
ret = u3k(sut);
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
u3_noun hub = _cqfu_cull
2014-08-21 01:18:34 +04:00
(van, p_sut, pol, lat, ref);
2013-09-29 00:21:18 +04:00
2014-11-06 22:13:57 +03:00
ret = u3qf_core(hub, q_sut);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(hub);
2013-09-29 00:21:18 +04:00
}
break;
}
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 hic = _cqfu_cull
2014-08-21 01:18:34 +04:00
(van, q_sut, pol, axe, ref);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(hic, q_sut) ) {
2014-09-06 00:13:24 +04:00
ret = u3k(sut);
2013-09-29 00:21:18 +04:00
} else {
2014-11-06 22:13:57 +03:00
ret = u3qf_face(p_sut, hic);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(hic);
2013-09-29 00:21:18 +04:00
break;
}
2014-11-06 03:20:01 +03:00
case c3__fork: u3x_cell(u3t(sut), &p_sut, &q_sut);
2013-09-29 00:21:18 +04:00
{
2014-11-06 22:13:57 +03:00
if ( c3y == u3qdi_has(vil, sut) ) {
2013-09-29 00:21:18 +04:00
return c3__void;
} else {
2014-11-06 22:13:57 +03:00
u3_noun zoc = u3qdi_put(vil, sut);
2014-09-06 00:13:24 +04:00
u3_noun sin = _cull_in
2014-08-21 01:18:34 +04:00
(van, p_sut, pol, axe, ref, now, lat, zoc);
2014-09-06 00:13:24 +04:00
u3_noun dex = _cull_in
2014-08-21 01:18:34 +04:00
(van, q_sut, pol, axe, ref, now, lat, zoc);
2014-11-06 22:13:57 +03:00
ret = u3qf_fork(sin, dex);
2014-09-06 00:13:24 +04:00
u3z(sin);
u3z(dex);
u3z(zoc);
2013-09-29 00:21:18 +04:00
return ret;
}
}
case c3__cube:
case c3__hold:
2013-09-29 00:21:18 +04:00
{
2014-11-06 22:13:57 +03:00
u3_noun pyr = u3qfu_repo(van, sut);
2014-09-06 00:13:24 +04:00
u3_noun yot = _cull_in(van, pyr, pol, axe, ref, now, lat, vil);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(pyr, yot) ) {
2013-09-29 00:21:18 +04:00
if ( c3__void == pyr ) {
ret = c3__void;
}
2014-09-06 00:13:24 +04:00
else ret = u3k(sut);
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
ret = u3k(yot);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(pyr);
u3z(yot);
2013-09-29 00:21:18 +04:00
break;
}
}
2014-09-06 00:13:24 +04:00
u3z(lat);
2013-09-29 00:21:18 +04:00
return ret;
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-09-03 09:46:55 +04:00
_cqfu_cull(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun sut,
2014-11-06 06:10:22 +03:00
u3_noun pol,
2014-09-06 00:13:24 +04:00
u3_atom axe,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
if ( 1 == axe ) {
2014-11-05 04:18:47 +03:00
if ( c3y == pol ) {
2014-11-06 22:13:57 +03:00
return u3qfu_fuse(van, sut, ref);
2013-09-29 00:21:18 +04:00
} else {
2014-11-06 22:13:57 +03:00
return u3qfu_crop(van, sut, ref);
2013-09-29 00:21:18 +04:00
}
} else {
2014-11-06 22:13:57 +03:00
u3_atom now = u3qc_cap(axe);
u3_atom lat = u3qc_mas(axe);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
return _cull_in(van, sut, pol, axe, ref, now, lat, u3_nul);
2013-09-29 00:21:18 +04:00
}
}
2013-09-29 00:21:18 +04:00
/* boilerplate
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wfu_cull(u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun sut, axe, pol, ref, van;
2014-11-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam_2, &pol,
u3x_sam_6, &axe,
u3x_sam_7, &ref,
u3x_con, &van, 0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3ud(axe)) ||
2014-11-18 00:56:51 +03:00
(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_cull(van, sut, pol, axe, 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_cull(u3_noun van,
2014-09-06 00:13:24 +04:00
u3_noun sut,
2014-11-06 06:10:22 +03:00
u3_noun pol,
2014-09-06 00:13:24 +04:00
u3_noun axe,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2014-08-28 23:40:40 +04:00
c3_m fun_m = c3__cull;
2014-11-06 03:20:01 +03:00
u3_noun pro = u3z_find_4(fun_m, sut, pol, axe, 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_cull(van, sut, pol, axe, ref);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
return u3z_save_4(fun_m, sut, pol, axe, ref, pro);
2013-09-29 00:21:18 +04:00
}
}