Skip to content

Commit

Permalink
fix random shit
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Aug 1, 2024
1 parent 2eef162 commit 0a3b555
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
5 changes: 0 additions & 5 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ inline uint32_t convert<uint32_t>(const std::string &value) {
return static_cast<uint32_t>(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,
Expand Down
25 changes: 19 additions & 6 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!");
}
Expand Down Expand Up @@ -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!");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/vm_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0a3b555

Please sign in to comment.