Browse Source

Everywhere: Change macro into a separate type

master
Riyyi 2 years ago
parent
commit
12d6b8ec88
  1. 17
      src/ast.cpp
  2. 23
      src/ast.h
  3. 2
      src/eval-special-form.cpp
  4. 3
      src/eval.cpp
  5. 12
      src/functions.cpp
  6. 7
      src/printer.cpp

17
src/ast.cpp

@ -260,11 +260,10 @@ Lambda::Lambda(const std::vector<std::string>& bindings, ValuePtr body, Environm
{ {
} }
Lambda::Lambda(std::shared_ptr<Lambda> that, bool is_macro) Lambda::Lambda(const Lambda& that)
: m_bindings(that->m_bindings) : m_bindings(that.m_bindings)
, m_body(that->m_body) , m_body(that.m_body)
, m_env(that->m_env) , m_env(that.m_env)
, m_is_macro(is_macro)
{ {
} }
@ -273,7 +272,13 @@ Lambda::Lambda(const Lambda& that, ValuePtr meta)
, m_bindings(that.m_bindings) , m_bindings(that.m_bindings)
, m_body(that.m_body) , m_body(that.m_body)
, m_env(that.m_env) , m_env(that.m_env)
, m_is_macro(that.m_is_macro) {
}
// -----------------------------------------
Macro::Macro(const Lambda& that)
: Lambda(that)
{ {
} }

23
src/ast.h

@ -58,6 +58,7 @@ public:
virtual bool isCallable() const { return false; } virtual bool isCallable() const { return false; }
virtual bool isFunction() const { return false; } virtual bool isFunction() const { return false; }
virtual bool isLambda() const { return false; } virtual bool isLambda() const { return false; }
virtual bool isMacro() const { return false; }
virtual bool isAtom() const { return false; } virtual bool isAtom() const { return false; }
protected: protected:
@ -343,17 +344,16 @@ private:
// ----------------------------------------- // -----------------------------------------
class Lambda final : public Callable { class Lambda : public Callable {
public: public:
Lambda(const std::vector<std::string>& bindings, ValuePtr body, EnvironmentPtr env); Lambda(const std::vector<std::string>& bindings, ValuePtr body, EnvironmentPtr env);
Lambda(std::shared_ptr<Lambda> that, bool is_macro); Lambda(const Lambda& that);
Lambda(const Lambda& that, ValuePtr meta); Lambda(const Lambda& that, ValuePtr meta);
virtual ~Lambda() = default; virtual ~Lambda() = default;
const std::vector<std::string>& bindings() const { return m_bindings; } const std::vector<std::string>& bindings() const { return m_bindings; }
ValuePtr body() const { return m_body; } ValuePtr body() const { return m_body; }
EnvironmentPtr env() const { return m_env; } EnvironmentPtr env() const { return m_env; }
bool isMacro() const { return m_is_macro; }
WITH_META(Lambda); WITH_META(Lambda);
@ -363,7 +363,19 @@ private:
const std::vector<std::string> m_bindings; const std::vector<std::string> m_bindings;
const ValuePtr m_body; const ValuePtr m_body;
const EnvironmentPtr m_env; 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<Function>() const { return isFunction(); }
template<> template<>
inline bool Value::fastIs<Lambda>() const { return isLambda(); } inline bool Value::fastIs<Lambda>() const { return isLambda(); }
template<>
inline bool Value::fastIs<Macro>() const { return isMacro(); }
template<> template<>
inline bool Value::fastIs<Atom>() const { return isAtom(); } inline bool Value::fastIs<Atom>() const { return isAtom(); }
// clang-format on // clang-format on

2
src/eval-special-form.cpp

@ -63,7 +63,7 @@ ValuePtr Eval::evalDefMacro(const std::list<ValuePtr>& nodes, EnvironmentPtr env
} }
// Modify existing environment // Modify existing environment
return env->set(symbol->symbol(), makePtr<Lambda>(lambda, true)); return env->set(symbol->symbol(), makePtr<Macro>(*lambda));
} }
// (fn* (x) x) // (fn* (x) x)

3
src/eval.cpp

@ -210,9 +210,8 @@ bool Eval::isMacroCall(ValuePtr ast, EnvironmentPtr env)
auto symbol = dynamic_cast<Symbol*>(front)->symbol(); auto symbol = dynamic_cast<Symbol*>(front)->symbol();
auto value = env->get(symbol).get(); auto value = env->get(symbol).get();
auto lambda = dynamic_cast<Lambda*>(value);
if (lambda == nullptr || !lambda->isMacro()) { if (!is<Macro>(value)) {
return false; return false;
} }

12
src/functions.cpp

@ -638,14 +638,11 @@ ADD_FUNCTION(
result = false; result = false;
break; break;
} }
if (is<Lambda>(node.get())) { if (is<Macro>(node.get())) {
auto lambda = std::static_pointer_cast<Lambda>(node);
if (lambda->isMacro()) {
result = false; result = false;
break; break;
} }
} }
}
return makePtr<Constant>(result); return makePtr<Constant>(result);
}); });
@ -660,12 +657,7 @@ ADD_FUNCTION(
} }
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Lambda>(node.get())) { if (!is<Macro>(node.get())) {
result = false;
break;
}
auto lambda = std::static_pointer_cast<Lambda>(node);
if (!lambda->isMacro()) {
result = false; result = false;
break; break;
} }

7
src/printer.cpp

@ -139,8 +139,11 @@ void Printer::printImpl(ValuePtr node, bool print_readably)
} }
else if (is<Lambda>(node_raw_ptr)) { else if (is<Lambda>(node_raw_ptr)) {
printSpacing(); printSpacing();
auto lambda = std::static_pointer_cast<Lambda>(node); m_print += format("#<user-function>({:p})", node_raw_ptr);
m_print += format("#<user-{}>({:p})", (lambda->isMacro()) ? "macro" : "function", node_raw_ptr); }
else if (is<Macro>(node_raw_ptr)) {
printSpacing();
m_print += format("#<user-macro>({:p})", node_raw_ptr);
} }
else if (is<Atom>(node_raw_ptr)) { else if (is<Atom>(node_raw_ptr)) {
printSpacing(); printSpacing();

Loading…
Cancel
Save