Add TYPE_PROCEDURE
This commit is contained in:
parent
a222a8bbca
commit
d732c955ce
5 changed files with 46 additions and 6 deletions
|
@ -259,6 +259,13 @@ struct Object *func_display(struct Object *const args)
|
|||
}
|
||||
|
||||
switch (object->type) {
|
||||
case TYPE_PROCEDURE:
|
||||
if (object->procedure.name) {
|
||||
printf("#<procedure:%s>", object->procedure.name);
|
||||
} else {
|
||||
printf("#<procedure>");
|
||||
}
|
||||
break;
|
||||
case TYPE_PAIR:
|
||||
printf("(");
|
||||
display_pair(object);
|
||||
|
|
13
enums.c
13
enums.c
|
@ -38,12 +38,13 @@ const char *TokenType_to_str(const enum TokenType token_type)
|
|||
const char *Type_to_str(const enum Type type)
|
||||
{
|
||||
switch (type) {
|
||||
case TYPE_PAIR: return "TYPE_PAIR";
|
||||
case TYPE_BOOLEAN: return "TYPE_BOOLEAN";
|
||||
case TYPE_CHAR: return "TYPE_CHAR";
|
||||
case TYPE_SYMBOL: return "TYPE_SYMBOL";
|
||||
case TYPE_STRING: return "TYPE_STRING";
|
||||
case TYPE_NUMBER: return "TYPE_NUMBER";
|
||||
case TYPE_PROCEDURE: return "TYPE_PROCEDURE";
|
||||
case TYPE_PAIR: return "TYPE_PAIR";
|
||||
case TYPE_BOOLEAN: return "TYPE_BOOLEAN";
|
||||
case TYPE_CHAR: return "TYPE_CHAR";
|
||||
case TYPE_SYMBOL: return "TYPE_SYMBOL";
|
||||
case TYPE_STRING: return "TYPE_STRING";
|
||||
case TYPE_NUMBER: return "TYPE_NUMBER";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
1
enums.h
1
enums.h
|
@ -28,6 +28,7 @@ enum TokenType {
|
|||
};
|
||||
|
||||
enum Type {
|
||||
TYPE_PROCEDURE,
|
||||
TYPE_PAIR,
|
||||
TYPE_BOOLEAN,
|
||||
TYPE_CHAR,
|
||||
|
|
22
object.c
22
object.c
|
@ -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);
|
||||
|
|
9
object.h
9
object.h
|
@ -16,6 +16,11 @@
|
|||
struct Object {
|
||||
enum Type type;
|
||||
union {
|
||||
// For PROCEDURE
|
||||
struct {
|
||||
char *name;
|
||||
struct Object *(*func)(struct Object *args);
|
||||
} procedure;
|
||||
// For PAIR
|
||||
struct {
|
||||
struct Object *a, *b;
|
||||
|
@ -31,6 +36,10 @@ struct Object {
|
|||
};
|
||||
};
|
||||
|
||||
struct Object *Object_new_procedure(
|
||||
const char *name,
|
||||
struct Object *(*func)(struct Object *args)
|
||||
);
|
||||
struct Object *Object_new_pair(struct Object *a, struct Object *b);
|
||||
struct Object *Object_new_boolean(bool boolean);
|
||||
struct Object *Object_new_char(char chr);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue