Browse Source

Reader: Add support for more Tokens

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

4
src/lexer.cpp

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

84
src/reader.cpp

@ -73,6 +73,9 @@ ASTNode* Reader::readImpl()
} }
switch (peek().type) { switch (peek().type) {
case Token::Type::Special: // ~@
return readSpliceUnquote();
break;
case Token::Type::ParenOpen: // ( case Token::Type::ParenOpen: // (
return readList(); return readList();
break; break;
@ -81,6 +84,15 @@ ASTNode* Reader::readImpl()
m_error_character = ')'; m_error_character = ')';
return nullptr; return nullptr;
break; 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: case Token::Type::String:
return readString(); return readString();
break; break;
@ -88,10 +100,28 @@ ASTNode* Reader::readImpl()
return readValue(); return readValue();
break; break;
default: default:
// Unimplemented token break;
VERIFY_NOT_REACHED();
return nullptr;
}; };
// 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() ASTNode* Reader::readList()
@ -111,6 +141,54 @@ ASTNode* Reader::readList()
return list; 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() ASTNode* Reader::readString()
{ {
std::string symbol = consume().symbol; std::string symbol = consume().symbol;

4
src/reader.h

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

Loading…
Cancel
Save