added prefix parsing for boolean literals
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
|
||||||
#include "ast/errors/error.hpp"
|
#include "ast/errors/error.hpp"
|
||||||
|
#include "ast/expressions/boolean.hpp"
|
||||||
#include "ast/expressions/identifier.hpp"
|
#include "ast/expressions/identifier.hpp"
|
||||||
#include "ast/expressions/infix.hpp"
|
#include "ast/expressions/infix.hpp"
|
||||||
#include "ast/expressions/integer.hpp"
|
#include "ast/expressions/integer.hpp"
|
||||||
@@ -39,6 +40,16 @@ namespace parser {
|
|||||||
std::bind(&parser::parse_prefix_expr, this)
|
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;
|
using namespace std::placeholders;
|
||||||
register_infix(
|
register_infix(
|
||||||
token::type::PLUS,
|
token::type::PLUS,
|
||||||
@@ -223,6 +234,14 @@ namespace parser {
|
|||||||
return new ast::integer_literal(current, std::stoi(current.literal));
|
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() {
|
ast::expression* parser::parse_prefix_expr() {
|
||||||
// TRACE_FUNCTION;
|
// TRACE_FUNCTION;
|
||||||
ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal);
|
ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal);
|
||||||
|
@@ -49,6 +49,7 @@ namespace parser {
|
|||||||
|
|
||||||
ast::expression* parse_identifier();
|
ast::expression* parse_identifier();
|
||||||
ast::expression* parse_integer();
|
ast::expression* parse_integer();
|
||||||
|
ast::expression* parse_boolean();
|
||||||
ast::expression* parse_prefix_expr();
|
ast::expression* parse_prefix_expr();
|
||||||
|
|
||||||
ast::expression* parse_infix_expr(ast::expression*);
|
ast::expression* parse_infix_expr(ast::expression*);
|
||||||
|
@@ -58,7 +58,7 @@ void test_boolean_literal(ast::expression* expr, bool value) {
|
|||||||
|
|
||||||
REQUIRE(bool_lit->value == value);
|
REQUIRE(bool_lit->value == value);
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << value;
|
oss << (value ? "true" : "false");
|
||||||
REQUIRE(bool_lit->token_literal() == oss.str());
|
REQUIRE(bool_lit->token_literal() == oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user