From d732c955ce052eb7c90539586ec054b28c2bc3eb Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 4 May 2023 21:24:54 +0400 Subject: [PATCH] Add TYPE_PROCEDURE --- builtins.c | 7 +++++++ enums.c | 13 +++++++------ enums.h | 1 + object.c | 22 ++++++++++++++++++++++ object.h | 9 +++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/builtins.c b/builtins.c index ac5cc4c..dc52206 100644 --- a/builtins.c +++ b/builtins.c @@ -259,6 +259,13 @@ struct Object *func_display(struct Object *const args) } switch (object->type) { + case TYPE_PROCEDURE: + if (object->procedure.name) { + printf("#", object->procedure.name); + } else { + printf("#"); + } + break; case TYPE_PAIR: printf("("); display_pair(object); diff --git a/enums.c b/enums.c index 7eb1b6a..f86d153 100644 --- a/enums.c +++ b/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; } diff --git a/enums.h b/enums.h index a26a593..a9d3b3f 100644 --- a/enums.h +++ b/enums.h @@ -28,6 +28,7 @@ enum TokenType { }; enum Type { + TYPE_PROCEDURE, TYPE_PAIR, TYPE_BOOLEAN, TYPE_CHAR, diff --git a/object.c b/object.c index 7d600aa..d6921f6 100644 --- a/object.c +++ b/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("#\n", self->procedure.name); + } else { + printf("#\n"); + } + break; case TYPE_PAIR: printf("\n"); Object_print(self->pair.a, indent + 1); diff --git a/object.h b/object.h index e72044f..1df2262 100644 --- a/object.h +++ b/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);