From 3a0ffd1e1563878e8c44c9f17affc650fbd62c8a Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Sat, 6 Jul 2024 17:33:46 +0100 Subject: [PATCH] VM: more work into supporting i64 type --- src/ast.cpp | 39 ++++++++++++++++++++++++++++++++++++--- tests/vm_tests.cpp | 21 ++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/ast.cpp b/src/ast.cpp index 01617e9..e2743c4 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -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 @@ -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 diff --git a/tests/vm_tests.cpp b/tests/vm_tests.cpp index f6850e4..90cde6a 100644 --- a/tests/vm_tests.cpp +++ b/tests/vm_tests.cpp @@ -58,7 +58,6 @@ TEST(VM, SimpleVariableDeclaration) { ASSERT_EQ(*static_cast(vm.topStack(sizeof(int32_t))), 42); } -// TODO: fix this TEST(VM, SimpleI64VariableDeclaration) { const char *input = "define a : i64 = 42;" "a;"; @@ -86,6 +85,16 @@ TEST(VM, RightIncrementUnaryOperator) { ASSERT_EQ(*static_cast(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(vm.topStack(sizeof(int64_t ))), 43); +} + TEST(VM, RightDecrementUnaryOperator) { const char *input = "define a : i32 = 42;" "a--;" @@ -96,6 +105,16 @@ TEST(VM, RightDecrementUnaryOperator) { ASSERT_EQ(*static_cast(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(vm.topStack(sizeof(int64_t))), 41); +} + TEST(VM, LeftIncrementUnaryOperator) { const char *input = "define a : i32 = 42;" "++a;"