From ff4b4c94ad5e7a70af31e3bcb63f84b9b7cdc626 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 22 Dec 2021 18:22:23 +0100 Subject: Integrate builtin text into objects --- src/parser.c | 79 ++++++++++++++++++++++++++---------------------------------- src/parser.h | 1 + 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/parser.c b/src/parser.c index 298220a..3051a28 100644 --- a/src/parser.c +++ b/src/parser.c @@ -5,37 +5,28 @@ static Object **objects = NULL; static Root *roots = NULL; static Environment **environments = NULL; -static char *builtin_names[] = { - "+", "-", "*", "/", "%", - "=", "<", ">", "<=", ">=", - "not", "and", "or", - "nil?", "zero?", "fixnum?", "bool?", - "display", - "cons", "car", "cdr", -}; - static Object builtins[] = { - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_ADD } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_SUB } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_MUL } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_DIV } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_MOD } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_EQ } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_LT } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_GT } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_LE } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_GE } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_NOT } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_AND } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_OR } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_NIL } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_ZERO } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_FIXNUM } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_BOOL } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_PRINT } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_CONS } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_CAR } , - { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_CDR } , + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_ADD, .builtin_text = STRING("+") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_SUB, .builtin_text = STRING("-") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_MUL, .builtin_text = STRING("*") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_DIV, .builtin_text = STRING("/") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_MOD, .builtin_text = STRING("%") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_EQ, .builtin_text = STRING("=") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_LT, .builtin_text = STRING("<") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_GT, .builtin_text = STRING(">") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_LE, .builtin_text = STRING("<=") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_GE, .builtin_text = STRING(">=") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_NOT, .builtin_text = STRING("not") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_AND, .builtin_text = STRING("and") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_OR, .builtin_text = STRING("or") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_NIL, .builtin_text = STRING("nil?") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_ZERO, .builtin_text = STRING("zero?") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_FIXNUM, .builtin_text = STRING("fixnum?") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_BOOL, .builtin_text = STRING("bool?") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_PRINT, .builtin_text = STRING("display") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_CONS, .builtin_text = STRING("cons") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_CAR, .builtin_text = STRING("car") }, + { .type = OBJ_TYPE_BUILTIN, .builtin = BUILTIN_IS_CDR, .builtin_text = STRING("cdr") }, }; Token @@ -104,11 +95,9 @@ parse_string(Token tok) { Object * parse_symbol(Token tok) { // Check if symbol is a builtin procedure. - size_t n_builtins = sizeof(builtin_names) / sizeof(char*); + size_t n_builtins = sizeof(builtins) / sizeof(Object); for (size_t i = 0; i < n_builtins; ++i) { - char *str = builtin_names[i]; - size_t str_n = strlen(str); - if (sv_equal(&tok.value, &(StringView){str, str_n})) { + if (sv_equal(&tok.value, &builtins[i].builtin_text)) { return &builtins[i]; } } @@ -612,11 +601,11 @@ semantic_analysis(Environment *env, Object *obj, Errors *errors) { Object *expr = obj->body[i]; if (i != array_size(obj->body) - 1) { if (IS_FIXNUM(expr) || - IS_STRING(expr) || - IS_SYMBOL(expr) || - IS_LAMBDA(expr) || - IS_BOOL(expr) || - IS_NIL(expr)) { + IS_STRING(expr) || + IS_SYMBOL(expr) || + IS_LAMBDA(expr) || + IS_BOOL(expr) || + IS_NIL(expr)) { continue; } } @@ -662,11 +651,11 @@ parse(Token *tokens, Errors *errors) { Object *root = roots[i]; if (i != array_size(roots) - 1) { if (IS_FIXNUM(root) || - IS_STRING(root) || - IS_SYMBOL(root) || - IS_LAMBDA(root) || - IS_BOOL(root) || - IS_NIL(root)) { + IS_STRING(root) || + IS_SYMBOL(root) || + IS_LAMBDA(root) || + IS_BOOL(root) || + IS_NIL(root)) { continue; } } @@ -832,7 +821,7 @@ object_display(Object *obj) { printf(" }"); } break; case OBJ_TYPE_BUILTIN: { - printf("%s", builtin_names[obj->builtin]); + printf("%.*s", (int)obj->builtin_text.n, obj->builtin_text.start); } break; } return; diff --git a/src/parser.h b/src/parser.h index 41f1fc1..c26e265 100644 --- a/src/parser.h +++ b/src/parser.h @@ -91,6 +91,7 @@ typedef struct Object { // OBJ_TYPE_BUILTIN struct { Builtin builtin; + StringView builtin_text; }; }; -- cgit v1.2.1