From f51b5fa5d9776876731342d69103a1c93da8cc02 Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Sun, 28 Jul 2024 21:22:59 +0100 Subject: [PATCH] VM: remove I32 type --- README.md | 4 +- include/utils.h | 30 --------------- include/vm.h | 29 -------------- main.cpp | 5 +-- src/ast.cpp | 28 +++++++++++--- src/lexer.l | 11 +----- src/parser.y | 17 +++------ src/utils.cpp | 62 +----------------------------- src/vm.cpp | 87 +----------------------------------------- tests/lexer_tests.cpp | 14 ++----- tests/parser_tests.cpp | 44 ++++++++++----------- tests/vm_tests.cpp | 74 +++++++---------------------------- 12 files changed, 72 insertions(+), 333 deletions(-) diff --git a/README.md b/README.md index 09273c2..1c3adbe 100644 --- a/README.md +++ b/README.md @@ -28,12 +28,12 @@ make -j`nproc` ## Syntax ### Variable declaration ```c -define x : i32 = 42; +define x : int = 42; define y = 42; ``` ### Function declaration ```c -define add : function(x : i32, y : i32) -> i32 = { +define add : function(x : int, y : int) -> int = { return x + y; }; ``` diff --git a/include/utils.h b/include/utils.h index af2cc39..0c14b83 100644 --- a/include/utils.h +++ b/include/utils.h @@ -27,12 +27,6 @@ : Instruction::InstructionType::OPERATION##GlobalObject; \ instruction.params.index = id; \ break; \ - case VariableType::Type::Bool: \ - case VariableType::Type::I32: \ - instruction.type = isLocal ? Instruction::InstructionType::OPERATION##LocalI32 \ - : Instruction::InstructionType::OPERATION##GlobalI32; \ - instruction.params.index = id; \ - break; \ case VariableType::Type::I64: \ instruction.type = isLocal ? Instruction::InstructionType::OPERATION##LocalI64 \ : Instruction::InstructionType::OPERATION##GlobalI64; \ @@ -55,30 +49,6 @@ inline uint32_t convert(const std::string &value) { return static_cast(std::stoull(value)); } -#define DECLARE_VAR_CASE(TYPE, PARAM, CONVERT_TYPE) \ - case TYPE: { \ - if (!value.has_value()) { \ - segment.instructions.push_back({ \ - .type = Instruction::InstructionType::Load##TYPE, \ - .params = {.PARAM = 0}, \ - }); \ - } else if (((Node *) value.value())->token.type == Number) { \ - segment.instructions.push_back({ \ - .type = Instruction::InstructionType::Load##TYPE, \ - .params = {.PARAM = convert(((Node *) value.value())->token.value)}, \ - }); \ - } else { \ - ((Node *) value.value())->compile(program, segment); \ - } \ - segment.declare_variable(identifier.token.value, new VariableType(VariableType::Type::TYPE)); \ - segment.instructions.push_back({ \ - .type = segment.id == 0 \ - ? Instruction::InstructionType::StoreGlobal##TYPE \ - : Instruction::InstructionType::StoreLocal##TYPE, \ - .params = {.index = segment.find_local(identifier.token.value)}, \ - }); \ - } break; - #define VAR_CASE(OP, TYPE) \ case VariableType::Type::TYPE: \ segment.instructions.push_back(Instruction{.type = Instruction::InstructionType::OP##TYPE}); \ diff --git a/include/vm.h b/include/vm.h index 215517c..c48a8e8 100644 --- a/include/vm.h +++ b/include/vm.h @@ -12,48 +12,29 @@ struct Instruction { enum InstructionType : uint8_t { Invalid = 0, - AddI32, AddI64, - SubI32, SubI64, - MulI32, MulI64, - DivI32, DivI64, - ModI32, ModI64, - GreaterI32, GreaterI64, - LessI32, LessI64, - GreaterEqualI32, GreaterEqualI64, - LessEqualI32, LessEqualI64, - EqualI32, EqualI64, - NotEqualI32, NotEqualI64, - IncrementI32, IncrementI64, - DecrementI32, DecrementI64, - StoreGlobalI32, StoreGlobalI64, StoreGlobalObject, - StoreLocalI32, StoreLocalI64, StoreLocalObject, - LoadI32, LoadI64, LoadObject, - LoadGlobalI32, LoadGlobalI64, LoadGlobalObject, - LoadLocalI32, LoadLocalI64, LoadLocalObject, - ConvertI32toI64, Return, Call, JumpIfFalse, @@ -62,16 +43,7 @@ struct Instruction { union { void *ptr; size_t index; - int32_t i32; int64_t i64; - struct { - size_t index; - int32_t i32; - } ri32; - struct { - size_t index; - int64_t i64; - } ri64; } params{}; }; @@ -79,7 +51,6 @@ struct VariableType { enum Type : uint32_t { Invalid = 0, Bool, - I32, I64, Object, Function diff --git a/main.cpp b/main.cpp index f8fac6c..a292ac6 100644 --- a/main.cpp +++ b/main.cpp @@ -12,11 +12,8 @@ void printTopStack(const VM &vm, const Program &program) { case VariableType::Bool: std::cout << (vm.topStack() == 0 ? "false" : "true") << std::endl; break; - case VariableType::I32: - std::cout << (int32_t) vm.topStack() << std::endl; - break; case VariableType::I64: { - std::cout << vm.topStack() << std::endl; + std::cout << (int64_t) vm.topStack() << std::endl; } break; case VariableType::Object: { auto obj = std::bit_cast(vm.topStack()); diff --git a/src/ast.cpp b/src/ast.cpp index 9591ea8..b98736f 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -32,8 +32,8 @@ void Node::compile(Program &program, Segment &segment) const { case True: return segment.instructions.push_back( Instruction{ - .type = Instruction::InstructionType::LoadI32, - .params = {.i32 = token.value == "true"}, + .type = Instruction::InstructionType::LoadI64, + .params = {.i64 = token.value == "true"}, }); default: throw std::runtime_error("[Node::compile] This should not be accessed!"); @@ -151,8 +151,26 @@ void Declaration::compile(Program &program, Segment &segment) const { emitStore(program, segment, identifier.token.value); } break; case Bool: - DECLARE_VAR_CASE(I32, i32, int32_t) - DECLARE_VAR_CASE(I64, i64, int64_t) + case Int: { + if (!value.has_value()) { + segment.instructions.push_back({ + .type = Instruction::InstructionType::LoadI64, + .params = {.i64 = 0}, + }); + } else if (((Node *) value.value())->token.type == Number) { + segment.instructions.push_back({ + .type = Instruction::InstructionType::LoadI64, + .params = {.i64 = convert(((Node *) value.value())->token.value)}, + }); + } else { + ((Node *) value.value())->compile(program, segment); + } + segment.declare_variable(identifier.token.value, new VariableType(VariableType::Type::I64)); + segment.instructions.push_back({ + .type = segment.id == 0 ? Instruction::InstructionType::StoreGlobalI64 : Instruction::InstructionType::StoreLocalI64, + .params = {.index = segment.find_local(identifier.token.value)}, + }); + } break; default: throw std::runtime_error("[Declaration::compile] Unimplemented type handler!"); } @@ -393,7 +411,6 @@ void UnaryExpression::compile(Program &program, Segment &segment) const { switch (op.type) { case Increment: switch (varType->type) { - VAR_CASE(Increment, I32) VAR_CASE(Increment, I64) default: throw std::runtime_error("[UnaryExpression::compile] Invalid varType!"); @@ -401,7 +418,6 @@ void UnaryExpression::compile(Program &program, Segment &segment) const { break; case Decrement: switch (varType->type) { - VAR_CASE(Decrement, I32) VAR_CASE(Decrement, I64) default: throw std::runtime_error("[UnaryExpression::compile] Invalid varType!"); diff --git a/src/lexer.l b/src/lexer.l index 647f8cf..ac2516b 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -50,15 +50,8 @@ "while" { return While; } "for" { return For; } "return" { return Return; } -"u8" { return U8; } -"u16" { return U16; } -"u32" { return U32; } -"u64" { return U64; } -"i8" { return I8; } -"i16" { return I16; } -"i32" { return I32; } -"i64" { return I64; } -"f32" { return F32; } +"uint" { return UInt; } +"int" { return Int; } "f64" { return F64; } "bool" { return Bool; } "str" { return Str; } diff --git a/src/parser.y b/src/parser.y index 26a144b..53b70cc 100644 --- a/src/parser.y +++ b/src/parser.y @@ -24,7 +24,7 @@ %token Increment Decrement IncrementAssign DecrementAssign %token Equal NotEqual Less Greater LessEqual GreaterEqual And Or Not %token Define Function If Else While For Return -%token U8 U16 U32 U64 I8 I16 I32 I64 F32 F64 Bool True False Str +%token Int UInt F64 Bool True False Str %token Colon Comma Semicolon Arrow Newline %token LParen RParen LBrace RBrace LBracket RBracket @@ -67,19 +67,10 @@ ForLoop: ; VarType: - U8 { $$ = new Node({U8, "u8"}); } - | U16 { $$ = new Node({U16, "u16"}); } - | U32 { $$ = new Node({U32, "u32"}); } - | U64 { $$ = new Node({U64, "u64"}); } - | I8 { $$ = new Node({I8, "i8"}); } - | I16 { $$ = new Node({I16, "i16"}); } - | I32 { $$ = new Node({I32, "i32"}); } - | I64 { $$ = new Node({I64, "i64"}); } - | F32 { $$ = new Node({F32, "f32"}); } + Int { $$ = new Node({Int, "int"}); } + | UInt { $$ = new Node({UInt, "uint"}); } | F64 { $$ = new Node({F64, "f64"}); } | Bool { $$ = new Node({Bool, "bool"}); } - | True { $$ = new Node({True, "true"}); } - | False { $$ = new Node({False, "false"}); } | Str { $$ = new Node({Str, "str"}); } ; @@ -170,6 +161,8 @@ UnaryExpression: Expression: Identifier { $$ = new Node({Identifier, $1}); } + | False { $$ = new Node({False, "false"}); } + | True { $$ = new Node({True, "true"}); } | Number { $$ = new Node({Number, $1}); } | String { $$ = new Node({String, $1}); } | FunctionDeclaration { $$ = $1; } diff --git a/src/utils.cpp b/src/utils.cpp index 5bc06ff..b2c2d50 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,8 +11,7 @@ VariableType::Type varTypeConvert(AbstractSyntaxTree *ast) { auto token = dynamic_cast(ast)->token; static std::unordered_map types = { {Bool, VariableType::Bool}, - {I32, VariableType::I32}, - {I64, VariableType::I64}, + {Int, VariableType::I64}, {Str, VariableType::Object}, }; if (types.find(token.type) == types.end()) @@ -22,18 +21,8 @@ VariableType::Type varTypeConvert(AbstractSyntaxTree *ast) { VariableType::Type biggestType(VariableType::Type first, VariableType::Type second) { switch (first) { - case VariableType::I32: - switch (second) { - case VariableType::I32: - return first; - case VariableType::I64: - return second; - default: - throw std::runtime_error("This should not be accessed!"); - } case VariableType::I64: switch (second) { - case VariableType::I32: case VariableType::I64: return first; default: @@ -55,13 +44,6 @@ VariableType::Type deduceType(Program &program, Segment &segment, AbstractSyntax case False: return VariableType::Bool; case Number: { - try { - std::stoi(token.value); - return VariableType::I32; - } catch (std::out_of_range &) { - goto long64; - } - long64: try { std::stol(token.value); return VariableType::I64; @@ -112,9 +94,6 @@ VariableType::Type deduceType(Program &program, Segment &segment, AbstractSyntax #define TYPE_CASE(INS) \ case GenericInstruction::INS: { \ switch (type) { \ - case VariableType::Bool: \ - case VariableType::I32: \ - return {Instruction::InstructionType::INS##I32}; \ case VariableType::I64: \ return {Instruction::InstructionType::INS##I64}; \ default: \ @@ -140,11 +119,6 @@ Instruction getInstructionWithType(GenericInstruction instruction, VariableType: Instruction emitLoad(VariableType::Type type, const Token &token) { switch (type) { case VariableType::Bool: - case VariableType::I32: - return Instruction{ - .type = Instruction::InstructionType::LoadI32, - .params = {.i32 = std::stoi(token.value)}, - }; case VariableType::I64: return Instruction{ .type = Instruction::InstructionType::LoadI64, @@ -159,20 +133,6 @@ void typeCast(std::vector &instructions, VariableType::Type from, V if (from == to) return; switch (from) { - case VariableType::I32: - switch (to) { - case VariableType::I64: { - if (instructions.back().type == Instruction::InstructionType::LoadI32) { - instructions.back().type = Instruction::InstructionType::LoadI64; - auto oldVal = instructions.back().params.i32; - instructions.back().params.i64 = oldVal; - return; - } - return instructions.push_back({.type = Instruction::InstructionType::ConvertI32toI64}); - } - default: - throw std::runtime_error("Invalid type cast"); - } default: throw std::runtime_error("Invalid type cast"); } @@ -180,12 +140,6 @@ void typeCast(std::vector &instructions, VariableType::Type from, V VariableType::Type getInstructionType(const Program &program, const Instruction &instruction) { switch (instruction.type) { - case Instruction::InstructionType::EqualI32: - case Instruction::InstructionType::LessI32: - case Instruction::InstructionType::GreaterI32: - case Instruction::InstructionType::GreaterEqualI32: - case Instruction::InstructionType::LessEqualI32: - case Instruction::InstructionType::NotEqualI32: case Instruction::InstructionType::EqualI64: case Instruction::InstructionType::LessI64: case Instruction::InstructionType::GreaterI64: @@ -193,19 +147,6 @@ VariableType::Type getInstructionType(const Program &program, const Instruction case Instruction::InstructionType::LessEqualI64: case Instruction::InstructionType::NotEqualI64: return VariableType::Bool; - case Instruction::InstructionType::LoadI32: - case Instruction::InstructionType::AddI32: - case Instruction::InstructionType::SubI32: - case Instruction::InstructionType::MulI32: - case Instruction::InstructionType::DivI32: - case Instruction::InstructionType::ModI32: - case Instruction::InstructionType::IncrementI32: - case Instruction::InstructionType::DecrementI32: - case Instruction::InstructionType::StoreLocalI32: - case Instruction::InstructionType::LoadLocalI32: - case Instruction::InstructionType::StoreGlobalI32: - case Instruction::InstructionType::LoadGlobalI32: - return VariableType::I32; case Instruction::InstructionType::LoadI64: case Instruction::InstructionType::AddI64: case Instruction::InstructionType::SubI64: @@ -218,7 +159,6 @@ VariableType::Type getInstructionType(const Program &program, const Instruction case Instruction::InstructionType::LoadLocalI64: case Instruction::InstructionType::StoreGlobalI64: case Instruction::InstructionType::LoadGlobalI64: - case Instruction::InstructionType::ConvertI32toI64: return VariableType::I64; case Instruction::InstructionType::LoadObject: case Instruction::InstructionType::StoreLocalObject: diff --git a/src/vm.cpp b/src/vm.cpp index 0bea2f3..5757f6f 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -1,4 +1,5 @@ #include "vm.h" +#include "utils.h" #include #include #include @@ -121,68 +122,6 @@ void VM::run(const Program &program) { switch (instruction.type) { case Instruction::InstructionType::Invalid: throw std::runtime_error("[VM::run] Invalid instruction!"); - case Instruction::InstructionType::AddI32: { - auto a = popStack(); - auto b = popStack(); - pushStack(a + b); - } break; - case Instruction::InstructionType::SubI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a - b); - } break; - case Instruction::InstructionType::MulI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a * b); - } break; - case Instruction::InstructionType::DivI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a / b); - } break; - case Instruction::InstructionType::ModI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a % b); - } break; - case Instruction::InstructionType::GreaterI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a > b); - } break; - case Instruction::InstructionType::LessI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a < b); - } break; - case Instruction::InstructionType::IncrementI32: { - auto val = popStack(); - pushStack(val + 1); - } break; - case Instruction::InstructionType::DecrementI32: { - auto val = popStack(); - pushStack(val - 1); - } break; - case Instruction::InstructionType::LoadI32: { - pushStack(instruction.params.i32); - } break; - case Instruction::InstructionType::StoreGlobalI32: { - auto val = popStack(); - setGlobal(instruction.params.index, val); - } break; - case Instruction::InstructionType::LoadGlobalI32: { - auto val = getGlobal(instruction.params.index); - pushStack(val); - } break; - case Instruction::InstructionType::StoreLocalI32: { - auto val = popStack(); - setLocal(instruction.params.index, val); - } break; - case Instruction::InstructionType::LoadLocalI32: { - auto val = getLocal(instruction.params.index); - pushStack(val); - } break; case Instruction::InstructionType::Return: popStackFrame(); continue; @@ -236,41 +175,21 @@ void VM::run(const Program &program) { auto a = popStack(); pushStack(a < b); } break; - case Instruction::GreaterEqualI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a >= b); - } break; case Instruction::GreaterEqualI64: { auto b = popStack(); auto a = popStack(); pushStack(a >= b); } break; - case Instruction::LessEqualI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a <= b); - } break; case Instruction::LessEqualI64: { auto b = popStack(); auto a = popStack(); pushStack(a <= b); } break; - case Instruction::EqualI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a == b); - } break; case Instruction::EqualI64: { auto b = popStack(); auto a = popStack(); pushStack(a == b); } break; - case Instruction::NotEqualI32: { - auto b = popStack(); - auto a = popStack(); - pushStack(a != b); - } break; case Instruction::NotEqualI64: { auto b = popStack(); auto a = popStack(); @@ -307,10 +226,6 @@ void VM::run(const Program &program) { auto val = getLocal(instruction.params.index); pushStack(val); } break; - case Instruction::ConvertI32toI64: { - auto val = popStack(); - pushStack(val); - } break; case Instruction::LoadObject: { pushStack(std::bit_cast(instruction.params.ptr)); } break; diff --git a/tests/lexer_tests.cpp b/tests/lexer_tests.cpp index 628f5cb..2742ed8 100644 --- a/tests/lexer_tests.cpp +++ b/tests/lexer_tests.cpp @@ -110,8 +110,7 @@ TEST(Lexer, Symbols) { TEST(Lexer, Keywords) { auto actual = lex( "define if else while for return " - "u8 u16 u32 u64 i8 i16 i32 i64 str " - "f32 f64 bool true false"); + "int uint str f64 bool true false"); std::vector expected = { {Define, "define"}, @@ -120,16 +119,9 @@ TEST(Lexer, Keywords) { {While, "while"}, {For, "for"}, {Return, "return"}, - {U8, "u8"}, - {U16, "u16"}, - {U32, "u32"}, - {U64, "u64"}, - {I8, "i8"}, - {I16, "i16"}, - {I32, "i32"}, - {I64, "i64"}, + {Int, "int"}, + {UInt, "uint"}, {Str, "str"}, - {F32, "f32"}, {F64, "f64"}, {Bool, "bool"}, {True, "true"}, diff --git a/tests/parser_tests.cpp b/tests/parser_tests.cpp index a48e0d6..0619c19 100644 --- a/tests/parser_tests.cpp +++ b/tests/parser_tests.cpp @@ -92,10 +92,10 @@ TEST(ParserTests, OperationsPriority) { } TEST(ParserTests, Declaration) { - const char *input = "define a : u32;"; + const char *input = "define a : uint;"; auto expectedResult = std::vector{ new Declaration( - new Node({U32, "u32"}), + new Node({UInt, "uint"}), Node({Identifier, "a"})), }; auto actualResult = parse(input); @@ -106,10 +106,10 @@ TEST(ParserTests, Declaration) { } TEST(ParserTests, DeclarationWithInitialization) { - const char *input = "define a : u32 = 42;"; + const char *input = "define a : uint = 42;"; auto expectedResult = std::vector{ new Declaration( - new Node({U32, "u32"}), + new Node({UInt, "uint"}), Node({Identifier, "a"}), new Node({Number, "42"})), }; @@ -150,16 +150,16 @@ TEST(ParserTests, VariableAssignment) { } TEST(ParserTests, FunctionDeclaration) { - const char *input = "define max : function(x: i32, y: i32) -> i32;"; + const char *input = "define max : function(x: int, y: int) -> int;"; auto expectedResult = std::vector{ new Declaration( new FunctionDeclaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), {new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "x"})), new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "y"}))}), Node({Identifier, "max"})), }; @@ -171,19 +171,19 @@ TEST(ParserTests, FunctionDeclaration) { } TEST(ParserTests, FunctionDeclarationWithBody) { - const char *input = "define max : function(x: i32, y: i32) -> i32 = {\n" + const char *input = "define max : function(x: int, y: int) -> int = {\n" "\treturn x + y;\n" "};"; auto expectedResult = std::vector{ new Declaration( new FunctionDeclaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), { new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "x"})), new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "y"})), }), Node({Identifier, "max"}), @@ -202,26 +202,26 @@ TEST(ParserTests, FunctionDeclarationWithBody) { } TEST(ParserTests, FunctionDeclarationWithBodyWithMultipleExpressions) { - const char *input = "define max : function(x: i32, y: i32) -> i32 = {\n" - "\tdefine result : i32 = x + y;\n" + const char *input = "define max : function(x: int, y: int) -> int = {\n" + "\tdefine result : int = x + y;\n" "\treturn result;\n" "};"; auto expectedResult = std::vector{ new Declaration( new FunctionDeclaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), { new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "x"})), new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "y"})), }), Node({Identifier, "max"}), new ScopedBody({ new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "result"}), new BinaryExpression( new Node({Identifier, "x"}), @@ -238,7 +238,7 @@ TEST(ParserTests, FunctionDeclarationWithBodyWithMultipleExpressions) { } TEST(ParserTests, FunctionDeclarationWithAutoTypeDeduction) { - const char *input = "define max = (function (x: i32, y: i32) -> i32) {\n" + const char *input = "define max = (function (x: int, y: int) -> int) {\n" "\treturn x + y;\n" "};"; auto expectedResult = std::vector{ @@ -252,13 +252,13 @@ TEST(ParserTests, FunctionDeclarationWithAutoTypeDeduction) { {Plus, "+"})), }), new FunctionDeclaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), { new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "x"})), new Declaration( - new Node({I32, "i32"}), + new Node({Int, "int"}), Node({Identifier, "y"})), }))), }; diff --git a/tests/vm_tests.cpp b/tests/vm_tests.cpp index 57efed5..5859570 100644 --- a/tests/vm_tests.cpp +++ b/tests/vm_tests.cpp @@ -50,7 +50,7 @@ TEST(VM, CompoundExpression) { } TEST(VM, SimpleVariableDeclaration) { - const char *input = "define a : i32 = 42;" + const char *input = "define a : int = 42;" "a;"; VM vm; auto program = compile(input); @@ -59,7 +59,7 @@ TEST(VM, SimpleVariableDeclaration) { } TEST(VM, VariablesShouldBeInitializedWithZero) { - const char *input = "define a : i32;" + const char *input = "define a : int;" "a;"; VM vm; auto program = compile(input); @@ -86,27 +86,8 @@ TEST(VM, DeclareBooleans) { ASSERT_EQ(vm.topStack(), 0); } -TEST(VM, SimpleI64VariableDeclaration) { - const char *input = "define a : i64 = 42;" - "a;"; - VM vm; - auto program = compile(input); - vm.run(program); - ASSERT_EQ(vm.topStack(), static_cast(42)); -} - -TEST(VM, BinaryExpressionWithMultipleTypes) { - const char *input = "define a : i64 = 42;" - "define b : i32 = 42;" - "a + b;"; - VM vm; - auto program = compile(input); - vm.run(program); - ASSERT_EQ(vm.topStack(), 84); -} - TEST(VM, SimpleVariableAssignment) { - const char *input = "define a : i32 = 42; a = 43; a;"; + const char *input = "define a : int = 42; a = 43; a;"; VM vm; auto program = compile(input); vm.run(program); @@ -114,17 +95,7 @@ TEST(VM, SimpleVariableAssignment) { } TEST(VM, RightIncrementUnaryOperator) { - const char *input = "define a : i32 = 42;" - "a++;" - "a;"; - VM vm; - auto program = compile(input); - vm.run(program); - ASSERT_EQ(vm.topStack(), 43); -} - -TEST(VM, RightIncrementUnaryOperatorI64) { - const char *input = "define a : i64 = 42;" + const char *input = "define a : int = 42;" "a++;" "a;"; VM vm; @@ -134,17 +105,7 @@ TEST(VM, RightIncrementUnaryOperatorI64) { } TEST(VM, RightDecrementUnaryOperator) { - const char *input = "define a : i32 = 42;" - "a--;" - "a;"; - VM vm; - auto program = compile(input); - vm.run(program); - ASSERT_EQ(vm.topStack(), 41); -} - -TEST(VM, RightDecrementUnaryOperatorI64) { - const char *input = "define a : i64 = 42;" + const char *input = "define a : int = 42;" "a--;" "a;"; VM vm; @@ -154,7 +115,7 @@ TEST(VM, RightDecrementUnaryOperatorI64) { } TEST(VM, LeftIncrementUnaryOperator) { - const char *input = "define a : i32 = 42;" + const char *input = "define a : int = 42;" "++a;" "a;"; VM vm; @@ -164,7 +125,7 @@ TEST(VM, LeftIncrementUnaryOperator) { } TEST(VM, LeftDecrementUnaryOperator) { - const char *input = "define a : i32 = 42;" + const char *input = "define a : int = 42;" "--a;" "a;"; VM vm; @@ -174,7 +135,7 @@ TEST(VM, LeftDecrementUnaryOperator) { } TEST(VM, SimpleIfCondition) { - const char *input = "define a : i32 = 69;" + const char *input = "define a : int = 69;" "if 10 > 0 { a = 42; };" "a;"; VM vm; @@ -184,7 +145,7 @@ TEST(VM, SimpleIfCondition) { } TEST(VM, SimpleIfElseCondition) { - const char *input = "define a : i32 = 69;" + const char *input = "define a : int = 69;" "if 10 < 0 { a = 42; } else { a = 43; };" "a;"; VM vm; @@ -194,7 +155,7 @@ TEST(VM, SimpleIfElseCondition) { } TEST(VM, SimpleFunctionDeclaration) { - const char *input = "define add : function(x: i32, y: i32) -> i32 = { return x + y; };" + const char *input = "define add : function(x: int, y: int) -> int = { return x + y; };" "add(1, 2);"; VM vm; auto program = compile(input); @@ -202,17 +163,8 @@ TEST(VM, SimpleFunctionDeclaration) { ASSERT_EQ(vm.topStack(), 3); } -TEST(VM, FunctionWithDifferentReturnType) { - const char *input = "define add : function(x: i32, y: i32) -> i64 = { return x + y; };" - "add(20, 10);"; - VM vm; - auto program = compile(input); - vm.run(program); - ASSERT_EQ(vm.topStack(), 30); -} - TEST(VM, RecursiveFunction) { - const char *input = "define fib : function(n: i32) -> i32 = { if n < 2 { return n; } else { return fib(n - 1) + fib(n - 2); }; };" + const char *input = "define fib : function(n: int) -> int = { if n < 2 { return n; } else { return fib(n - 1) + fib(n - 2); }; };" "fib(10);"; VM vm; auto program = compile(input); @@ -221,7 +173,7 @@ TEST(VM, RecursiveFunction) { } TEST(VM, SimpleWhileLoop) { - const char *input = "define a : i32 = 0;" + const char *input = "define a : int = 0;" "while a < 10 { a = a + 1; };" "a;"; VM vm; @@ -231,7 +183,7 @@ TEST(VM, SimpleWhileLoop) { } TEST(VM, SimpleForLoop) { - const char *input = "define sum : i32 = 0;" + const char *input = "define sum : int = 0;" "for define i = 0; i < 10; i++ {" "sum = sum + i;" "};"