very basic parser of let statements
This commit is contained in:
78
src/parser/parser.cpp
Normal file
78
src/parser/parser.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "parser.hpp"
|
||||
|
||||
#include "token/type.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace parser {
|
||||
parser::parser(lexer::lexer& lexer)
|
||||
: lexer(lexer),
|
||||
current(token::type::ILLEGAL, ""),
|
||||
next(token::type::ILLEGAL, "") {
|
||||
next_token();
|
||||
next_token();
|
||||
}
|
||||
|
||||
void parser::next_token() {
|
||||
current = next;
|
||||
next = lexer.next_token();
|
||||
}
|
||||
|
||||
ast::program* parser::parse_program() {
|
||||
ast::program* p = new ast::program();
|
||||
|
||||
for (; current.type != token::type::END_OF_FILE; next_token()) {
|
||||
ast::statement* stmt = parse_statement();
|
||||
if (stmt != nullptr)
|
||||
p->statements.push_back(stmt);
|
||||
}
|
||||
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
ast::statement* parser::parse_statement() {
|
||||
switch (current.type) {
|
||||
case token::type::LET:
|
||||
return parse_let();
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool parser::expect_next(token::type t) {
|
||||
if (next.type == t) {
|
||||
next_token();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ast::let* parser::parse_let() {
|
||||
ast::let* stmt = new ast::let(current);
|
||||
|
||||
if (!expect_next(token::type::IDENTIFIER)) {
|
||||
delete stmt;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
stmt->name = new ast::identifier{current, current.literal};
|
||||
|
||||
if (!expect_next(token::type::ASSIGN)) {
|
||||
delete stmt;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// TODO: we are currently skipping expressions until we encounter a
|
||||
// semicolon
|
||||
for (; current.type != token::type::SEMICOLON; next_token()) {}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
void parser::next_error(token::type t) {
|
||||
std::stringstream ss;
|
||||
ss << "Expected next token to be " << t << " but instead got "
|
||||
<< next.type;
|
||||
errors.push_back(ss.str());
|
||||
}
|
||||
} // namespace parser
|
25
src/parser/parser.hpp
Normal file
25
src/parser/parser.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "ast/statements/let.hpp"
|
||||
#include "lexer/lexer.hpp"
|
||||
#include "token/token.hpp"
|
||||
|
||||
namespace parser {
|
||||
struct parser {
|
||||
parser(lexer::lexer& lexer);
|
||||
std::vector<std::string> errors;
|
||||
|
||||
ast::program* parse_program();
|
||||
|
||||
private:
|
||||
lexer::lexer& lexer;
|
||||
token::token current, next;
|
||||
|
||||
void next_token();
|
||||
ast::statement* parse_statement();
|
||||
ast::let* parse_let();
|
||||
bool expect_next(token::type);
|
||||
void next_error(token::type);
|
||||
};
|
||||
} // namespace parser
|
Reference in New Issue
Block a user