added prefix parsing for boolean literals

This commit is contained in:
Karma Riuk
2025-07-11 21:00:11 +02:00
parent 4da5f32aea
commit aaec4cefcf
3 changed files with 21 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
#include "parser.hpp"
#include "ast/errors/error.hpp"
#include "ast/expressions/boolean.hpp"
#include "ast/expressions/identifier.hpp"
#include "ast/expressions/infix.hpp"
#include "ast/expressions/integer.hpp"
@@ -39,6 +40,16 @@ namespace parser {
std::bind(&parser::parse_prefix_expr, this)
);
register_prefix(
token::type::TRUE,
std::bind(&parser::parse_boolean, this)
);
register_prefix(
token::type::FALSE,
std::bind(&parser::parse_boolean, this)
);
using namespace std::placeholders;
register_infix(
token::type::PLUS,
@@ -223,6 +234,14 @@ namespace parser {
return new ast::integer_literal(current, std::stoi(current.literal));
};
ast::expression* parser::parse_boolean() {
// TRACE_FUNCTION;
return new ast::boolean_literal(
current,
current.type == token::type::TRUE
);
};
ast::expression* parser::parse_prefix_expr() {
// TRACE_FUNCTION;
ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal);

View File

@@ -49,6 +49,7 @@ namespace parser {
ast::expression* parse_identifier();
ast::expression* parse_integer();
ast::expression* parse_boolean();
ast::expression* parse_prefix_expr();
ast::expression* parse_infix_expr(ast::expression*);

View File

@@ -58,7 +58,7 @@ void test_boolean_literal(ast::expression* expr, bool value) {
REQUIRE(bool_lit->value == value);
std::ostringstream oss;
oss << value;
oss << (value ? "true" : "false");
REQUIRE(bool_lit->token_literal() == oss.str());
}