Use binops for codegen as well

This commit is contained in:
Sofia 2026-04-13 18:24:07 +03:00
parent 1037024730
commit 8314fe2b61
3 changed files with 20 additions and 22 deletions

View File

@ -11,7 +11,7 @@
namespace codegen { namespace codegen {
Scope Scope::with_lvalue() { Scope Scope::with_lvalue() {
return Scope{ this->values, true }; return Scope{ this->binops, this->values, true };
} }
} }
@ -67,27 +67,18 @@ namespace AST {
case types::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 types::BinOp::Add:
return codegen::StackValue{
lhs.ty->add(builder, lhs.value, rhs.value),
lhs.ty
};
case types::BinOp::Sub:
return codegen::StackValue{
lhs.ty->sub(builder, lhs.value, rhs.value),
lhs.ty
};
case types::BinOp::LessThan:
return codegen::StackValue{
lhs.ty->lt(builder, lhs.value, rhs.value),
std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool),
};
case types::BinOp::GreaterThan:
return codegen::StackValue{
lhs.ty->gt(builder, lhs.value, rhs.value),
std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Bool),
};
default: default:
auto binop = types::find_binop(
scope.binops,
lhs.ty,
this->m_binop,
rhs.ty);
if (binop) {
return codegen::StackValue{
binop->codegen(builder, lhs.value, rhs.value),
binop->result
};
}
throw CompileError("invalid binop", this->m_meta); throw CompileError("invalid binop", this->m_meta);
} }
} }

View File

@ -8,6 +8,7 @@
#include "builder.h" #include "builder.h"
#include "types.h" #include "types.h"
#include "binops.h"
#include "tokens.h" #include "tokens.h"
namespace codegen { namespace codegen {
@ -17,6 +18,8 @@ namespace codegen {
}; };
struct Scope { struct Scope {
std::vector<types::BinopDefinition>& binops;
std::map<std::string, StackValue> values; std::map<std::string, StackValue> values;
bool is_lvalue; bool is_lvalue;

View File

@ -116,7 +116,11 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
return {}; return {};
} }
codegen::Scope cg_scope{}; codegen::Scope cg_scope{
.binops = typecheck_state.binops,
.values = {},
.is_lvalue = false,
};
// Compile parsed output // Compile parsed output
try { try {