diff --git a/src/codegen.cpp b/src/codegen.cpp index 7432de8..d1f1b77 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -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::FundamentalTypeKind::Bool), - }; - case types::BinOp::GreaterThan: - return codegen::StackValue{ - lhs.ty->gt(builder, lhs.value, rhs.value), - std::make_shared(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); } } diff --git a/src/codegen.h b/src/codegen.h index 84552f8..1bbdba2 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -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& binops; + std::map values; bool is_lvalue; diff --git a/src/main.cpp b/src/main.cpp index 3398e36..79eea6a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -116,7 +116,11 @@ std::optional 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 {