Everywhere: Change macro into a separate type
This commit is contained in:
+11
-6
@@ -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)
|
||||
: 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)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -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<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);
|
||||
virtual ~Lambda() = default;
|
||||
|
||||
const std::vector<std::string>& 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<std::string> 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<Function>() const { return isFunction(); }
|
||||
template<>
|
||||
inline bool Value::fastIs<Lambda>() const { return isLambda(); }
|
||||
|
||||
template<>
|
||||
inline bool Value::fastIs<Macro>() const { return isMacro(); }
|
||||
|
||||
template<>
|
||||
inline bool Value::fastIs<Atom>() const { return isAtom(); }
|
||||
// clang-format on
|
||||
|
||||
@@ -63,7 +63,7 @@ ValuePtr Eval::evalDefMacro(const std::list<ValuePtr>& nodes, EnvironmentPtr env
|
||||
}
|
||||
|
||||
// Modify existing environment
|
||||
return env->set(symbol->symbol(), makePtr<Lambda>(lambda, true));
|
||||
return env->set(symbol->symbol(), makePtr<Macro>(*lambda));
|
||||
}
|
||||
|
||||
// (fn* (x) x)
|
||||
|
||||
+1
-2
@@ -210,9 +210,8 @@ bool Eval::isMacroCall(ValuePtr ast, EnvironmentPtr env)
|
||||
|
||||
auto symbol = dynamic_cast<Symbol*>(front)->symbol();
|
||||
auto value = env->get(symbol).get();
|
||||
auto lambda = dynamic_cast<Lambda*>(value);
|
||||
|
||||
if (lambda == nullptr || !lambda->isMacro()) {
|
||||
if (!is<Macro>(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+2
-10
@@ -638,14 +638,11 @@ ADD_FUNCTION(
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
if (is<Lambda>(node.get())) {
|
||||
auto lambda = std::static_pointer_cast<Lambda>(node);
|
||||
if (lambda->isMacro()) {
|
||||
if (is<Macro>(node.get())) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return makePtr<Constant>(result);
|
||||
});
|
||||
@@ -660,12 +657,7 @@ ADD_FUNCTION(
|
||||
}
|
||||
|
||||
for (auto node : nodes) {
|
||||
if (!is<Lambda>(node.get())) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
auto lambda = std::static_pointer_cast<Lambda>(node);
|
||||
if (!lambda->isMacro()) {
|
||||
if (!is<Macro>(node.get())) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
+5
-2
@@ -139,8 +139,11 @@ void Printer::printImpl(ValuePtr node, bool print_readably)
|
||||
}
|
||||
else if (is<Lambda>(node_raw_ptr)) {
|
||||
printSpacing();
|
||||
auto lambda = std::static_pointer_cast<Lambda>(node);
|
||||
m_print += format("#<user-{}>({:p})", (lambda->isMacro()) ? "macro" : "function", node_raw_ptr);
|
||||
m_print += format("#<user-function>({:p})", 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)) {
|
||||
printSpacing();
|
||||
|
||||
Reference in New Issue
Block a user