From: Elijah Cohen Date: Sat, 17 Aug 2024 06:15:09 +0000 (+0000) Subject: refactored environment loading, much simpler X-Git-Tag: v-12.13.14~31 X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=72404bb5296f9ba41bdc8654dfc16629af217329;p=klapaucius refactored environment loading, much simpler --- diff --git a/src/builtins.c b/src/builtins.c index 9641ea0..c336bfa 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -39,6 +39,13 @@ uint64_t u64_get_num_args(Sexpr* b) { return curr_num; } +void load_builtin(char* ks, uint64_t op, Sexpr* env) { + Sexpr* k = from_sym(ks); + Sexpr* v = from_opcode(op); + append_to_dict(env, k, v); + sexpr_free(v); + sexpr_free(k); +} Sexpr* load_env(Sexpr* env) { Sexpr* nilk = from_sym("nil"); diff --git a/src/builtins.h b/src/builtins.h index c20e829..a37b2c6 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -7,6 +7,7 @@ Sexpr* dispatch(Sexpr* b, Sexpr* rest, Sexpr* env); uint64_t u64_get_num_args(Sexpr* b); +void load_builtin(char* ks, uint64_t op, Sexpr* env); Sexpr* load_env(Sexpr* env); #endif diff --git a/src/builtins/arithmetic.c b/src/builtins/arithmetic.c index 521ba17..c99cda0 100644 --- a/src/builtins/arithmetic.c +++ b/src/builtins/arithmetic.c @@ -107,37 +107,11 @@ Sexpr* x_arith_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { } Sexpr* load_arith_env(Sexpr* env) { - Sexpr* k; - Sexpr* v; - - k = from_sym("+"); - v = from_opcode((ARITH_PREFIX << 8) | ARITH_ADD); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("-"); - v = from_opcode((ARITH_PREFIX << 8) | ARITH_SUB); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("*"); - v = from_opcode((ARITH_PREFIX << 8) | ARITH_MUL); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("/"); - v = from_opcode((ARITH_PREFIX << 8) | ARITH_DIV); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("%"); - v = from_opcode((ARITH_PREFIX << 8) | ARITH_MOD); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); + load_builtin("+", (ARITH_PREFIX << 8) | ARITH_ADD, env); + load_builtin("-", (ARITH_PREFIX << 8) | ARITH_SUB, env); + load_builtin("*", (ARITH_PREFIX << 8) | ARITH_MUL, env); + load_builtin("/", (ARITH_PREFIX << 8) | ARITH_DIV, env); + load_builtin("%", (ARITH_PREFIX << 8) | ARITH_MOD, env); + return env; } diff --git a/src/builtins/combinators.c b/src/builtins/combinators.c index b9d1ac9..ac1d352 100644 --- a/src/builtins/combinators.c +++ b/src/builtins/combinators.c @@ -113,44 +113,12 @@ Sexpr* x_comb_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { } Sexpr* load_comb_env(Sexpr* env) { - Sexpr* k; - Sexpr* v; + load_builtin("I", (COMB_PREFIX << 8) | COMB_I, env); + load_builtin("S", (COMB_PREFIX << 8) | COMB_S, env); + load_builtin("K", (COMB_PREFIX << 8) | COMB_K, env); + load_builtin("B", (COMB_PREFIX << 8) | COMB_B, env); + load_builtin("C", (COMB_PREFIX << 8) | COMB_C, env); + load_builtin("W", (COMB_PREFIX << 8) | COMB_W, env); - k = from_sym("I"); - v = from_opcode((COMB_PREFIX << 8) | COMB_I); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("S"); - v = from_opcode((COMB_PREFIX << 8) | COMB_S); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("K"); - v = from_opcode((COMB_PREFIX << 8) | COMB_K); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("B"); - v = from_opcode((COMB_PREFIX << 8) | COMB_B); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("C"); - v = from_opcode((COMB_PREFIX << 8) | COMB_C); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - - k = from_sym("W"); - v = from_opcode((COMB_PREFIX << 8) | COMB_W); - append_to_dict(env, k, v); - sexpr_free(v); - sexpr_free(k); - return env; } diff --git a/src/builtins/core.c b/src/builtins/core.c index d7b97e1..a72b87f 100644 --- a/src/builtins/core.c +++ b/src/builtins/core.c @@ -189,65 +189,17 @@ Sexpr* x_core_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* load_core_env(Sexpr* env) { - Sexpr* k = from_sym("quote"); - Sexpr* c_quote = from_opcode(CORE_QUOTE); - append_to_dict(env, k, c_quote); - sexpr_free(c_quote); - sexpr_free(k); + load_builtin("quote", (CORE_PREFIX << 8) | CORE_QUOTE, env); + load_builtin("cons", (CORE_PREFIX << 8) | CORE_CONS, env); + load_builtin("car", (CORE_PREFIX << 8) | CORE_CAR, env); + load_builtin("cdr", (CORE_PREFIX << 8) | CORE_CDR, env); + load_builtin("if", (CORE_PREFIX << 8) | CORE_IF, env); + load_builtin("eq", (CORE_PREFIX << 8) | CORE_EQ, env); + load_builtin("not", (CORE_PREFIX << 8) | CORE_NOT, env); + load_builtin("atom", (CORE_PREFIX << 8) | CORE_ATOM, env); - k = from_sym("cons"); - Sexpr* c_cons = from_opcode(CORE_CONS); - append_to_dict(env, k, c_cons); - sexpr_free(c_cons); - sexpr_free(k); - - k = from_sym("car"); - Sexpr* c_car = from_opcode(CORE_CAR); - append_to_dict(env, k, c_car); - sexpr_free(c_car); - sexpr_free(k); + load_builtin("def", (CORE_PREFIX << 8) | CORE_DEF, env); + load_builtin("exit", (CORE_PREFIX << 8) | CORE_EXIT, env); - k = from_sym("cdr"); - Sexpr* c_cdr = from_opcode(CORE_CDR); - append_to_dict(env, k, c_cdr); - sexpr_free(c_cdr); - sexpr_free(k); - - k = from_sym("if"); - Sexpr* c_if = from_opcode(CORE_IF); - append_to_dict(env, k, c_if); - sexpr_free(c_if); - sexpr_free(k); - - k = from_sym("eq"); - Sexpr* c_eq = from_opcode(CORE_EQ); - append_to_dict(env, k, c_eq); - sexpr_free(c_eq); - sexpr_free(k); - - k = from_sym("not"); - Sexpr* c_not = from_opcode(CORE_NOT); - append_to_dict(env, k, c_not); - sexpr_free(c_not); - sexpr_free(k); - - k = from_sym("atom"); - Sexpr* c_atom = from_opcode(CORE_ATOM); - append_to_dict(env, k, c_atom); - sexpr_free(c_atom); - sexpr_free(k); - - k = from_sym("def"); - Sexpr* c_def = from_opcode(CORE_DEF); - append_to_dict(env, k, c_def); - sexpr_free(c_def); - sexpr_free(k); - - k = from_sym("exit"); - Sexpr* c_exit = from_opcode(CORE_EXIT); - append_to_dict(env, k, c_exit); - sexpr_free(c_exit); - sexpr_free(k); - return env; }