urbit/j/6/ut_heal.c

246 lines
5.5 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/6/heal.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
/* internals
*/
2014-09-06 00:13:24 +04:00
static u3_noun
_heal_as(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
_heal_by(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun sut,
u3_noun qog,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun p_sut, q_sut;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(sut) ) {
2014-11-06 03:20:01 +03:00
return u3m_error("heal-name");
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
else switch ( u3h(sut) ) {
2014-11-06 03:20:01 +03:00
default: return u3m_error("heal-name");
2013-09-29 00:21:18 +04:00
case c3__core:
{
2014-09-06 00:13:24 +04:00
return u3k(ref);
2013-09-29 00:21:18 +04:00
}
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-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(u3h(p_sut), u3t(qog)) ) {
2014-09-06 00:13:24 +04:00
return u3k(ref);
2014-02-11 09:34:24 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_noun sub = _heal_by(van, q_sut, qog, ref);
u3_noun ret;
2014-01-04 06:59:56 +04:00
2014-09-06 00:13:24 +04:00
ret = u3_cqfu_busk
(van, sub, u3h(p_sut), u3h(u3t(p_sut)));
2014-01-04 06:59:56 +04:00
2014-09-06 00:13:24 +04:00
u3z(sub);
2014-02-11 09:34:24 +04:00
return ret;
}
2013-12-26 21:12:07 +04:00
}
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-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(p_sut, u3t(qog)) ) {
2014-09-06 00:13:24 +04:00
return u3_cqf_face(p_sut, ref);
2013-09-29 00:21:18 +04:00
}
2014-11-06 03:20:01 +03:00
else return u3m_error("heal-name");
2013-09-29 00:21:18 +04:00
}
case c3__hold: {
2014-09-06 00:13:24 +04:00
u3_noun rep = u3_cqfu_repo(van, sut);
u3_noun ret = _heal_by(van, rep, qog, ref);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(rep);
2013-09-29 00:21:18 +04:00
return ret;
}
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-09-06 00:13:24 +04:00
u3_noun dis = _heal_by(van, p_sut, qog, ref);
u3_noun dat = _heal_by(van, q_sut, qog, ref);
u3_noun ret = u3_cqf_fork(dis, dat);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(dis);
u3z(dat);
2013-09-29 00:21:18 +04:00
return ret;
}
}
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-08-21 01:18:34 +04:00
_heal_to(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun sut,
u3_noun gil,
u3_noun qog,
u3_noun ref,
u3_atom now,
u3_atom lat)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun p_sut, q_sut;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(sut) ) {
2013-09-29 00:21:18 +04:00
goto repo;
}
2014-09-06 00:13:24 +04:00
else switch ( u3h(sut) ) {
2013-09-29 00:21:18 +04:00
default: goto repo;
case c3__atom: return c3__void;
2014-01-04 06:59:56 +04:00
2014-11-06 03:20:01 +03:00
case c3__bull: u3x_cell(u3t(sut), &p_sut, &q_sut);
2014-01-04 06:59:56 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun sub = _heal_to(van, q_sut, gil, qog, ref, now, lat);
u3_noun ret;
2014-01-04 06:59:56 +04:00
2014-09-06 00:13:24 +04:00
ret = u3_cqfu_busk
(van, sub, u3h(p_sut), u3h(u3t(p_sut)));
2014-01-04 06:59:56 +04:00
2014-09-06 00:13:24 +04:00
u3z(sub);
2014-01-04 06:59:56 +04:00
return ret;
}
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 ter, ret;
2013-09-29 00:21:18 +04:00
2014-08-21 01:18:34 +04:00
if ( 2 == now ) {
ter = _heal_as(van, p_sut, qog, lat, ref);
2014-09-06 00:13:24 +04:00
ret = u3_cqf_cell(ter, q_sut);
2013-09-29 00:21:18 +04:00
} else {
2014-08-21 01:18:34 +04:00
ter = _heal_as(van, q_sut, qog, lat, ref);
2014-09-06 00:13:24 +04:00
ret = u3_cqf_cell(p_sut, ter);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
u3z(ter);
2013-09-29 00:21:18 +04:00
return ret;
}
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-11-06 03:20:01 +03:00
return u3m_error("heal-core");
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
u3_noun ter = _heal_as(van, p_sut, qog, lat, ref);
u3_noun ret = u3_cqf_core(ter, q_sut);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(ter);
2013-09-29 00:21:18 +04:00
return ret;
}
}
2014-11-06 03:20:01 +03:00
case c3__face: u3x_cell(u3t(sut), &p_sut, &q_sut);
2014-04-03 05:06:45 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun dun = _heal_to(van, q_sut, gil, qog, ref, now, lat);
u3_noun ret = u3_cqf_face(p_sut, dun);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(dun);
2014-04-03 05:06:45 +04:00
return ret;
}
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-09-06 00:13:24 +04:00
u3_noun dis = _heal_to(van, p_sut, gil, qog, ref, now, lat);
u3_noun dat = _heal_to(van, q_sut, gil, qog, ref, now, lat);
u3_noun ret = u3_cqf_fork(dis, dat);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(dis);
u3z(dat);
2013-09-29 00:21:18 +04:00
return ret;
}
case c3__hold: {
2014-11-05 04:18:47 +03:00
if ( (c3y == u3_cqdi_has(gil, sut)) ) {
2013-09-29 00:21:18 +04:00
return c3__void;
}
2013-09-29 00:21:18 +04:00
else {
2014-09-06 00:13:24 +04:00
u3_noun zoc = u3_cqdi_put(gil, sut);
u3_noun rep = u3_cqfu_repo(van, sut);
u3_noun ret = _heal_to(van, rep, zoc, qog, ref, now, lat);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(rep);
u3z(zoc);
2013-09-29 00:21:18 +04:00
return ret;
}
}
}
repo: {
2014-09-06 00:13:24 +04:00
u3_noun rep = u3_cqfu_repo(van, sut);
u3_noun ret = _heal_to(van, rep, gil, qog, ref, now, lat);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(rep);
2013-09-29 00:21:18 +04:00
return ret;
}
}
2014-09-06 00:13:24 +04:00
static u3_noun
2014-08-21 01:18:34 +04:00
_heal_as(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun sut,
u3_noun qog,
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-09-06 00:13:24 +04:00
if ( u3_nul == qog ) {
return u3k(ref);
2014-08-21 01:18:34 +04:00
} else return _heal_by(van, sut, qog, ref);
2013-09-29 00:21:18 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_atom now = u3_cqc_cap(axe);
u3_atom lat = u3_cqc_mas(axe);
u3_noun ret = _heal_to(van, sut, u3_nul, qog, ref, now, lat);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(lat);
2013-09-29 00:21:18 +04:00
return ret;
}
}
2013-09-29 00:21:18 +04:00
/* functions
*/
2014-09-06 00:13:24 +04:00
u3_noun
2014-09-03 09:46:55 +04:00
_cqfu_heal(
2014-09-06 00:13:24 +04:00
u3_noun van,
u3_noun sut,
u3_noun qog,
u3_noun axe,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2014-08-21 01:18:34 +04:00
return _heal_as(van, sut, qog, axe, ref);
2013-09-29 00:21:18 +04:00
}
/* boilerplate
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwfu_heal(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun van, sut, qog, axe, ref;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_mean(cor, u3v_sam_2, &qog,
u3v_sam_6, &axe,
u3v_sam_7, &ref,
u3v_con, &van,
2013-09-29 00:21:18 +04:00
0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3ud(axe)) ||
2014-11-06 03:20:01 +03:00
(u3_none == (sut = u3r_at(u3v_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_heal(van, sut, qog, axe, ref);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cqfu_heal(u3_noun van,
u3_noun sut,
u3_noun qog,
u3_noun axe,
u3_noun ref)
2013-09-29 00:21:18 +04:00
{
2014-09-03 09:46:55 +04:00
return _cqfu_heal(van, sut, qog, axe, ref);
2013-09-29 00:21:18 +04:00
}