comment eval
This commit is contained in:
parent
c8b083d826
commit
bff63075d0
13
main.c
13
main.c
|
@ -337,31 +337,36 @@ struct Object *parens_part()
|
|||
|
||||
struct Object *eval(struct Object *const program)
|
||||
{
|
||||
// NULL is an empty list, can't eval
|
||||
if (!program) {
|
||||
error("can't eval null");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Almost everything evaluates to itself
|
||||
if (program->type != TYPE_PAIR && program->type != TYPE_ATOM) {
|
||||
return program;
|
||||
}
|
||||
|
||||
// Atoms are variable names, but we can't lookup
|
||||
if (program->type == TYPE_ATOM) {
|
||||
error("can't eval atoms");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// The first item of pair should be an atom - a function name
|
||||
if (!program->pair.a || program->pair.a->type != TYPE_ATOM) {
|
||||
error("eval expects atom");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strcmp(program->pair.a->s, "sum") != 0) {
|
||||
error("unknown func");
|
||||
return NULL;
|
||||
// The func "sum"
|
||||
if (strcmp(program->pair.a->s, "sum") == 0) {
|
||||
return func_sum(program->pair.b);
|
||||
}
|
||||
|
||||
return func_sum(program->pair.b);
|
||||
error("unknown func");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Object *func_sum(struct Object *const numbers)
|
||||
|
|
Loading…
Reference in New Issue