lisp-take-1/builtins/core/ifelse.c
2024-11-28 18:36:25 -06:00

91 lines
1.4 KiB
C

#include <debug.h>
#include "../defines/BUILTIN_PARAMETER_DECLARATION.h"
#include <gc/dec_external_refcount.h>
#include <value/struct.h>
#include "ifelse.h"
struct value* builtin_ifelse(
BUILTIN_PARAMETER_DECLARATION)
{
ENTER;
if (arguments->kind != vk_list)
{
TODO;
exit(1);
}
struct list_value* arguments_lv = &arguments->subclass.list;
struct value* first = arguments_lv->first;
struct value* rest = arguments_lv->rest;
if (rest->kind != vk_list)
{
TODO;
exit(1);
}
struct value* second = rest->subclass.list.first;
struct value* restrest = rest->subclass.list.rest;
if (restrest->kind != vk_list)
{
TODO;
exit(1);
}
struct value* third = restrest->subclass.list.first;
struct value* restrestrest = restrest->subclass.list.rest;
if (restrestrest->kind != vk_null)
{
TODO;
exit(1);
}
struct value* conditional = evalfunc(first, environment, gc);
if (conditional->kind != vk_boolean)
{
TODO;
exit(1);
}
struct value* result = evalfunc(
/* expression: */ conditional->subclass.boolean.value ? second : third,
/* environment: */ environment,
/* garbage collection: */ gc);
gc_dec_external_refcount(gc, conditional);
EXIT;
return result;
}