1
0
Fork 0

Add TYPE_PROCEDURE

This commit is contained in:
Alex Kotov 2023-05-04 21:24:54 +04:00
parent a222a8bbca
commit d732c955ce
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 46 additions and 6 deletions

View file

@ -16,6 +16,21 @@ static struct Object *new(const enum Type type)
return object;
}
struct Object *Object_new_procedure(
const char *const name,
struct Object *(*const func)(struct Object *args)
) {
struct Object *const object = new(TYPE_PROCEDURE);
object->procedure.name = NULL;
if (name && name[0]) {
object->procedure.name = malloc(strlen(name) + 1);
assert(object->procedure.name);
strcpy(object->procedure.name, name);
}
object->procedure.func = func;
return object;
}
struct Object *Object_new_pair(struct Object *const a, struct Object *const b)
{
struct Object *const object = new(TYPE_PAIR);
@ -104,6 +119,13 @@ void Object_print(struct Object *const self, const unsigned indent)
printf("%s:", Type_to_str(self->type));
switch (self->type) {
case TYPE_PROCEDURE:
if (self->procedure.name) {
printf("#<procedure:%s>\n", self->procedure.name);
} else {
printf("#<procedure>\n");
}
break;
case TYPE_PAIR:
printf("\n");
Object_print(self->pair.a, indent + 1);