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");
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
}
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;
}
}
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;
}
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;
}