From 835669c4eba9f8e55caa3ee32a2b510f6540695b Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 5 Apr 2023 20:37:18 +0200 Subject: [PATCH] AST: Add Atom data type --- src/ast.cpp | 7 +++++++ src/ast.h | 21 +++++++++++++++++++++ src/printer.cpp | 6 ++++++ 3 files changed, 34 insertions(+) diff --git a/src/ast.cpp b/src/ast.cpp index a891168..e83a52b 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -80,6 +80,13 @@ Lambda::Lambda(std::vector bindings, ASTNodePtr body, EnvironmentPt { } +// ----------------------------------------- + +Atom::Atom(ASTNodePtr pointer) + : m_value(pointer) +{ +} + } // namespace blaze // ----------------------------------------- diff --git a/src/ast.h b/src/ast.h index b4ae658..5021d6d 100644 --- a/src/ast.h +++ b/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 std::shared_ptr makePtr(Args&&... args) { @@ -279,6 +297,9 @@ inline bool ASTNode::fastIs() const { return isFunction(); } template<> inline bool ASTNode::fastIs() const { return isLambda(); } + +template<> +inline bool ASTNode::fastIs() const { return isAtom(); } // clang-format on } // namespace blaze diff --git a/src/printer.cpp b/src/printer.cpp index 5f52c41..a4343bd 100644 --- a/src/printer.cpp +++ b/src/printer.cpp @@ -141,6 +141,12 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably) printSpacing(); m_print += format("#({:p})", node_raw_ptr); } + else if (is(node_raw_ptr)) { + printSpacing(); + m_print += "(atom "; + printImpl(std::static_pointer_cast(node)->deref(), print_readably); + m_print += ")"; + } } void Printer::printError()