Browse Source

Reader: Add support for more Tokens

master
Riyyi 2 years ago
parent
commit
68303dfe7b
  1. 2
      src/lexer.cpp
  2. 80
      src/reader.cpp
  3. 4
      src/reader.h

2
src/lexer.cpp

@ -105,8 +105,8 @@ bool Lexer::consumeSpliceUnquoteOrUnquote()
{
size_t column = m_column;
if (peek(1) == '@') {
ignore(); // ~
if (peek() == '@') {
m_tokens.push_back({ Token::Type::Special, m_line, column, "~@" });
}
else {

80
src/reader.cpp

@ -73,6 +73,9 @@ ASTNode* Reader::readImpl()
}
switch (peek().type) {
case Token::Type::Special: // ~@
return readSpliceUnquote();
break;
case Token::Type::ParenOpen: // (
return readList();
break;
@ -81,6 +84,15 @@ ASTNode* Reader::readImpl()
m_error_character = ')';
return nullptr;
break;
case Token::Type::Quote: // '
return readQuote();
break;
case Token::Type::Backtick: // `
return readQuasiQuote();
break;
case Token::Type::Tilde: // ~
return readUnquote();
break;
case Token::Type::String:
return readString();
break;
@ -88,10 +100,28 @@ ASTNode* Reader::readImpl()
return readValue();
break;
default:
break;
};
// Unimplemented token
VERIFY_NOT_REACHED();
return nullptr;
};
}
ASTNode* Reader::readSpliceUnquote()
{
ignore(); // ~@
if (isEOF()) {
Error::the().addError("expected form, got EOF");
return nullptr;
}
List* list = new List();
list->addNode(new Symbol("splice-unquote"));
list->addNode(readImpl());
return list;
}
ASTNode* Reader::readList()
@ -111,6 +141,54 @@ ASTNode* Reader::readList()
return list;
}
ASTNode* Reader::readQuote()
{
ignore(); // '
if (isEOF()) {
Error::the().addError("expected form, got EOF");
return nullptr;
}
List* list = new List();
list->addNode(new Symbol("quote"));
list->addNode(readImpl());
return list;
}
ASTNode* Reader::readQuasiQuote()
{
ignore(); // `
if (isEOF()) {
Error::the().addError("expected form, got EOF");
return nullptr;
}
List* list = new List();
list->addNode(new Symbol("quasiquote"));
list->addNode(readImpl());
return list;
}
ASTNode* Reader::readUnquote()
{
ignore(); // ~
if (isEOF()) {
Error::the().addError("expected form, got EOF");
return nullptr;
}
List* list = new List();
list->addNode(new Symbol("unquote"));
list->addNode(readImpl());
return list;
}
ASTNode* Reader::readString()
{
std::string symbol = consume().symbol;

4
src/reader.h

@ -34,7 +34,11 @@ private:
void ignore();
ASTNode* readImpl();
ASTNode* readSpliceUnquote();
ASTNode* readList();
ASTNode* readQuote();
ASTNode* readQuasiQuote();
ASTNode* readUnquote();
ASTNode* readString();
ASTNode* readValue();

Loading…
Cancel
Save