added the parsing of expression statements
This commit is contained in:
16
src/ast/statements/expression.cpp
Normal file
16
src/ast/statements/expression.cpp
Normal 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
|
15
src/ast/statements/expression.hpp
Normal file
15
src/ast/statements/expression.hpp
Normal 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
|
@@ -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) {
|
||||||
|
@@ -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);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user