From: Elijah Cohen Date: Fri, 27 Sep 2024 18:02:43 +0000 (-0500) Subject: did macro for compile-time uint sizing X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=1deb8e57e1c73eada75a1b0257f315c48a26bcce;p=klapaucius did macro for compile-time uint sizing --- diff --git a/src/builtins.c b/src/builtins.c index fa344f9..6edb70c 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -63,6 +63,8 @@ Sexpr* dispatch_others(Sexpr* b, Sexpr* rest, Sexpr* env) { return from_nil(); } + +// do i change this with variable uintness? uint64_t u64_get_num_args(Sexpr* b) { uint64_t curr_num = 0; Sexpr* args = b->value.b.args; diff --git a/src/builtins/arithmetic.c b/src/builtins/arithmetic.c index c99cda0..a64e5c5 100644 --- a/src/builtins/arithmetic.c +++ b/src/builtins/arithmetic.c @@ -17,8 +17,8 @@ Sexpr* a_plus(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* i = eval(clone(car(args)), env); Sexpr* j = eval(clone(car(cdr(args))), env); - uint64_t m = unquote(i)->value.u; - uint64_t n = unquote(j)->value.u; + K_UINT_TYPE m = unquote(i)->value.u; + K_UINT_TYPE n = unquote(j)->value.u; sexpr_free(b); sexpr_free(i); sexpr_free(j); @@ -32,8 +32,8 @@ Sexpr* a_minus(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* i = eval(clone(car(args)), env); Sexpr* j = eval(clone(car(cdr(args))), env); - uint64_t m = unquote(i)->value.u; - uint64_t n = unquote(j)->value.u; + K_UINT_TYPE m = unquote(i)->value.u; + K_UINT_TYPE n = unquote(j)->value.u; sexpr_free(b); sexpr_free(i); sexpr_free(j); @@ -47,8 +47,8 @@ Sexpr* a_mul(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* i = eval(clone(car(args)), env); Sexpr* j = eval(clone(car(cdr(args))), env); - uint64_t m = unquote(i)->value.u; - uint64_t n = unquote(j)->value.u; + K_UINT_TYPE m = unquote(i)->value.u; + K_UINT_TYPE n = unquote(j)->value.u; sexpr_free(b); sexpr_free(i); sexpr_free(j); @@ -62,8 +62,8 @@ Sexpr* a_div(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* i = eval(clone(car(args)), env); Sexpr* j = eval(clone(car(cdr(args))), env); - uint64_t m = unquote(i)->value.u; - uint64_t n = unquote(j)->value.u; + K_UINT_TYPE m = unquote(i)->value.u; + K_UINT_TYPE n = unquote(j)->value.u; sexpr_free(b); sexpr_free(i); sexpr_free(j); @@ -77,8 +77,8 @@ Sexpr* a_mod(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* i = eval(clone(car(args)), env); Sexpr* j = eval(clone(car(cdr(args))), env); - uint64_t m = unquote(i)->value.u; - uint64_t n = unquote(j)->value.u; + K_UINT_TYPE m = unquote(i)->value.u; + K_UINT_TYPE n = unquote(j)->value.u; sexpr_free(b); sexpr_free(i); sexpr_free(j); diff --git a/src/builtins/core.c b/src/builtins/core.c index 504d127..cec329e 100644 --- a/src/builtins/core.c +++ b/src/builtins/core.c @@ -1,5 +1,5 @@ - +#include "../config.h" #include "../types.h" #include "../builtins.h" #include "../sexpr.h" @@ -142,7 +142,7 @@ Sexpr* c_applyn(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* fun = car(cdr(args)); Sexpr* snum = eval(clone(car(cdr(cdr(args)))), env); // uh yeah i'm not typechecking lol - uint64_t num = snum->value.u; + K_UINT_TYPE num = snum->value.u; sexpr_free(snum); // fun not cloned due since i'll be cloning it in the loop Sexpr* ret = cons(arg, from_nil()); @@ -170,7 +170,7 @@ Sexpr* c_exit(Sexpr* b, Sexpr* rest, Sexpr* env) { if(CORE_EXIT_ARGS != u64_get_num_args(b)) return cons(b, rest); Sexpr* args = b->value.b.args; - uint64_t value = car(args)->value.u; + K_UINT_TYPE value = car(args)->value.u; sexpr_free(b); sexpr_free(rest); sexpr_free(env); diff --git a/src/parser.c b/src/parser.c index 5e3124a..d70ea59 100644 --- a/src/parser.c +++ b/src/parser.c @@ -4,6 +4,7 @@ #include #include +#include "config.h" #include "types.h" #include "sexpr.h" @@ -62,7 +63,7 @@ Sexpr* tokenize(char* s) { return tokens; } -bool is_u64(char* s) { +bool is_uint(char* s) { while(*s!='\0') { if(!isdigit(*s)) return false; s++; @@ -78,9 +79,9 @@ Sexpr* vals_parse(Sexpr* tokens) { Sexpr* next = NULL; while(tokens->type != NIL) { char* s = car(tokens)->value.s; - if(is_u64(s)) { - uint64_t u; - sscanf(s, "%" SCNu64, &u); + if(is_uint(s)) { + K_UINT_TYPE u; + sscanf(s, "%" K_UINT_SCAN, &u); next = from_uint(u); } else { diff --git a/src/sexpr.c b/src/sexpr.c index 07bdecf..c77bfdb 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -3,6 +3,8 @@ #include #include +#include "config.h" + #include "types.h" #include "sexpr.h" @@ -29,7 +31,7 @@ Sexpr* from_sym(char* s) { return ret; } -Sexpr* from_uint(uint64_t u) { +Sexpr* from_uint(K_UINT_TYPE u) { Sexpr* ret = malloc(sizeof(Sexpr)); ret->type = UINT; ret->value.u = u; @@ -210,7 +212,7 @@ char* sprint_sexpr(Sexpr* s) { return out; } else if(s->type == UINT) { - nbytes = snprintf(NULL, 0, "%" PRIu64 "", s->value.u) + 1; + nbytes = snprintf(NULL, 0, "%" K_UINT_PRINT "", s->value.u) + 1; out = malloc(nbytes*sizeof(char)); snprintf(out, nbytes, "%" PRIu64 "", s->value.u); return out; diff --git a/src/types.h b/src/types.h index f5c377a..b48ba4c 100644 --- a/src/types.h +++ b/src/types.h @@ -7,6 +7,8 @@ #include #include +#include "config.h" + typedef char* Symbol_t; typedef void* Nil_t; typedef void* Truth_t; @@ -22,6 +24,8 @@ typedef struct Cons { typedef struct Builtin { struct Sexpr* args; + // what shall i do with my plan for compile-time uint sizes here? + // it can stay as-is for now uint64_t opcode; } Builtin_t; @@ -29,7 +33,7 @@ typedef struct Builtin { typedef struct Sexpr { Sexpr_Type type; union { - uint64_t u; + K_UINT_TYPE u; Builtin_t b; Symbol_t s; Cons_t* c;