Reader+Printer: Reorder tokens
This commit is contained in:
@@ -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 {
|
class Vector final : public Collection {
|
||||||
public:
|
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"
|
// "string"
|
||||||
class String final : public ASTNode {
|
class String final : public ASTNode {
|
||||||
public:
|
public:
|
||||||
@@ -201,15 +201,15 @@ private:
|
|||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
|
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline bool ASTNode::fastIs<List>() const { return isList(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
|
inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
|
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
|
||||||
|
|
||||||
template<>
|
|
||||||
inline bool ASTNode::fastIs<List>() const { return isList(); }
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fastIs<String>() const { return isString(); }
|
inline bool ASTNode::fastIs<String>() const { return isString(); }
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -38,6 +38,12 @@ void Lexer::tokenize()
|
|||||||
case '~': // ~@ or ~
|
case '~': // ~@ or ~
|
||||||
consumeSpliceUnquoteOrUnquote();
|
consumeSpliceUnquoteOrUnquote();
|
||||||
break;
|
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 '[':
|
case '[':
|
||||||
m_tokens.push_back({ Token::Type::BracketOpen, m_line, m_column, "[" });
|
m_tokens.push_back({ Token::Type::BracketOpen, m_line, m_column, "[" });
|
||||||
break;
|
break;
|
||||||
@@ -50,12 +56,6 @@ void Lexer::tokenize()
|
|||||||
case '}':
|
case '}':
|
||||||
m_tokens.push_back({ Token::Type::BraceClose, m_line, m_column, "}" });
|
m_tokens.push_back({ Token::Type::BraceClose, m_line, m_column, "}" });
|
||||||
break;
|
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 '\'':
|
case '\'':
|
||||||
m_tokens.push_back({ Token::Type::Quote, m_line, m_column, "'" });
|
m_tokens.push_back({ Token::Type::Quote, m_line, m_column, "'" });
|
||||||
break;
|
break;
|
||||||
|
|||||||
+2
-2
@@ -20,12 +20,12 @@ struct Token {
|
|||||||
enum class Type : uint8_t {
|
enum class Type : uint8_t {
|
||||||
None,
|
None,
|
||||||
Special, // ~@
|
Special, // ~@
|
||||||
|
ParenOpen, // (
|
||||||
|
ParenClose, // )
|
||||||
BracketOpen, // [
|
BracketOpen, // [
|
||||||
BracketClose, // ]
|
BracketClose, // ]
|
||||||
BraceOpen, // {
|
BraceOpen, // {
|
||||||
BraceClose, // }
|
BraceClose, // }
|
||||||
ParenOpen, // (
|
|
||||||
ParenClose, // )
|
|
||||||
Quote, // '
|
Quote, // '
|
||||||
Backtick, // `
|
Backtick, // `
|
||||||
Tilde, // ~
|
Tilde, // ~
|
||||||
|
|||||||
+13
-13
@@ -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();
|
printSpacing();
|
||||||
print("[");
|
print("[");
|
||||||
m_firstNode = false;
|
m_firstNode = false;
|
||||||
@@ -72,18 +84,6 @@ void Printer::dumpImpl(ASTNode* node)
|
|||||||
}
|
}
|
||||||
print("}}");
|
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)) {
|
else if (is<String>(node)) {
|
||||||
printSpacing();
|
printSpacing();
|
||||||
print("{}", static_cast<String*>(node)->data());
|
print("{}", static_cast<String*>(node)->data());
|
||||||
|
|||||||
+17
-17
@@ -155,6 +155,23 @@ ASTNode* Reader::readSpliceUnquote()
|
|||||||
return list;
|
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()
|
ASTNode* Reader::readVector()
|
||||||
{
|
{
|
||||||
ignore(); // [
|
ignore(); // [
|
||||||
@@ -189,23 +206,6 @@ ASTNode* Reader::readHashMap()
|
|||||||
return vector;
|
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()
|
ASTNode* Reader::readQuote()
|
||||||
{
|
{
|
||||||
ignore(); // '
|
ignore(); // '
|
||||||
|
|||||||
+1
-1
@@ -36,9 +36,9 @@ private:
|
|||||||
|
|
||||||
ASTNode* readImpl();
|
ASTNode* readImpl();
|
||||||
ASTNode* readSpliceUnquote(); // ~@
|
ASTNode* readSpliceUnquote(); // ~@
|
||||||
|
ASTNode* readList(); // ()
|
||||||
ASTNode* readVector(); // []
|
ASTNode* readVector(); // []
|
||||||
ASTNode* readHashMap(); // {}
|
ASTNode* readHashMap(); // {}
|
||||||
ASTNode* readList(); // ()
|
|
||||||
ASTNode* readQuote(); // '
|
ASTNode* readQuote(); // '
|
||||||
ASTNode* readQuasiQuote(); // `
|
ASTNode* readQuasiQuote(); // `
|
||||||
ASTNode* readUnquote(); // ~
|
ASTNode* readUnquote(); // ~
|
||||||
|
|||||||
Reference in New Issue
Block a user