1
0
Fork 0

comment eval

This commit is contained in:
Alex Kotov 2023-05-04 03:29:29 +04:00
parent c8b083d826
commit bff63075d0
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 9 additions and 4 deletions

13
main.c
View File

@ -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)