From 5c5a766b7e4e81ac7292a2f08568f756500785fd Mon Sep 17 00:00:00 2001 From: Riyyi Date: Fri, 24 Mar 2023 21:49:08 +0100 Subject: [PATCH] Reader+Printer: Reorder tokens --- src/ast.h | 30 +++++++++++++++--------------- src/lexer.cpp | 12 ++++++------ src/lexer.h | 4 ++-- src/printer.cpp | 26 +++++++++++++------------- src/reader.cpp | 34 +++++++++++++++++----------------- src/reader.h | 2 +- 6 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/ast.h b/src/ast.h index 6cf3134..0fcd2d0 100644 --- a/src/ast.h +++ b/src/ast.h @@ -65,6 +65,18 @@ private: // ----------------------------------------- +// () +class List final : public Collection { +public: + List() = default; + virtual ~List() = default; + + virtual bool isCollection() const override { return false; } + virtual bool isList() const override { return true; } +}; + +// ----------------------------------------- + // [] class Vector final : public Collection { public: @@ -89,18 +101,6 @@ public: // ----------------------------------------- -// () -class List final : public Collection { -public: - List() = default; - virtual ~List() = default; - - virtual bool isCollection() const override { return false; } - virtual bool isList() const override { return true; } -}; - -// ----------------------------------------- - // "string" class String final : public ASTNode { public: @@ -202,13 +202,13 @@ template<> inline bool ASTNode::fastIs() const { return isCollection(); } template<> -inline bool ASTNode::fastIs() const { return isVector(); } +inline bool ASTNode::fastIs() const { return isList(); } template<> -inline bool ASTNode::fastIs() const { return isHashMap(); } +inline bool ASTNode::fastIs() const { return isVector(); } template<> -inline bool ASTNode::fastIs() const { return isList(); } +inline bool ASTNode::fastIs() const { return isHashMap(); } template<> inline bool ASTNode::fastIs() const { return isString(); } diff --git a/src/lexer.cpp b/src/lexer.cpp index 7bdec87..71e970b 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -38,6 +38,12 @@ void Lexer::tokenize() case '~': // ~@ or ~ consumeSpliceUnquoteOrUnquote(); break; + case '(': + m_tokens.push_back({ Token::Type::ParenOpen, m_line, m_column, "(" }); + break; + case ')': + m_tokens.push_back({ Token::Type::ParenClose, m_line, m_column, ")" }); + break; case '[': m_tokens.push_back({ Token::Type::BracketOpen, m_line, m_column, "[" }); break; @@ -50,12 +56,6 @@ void Lexer::tokenize() case '}': m_tokens.push_back({ Token::Type::BraceClose, m_line, m_column, "}" }); break; - case '(': - m_tokens.push_back({ Token::Type::ParenOpen, m_line, m_column, "(" }); - break; - case ')': - m_tokens.push_back({ Token::Type::ParenClose, m_line, m_column, ")" }); - break; case '\'': m_tokens.push_back({ Token::Type::Quote, m_line, m_column, "'" }); break; diff --git a/src/lexer.h b/src/lexer.h index 68558e4..175ab7d 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -20,12 +20,12 @@ struct Token { enum class Type : uint8_t { None, Special, // ~@ + ParenOpen, // ( + ParenClose, // ) BracketOpen, // [ BracketClose, // ] BraceOpen, // { BraceClose, // } - ParenOpen, // ( - ParenClose, // ) Quote, // ' Backtick, // ` Tilde, // ~ diff --git a/src/printer.cpp b/src/printer.cpp index 819e4ca..8e17355 100644 --- a/src/printer.cpp +++ b/src/printer.cpp @@ -48,7 +48,19 @@ void Printer::dumpImpl(ASTNode* node) } }; - if (is(node)) { + if (is(node)) { + printSpacing(); + print("("); + m_firstNode = false; + m_previousNodeIsList = true; + List* list = static_cast(node); + for (size_t i = 0; i < list->nodes().size(); ++i) { + dumpImpl(list->nodes()[i]); + m_previousNodeIsList = false; + } + print(")"); + } + else if (is(node)) { printSpacing(); print("["); m_firstNode = false; @@ -72,18 +84,6 @@ void Printer::dumpImpl(ASTNode* node) } print("}}"); } - else if (is(node)) { - printSpacing(); - print("("); - m_firstNode = false; - m_previousNodeIsList = true; - List* list = static_cast(node); - for (size_t i = 0; i < list->nodes().size(); ++i) { - dumpImpl(list->nodes()[i]); - m_previousNodeIsList = false; - } - print(")"); - } else if (is(node)) { printSpacing(); print("{}", static_cast(node)->data()); diff --git a/src/reader.cpp b/src/reader.cpp index a6cccec..d896a5f 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -155,6 +155,23 @@ ASTNode* Reader::readSpliceUnquote() return list; } +ASTNode* Reader::readList() +{ + ignore(); // ( + + List* list = new List(); + while (!isEOF() && peek().type != Token::Type::ParenClose) { + list->addNode(readImpl()); + } + + if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // ) + m_error_character = ')'; + m_is_unbalanced = true; + } + + return list; +} + ASTNode* Reader::readVector() { ignore(); // [ @@ -189,23 +206,6 @@ ASTNode* Reader::readHashMap() return vector; } -ASTNode* Reader::readList() -{ - ignore(); // ( - - List* list = new List(); - while (!isEOF() && peek().type != Token::Type::ParenClose) { - list->addNode(readImpl()); - } - - if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // ) - m_error_character = ')'; - m_is_unbalanced = true; - } - - return list; -} - ASTNode* Reader::readQuote() { ignore(); // ' diff --git a/src/reader.h b/src/reader.h index 833e1ec..1f30ec8 100644 --- a/src/reader.h +++ b/src/reader.h @@ -36,9 +36,9 @@ private: ASTNode* readImpl(); ASTNode* readSpliceUnquote(); // ~@ + ASTNode* readList(); // () ASTNode* readVector(); // [] ASTNode* readHashMap(); // {} - ASTNode* readList(); // () ASTNode* readQuote(); // ' ASTNode* readQuasiQuote(); // ` ASTNode* readUnquote(); // ~