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> #include <sstream>
namespace AST { 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::string IntLiteralExpression::formatted() {
std::stringstream out{ "" }; std::stringstream out{ "" };
out << this->m_value; out << this->m_value;

View File

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

View File

@ -45,13 +45,13 @@ namespace AST {
codegen::StackValue BinaryOperationExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) { codegen::StackValue BinaryOperationExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
auto lvalued = scope.with_lvalue(); 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); auto rhs = this->m_rhs->codegen(builder, scope);
switch (this->m_binop) { switch (this->m_binop) {
case BinOp::Assignment: case types::BinOp::Assignment:
builder.builder->CreateStore(rhs.value, lhs.value, false); builder.builder->CreateStore(rhs.value, lhs.value, false);
return rhs; return rhs;
case BinOp::Add: case types::BinOp::Add:
return codegen::StackValue{ return codegen::StackValue{
lhs.ty->add(builder, lhs.value, rhs.value), lhs.ty->add(builder, lhs.value, rhs.value),
lhs.ty 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 }; token::TokenStream inner{ stream };
try { try {
auto token = inner.next(); auto token = inner.next();
@ -53,11 +53,11 @@ namespace parsing {
} }
if (token.content == "=") { if (token.content == "=") {
stream.m_position = inner.m_position; stream.m_position = inner.m_position;
return new AST::BinOp{ AST::BinOp::Assignment }; return new types::BinOp{ types::BinOp::Assignment };
} }
if (token.content == "+") { if (token.content == "+") {
stream.m_position = inner.m_position; 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"); throw std::runtime_error("Expected binop");
@ -75,8 +75,8 @@ namespace parsing {
auto binop = binop_res.unwrap(); auto binop = binop_res.unwrap();
auto rhs = parse_primary_expression(stream).unwrap(); auto rhs = parse_primary_expression(stream).unwrap();
if (AST::operator_precedence(binop) > prev_precedence) { if (types::operator_precedence(binop) > prev_precedence) {
rhs = parse_rhs(stream, std::move(rhs), AST::operator_precedence(binop)); rhs = parse_rhs(stream, std::move(rhs), types::operator_precedence(binop));
} }
auto binop_expr = new AST::BinaryOperationExpression{ std::move(lhs), binop, std::move(rhs) }; auto binop_expr = new AST::BinaryOperationExpression{ std::move(lhs), binop, std::move(rhs) };

View File

@ -3,6 +3,28 @@
#include <sstream> #include <sstream>
namespace types { 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() { std::string FundamentalType::formatted() {
switch (this->m_ty) { switch (this->m_ty) {
case FundamentalTypeKind::Int: case FundamentalTypeKind::Int:

View File

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