Browse Source

AST: Add Atom data type

master
Riyyi 2 years ago
parent
commit
835669c4eb
  1. 7
      src/ast.cpp
  2. 21
      src/ast.h
  3. 6
      src/printer.cpp

7
src/ast.cpp

@ -80,6 +80,13 @@ Lambda::Lambda(std::vector<std::string> bindings, ASTNodePtr body, EnvironmentPt
{
}
// -----------------------------------------
Atom::Atom(ASTNodePtr pointer)
: m_value(pointer)
{
}
} // namespace blaze
// -----------------------------------------

21
src/ast.h

@ -43,6 +43,7 @@ public:
virtual bool isSymbol() const { return false; }
virtual bool isFunction() const { return false; }
virtual bool isLambda() const { return false; }
virtual bool isAtom() const { return false; }
protected:
ASTNode() {}
@ -238,6 +239,23 @@ private:
// -----------------------------------------
class Atom final : public ASTNode {
public:
Atom() = default;
Atom(ASTNodePtr pointer);
virtual ~Atom() = default;
ASTNodePtr reset(ASTNodePtr value) { return m_value = value; }
ASTNodePtr deref() const { return m_value; }
private:
virtual bool isAtom() const override { return true; }
ASTNodePtr m_value;
};
// -----------------------------------------
template<typename T, typename... Args>
std::shared_ptr<T> makePtr(Args&&... args)
{
@ -279,6 +297,9 @@ inline bool ASTNode::fastIs<Function>() const { return isFunction(); }
template<>
inline bool ASTNode::fastIs<Lambda>() const { return isLambda(); }
template<>
inline bool ASTNode::fastIs<Atom>() const { return isAtom(); }
// clang-format on
} // namespace blaze

6
src/printer.cpp

@ -141,6 +141,12 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably)
printSpacing();
m_print += format("#<user-function>({:p})", node_raw_ptr);
}
else if (is<Atom>(node_raw_ptr)) {
printSpacing();
m_print += "(atom ";
printImpl(std::static_pointer_cast<Atom>(node)->deref(), print_readably);
m_print += ")";
}
}
void Printer::printError()

Loading…
Cancel
Save