Add addition binop
This commit is contained in:
parent
43abaa4a46
commit
8e2bc3a7f7
11
src/ast.cpp
11
src/ast.cpp
@ -4,13 +4,22 @@
|
||||
|
||||
namespace AST {
|
||||
int operator_precedence(BinOp& op) {
|
||||
return 0;
|
||||
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 "??";
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
namespace AST {
|
||||
enum class BinOp {
|
||||
Assignment,
|
||||
Add,
|
||||
};
|
||||
|
||||
int operator_precedence(BinOp& op);
|
||||
|
||||
@ -38,17 +38,17 @@ namespace AST {
|
||||
|
||||
codegen::StackValue BinaryOperationExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
auto lvalued = scope.with_lvalue();
|
||||
auto lhs = this->m_lhs->codegen(builder, lvalued);
|
||||
auto lhs = this->m_lhs->codegen(builder, this->m_binop == BinOp::Assignment ? lvalued : scope);
|
||||
auto rhs = this->m_rhs->codegen(builder, scope);
|
||||
switch (this->m_binop) {
|
||||
case BinOp::Assignment:
|
||||
builder.builder->CreateStore(rhs.value, lhs.value, false);
|
||||
if (scope.is_lvalue) {
|
||||
return lhs;
|
||||
}
|
||||
else {
|
||||
return rhs;
|
||||
}
|
||||
case BinOp::Add:
|
||||
return codegen::StackValue{
|
||||
builder.builder->CreateAdd(lhs.value, rhs.value, "add"),
|
||||
lhs.ty
|
||||
};
|
||||
default:
|
||||
throw std::runtime_error("invalid binop");
|
||||
}
|
||||
|
||||
@ -54,6 +54,10 @@ namespace parsing {
|
||||
stream.m_position = inner.m_position;
|
||||
return new AST::BinOp{ AST::BinOp::Assignment };
|
||||
}
|
||||
if (token.content == "+") {
|
||||
stream.m_position = inner.m_position;
|
||||
return new AST::BinOp{ AST::BinOp::Add };
|
||||
}
|
||||
|
||||
throw std::runtime_error("Expected binop");
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user