Browse Source

Reader+Printer: Reorder tokens

master
Riyyi 2 years ago
parent
commit
5c5a766b7e
  1. 30
      src/ast.h
  2. 12
      src/lexer.cpp
  3. 4
      src/lexer.h
  4. 26
      src/printer.cpp
  5. 34
      src/reader.cpp
  6. 2
      src/reader.h

30
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 { 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:
@ -202,13 +202,13 @@ template<>
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); } inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
template<> template<>
inline bool ASTNode::fastIs<Vector>() const { return isVector(); } inline bool ASTNode::fastIs<List>() const { return isList(); }
template<> template<>
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); } inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
template<> template<>
inline bool ASTNode::fastIs<List>() const { return isList(); } inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
template<> template<>
inline bool ASTNode::fastIs<String>() const { return isString(); } inline bool ASTNode::fastIs<String>() const { return isString(); }

12
src/lexer.cpp

@ -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;

4
src/lexer.h

@ -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, // ~

26
src/printer.cpp

@ -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());

34
src/reader.cpp

@ -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(); // '

2
src/reader.h

@ -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(); // ~

Loading…
Cancel
Save