Skip to content

Commit

Permalink
VM: more work into supporting i64 type
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Jul 6, 2024
1 parent 85b0b76 commit 3a0ffd1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
39 changes: 36 additions & 3 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,42 @@ void Declaration::compile(Program &program, Segment &segment) const {
throw std::runtime_error("[Declaration::compile] Type deduction is not implemented!");
switch (type.value()->nodeType) {
case AbstractSyntaxTree::Type::Node: {
switch (((Node *) type.value())->token.type) {
Node *initNode = (Node *) value.value();
Node *typeNode = (Node *) type.value();
if (initNode->token.type == Number) {
switch (typeNode->token.type) {
case I32: {
segment.instructions.push_back({
.type = Instruction::InstructionType::LoadI32,
.params = {.i32 = std::stoi(initNode->token.value)}
});
segment.instructions.push_back({
.type = segment.id == 0
? Instruction::InstructionType::StoreGlobalI32
: Instruction::InstructionType::StoreLocalI32,
.params = {.index = segment.locals.size()},
});
segment.declare_variable(identifier.token.value, Variable::Type::I32);
} break;
case I64: {
segment.instructions.push_back({
.type = Instruction::InstructionType::LoadI64,
.params = {.i64 = std::stol(initNode->token.value)}
});
segment.instructions.push_back({
.type = segment.id == 0
? Instruction::InstructionType::StoreGlobalI64
: Instruction::InstructionType::StoreLocalI64,
.params = {.index = segment.locals.size()},
});
segment.declare_variable(identifier.token.value, Variable::Type::I64);
} break;
}
return;
}
switch (typeNode->token.type) {
case I32: {
value.value()->compile(program, segment);
initNode->compile(program, segment);
segment.instructions.push_back({
.type = segment.id == 0
? Instruction::InstructionType::StoreGlobalI32
Expand All @@ -223,7 +256,7 @@ void Declaration::compile(Program &program, Segment &segment) const {
segment.declare_variable(identifier.token.value, Variable::Type::I32);
} break;
case I64: {
value.value()->compile(program, segment);
initNode->compile(program, segment);
segment.instructions.push_back({
.type = segment.id == 0
? Instruction::InstructionType::StoreGlobalI64
Expand Down
21 changes: 20 additions & 1 deletion tests/vm_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ TEST(VM, SimpleVariableDeclaration) {
ASSERT_EQ(*static_cast<int32_t *>(vm.topStack(sizeof(int32_t))), 42);
}

// TODO: fix this
TEST(VM, SimpleI64VariableDeclaration) {
const char *input = "define a : i64 = 42;"
"a;";
Expand Down Expand Up @@ -86,6 +85,16 @@ TEST(VM, RightIncrementUnaryOperator) {
ASSERT_EQ(*static_cast<int32_t *>(vm.topStack(sizeof(int32_t))), 43);
}

TEST(VM, RightIncrementUnaryOperatorI64) {
const char *input = "define a : i64 = 42;"
"a++;"
"a;";
VM vm;
auto program = compile(input);
vm.run(program);
ASSERT_EQ(*static_cast<int32_t *>(vm.topStack(sizeof(int64_t ))), 43);
}

TEST(VM, RightDecrementUnaryOperator) {
const char *input = "define a : i32 = 42;"
"a--;"
Expand All @@ -96,6 +105,16 @@ TEST(VM, RightDecrementUnaryOperator) {
ASSERT_EQ(*static_cast<int32_t *>(vm.topStack(sizeof(int32_t))), 41);
}

TEST(VM, RightDecrementUnaryOperatorI64) {
const char *input = "define a : i64 = 42;"
"a--;"
"a;";
VM vm;
auto program = compile(input);
vm.run(program);
ASSERT_EQ(*static_cast<int32_t *>(vm.topStack(sizeof(int64_t))), 41);
}

TEST(VM, LeftIncrementUnaryOperator) {
const char *input = "define a : i32 = 42;"
"++a;"
Expand Down

0 comments on commit 3a0ffd1

Please sign in to comment.