-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move instructions code to execute(VM&) method
- Loading branch information
Showing
30 changed files
with
193 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
#pragma once | ||
|
||
#include "bytecode/instructions/Instruction.h" | ||
#include "exceptions/SyntaxError.h" | ||
|
||
namespace Bytecode { | ||
class Add final : public Instruction { | ||
public: | ||
Add() { type = InstructionType::Add; } | ||
void execute(Bytecode::VM &vm) override { | ||
const auto object2 = vm.program_stack.pop(); | ||
const auto object1 = vm.program_stack.pop(); | ||
if (object1.type != ObjectType::Number || | ||
object2.type != ObjectType::Number) { | ||
throw SyntaxError("Invalid argument type for function \"+\", Expected number, got string"); | ||
} | ||
vm.program_stack.push(object1.asNumber() + object2.asNumber()); | ||
} | ||
[[nodiscard]] std::string toString() const override { return "Add"; } | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
#pragma once | ||
|
||
#include "bytecode/instructions/Instruction.h" | ||
#include "exceptions/SyntaxError.h" | ||
|
||
namespace Bytecode { | ||
class Divide final : public Instruction { | ||
public: | ||
Divide() { type = InstructionType::Divide; }; | ||
void execute(Bytecode::VM &vm) override { | ||
const auto object2 = vm.program_stack.pop(); | ||
const auto object1 = vm.program_stack.pop(); | ||
if (object1.type != ObjectType::Number || | ||
object2.type != ObjectType::Number) { | ||
throw SyntaxError("Invalid argument type for function \"/\", Expected number, got string"); | ||
} | ||
if (object2.asNumber() == 0) { | ||
throw SyntaxError("Division by zero", 0, 0); | ||
} | ||
vm.program_stack.push(object1.asNumber() / object2.asNumber()); | ||
} | ||
[[nodiscard]] std::string toString() const override { return "Divide"; } | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
#pragma once | ||
|
||
#include "bytecode/instructions/Instruction.h" | ||
#include "exceptions/SyntaxError.h" | ||
|
||
namespace Bytecode { | ||
class Equals final : public Instruction { | ||
public: | ||
Equals() { type = InstructionType::Equals; }; | ||
void execute(Bytecode::VM &vm) override { | ||
const auto object2 = vm.program_stack.pop(); | ||
const auto object1 = vm.program_stack.pop(); | ||
if (object1.type != ObjectType::Number || | ||
object2.type != ObjectType::Number) { | ||
throw SyntaxError("Invalid argument type for function \"=\", Expected number, got string"); | ||
} | ||
vm.program_stack.push(object1.asNumber() == object2.asNumber()); | ||
} | ||
[[nodiscard]] std::string toString() const override { return "Equals"; } | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
#pragma once | ||
|
||
#include "bytecode/instructions/Instruction.h" | ||
#include "exceptions/SyntaxError.h" | ||
|
||
namespace Bytecode { | ||
class GreaterThan final : public Instruction { | ||
public: | ||
GreaterThan() { type = InstructionType::GreaterThan; }; | ||
void execute(Bytecode::VM &vm) override { | ||
const auto object2 = vm.program_stack.pop(); | ||
const auto object1 = vm.program_stack.pop(); | ||
if (object1.type != ObjectType::Number || | ||
object2.type != ObjectType::Number) { | ||
throw SyntaxError("Invalid argument type for function \">\", Expected number, got string"); | ||
} | ||
vm.program_stack.push(object1.asNumber() > object2.asNumber()); | ||
} | ||
[[nodiscard]] std::string toString() const override { return "GreaterThan"; } | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
#pragma once | ||
|
||
#include "bytecode/instructions/Instruction.h" | ||
#include "exceptions/SyntaxError.h" | ||
|
||
namespace Bytecode { | ||
class LessThan final : public Instruction { | ||
public: | ||
LessThan() { type = InstructionType::LessThan; }; | ||
void execute(Bytecode::VM &vm) override { | ||
const auto object2 = vm.program_stack.pop(); | ||
const auto object1 = vm.program_stack.pop(); | ||
if (object1.type != ObjectType::Number || | ||
object2.type != ObjectType::Number) { | ||
throw SyntaxError("Invalid argument type for function \"<\", Expected number, got string"); | ||
} | ||
vm.program_stack.push(object1.asNumber() < object2.asNumber()); | ||
} | ||
[[nodiscard]] std::string toString() const override { return "LessThan"; } | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
#pragma once | ||
|
||
#include "bytecode/instructions/Instruction.h" | ||
#include "exceptions/SyntaxError.h" | ||
|
||
namespace Bytecode { | ||
class Multiply final : public Instruction { | ||
public: | ||
Multiply() { type = InstructionType::Multiply; }; | ||
void execute(VM &vm) override { | ||
const auto object2 = vm.program_stack.pop(); | ||
const auto object1 = vm.program_stack.pop(); | ||
if (object1.type != ObjectType::Number || | ||
object2.type != ObjectType::Number) { | ||
throw SyntaxError("Invalid argument type for function \"*\", Expected number, got string"); | ||
} | ||
vm.program_stack.push(object1.asNumber() * object2.asNumber()); | ||
} | ||
[[nodiscard]] std::string toString() const override { return "Multiply"; } | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.