Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added expect for improved readability #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ class Parser {

std::optional<NodeStmt*> parse_stmt() // NOLINT(*-no-recursion)
{
if (peek().has_value() && peek().value().type == TokenType::exit && peek(1).has_value()
&& peek(1).value().type == TokenType::open_paren) {
if (expect(TokenType::exit) && expect(TokenType::open_paren, 1)) {
consume();
consume();
auto stmt_exit = m_allocator.emplace<NodeStmtExit>();
Expand All @@ -265,9 +264,7 @@ class Parser {
stmt->var = stmt_exit;
return stmt;
}
if (peek().has_value() && peek().value().type == TokenType::let && peek(1).has_value()
&& peek(1).value().type == TokenType::ident && peek(2).has_value()
&& peek(2).value().type == TokenType::eq) {
if (expect(TokenType::let) && expect(TokenType::ident, 1) && expect(TokenType::eq, 2)) {
consume();
auto stmt_let = m_allocator.emplace<NodeStmtLet>();
stmt_let->ident = consume();
Expand All @@ -283,8 +280,7 @@ class Parser {
stmt->var = stmt_let;
return stmt;
}
if (peek().has_value() && peek().value().type == TokenType::ident && peek(1).has_value()
&& peek(1).value().type == TokenType::eq) {
if (expect(TokenType::ident) && expect(TokenType::eq, 1)) {
const auto assign = m_allocator.alloc<NodeStmtAssign>();
assign->ident = consume();
consume();
Expand All @@ -298,7 +294,7 @@ class Parser {
auto stmt = m_allocator.emplace<NodeStmt>(assign);
return stmt;
}
if (peek().has_value() && peek().value().type == TokenType::open_curly) {
if (expect(TokenType::open_curly)) {
if (auto scope = parse_scope()) {
auto stmt = m_allocator.emplace<NodeStmt>(scope.value());
return stmt;
Expand Down Expand Up @@ -356,9 +352,14 @@ class Parser {
return m_tokens.at(m_index++);
}

[[nodiscard]] bool expect(TokenType type, int offset = 0) const
{
return peek(offset).has_value() && peek(offset).value().type == type;
}

Token try_consume_err(const TokenType type)
{
if (peek().has_value() && peek().value().type == type) {
if (expect(type)) {
return consume();
}
error_expected(to_string(type));
Expand All @@ -367,7 +368,7 @@ class Parser {

std::optional<Token> try_consume(const TokenType type)
{
if (peek().has_value() && peek().value().type == type) {
if (expect(type)) {
return consume();
}
return {};
Expand All @@ -376,4 +377,4 @@ class Parser {
const std::vector<Token> m_tokens;
size_t m_index = 0;
ArenaAllocator m_allocator;
};
};
36 changes: 21 additions & 15 deletions src/tokenization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@ class Tokenizer {
tokens.push_back({ TokenType::int_lit, line_count, buf });
buf.clear();
}
else if (peek().value() == '/' && peek(1).has_value() && peek(1).value() == '/') {
else if (expect("//")) {
consume();
consume();
while (peek().has_value() && peek().value() != '\n') {
consume();
}
}
else if (peek().value() == '/' && peek(1).has_value() && peek(1).value() == '*') {
else if (expect("/*")) {
consume();
consume();
while (peek().has_value()) {
if (peek().value() == '*' && peek(1).has_value() && peek(1).value() == '/') {
if (expect("*/")) {
break;
}
consume();
Expand All @@ -158,47 +158,47 @@ class Tokenizer {
consume();
}
}
else if (peek().value() == '(') {
else if (expect("(")) {
consume();
tokens.push_back({ TokenType::open_paren, line_count });
}
else if (peek().value() == ')') {
else if (expect(")")) {
consume();
tokens.push_back({ TokenType::close_paren, line_count });
}
else if (peek().value() == ';') {
else if (expect(";")) {
consume();
tokens.push_back({ TokenType::semi, line_count });
}
else if (peek().value() == '=') {
else if (expect("=")) {
consume();
tokens.push_back({ TokenType::eq, line_count });
}
else if (peek().value() == '+') {
else if (expect("+")) {
consume();
tokens.push_back({ TokenType::plus, line_count });
}
else if (peek().value() == '*') {
else if (expect("*")) {
consume();
tokens.push_back({ TokenType::star, line_count });
}
else if (peek().value() == '-') {
else if (expect("-")) {
consume();
tokens.push_back({ TokenType::minus, line_count });
}
else if (peek().value() == '/') {
else if (expect("/")) {
consume();
tokens.push_back({ TokenType::fslash, line_count });
}
else if (peek().value() == '{') {
else if (expect("{")) {
consume();
tokens.push_back({ TokenType::open_curly, line_count });
}
else if (peek().value() == '}') {
else if (expect("}")) {
consume();
tokens.push_back({ TokenType::close_curly, line_count });
}
else if (peek().value() == '\n') {
else if (expect("\n")) {
consume();
line_count++;
}
Expand Down Expand Up @@ -228,6 +228,12 @@ class Tokenizer {
return m_src.at(m_index++);
}

bool expect(const std::string& next)
{
return m_index + next.length() < m_src.length() //
&& m_src.substr(m_index, next.length()) == next;
}

const std::string m_src;
size_t m_index = 0;
};
};
3 changes: 2 additions & 1 deletion test.hy
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ if (0) {
} elif (0) {
x = 2;
} else {
exit(3);
x = 3;
}

exit(x);

/*
exit(4);
*/
*/