From 12d6b8ec885a82405f37302342e22bc04d08021d Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 1 May 2023 10:24:38 +0200 Subject: [PATCH] Everywhere: Change macro into a separate type --- src/ast.cpp | 17 +++++++++++------ src/ast.h | 23 +++++++++++++++++++---- src/eval-special-form.cpp | 2 +- src/eval.cpp | 3 +-- src/functions.cpp | 16 ++++------------ src/printer.cpp | 7 +++++-- 6 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/ast.cpp b/src/ast.cpp index a26b4d9..1fc165e 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -260,11 +260,10 @@ Lambda::Lambda(const std::vector& bindings, ValuePtr body, Environm { } -Lambda::Lambda(std::shared_ptr that, bool is_macro) - : m_bindings(that->m_bindings) - , m_body(that->m_body) - , m_env(that->m_env) - , m_is_macro(is_macro) +Lambda::Lambda(const Lambda& that) + : m_bindings(that.m_bindings) + , m_body(that.m_body) + , m_env(that.m_env) { } @@ -273,7 +272,13 @@ Lambda::Lambda(const Lambda& that, ValuePtr meta) , m_bindings(that.m_bindings) , m_body(that.m_body) , m_env(that.m_env) - , m_is_macro(that.m_is_macro) +{ +} + +// ----------------------------------------- + +Macro::Macro(const Lambda& that) + : Lambda(that) { } diff --git a/src/ast.h b/src/ast.h index f064c3f..cbbfbf0 100644 --- a/src/ast.h +++ b/src/ast.h @@ -58,6 +58,7 @@ public: virtual bool isCallable() const { return false; } virtual bool isFunction() const { return false; } virtual bool isLambda() const { return false; } + virtual bool isMacro() const { return false; } virtual bool isAtom() const { return false; } protected: @@ -343,17 +344,16 @@ private: // ----------------------------------------- -class Lambda final : public Callable { +class Lambda : public Callable { public: Lambda(const std::vector& bindings, ValuePtr body, EnvironmentPtr env); - Lambda(std::shared_ptr that, bool is_macro); + Lambda(const Lambda& that); Lambda(const Lambda& that, ValuePtr meta); virtual ~Lambda() = default; const std::vector& bindings() const { return m_bindings; } ValuePtr body() const { return m_body; } EnvironmentPtr env() const { return m_env; } - bool isMacro() const { return m_is_macro; } WITH_META(Lambda); @@ -363,7 +363,19 @@ private: const std::vector m_bindings; const ValuePtr m_body; const EnvironmentPtr m_env; - const bool m_is_macro { false }; +}; + +// ----------------------------------------- + +class Macro final : public Lambda { +public: + Macro(const Lambda& that); + + WITH_NO_META(); + +private: + virtual bool isLambda() const override { return false; } + virtual bool isMacro() const override { return true; } }; // ----------------------------------------- @@ -424,6 +436,9 @@ inline bool Value::fastIs() const { return isFunction(); } template<> inline bool Value::fastIs() const { return isLambda(); } +template<> +inline bool Value::fastIs() const { return isMacro(); } + template<> inline bool Value::fastIs() const { return isAtom(); } // clang-format on diff --git a/src/eval-special-form.cpp b/src/eval-special-form.cpp index 0c8bb1f..7b51c77 100644 --- a/src/eval-special-form.cpp +++ b/src/eval-special-form.cpp @@ -63,7 +63,7 @@ ValuePtr Eval::evalDefMacro(const std::list& nodes, EnvironmentPtr env } // Modify existing environment - return env->set(symbol->symbol(), makePtr(lambda, true)); + return env->set(symbol->symbol(), makePtr(*lambda)); } // (fn* (x) x) diff --git a/src/eval.cpp b/src/eval.cpp index 2c717f0..20aeffe 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -210,9 +210,8 @@ bool Eval::isMacroCall(ValuePtr ast, EnvironmentPtr env) auto symbol = dynamic_cast(front)->symbol(); auto value = env->get(symbol).get(); - auto lambda = dynamic_cast(value); - if (lambda == nullptr || !lambda->isMacro()) { + if (!is(value)) { return false; } diff --git a/src/functions.cpp b/src/functions.cpp index b342bba..972e408 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -638,12 +638,9 @@ ADD_FUNCTION( result = false; break; } - if (is(node.get())) { - auto lambda = std::static_pointer_cast(node); - if (lambda->isMacro()) { - result = false; - break; - } + if (is(node.get())) { + result = false; + break; } } @@ -660,12 +657,7 @@ ADD_FUNCTION( } for (auto node : nodes) { - if (!is(node.get())) { - result = false; - break; - } - auto lambda = std::static_pointer_cast(node); - if (!lambda->isMacro()) { + if (!is(node.get())) { result = false; break; } diff --git a/src/printer.cpp b/src/printer.cpp index 8ca2cf5..1eecf9f 100644 --- a/src/printer.cpp +++ b/src/printer.cpp @@ -139,8 +139,11 @@ void Printer::printImpl(ValuePtr node, bool print_readably) } else if (is(node_raw_ptr)) { printSpacing(); - auto lambda = std::static_pointer_cast(node); - m_print += format("#({:p})", (lambda->isMacro()) ? "macro" : "function", node_raw_ptr); + m_print += format("#({:p})", node_raw_ptr); + } + else if (is(node_raw_ptr)) { + printSpacing(); + m_print += format("#({:p})", node_raw_ptr); } else if (is(node_raw_ptr)) { printSpacing();