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 {
|
namespace AST {
|
||||||
int operator_precedence(BinOp& op) {
|
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) {
|
std::string format_operator(BinOp& op) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case BinOp::Assignment:
|
case BinOp::Assignment:
|
||||||
return "=";
|
return "=";
|
||||||
|
case BinOp::Add:
|
||||||
|
return "+";
|
||||||
default:
|
default:
|
||||||
return "??";
|
return "??";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
namespace AST {
|
namespace AST {
|
||||||
enum class BinOp {
|
enum class BinOp {
|
||||||
Assignment,
|
Assignment,
|
||||||
|
Add,
|
||||||
};
|
};
|
||||||
|
|
||||||
int operator_precedence(BinOp& op);
|
int operator_precedence(BinOp& op);
|
||||||
|
|||||||
@ -38,17 +38,17 @@ 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, lvalued);
|
auto lhs = this->m_lhs->codegen(builder, this->m_binop == 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 BinOp::Assignment:
|
||||||
builder.builder->CreateStore(rhs.value, lhs.value, false);
|
builder.builder->CreateStore(rhs.value, lhs.value, false);
|
||||||
if (scope.is_lvalue) {
|
|
||||||
return lhs;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return rhs;
|
return rhs;
|
||||||
}
|
case BinOp::Add:
|
||||||
|
return codegen::StackValue{
|
||||||
|
builder.builder->CreateAdd(lhs.value, rhs.value, "add"),
|
||||||
|
lhs.ty
|
||||||
|
};
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("invalid binop");
|
throw std::runtime_error("invalid binop");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,6 +54,10 @@ namespace parsing {
|
|||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
return new AST::BinOp{ AST::BinOp::Assignment };
|
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");
|
throw std::runtime_error("Expected binop");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user