goalc: add macro-expand form (#3000)

This commit is contained in:
Hat Kid
2023-09-17 22:55:25 +02:00
committed by GitHub
parent 36213aaedb
commit bfb03d4934
3 changed files with 19 additions and 0 deletions
+1
View File
@@ -658,6 +658,7 @@ class Compiler {
Val* compile_defglobalconstant(const goos::Object& form, const goos::Object& rest, Env* env);
Val* compile_defconstant(const goos::Object& form, const goos::Object& rest, Env* env);
Val* compile_mlet(const goos::Object& form, const goos::Object& rest, Env* env);
Val* compile_macro_expand(const goos::Object& form, const goos::Object& rest, Env* env);
// Math
Val* compile_add(const goos::Object& form, const goos::Object& rest, Env* env);
+3
View File
@@ -226,6 +226,9 @@ const std::unordered_map<
{"quote", {"", &Compiler::compile_quote}},
{"mlet", {"", &Compiler::compile_mlet}},
{"defconstant", {"", &Compiler::compile_defconstant}},
{"macro-expand",
{"Displays the expanded form of a macro without evaluating it.",
&Compiler::compile_macro_expand}},
// OBJECT
// {"current-method-type", {"", &Compiler::compile_current_method_type}},
+15
View File
@@ -265,6 +265,21 @@ Val* Compiler::compile_mlet(const goos::Object& form, const goos::Object& rest,
return result;
}
Val* Compiler::compile_macro_expand(const goos::Object& form, const goos::Object& rest, Env* env) {
auto macro = pair_car(rest);
goos::Object macro_obj;
if (!try_getting_macro_from_goos(pair_car(macro), &macro_obj)) {
throw_compiler_error(form, "{} is not a macro.", pair_car(macro).print());
}
// the pretty printer doesn't support macro objects, so we use the pair
auto result = goos::Object(macro);
while (expand_macro_once(result, &result, env)) {
}
auto code = pretty_print::to_string(result);
lg::print("{}\n", code);
return get_none();
}
bool Compiler::expand_macro_once(const goos::Object& src, goos::Object* out, Env*) {
if (!src.is_pair()) {
return false;