can now parse prefix expressions!

This commit is contained in:
Karma Riuk
2025-07-09 12:02:01 +02:00
parent c7a30a0028
commit 7f1cc6f45e
6 changed files with 118 additions and 52 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include "token/token.hpp"
#include "token/type.hpp"
namespace ast::error {
@@ -12,6 +13,14 @@ namespace ast::error {
explicit parser_error(const std::string& message): error(message) {}
};
struct unkown_prefix : parser_error {
token::token prefix;
explicit unkown_prefix(token::token prefix, const std::string& message)
: parser_error(message),
prefix(prefix) {}
};
struct expected_next : parser_error {
token::type expected_type;

View File

@@ -0,0 +1,19 @@
#include "prefix.hpp"
#include "token/token.hpp"
namespace ast {
prefix_expr::prefix_expr(token::token token, std::string op)
: token(std::move(token)),
op(op),
right(nullptr) {}
std::string prefix_expr::token_literal() const {
return token.literal;
}
std::string prefix_expr::str() const {
return token.literal + right->str();
}
} // namespace ast

View File

@@ -0,0 +1,16 @@
#pragma once
#include "ast/ast.hpp"
#include "token/token.hpp"
namespace ast {
struct prefix_expr : expression {
prefix_expr(token::token token, std::string op);
token::token token;
std::string op;
expression* right;
std::string token_literal() const override;
std::string str() const override;
};
} // namespace ast