Move binop to types
This commit is contained in:
parent
dcb6e60a3b
commit
d69e2c0929
22
src/ast.cpp
22
src/ast.cpp
@ -3,28 +3,6 @@
|
||||
#include <sstream>
|
||||
|
||||
namespace AST {
|
||||
int operator_precedence(BinOp& op) {
|
||||
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 "??";
|
||||
}
|
||||
}
|
||||
|
||||
std::string IntLiteralExpression::formatted() {
|
||||
std::stringstream out{ "" };
|
||||
out << this->m_value;
|
||||
|
||||
12
src/ast.h
12
src/ast.h
@ -9,14 +9,6 @@
|
||||
#include <memory>
|
||||
|
||||
namespace AST {
|
||||
enum class BinOp {
|
||||
Assignment,
|
||||
Add,
|
||||
};
|
||||
|
||||
int operator_precedence(BinOp& op);
|
||||
std::string format_operator(BinOp& op);
|
||||
|
||||
class Node {
|
||||
public:
|
||||
virtual std::string formatted() = 0;
|
||||
@ -56,10 +48,10 @@ namespace AST {
|
||||
class BinaryOperationExpression : public Expression {
|
||||
private:
|
||||
std::unique_ptr<Expression> m_lhs;
|
||||
BinOp m_binop;
|
||||
types::BinOp m_binop;
|
||||
std::unique_ptr<Expression> m_rhs;
|
||||
public:
|
||||
BinaryOperationExpression(std::unique_ptr<Expression> lhs, BinOp op, std::unique_ptr<Expression> rhs)
|
||||
BinaryOperationExpression(std::unique_ptr<Expression> lhs, types::BinOp op, std::unique_ptr<Expression> rhs)
|
||||
: m_lhs{ std::move(lhs) }, m_binop{ op }, m_rhs{ std::move(rhs) } {
|
||||
}
|
||||
virtual ~BinaryOperationExpression() override = default;
|
||||
|
||||
@ -45,13 +45,13 @@ namespace AST {
|
||||
|
||||
codegen::StackValue BinaryOperationExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
auto lvalued = scope.with_lvalue();
|
||||
auto lhs = this->m_lhs->codegen(builder, this->m_binop == BinOp::Assignment ? lvalued : scope);
|
||||
auto lhs = this->m_lhs->codegen(builder, this->m_binop == types::BinOp::Assignment ? lvalued : scope);
|
||||
auto rhs = this->m_rhs->codegen(builder, scope);
|
||||
switch (this->m_binop) {
|
||||
case BinOp::Assignment:
|
||||
case types::BinOp::Assignment:
|
||||
builder.builder->CreateStore(rhs.value, lhs.value, false);
|
||||
return rhs;
|
||||
case BinOp::Add:
|
||||
case types::BinOp::Add:
|
||||
return codegen::StackValue{
|
||||
lhs.ty->add(builder, lhs.value, rhs.value),
|
||||
lhs.ty
|
||||
|
||||
@ -44,7 +44,7 @@ namespace parsing {
|
||||
}
|
||||
}
|
||||
|
||||
Result<AST::BinOp, std::string> parse_binop(token::TokenStream& stream) {
|
||||
Result<types::BinOp, std::string> parse_binop(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
auto token = inner.next();
|
||||
@ -53,11 +53,11 @@ namespace parsing {
|
||||
}
|
||||
if (token.content == "=") {
|
||||
stream.m_position = inner.m_position;
|
||||
return new AST::BinOp{ AST::BinOp::Assignment };
|
||||
return new types::BinOp{ types::BinOp::Assignment };
|
||||
}
|
||||
if (token.content == "+") {
|
||||
stream.m_position = inner.m_position;
|
||||
return new AST::BinOp{ AST::BinOp::Add };
|
||||
return new types::BinOp{ types::BinOp::Add };
|
||||
}
|
||||
|
||||
throw std::runtime_error("Expected binop");
|
||||
@ -75,8 +75,8 @@ namespace parsing {
|
||||
auto binop = binop_res.unwrap();
|
||||
auto rhs = parse_primary_expression(stream).unwrap();
|
||||
|
||||
if (AST::operator_precedence(binop) > prev_precedence) {
|
||||
rhs = parse_rhs(stream, std::move(rhs), AST::operator_precedence(binop));
|
||||
if (types::operator_precedence(binop) > prev_precedence) {
|
||||
rhs = parse_rhs(stream, std::move(rhs), types::operator_precedence(binop));
|
||||
}
|
||||
|
||||
auto binop_expr = new AST::BinaryOperationExpression{ std::move(lhs), binop, std::move(rhs) };
|
||||
|
||||
@ -3,6 +3,28 @@
|
||||
#include <sstream>
|
||||
|
||||
namespace types {
|
||||
int operator_precedence(BinOp& op) {
|
||||
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 "??";
|
||||
}
|
||||
}
|
||||
|
||||
std::string FundamentalType::formatted() {
|
||||
switch (this->m_ty) {
|
||||
case FundamentalTypeKind::Int:
|
||||
|
||||
@ -7,6 +7,14 @@
|
||||
#include <memory>
|
||||
|
||||
namespace types {
|
||||
enum class BinOp {
|
||||
Assignment,
|
||||
Add,
|
||||
};
|
||||
|
||||
int operator_precedence(BinOp& op);
|
||||
std::string format_operator(BinOp& op);
|
||||
|
||||
enum FundamentalTypeKind {
|
||||
Int,
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user