1
0
Fork 0

Perform lookup

This commit is contained in:
Alex Kotov 2023-05-07 16:19:17 +04:00
parent 74fa59f915
commit 4f9bbf6bdf
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -9,6 +9,8 @@
#include <stddef.h>
#include <string.h>
static struct Object *lookup(struct Object *namespace, struct Object *name);
static struct Object *eval_list(struct Object *object);
struct Object *eval_str(const char *const str)
@ -41,11 +43,7 @@ struct Object *eval(struct Object *const object)
assert(object);
// SYMBOL performs lookup
if (Object_is_symbol(object)) {
struct Object *const procedure = builtins_get(object->s);
assert(procedure);
return procedure;
}
if (Object_is_symbol(object)) return lookup(NULL, object);
// Almost everything evaluates to itself
if (!Object_is_pair(object)) return object;
@ -83,3 +81,23 @@ struct Object *eval_list(struct Object *const object)
);
}
}
struct Object *lookup(struct Object *namespace, struct Object *const name)
{
assert(OBJECT_IS_LIST_HEAD(namespace));
assert(Object_is_symbol(name));
while (!OBJECT_IS_NULL(namespace)) {
assert(Object_is_pair(namespace));
struct Object *const item = namespace->pair.car;
assert(Object_is_pair(item));
assert(Object_is_symbol(item->pair.car));
if (strcmp(item->pair.car->s, name->s) == 0) return item->pair.cdr;
namespace = namespace->pair.cdr;
}
struct Object *const builtin_procedure = builtins_get(name->s);
if (builtin_procedure) return builtin_procedure;
assert(0);
}