Move binop to types

This commit is contained in:
Sofia 2026-04-10 16:58:27 +03:00
parent dcb6e60a3b
commit d69e2c0929
6 changed files with 40 additions and 40 deletions

View File

@ -3,28 +3,6 @@
#include <sstream>
namespace AST {
int operator_precedence(BinOp& op) {
switch (op) {
case BinOp::Assignment:
return 1000;
case BinOp::Add:
return 10;
default:
return 1000;
}
}
std::string format_operator(BinOp& op) {
switch (op) {
case BinOp::Assignment:
return "=";
case BinOp::Add:
return "+";
default:
return "??";
}
}
std::string IntLiteralExpression::formatted() {
std::stringstream out{ "" };
out << this->m_value;

View File

@ -9,14 +9,6 @@
#include <memory>
namespace AST {
enum class BinOp {
Assignment,
Add,
};
int operator_precedence(BinOp& op);
std::string format_operator(BinOp& op);
class Node {
public:
virtual std::string formatted() = 0;
@ -56,10 +48,10 @@ namespace AST {
class BinaryOperationExpression : public Expression {
private:
std::unique_ptr<Expression> m_lhs;
BinOp m_binop;
types::BinOp m_binop;
std::unique_ptr<Expression> m_rhs;
public:
BinaryOperationExpression(std::unique_ptr<Expression> lhs, BinOp op, std::unique_ptr<Expression> rhs)
BinaryOperationExpression(std::unique_ptr<Expression> lhs, types::BinOp op, std::unique_ptr<Expression> rhs)
: m_lhs{ std::move(lhs) }, m_binop{ op }, m_rhs{ std::move(rhs) } {
}
virtual ~BinaryOperationExpression() override = default;

View File

@ -45,13 +45,13 @@ namespace AST {
codegen::StackValue BinaryOperationExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
auto lvalued = scope.with_lvalue();
auto lhs = this->m_lhs->codegen(builder, this->m_binop == BinOp::Assignment ? lvalued : scope);
auto lhs = this->m_lhs->codegen(builder, this->m_binop == types::BinOp::Assignment ? lvalued : scope);
auto rhs = this->m_rhs->codegen(builder, scope);
switch (this->m_binop) {
case BinOp::Assignment:
case types::BinOp::Assignment:
builder.builder->CreateStore(rhs.value, lhs.value, false);
return rhs;
case BinOp::Add:
case types::BinOp::Add:
return codegen::StackValue{
lhs.ty->add(builder, lhs.value, rhs.value),
lhs.ty

View File

@ -44,7 +44,7 @@ namespace parsing {
}
}
Result<AST::BinOp, std::string> parse_binop(token::TokenStream& stream) {
Result<types::BinOp, std::string> parse_binop(token::TokenStream& stream) {
token::TokenStream inner{ stream };
try {
auto token = inner.next();
@ -53,11 +53,11 @@ namespace parsing {
}
if (token.content == "=") {
stream.m_position = inner.m_position;
return new AST::BinOp{ AST::BinOp::Assignment };
return new types::BinOp{ types::BinOp::Assignment };
}
if (token.content == "+") {
stream.m_position = inner.m_position;
return new AST::BinOp{ AST::BinOp::Add };
return new types::BinOp{ types::BinOp::Add };
}
throw std::runtime_error("Expected binop");
@ -75,8 +75,8 @@ namespace parsing {
auto binop = binop_res.unwrap();
auto rhs = parse_primary_expression(stream).unwrap();
if (AST::operator_precedence(binop) > prev_precedence) {
rhs = parse_rhs(stream, std::move(rhs), AST::operator_precedence(binop));
if (types::operator_precedence(binop) > prev_precedence) {
rhs = parse_rhs(stream, std::move(rhs), types::operator_precedence(binop));
}
auto binop_expr = new AST::BinaryOperationExpression{ std::move(lhs), binop, std::move(rhs) };

View File

@ -3,6 +3,28 @@
#include <sstream>
namespace types {
int operator_precedence(BinOp& op) {
switch (op) {
case BinOp::Assignment:
return 1000;
case BinOp::Add:
return 10;
default:
return 1000;
}
}
std::string format_operator(BinOp& op) {
switch (op) {
case BinOp::Assignment:
return "=";
case BinOp::Add:
return "+";
default:
return "??";
}
}
std::string FundamentalType::formatted() {
switch (this->m_ty) {
case FundamentalTypeKind::Int:

View File

@ -7,6 +7,14 @@
#include <memory>
namespace types {
enum class BinOp {
Assignment,
Add,
};
int operator_precedence(BinOp& op);
std::string format_operator(BinOp& op);
enum FundamentalTypeKind {
Int,
};