urbit/j/6/ut_rest.c

137 lines
2.6 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/6/ut_rest.c
**
** This file is in the public domain.
*/
#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
2014-08-21 01:18:34 +04:00
_rest_in_list(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun leg)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_nul == leg ) {
return u3_nul;
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
u3_noun i_leg = u3h(leg);
u3_noun t_leg = u3t(leg);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
return u3nc
(u3_cqfu_play(van, u3h(i_leg), u3t(i_leg)),
2014-08-21 01:18:34 +04:00
_rest_in_list(van, t_leg));
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
_rest_in_stil(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun gar)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun gun = u3_cqdi_gas(u3_nul, gar);
u3_noun nog = u3_cqdi_tap(gun, u3_nul);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(gun);
2013-09-29 00:21:18 +04:00
return nog;
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-08-21 01:18:34 +04:00
_rest_in_fork(
2014-09-06 00:13:24 +04:00
u3_noun nog,
u3_noun fub)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_no == u3du(nog) ) {
2013-09-29 00:21:18 +04:00
return fub;
} else {
2014-09-06 00:13:24 +04:00
u3_noun buf = _rest_in_fork
2014-08-21 01:18:34 +04:00
(
2014-09-06 00:13:24 +04:00
u3t(nog),
u3_cqf_fork(u3h(nog), fub));
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(fub);
2013-09-29 00:21:18 +04:00
return buf;
}
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-09-17 23:56:55 +04:00
_rest_in(u3_noun van,
2014-09-06 00:13:24 +04:00
u3_noun leg)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun gar = _rest_in_list(van, leg);
u3_noun nog = _rest_in_stil(van, gar);
u3_noun fub = _rest_in_fork(nog, c3__void);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(gar);
u3z(nog);
2013-09-29 00:21:18 +04:00
return fub;
}
2014-09-06 00:13:24 +04:00
static u3_bean
2014-08-21 01:18:34 +04:00
_rest_hit_fan(
2014-09-06 00:13:24 +04:00
u3_noun fan,
u3_noun leg)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
if ( u3_nul == leg ) {
return u3_no;
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
return u3_or(u3_cqdi_has(fan, u3h(leg)),
_rest_hit_fan(fan, u3t(leg)));
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-09-17 23:56:55 +04:00
_cqfu_rest(u3_noun van, u3_noun sut, u3_noun leg)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun fan = u3_cr_at(u3_cqfu_van_fan, van);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
if ( u3_yes == _rest_hit_fan(fan, leg) ) {
return u3_cm_error("rest-loop");
2013-09-29 00:21:18 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_noun naf = u3_cqdi_gas(fan, leg);
2014-09-17 23:56:55 +04:00
u3_noun nav = u3_ci_molt(u3k(van), u3_cqfu_van_fan, u3k(naf), 0);
2014-09-06 00:13:24 +04:00
u3_noun mez = _rest_in(nav, leg);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(naf);
u3z(nav);
2013-09-29 00:21:18 +04:00
return mez;
}
}
2013-09-29 00:21:18 +04:00
/* boilerplate
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwfu_rest(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun sut, leg, van;
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
if ( (u3_no == u3_cr_mean(cor, u3_cv_sam, &leg, u3_cv_con, &van, 0)) ||
(u3_none == (sut = u3_cr_at(u3_cv_sam, van))) )
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-09-03 09:46:55 +04:00
return _cqfu_rest(van, sut, leg);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cqfu_rest(u3_noun van,
u3_noun sut,
u3_noun leg)
2013-09-29 00:21:18 +04:00
{
2014-08-28 23:40:40 +04:00
c3_m fun_m = c3__rest;
2014-09-06 00:13:24 +04:00
u3_noun pro = u3_cz_find_2(fun_m, sut, leg);
2014-08-28 23:40:40 +04:00
2014-09-06 00:13:24 +04: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_rest(van, sut, leg);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
return u3_cz_save_2(fun_m, sut, leg, pro);
2013-09-29 00:21:18 +04:00
}
}