From 62319b0c16d79d814cf4040aa0a05c4f4c0870d0 Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Sun, 3 Mar 2024 10:58:03 +0100 Subject: [PATCH] refactor, perf: replace StackObject with abstract Literal class --- src/bytecode/instructions/Add.h | 6 +++--- src/bytecode/instructions/CondJumpIfNot.h | 2 +- src/bytecode/instructions/Divide.h | 6 +++--- src/bytecode/instructions/Equals.h | 6 +++--- src/bytecode/instructions/GreaterThan.h | 6 +++--- src/bytecode/instructions/LessThan.h | 6 +++--- src/bytecode/instructions/LoadLiteral.h | 6 +++--- src/bytecode/instructions/Multiply.h | 6 +++--- src/bytecode/instructions/Not.h | 4 ++-- src/bytecode/instructions/Subtract.h | 6 +++--- src/bytecode/objects/BooleanLiteral.h | 8 ++++---- src/bytecode/objects/GlobalRegister.h | 2 +- src/bytecode/objects/Literal.h | 21 --------------------- src/bytecode/objects/LocalRegister.h | 2 +- src/bytecode/objects/NumberLiteral.h | 8 ++++---- src/bytecode/objects/StackObject.h | 21 +++++++++++++-------- src/bytecode/objects/StdOutRegister.h | 18 +++++++++--------- src/bytecode/objects/StringLiteral.h | 8 ++++---- src/main.cpp | 6 +++--- tests/bytecode/vm/add_test.cpp | 12 ++++++------ tests/bytecode/vm/calls_test.cpp | 8 ++++---- tests/bytecode/vm/cond_test.cpp | 8 ++++---- tests/bytecode/vm/divide_test.cpp | 12 ++++++------ tests/bytecode/vm/equals_test.cpp | 16 ++++++++-------- tests/bytecode/vm/greater_than_test.cpp | 16 ++++++++-------- tests/bytecode/vm/less_than_test.cpp | 16 ++++++++-------- tests/bytecode/vm/load_test.cpp | 9 ++++----- tests/bytecode/vm/multiply_test.cpp | 12 ++++++------ tests/bytecode/vm/not_test.cpp | 8 ++++---- tests/bytecode/vm/subtract_test.cpp | 12 ++++++------ 30 files changed, 130 insertions(+), 147 deletions(-) delete mode 100644 src/bytecode/objects/Literal.h diff --git a/src/bytecode/instructions/Add.h b/src/bytecode/instructions/Add.h index 355e74e..7faf4b2 100644 --- a/src/bytecode/instructions/Add.h +++ b/src/bytecode/instructions/Add.h @@ -10,9 +10,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object1 = vm->stackPop(); const auto object2 = vm->stackPop(); - auto *result = new StackObject(new NumberLiteral( - ((NumberLiteral *) object2->literal)->asNumber() + - ((NumberLiteral *) object1->literal)->asNumber())); + auto *result = new NumberLiteral( + ((NumberLiteral *) object2)->asNumber() + + ((NumberLiteral *) object1)->asNumber()); delete object1; delete object2; vm->stackPush(result); diff --git a/src/bytecode/instructions/CondJumpIfNot.h b/src/bytecode/instructions/CondJumpIfNot.h index cdf3443..cbece8d 100644 --- a/src/bytecode/instructions/CondJumpIfNot.h +++ b/src/bytecode/instructions/CondJumpIfNot.h @@ -9,7 +9,7 @@ namespace Bytecode { explicit CondJumpIfNot(const size_t line) : line(line) { type = InstructionType::CondJumpIfNot; } void execute(VM *vm) override { const auto cond = vm->stackPop(); - if (!((BooleanLiteral *) cond->literal)->asBoolean()) + if (!((BooleanLiteral *) cond)->asBoolean()) vm->jump(line); delete cond; } diff --git a/src/bytecode/instructions/Divide.h b/src/bytecode/instructions/Divide.h index fce1c1e..bb87a2e 100644 --- a/src/bytecode/instructions/Divide.h +++ b/src/bytecode/instructions/Divide.h @@ -10,9 +10,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object1 = vm->stackPop(); const auto object2 = vm->stackPop(); - auto *result = new StackObject(new NumberLiteral( - ((NumberLiteral *) object2->literal)->asNumber() / - ((NumberLiteral *) object1->literal)->asNumber())); + auto *result = new NumberLiteral( + ((NumberLiteral *) object2)->asNumber() / + ((NumberLiteral *) object1)->asNumber()); vm->stackPush(result); } [[nodiscard]] std::string toString() const override { return "Divide"; } diff --git a/src/bytecode/instructions/Equals.h b/src/bytecode/instructions/Equals.h index 2ddde1e..efea78a 100644 --- a/src/bytecode/instructions/Equals.h +++ b/src/bytecode/instructions/Equals.h @@ -11,9 +11,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object1 = vm->stackPop(); const auto object2 = vm->stackPop(); - auto *result = new StackObject(new BooleanLiteral( - ((NumberLiteral *) object2->literal)->asNumber() == - ((NumberLiteral *) object1->literal)->asNumber())); + auto *result = new BooleanLiteral( + ((NumberLiteral *) object2)->asNumber() == + ((NumberLiteral *) object1)->asNumber()); vm->stackPush(result); delete object1; delete object2; diff --git a/src/bytecode/instructions/GreaterThan.h b/src/bytecode/instructions/GreaterThan.h index de89bb6..af070c0 100644 --- a/src/bytecode/instructions/GreaterThan.h +++ b/src/bytecode/instructions/GreaterThan.h @@ -11,9 +11,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object2 = vm->stackPop(); const auto object1 = vm->stackPop(); - auto *result = new StackObject(new BooleanLiteral( - ((NumberLiteral *) object1->literal)->asNumber() > - ((NumberLiteral *) object2->literal)->asNumber())); + auto *result = new BooleanLiteral( + ((NumberLiteral *) object1)->asNumber() > + ((NumberLiteral *) object2)->asNumber()); vm->stackPush(result); delete object1; delete object2; diff --git a/src/bytecode/instructions/LessThan.h b/src/bytecode/instructions/LessThan.h index 596a05d..e0a0fd6 100644 --- a/src/bytecode/instructions/LessThan.h +++ b/src/bytecode/instructions/LessThan.h @@ -11,9 +11,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object2 = vm->stackPop(); const auto object1 = vm->stackPop(); - auto *result = new StackObject(new BooleanLiteral( - ((NumberLiteral *) object1->literal)->asNumber() < - ((NumberLiteral *) object2->literal)->asNumber())); + auto *result = new BooleanLiteral( + ((NumberLiteral *) object1)->asNumber() < + ((NumberLiteral *) object2)->asNumber()); vm->stackPush(result); delete object1; delete object2; diff --git a/src/bytecode/instructions/LoadLiteral.h b/src/bytecode/instructions/LoadLiteral.h index f8f9982..776716b 100644 --- a/src/bytecode/instructions/LoadLiteral.h +++ b/src/bytecode/instructions/LoadLiteral.h @@ -6,17 +6,17 @@ namespace Bytecode { class LoadLiteral final : public Instruction { public: - Literal *literal; + StackObject *literal; explicit LoadLiteral(double value) { type = InstructionType::LoadLiteral; literal = new NumberLiteral(value); }; - explicit LoadLiteral(Literal *literal) : literal(literal) {} + explicit LoadLiteral(StackObject *literal) : literal(literal) {} void execute(VM *vm) override { - vm->stackPush(new StackObject(literal->copy())); + vm->stackPush(literal->copy()); } [[nodiscard]] std::string toString() const override { return "LoadLiteral " + literal->toString(); diff --git a/src/bytecode/instructions/Multiply.h b/src/bytecode/instructions/Multiply.h index 21e4d75..6c8796f 100644 --- a/src/bytecode/instructions/Multiply.h +++ b/src/bytecode/instructions/Multiply.h @@ -10,9 +10,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object2 = vm->stackPop(); const auto object1 = vm->stackPop(); - auto *result = new StackObject(new NumberLiteral( - ((NumberLiteral *) object2->literal)->asNumber() * - ((NumberLiteral *) object1->literal)->asNumber())); + auto *result = new NumberLiteral( + ((NumberLiteral *) object2)->asNumber() * + ((NumberLiteral *) object1)->asNumber()); vm->stackPush(result); delete object1; delete object2; diff --git a/src/bytecode/instructions/Not.h b/src/bytecode/instructions/Not.h index a6ce8d3..d11aae0 100644 --- a/src/bytecode/instructions/Not.h +++ b/src/bytecode/instructions/Not.h @@ -11,8 +11,8 @@ namespace Bytecode { void execute(VM *vm) override { const auto object = vm->stackPop(); - const auto boolean = ((BooleanLiteral *) object->literal)->asBoolean(); - const auto return_object = new StackObject(new BooleanLiteral(!boolean)); + const auto boolean = ((BooleanLiteral *) object)->asBoolean(); + const auto return_object = new BooleanLiteral(!boolean); vm->stackPush(return_object); } diff --git a/src/bytecode/instructions/Subtract.h b/src/bytecode/instructions/Subtract.h index bc3863d..23b1130 100644 --- a/src/bytecode/instructions/Subtract.h +++ b/src/bytecode/instructions/Subtract.h @@ -9,9 +9,9 @@ namespace Bytecode { void execute(VM *vm) override { const auto object2 = vm->stackPop(); const auto object1 = vm->stackPop(); - auto *result = new StackObject(new NumberLiteral( - ((NumberLiteral *) object1->literal)->asNumber() - - ((NumberLiteral *) object2->literal)->asNumber())); + auto *result = new NumberLiteral( + ((NumberLiteral *) object1)->asNumber() - + ((NumberLiteral *) object2)->asNumber()); vm->stackPush(result); delete object1; delete object2; diff --git a/src/bytecode/objects/BooleanLiteral.h b/src/bytecode/objects/BooleanLiteral.h index 01ed85f..1ab87c7 100644 --- a/src/bytecode/objects/BooleanLiteral.h +++ b/src/bytecode/objects/BooleanLiteral.h @@ -1,8 +1,8 @@ #pragma once -#include "Literal.h" +#include "StackObject.h" namespace Bytecode { - class BooleanLiteral : public Literal { + class BooleanLiteral : public StackObject { bool boolean; public: @@ -12,7 +12,7 @@ namespace Bytecode { [[nodiscard]] bool asBoolean() const { return boolean; } - [[nodiscard]] Literal *copy() const override { + [[nodiscard]] StackObject *copy() const override { return new BooleanLiteral(boolean); } @@ -20,7 +20,7 @@ namespace Bytecode { return "#" + (boolean ? std::string("true") : std::string("false")); } - bool operator==(const Literal &l) const override { + bool operator==(const StackObject &l) const override { return type == Type::Boolean && ((BooleanLiteral *) &l)->boolean == boolean; } diff --git a/src/bytecode/objects/GlobalRegister.h b/src/bytecode/objects/GlobalRegister.h index 88fa301..c6cae81 100644 --- a/src/bytecode/objects/GlobalRegister.h +++ b/src/bytecode/objects/GlobalRegister.h @@ -16,7 +16,7 @@ namespace Bytecode { } StackObject *get(VM *vm) override { - return new StackObject(vm->getGlobal(reg)->literal->copy()); + return vm->getGlobal(reg)->copy(); } void store(VM *vm) override { diff --git a/src/bytecode/objects/Literal.h b/src/bytecode/objects/Literal.h deleted file mode 100644 index ff906fd..0000000 --- a/src/bytecode/objects/Literal.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace Bytecode { - class Literal { - public: - enum Type { - None, - Number, - Boolean, - String, - }; - Type type = None; - virtual ~Literal() = default; - [[nodiscard]] virtual Literal *copy() const = 0; - [[nodiscard]] virtual std::string toString() const = 0; - virtual bool operator==(const Literal &l) const = 0; - }; -}// namespace Bytecode diff --git a/src/bytecode/objects/LocalRegister.h b/src/bytecode/objects/LocalRegister.h index 7595a5a..eb3d967 100644 --- a/src/bytecode/objects/LocalRegister.h +++ b/src/bytecode/objects/LocalRegister.h @@ -16,7 +16,7 @@ namespace Bytecode { } StackObject *get(VM *vm) override { - return new StackObject(vm->getLocal(reg)->literal->copy()); + return vm->getLocal(reg)->copy(); } void store(VM *vm) override {} diff --git a/src/bytecode/objects/NumberLiteral.h b/src/bytecode/objects/NumberLiteral.h index 22c48ea..d308b55 100644 --- a/src/bytecode/objects/NumberLiteral.h +++ b/src/bytecode/objects/NumberLiteral.h @@ -1,12 +1,12 @@ #pragma once -#include "Literal.h" +#include "StackObject.h" #include #include #include namespace Bytecode { - class NumberLiteral : public Literal { + class NumberLiteral : public StackObject { double number; public: @@ -16,7 +16,7 @@ namespace Bytecode { [[nodiscard]] double asNumber() const { return number; } - [[nodiscard]] Literal *copy() const override { + [[nodiscard]] StackObject *copy() const override { return new NumberLiteral(number); } @@ -26,7 +26,7 @@ namespace Bytecode { return s.str(); } - bool operator==(const Literal &l) const override { + bool operator==(const StackObject &l) const override { if (type == Type::Number) { return std::abs(((NumberLiteral *) &l)->number - number) < (std::numeric_limits::epsilon() * 10); diff --git a/src/bytecode/objects/StackObject.h b/src/bytecode/objects/StackObject.h index 7abb602..9ca5beb 100644 --- a/src/bytecode/objects/StackObject.h +++ b/src/bytecode/objects/StackObject.h @@ -1,16 +1,21 @@ #pragma once -#include "bytecode/objects/Literal.h" +#include +#include namespace Bytecode { class StackObject { public: - Literal *literal; - ~StackObject() { delete literal; } - explicit StackObject(Literal *literal) : literal(literal){}; - - bool operator==(const StackObject &so) const { - return *so.literal == *literal; - } + enum Type { + None, + Number, + Boolean, + String, + }; + Type type = None; + virtual ~StackObject() = default; + [[nodiscard]] virtual StackObject *copy() const = 0; + [[nodiscard]] virtual std::string toString() const = 0; + virtual bool operator==(const StackObject &l) const = 0; }; }// namespace Bytecode diff --git a/src/bytecode/objects/StdOutRegister.h b/src/bytecode/objects/StdOutRegister.h index 4dd52f2..27def47 100644 --- a/src/bytecode/objects/StdOutRegister.h +++ b/src/bytecode/objects/StdOutRegister.h @@ -18,21 +18,21 @@ namespace Bytecode { } void store(VM *vm) override { - const auto literal = vm->stackPop()->literal; - switch (literal->type) { - case Literal::Boolean: - std::cout << (((BooleanLiteral *) literal)->asBoolean() + const auto stackObject = vm->stackPop(); + switch (stackObject->type) { + case StackObject::Boolean: + std::cout << (((BooleanLiteral *) stackObject)->asBoolean() ? std::string("#true") : std::string("#false")); break; - case Literal::Number: - std::cout << ((NumberLiteral *) literal)->asNumber(); + case StackObject::Number: + std::cout << ((NumberLiteral *) stackObject)->asNumber(); break; - case Literal::String: - std::cout << ((StringLiteral *) literal)->asString(); + case StackObject::String: + std::cout << ((StringLiteral *) stackObject)->asString(); break; default: - std::cout << literal->toString(); + std::cout << stackObject->toString(); } } diff --git a/src/bytecode/objects/StringLiteral.h b/src/bytecode/objects/StringLiteral.h index 28273be..975633d 100644 --- a/src/bytecode/objects/StringLiteral.h +++ b/src/bytecode/objects/StringLiteral.h @@ -1,10 +1,10 @@ #pragma once -#include "Literal.h" +#include "StackObject.h" #include namespace Bytecode { - class StringLiteral : public Literal { + class StringLiteral : public StackObject { std::string value; public: @@ -18,7 +18,7 @@ namespace Bytecode { [[nodiscard]] std::string asString() const { return value; } - [[nodiscard]] Literal *copy() const override { + [[nodiscard]] StackObject *copy() const override { return new StringLiteral(value); } @@ -27,7 +27,7 @@ namespace Bytecode { return '"' + value + '"'; } - bool operator==(const Literal &l) const override { + bool operator==(const StackObject &l) const override { return type == Type::String && ((StringLiteral *) &l)->value == value; } diff --git a/src/main.cpp b/src/main.cpp index ddccb7f..d252d9a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,10 +55,10 @@ void exec_program(const std::string &program, struct options opts) { if (interpreter.vm.stackTop() == nullptr) continue; - if (interpreter.vm.stackTop()->literal->type == Bytecode::Literal::Type::Number) - std::cout << ((Bytecode::NumberLiteral *) interpreter.vm.stackTop()->literal)->asNumber() << '\n'; + if (interpreter.vm.stackTop()->type == Bytecode::StackObject::Type::Number) + std::cout << ((Bytecode::NumberLiteral *) interpreter.vm.stackTop())->asNumber() << '\n'; else - std::cout << interpreter.vm.stackTop()->literal->toString() << '\n'; + std::cout << interpreter.vm.stackTop()->toString() << '\n'; interpreter.vm.clearStack(); } catch (SyntaxError &error) { diff --git a/tests/bytecode/vm/add_test.cpp b/tests/bytecode/vm/add_test.cpp index cc5b06a..91728ad 100644 --- a/tests/bytecode/vm/add_test.cpp +++ b/tests/bytecode/vm/add_test.cpp @@ -15,12 +15,12 @@ TEST(vm_add_test, ShouldAddTwoLiterals) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(30)); + const auto expected_result = NumberLiteral(30); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_add_test, ShouldAddTwoDecimalNumbers) { @@ -32,12 +32,12 @@ TEST(vm_add_test, ShouldAddTwoDecimalNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(4.6415)); + const auto expected_result = NumberLiteral(4.6415); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_add_test, ShouldAddDecimalAndIntegerNumbers) { @@ -49,10 +49,10 @@ TEST(vm_add_test, ShouldAddDecimalAndIntegerNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(4.1415)); + const auto expected_result = NumberLiteral(4.1415); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/calls_test.cpp b/tests/bytecode/vm/calls_test.cpp index 7f9c7e1..697bc8f 100644 --- a/tests/bytecode/vm/calls_test.cpp +++ b/tests/bytecode/vm/calls_test.cpp @@ -22,12 +22,12 @@ TEST(vm_calls_test, function_with_a_single_arg) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(100)); + const auto expected_result = NumberLiteral(100); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_calls_test, function_with_multiple_args) { @@ -44,10 +44,10 @@ TEST(vm_calls_test, function_with_multiple_args) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(40)); + const auto expected_result = NumberLiteral(40); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } \ No newline at end of file diff --git a/tests/bytecode/vm/cond_test.cpp b/tests/bytecode/vm/cond_test.cpp index 46ac675..89c47de 100644 --- a/tests/bytecode/vm/cond_test.cpp +++ b/tests/bytecode/vm/cond_test.cpp @@ -21,12 +21,12 @@ TEST(vm_cond_test, ShouldNotJumpOnTrueCondition) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(20)); + const auto expected_result = NumberLiteral(20); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_cond_test, ShouldJumpOnFalseCondition) { @@ -61,10 +61,10 @@ TEST(vm_conf_test, ShouldJumpToOppositeCondition) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(20)); + const auto expected_result = NumberLiteral(20); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/divide_test.cpp b/tests/bytecode/vm/divide_test.cpp index 04fa2a2..2358c89 100644 --- a/tests/bytecode/vm/divide_test.cpp +++ b/tests/bytecode/vm/divide_test.cpp @@ -16,12 +16,12 @@ TEST(vm_divide_test, ShouldDivideTwoLiterals) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(15)); + const auto expected_result = NumberLiteral(15); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_divide_test, ShouldDivideTwoDecimalNumbers) { @@ -33,12 +33,12 @@ TEST(vm_divide_test, ShouldDivideTwoDecimalNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(3)); + const auto expected_result = NumberLiteral(3); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_divide_test, ShouldAddDecimalAndIntegerNumbers) { @@ -50,10 +50,10 @@ TEST(vm_divide_test, ShouldAddDecimalAndIntegerNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(2.25)); + const auto expected_result = NumberLiteral(2.25); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/equals_test.cpp b/tests/bytecode/vm/equals_test.cpp index 6e7046f..4c01e9f 100644 --- a/tests/bytecode/vm/equals_test.cpp +++ b/tests/bytecode/vm/equals_test.cpp @@ -15,12 +15,12 @@ TEST(vm_equals_test, ShouldReturnTrueOnEquality) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_equals_test, ShouldReturnFalseOnInequality) { @@ -32,12 +32,12 @@ TEST(vm_equals_test, ShouldReturnFalseOnInequality) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_equals_test, ShouldCompareTwoDecimalNumbersAndReturnTrueOnEquality) { @@ -49,12 +49,12 @@ TEST(vm_equals_test, ShouldCompareTwoDecimalNumbersAndReturnTrueOnEquality) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_equals_test, ShouldCompareTwoDecimalNumbersAndReturnFalseOnInequality) { @@ -66,10 +66,10 @@ TEST(vm_equals_test, ShouldCompareTwoDecimalNumbersAndReturnFalseOnInequality) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/greater_than_test.cpp b/tests/bytecode/vm/greater_than_test.cpp index 878fae8..9b9bd21 100644 --- a/tests/bytecode/vm/greater_than_test.cpp +++ b/tests/bytecode/vm/greater_than_test.cpp @@ -15,12 +15,12 @@ TEST(vm_greater_than, ShouldReturnTrueWhenGreaterThan) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_greater_than, ShouldReturnFalseWhenNotGreaterThan) { @@ -32,12 +32,12 @@ TEST(vm_greater_than, ShouldReturnFalseWhenNotGreaterThan) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_greater_than, ShouldCompareTwoDecimalNumbersAndReturnTrueWhenGreaterThan) { @@ -49,12 +49,12 @@ TEST(vm_greater_than, ShouldCompareTwoDecimalNumbersAndReturnTrueWhenGreaterThan }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_greater_than, ShouldCompareTwoDecimalNumbersAndReturnFalseWhenNotGreaterThan) { @@ -66,10 +66,10 @@ TEST(vm_greater_than, ShouldCompareTwoDecimalNumbersAndReturnFalseWhenNotGreater }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/less_than_test.cpp b/tests/bytecode/vm/less_than_test.cpp index b83ab9d..1a6dfe0 100644 --- a/tests/bytecode/vm/less_than_test.cpp +++ b/tests/bytecode/vm/less_than_test.cpp @@ -15,12 +15,12 @@ TEST(vm_less_than, ShouldReturnTrueWhenLessThan) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_less_than, ShouldReturnFalseWhenNotLessThan) { @@ -32,12 +32,12 @@ TEST(vm_less_than, ShouldReturnFalseWhenNotLessThan) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_less_than, ShouldCompareTwoDecimalNumbersAndReturnTrueWhenLessThan) { @@ -49,12 +49,12 @@ TEST(vm_less_than, ShouldCompareTwoDecimalNumbersAndReturnTrueWhenLessThan) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_less_than, ShouldCompareTwoDecimalNumbersAndReturnFalseWhenNotLessThan) { @@ -66,10 +66,10 @@ TEST(vm_less_than, ShouldCompareTwoDecimalNumbersAndReturnFalseWhenNotLessThan) }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/load_test.cpp b/tests/bytecode/vm/load_test.cpp index 11d01e2..e583915 100644 --- a/tests/bytecode/vm/load_test.cpp +++ b/tests/bytecode/vm/load_test.cpp @@ -2,7 +2,6 @@ #include "bytecode/instructions/Load.h" #include "bytecode/instructions/LoadLiteral.h" #include "bytecode/instructions/Store.h" -#include "bytecode/objects/Literal.h" #include "bytecode/objects/StackObject.h" #include "bytecode/vm/Interpreter.h" #include @@ -14,12 +13,12 @@ TEST(vm_load_test, ShouldLoadLiteralIntoStack) { new Segment({new LoadLiteral(10)}), }); - const auto expected_result = new StackObject(new NumberLiteral(10)); + const auto expected_result = NumberLiteral(10); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_load_test, ShouldLoadGlobalVariableIntoStack) { @@ -31,10 +30,10 @@ TEST(vm_load_test, ShouldLoadGlobalVariableIntoStack) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(10)); + const auto expected_result = NumberLiteral(10); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } \ No newline at end of file diff --git a/tests/bytecode/vm/multiply_test.cpp b/tests/bytecode/vm/multiply_test.cpp index 1749471..98dccc7 100644 --- a/tests/bytecode/vm/multiply_test.cpp +++ b/tests/bytecode/vm/multiply_test.cpp @@ -15,12 +15,12 @@ TEST(vm_multiply_test, ShouldMultiplyTwoLiterals) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(200)); + const auto expected_result = NumberLiteral(200); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_multiply_test, ShouldMultiplyTwoDecimalNumbers) { @@ -32,12 +32,12 @@ TEST(vm_multiply_test, ShouldMultiplyTwoDecimalNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(3.45565)); + const auto expected_result = NumberLiteral(3.45565); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_multiply_test, ShouldMultiplyDecimalAndIntegerNumbers) { @@ -49,10 +49,10 @@ TEST(vm_multiply_test, ShouldMultiplyDecimalAndIntegerNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(6.283)); + const auto expected_result = NumberLiteral(6.283); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } diff --git a/tests/bytecode/vm/not_test.cpp b/tests/bytecode/vm/not_test.cpp index 31c31c7..cb4fe75 100644 --- a/tests/bytecode/vm/not_test.cpp +++ b/tests/bytecode/vm/not_test.cpp @@ -18,12 +18,12 @@ TEST(vm_not_test, ShouldNegateATrueCondition) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(false)); + const auto expected_result = BooleanLiteral(false); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_not_test, ShouldNegateAFalseCondition) { @@ -36,10 +36,10 @@ TEST(vm_not_test, ShouldNegateAFalseCondition) { }), }); - const auto expected_result = new StackObject(new BooleanLiteral(true)); + const auto expected_result = BooleanLiteral(true); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } \ No newline at end of file diff --git a/tests/bytecode/vm/subtract_test.cpp b/tests/bytecode/vm/subtract_test.cpp index 44e2598..7cf1c4d 100644 --- a/tests/bytecode/vm/subtract_test.cpp +++ b/tests/bytecode/vm/subtract_test.cpp @@ -15,12 +15,12 @@ TEST(vm_subtract_test, ShouldSubtractTwoLiterals) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(10)); + const auto expected_result = NumberLiteral(10); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_subtract_test, ShouldSubtractTwoDecimalNumbers) { @@ -32,12 +32,12 @@ TEST(vm_subtract_test, ShouldSubtractTwoDecimalNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(2.0415)); + const auto expected_result = NumberLiteral(2.0415); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); } TEST(vm_subtract_test, ShouldSubtractDecimalAndIntegerNumbers) { @@ -49,10 +49,10 @@ TEST(vm_subtract_test, ShouldSubtractDecimalAndIntegerNumbers) { }), }); - const auto expected_result = new StackObject(new NumberLiteral(1.1415)); + const auto expected_result = NumberLiteral(1.1415); auto interpreter = Interpreter(); interpreter.execute(program); - EXPECT_EQ(*interpreter.vm.stackTop() == *expected_result, true); + EXPECT_EQ(*interpreter.vm.stackTop() == expected_result, true); }