Reader+Printer: Reorder tokens

This commit is contained in:
Riyyi
2023-03-24 21:51:11 +01:00
parent da0b0a91a6
commit 5c5a766b7e
6 changed files with 54 additions and 54 deletions
+15 -15
View File
@@ -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:
@@ -201,15 +201,15 @@ private:
template<>
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
template<>
inline bool ASTNode::fastIs<List>() const { return isList(); }
template<>
inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
template<>
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
template<>
inline bool ASTNode::fastIs<List>() const { return isList(); }
template<>
inline bool ASTNode::fastIs<String>() const { return isString(); }
+6 -6
View File
@@ -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;
+2 -2
View File
@@ -20,12 +20,12 @@ struct Token {
enum class Type : uint8_t {
None,
Special, // ~@
ParenOpen, // (
ParenClose, // )
BracketOpen, // [
BracketClose, // ]
BraceOpen, // {
BraceClose, // }
ParenOpen, // (
ParenClose, // )
Quote, // '
Backtick, // `
Tilde, // ~
+13 -13
View File
@@ -48,7 +48,19 @@ void Printer::dumpImpl(ASTNode* node)
}
};
if (is<Vector>(node)) {
if (is<List>(node)) {
printSpacing();
print("(");
m_firstNode = false;
m_previousNodeIsList = true;
List* list = static_cast<List*>(node);
for (size_t i = 0; i < list->nodes().size(); ++i) {
dumpImpl(list->nodes()[i]);
m_previousNodeIsList = false;
}
print(")");
}
else if (is<Vector>(node)) {
printSpacing();
print("[");
m_firstNode = false;
@@ -72,18 +84,6 @@ void Printer::dumpImpl(ASTNode* node)
}
print("}}");
}
else if (is<List>(node)) {
printSpacing();
print("(");
m_firstNode = false;
m_previousNodeIsList = true;
List* list = static_cast<List*>(node);
for (size_t i = 0; i < list->nodes().size(); ++i) {
dumpImpl(list->nodes()[i]);
m_previousNodeIsList = false;
}
print(")");
}
else if (is<String>(node)) {
printSpacing();
print("{}", static_cast<String*>(node)->data());
+17 -17
View File
@@ -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(); // '
+1 -1
View File
@@ -36,9 +36,9 @@ private:
ASTNode* readImpl();
ASTNode* readSpliceUnquote(); // ~@
ASTNode* readList(); // ()
ASTNode* readVector(); // []
ASTNode* readHashMap(); // {}
ASTNode* readList(); // ()
ASTNode* readQuote(); // '
ASTNode* readQuasiQuote(); // `
ASTNode* readUnquote(); // ~