Use binops for codegen as well
This commit is contained in:
parent
1037024730
commit
8314fe2b61
@ -11,7 +11,7 @@
|
||||
|
||||
namespace codegen {
|
||||
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:
|
||||
builder.builder->CreateStore(rhs.value, lhs.value, false);
|
||||
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:
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
#include "builder.h"
|
||||
#include "types.h"
|
||||
#include "binops.h"
|
||||
#include "tokens.h"
|
||||
|
||||
namespace codegen {
|
||||
@ -17,6 +18,8 @@ namespace codegen {
|
||||
};
|
||||
|
||||
struct Scope {
|
||||
std::vector<types::BinopDefinition>& binops;
|
||||
|
||||
std::map<std::string, StackValue> values;
|
||||
bool is_lvalue;
|
||||
|
||||
|
||||
@ -116,7 +116,11 @@ std::optional<CompileOutput> compile(std::string_view in_filename) {
|
||||
return {};
|
||||
}
|
||||
|
||||
codegen::Scope cg_scope{};
|
||||
codegen::Scope cg_scope{
|
||||
.binops = typecheck_state.binops,
|
||||
.values = {},
|
||||
.is_lvalue = false,
|
||||
};
|
||||
|
||||
// Compile parsed output
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user