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) {
|
switch (object->type) {
|
||||||
|
case TYPE_PROCEDURE:
|
||||||
|
if (object->procedure.name) {
|
||||||
|
printf("#<procedure:%s>", object->procedure.name);
|
||||||
|
} else {
|
||||||
|
printf("#<procedure>");
|
||||||
|
}
|
||||||
|
break;
|
||||||
case TYPE_PAIR:
|
case TYPE_PAIR:
|
||||||
printf("(");
|
printf("(");
|
||||||
display_pair(object);
|
display_pair(object);
|
||||||
|
|
1
enums.c
1
enums.c
|
@ -38,6 +38,7 @@ const char *TokenType_to_str(const enum TokenType token_type)
|
||||||
const char *Type_to_str(const enum Type type)
|
const char *Type_to_str(const enum Type type)
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
case TYPE_PROCEDURE: return "TYPE_PROCEDURE";
|
||||||
case TYPE_PAIR: return "TYPE_PAIR";
|
case TYPE_PAIR: return "TYPE_PAIR";
|
||||||
case TYPE_BOOLEAN: return "TYPE_BOOLEAN";
|
case TYPE_BOOLEAN: return "TYPE_BOOLEAN";
|
||||||
case TYPE_CHAR: return "TYPE_CHAR";
|
case TYPE_CHAR: return "TYPE_CHAR";
|
||||||
|
|
1
enums.h
1
enums.h
|
@ -28,6 +28,7 @@ enum TokenType {
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
|
TYPE_PROCEDURE,
|
||||||
TYPE_PAIR,
|
TYPE_PAIR,
|
||||||
TYPE_BOOLEAN,
|
TYPE_BOOLEAN,
|
||||||
TYPE_CHAR,
|
TYPE_CHAR,
|
||||||
|
|
22
object.c
22
object.c
|
@ -16,6 +16,21 @@ static struct Object *new(const enum Type type)
|
||||||
return object;
|
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 *Object_new_pair(struct Object *const a, struct Object *const b)
|
||||||
{
|
{
|
||||||
struct Object *const object = new(TYPE_PAIR);
|
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));
|
printf("%s:", Type_to_str(self->type));
|
||||||
|
|
||||||
switch (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:
|
case TYPE_PAIR:
|
||||||
printf("\n");
|
printf("\n");
|
||||||
Object_print(self->pair.a, indent + 1);
|
Object_print(self->pair.a, indent + 1);
|
||||||
|
|
9
object.h
9
object.h
|
@ -16,6 +16,11 @@
|
||||||
struct Object {
|
struct Object {
|
||||||
enum Type type;
|
enum Type type;
|
||||||
union {
|
union {
|
||||||
|
// For PROCEDURE
|
||||||
|
struct {
|
||||||
|
char *name;
|
||||||
|
struct Object *(*func)(struct Object *args);
|
||||||
|
} procedure;
|
||||||
// For PAIR
|
// For PAIR
|
||||||
struct {
|
struct {
|
||||||
struct Object *a, *b;
|
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_pair(struct Object *a, struct Object *b);
|
||||||
struct Object *Object_new_boolean(bool boolean);
|
struct Object *Object_new_boolean(bool boolean);
|
||||||
struct Object *Object_new_char(char chr);
|
struct Object *Object_new_char(char chr);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue