From 0a3b5557f8c795a7a4ea01faf1afae1de01e10aa Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Thu, 1 Aug 2024 19:43:59 +0100 Subject: [PATCH] fix random shit --- include/utils.h | 5 ----- src/ast.cpp | 25 +++++++++++++++++++------ tests/vm_tests.cpp | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/utils.h b/include/utils.h index e30d19f..22ae93b 100644 --- a/include/utils.h +++ b/include/utils.h @@ -50,11 +50,6 @@ inline uint32_t convert(const std::string &value) { return static_cast(std::stoull(value)); } -#define VAR_CASE(OP, TYPE) \ - case VariableType::Type::TYPE: \ - segment.instructions.push_back(Instruction{.type = Instruction::InstructionType::OP##TYPE}); \ - break; - enum class GenericInstruction { Add, diff --git a/src/ast.cpp b/src/ast.cpp index b522a16..a07c424 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -67,12 +67,16 @@ void BinaryExpression::compile(Program &program, Segment &segment) const { if (node->token.type != Identifier) throw std::runtime_error("[BinaryExpression::compile] Invalid expression varType!"); auto varType = segment.find_local(node->token.value) != -1 ? segment.locals[node->token.value] : program.segments[0].locals[node->token.value]; - right->compile(program, segment); switch (op.type) { case IncrementAssign: switch (varType.type->type) { - VAR_CASE(Add, I64) + case VariableType::Type::I64: + emitLoad(program, segment, node->token.value); + right->compile(program, segment); + segment.instructions.push_back(Instruction{.type = Instruction::InstructionType::AddI64}); + break; case VariableType::Array: { + right->compile(program, segment); left->compile(program, segment); segment.instructions.push_back({.type = Instruction::AppendToArray}); } break; @@ -82,7 +86,11 @@ void BinaryExpression::compile(Program &program, Segment &segment) const { break; case DecrementAssign: switch (varType.type->type) { - VAR_CASE(Sub, I64) + case VariableType::Type::I64: + emitLoad(program, segment, node->token.value); + right->compile(program, segment); + segment.instructions.push_back(Instruction{.type = Instruction::InstructionType::SubI64}); + break; default: throw std::runtime_error("[BinaryExpression::compile] Invalid varType!"); } @@ -455,18 +463,23 @@ void UnaryExpression::compile(Program &program, Segment &segment) const { throw std::runtime_error("[UnaryExpression::compile] Identifier not found: " + node->token.value); } - emitLoad(program, segment, node->token.value); switch (op.type) { case Increment: switch (varType->type) { - VAR_CASE(Increment, I64) + case VariableType::Type::I64: + emitLoad(program, segment, node->token.value); + segment.instructions.push_back(Instruction{.type = Instruction::InstructionType::IncrementI64}); + break; default: throw std::runtime_error("[UnaryExpression::compile] Invalid varType!"); } break; case Decrement: switch (varType->type) { - VAR_CASE(Decrement, I64) + case VariableType::Type::I64: + emitLoad(program, segment, node->token.value); + segment.instructions.push_back(Instruction{.type = Instruction::InstructionType::DecrementI64}); + break; default: throw std::runtime_error("[UnaryExpression::compile] Invalid varType!"); } diff --git a/tests/vm_tests.cpp b/tests/vm_tests.cpp index 5de3c23..abcd3e3 100644 --- a/tests/vm_tests.cpp +++ b/tests/vm_tests.cpp @@ -142,7 +142,7 @@ TEST(VM, DecrementAssign) { VM vm; auto program = compile(input); vm.run(program); - ASSERT_EQ(vm.topStack(), 42); + ASSERT_EQ((int64_t) vm.topStack(), 42); } TEST(VM, LeftDecrementUnaryOperator) {