Skip to content

Commit

Permalink
VM: add support for += and -= operators
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Jul 31, 2024
1 parent d453d94 commit 829d290
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ void BinaryExpression::compile(Program &program, Segment &segment) const {
emitStore(program, segment, dynamic_cast<Node &>(*left).token.value);
return;
}
if (op.type == IncrementAssign || op.type == DecrementAssign) {
auto node = dynamic_cast<Node *>(left);
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].type : program.segments[0].locals[node->token.value].type;
emitLoad(program, segment, node->token.value);
right->compile(program, segment);
switch (op.type) {
case IncrementAssign:
switch (varType->type) {
VAR_CASE(Add, I64)
default:
throw std::runtime_error("[BinaryExpression::compile] Invalid varType!");
}
break;
case DecrementAssign:
switch (varType->type) {
VAR_CASE(Sub, I64)
default:
throw std::runtime_error("[BinaryExpression::compile] Invalid varType!");
}
break;
default:
throw std::runtime_error("[BinaryExpression::compile] Invalid operator: " + op.value);
}
emitStore(program, segment, node->token.value);
return;
}

auto leftType = deduceType(program, segment, left);
auto rightType = deduceType(program, segment, right);
auto finalType = biggestType(leftType->type, rightType->type);
Expand Down
6 changes: 6 additions & 0 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ Expression:
| Expression GreaterEqual Expression {
$$ = new BinaryExpression(static_cast<AbstractSyntaxTree*>($1), static_cast<AbstractSyntaxTree*>($3), {GreaterEqual, ">="});
}
| Expression IncrementAssign Expression {
$$ = new BinaryExpression(static_cast<AbstractSyntaxTree*>($1), static_cast<AbstractSyntaxTree*>($3), {IncrementAssign, "+="});
}
| Expression DecrementAssign Expression {
$$ = new BinaryExpression(static_cast<AbstractSyntaxTree*>($1), static_cast<AbstractSyntaxTree*>($3), {DecrementAssign, "-="});
}
;
%%

Expand Down
20 changes: 20 additions & 0 deletions tests/vm_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ TEST(VM, LeftIncrementUnaryOperator) {
ASSERT_EQ(vm.topStack(), 43);
}

TEST(VM, IncrementAssign) {
const char *input = "define a : int = 42;"
"a += 27;"
"a;";
VM vm;
auto program = compile(input);
vm.run(program);
ASSERT_EQ(vm.topStack(), 69);
}

TEST(VM, DecrementAssign) {
const char *input = "define a : int = 69;"
"a -= 27;"
"a;";
VM vm;
auto program = compile(input);
vm.run(program);
ASSERT_EQ(vm.topStack(), 42);
}

TEST(VM, LeftDecrementUnaryOperator) {
const char *input = "define a : int = 42;"
"--a;"
Expand Down

0 comments on commit 829d290

Please sign in to comment.