added the parsing of expression statements

This commit is contained in:
Karma Riuk
2025-07-08 09:51:15 +02:00
parent 08aacf0416
commit d13f9bf9f8
4 changed files with 47 additions and 8 deletions

View File

@@ -0,0 +1,16 @@
#include "expression.hpp"
namespace ast {
expression_stmt::expression_stmt(token::token token)
: token(std::move(token)),
expression(nullptr) {}
std::string expression_stmt::token_literal() const {
return token.literal;
}
expression_stmt::~expression_stmt() {
delete expression;
}
} // namespace ast

View File

@@ -0,0 +1,15 @@
#include "ast/ast.hpp"
#include "token/token.hpp"
namespace ast {
struct expression_stmt : statement {
expression_stmt(token::token token);
token::token token;
ast::expression* expression;
std::string token_literal() const override;
~expression_stmt();
};
} // namespace ast

View File

@@ -42,14 +42,6 @@ namespace parser {
} }
} }
bool parser::expect_next(token::type t) {
if (next.type == t) {
next_token();
return true;
}
next_error(t);
return false;
}
ast::expression* parser::parse_expression() { ast::expression* parser::parse_expression() {
// TODO: we are currently skipping expressions until we encounter a // TODO: we are currently skipping expressions until we encounter a
// semicolon // semicolon
@@ -85,7 +77,21 @@ namespace parser {
return stmt; return stmt;
} }
ast::expression_stmt* parser::parse_expression_stmt() {
ast::expression_stmt* stmt = new ast::expression_stmt(current);
stmt->expression = parse_expression();
return stmt; return stmt;
};
bool parser::expect_next(token::type t) {
if (next.type == t) {
next_token();
return true;
}
next_error(t);
return false;
} }
void parser::next_error(token::type t) { void parser::next_error(token::type t) {

View File

@@ -2,6 +2,7 @@
#include "ast/ast.hpp" #include "ast/ast.hpp"
#include "ast/errors/error.hpp" #include "ast/errors/error.hpp"
#include "ast/statements/expression.hpp"
#include "ast/statements/let.hpp" #include "ast/statements/let.hpp"
#include "ast/statements/return.hpp" #include "ast/statements/return.hpp"
#include "lexer/lexer.hpp" #include "lexer/lexer.hpp"
@@ -24,6 +25,7 @@ namespace parser {
ast::expression* parse_expression(); ast::expression* parse_expression();
ast::let_stmt* parse_let(); ast::let_stmt* parse_let();
ast::return_stmt* parse_return(); ast::return_stmt* parse_return();
ast::expression_stmt* parse_expression_stmt();
bool expect_next(token::type); bool expect_next(token::type);
void next_error(token::type); void next_error(token::type);
}; };