diff --git a/src/ast.cpp b/src/ast.cpp index 9c8e880..0ee4ee1 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -3,28 +3,6 @@ #include 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; diff --git a/src/ast.h b/src/ast.h index 89cb4e9..71eb8ff 100644 --- a/src/ast.h +++ b/src/ast.h @@ -9,14 +9,6 @@ #include 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 m_lhs; - BinOp m_binop; + types::BinOp m_binop; std::unique_ptr m_rhs; public: - BinaryOperationExpression(std::unique_ptr lhs, BinOp op, std::unique_ptr rhs) + BinaryOperationExpression(std::unique_ptr lhs, types::BinOp op, std::unique_ptr rhs) : m_lhs{ std::move(lhs) }, m_binop{ op }, m_rhs{ std::move(rhs) } { } virtual ~BinaryOperationExpression() override = default; diff --git a/src/codegen.cpp b/src/codegen.cpp index c1896c9..d07b054 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -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 diff --git a/src/parsing.cpp b/src/parsing.cpp index 6faeecf..0fef6aa 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -44,7 +44,7 @@ namespace parsing { } } - Result parse_binop(token::TokenStream& stream) { + Result 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) }; diff --git a/src/types.cpp b/src/types.cpp index ecb067d..edb7859 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -3,6 +3,28 @@ #include 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: diff --git a/src/types.h b/src/types.h index 34a1389..1eeeead 100644 --- a/src/types.h +++ b/src/types.h @@ -7,6 +7,14 @@ #include namespace types { + enum class BinOp { + Assignment, + Add, + }; + + int operator_precedence(BinOp& op); + std::string format_operator(BinOp& op); + enum FundamentalTypeKind { Int, };