Skip to content

Commit

Permalink
perf: add LessThanRI instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 18, 2024
1 parent 75288fa commit 26b87db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/bytecode/builtin_functions/LessThanFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "LessThanFunction.h"
#include "bytecode/instructions/LessThan.h"
#include "bytecode/instructions/LessThanRI.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "exceptions/SyntaxError.h"

Expand All @@ -16,6 +17,19 @@ namespace Bytecode::BuiltinFunctions {
instructions.push_back(new LoadLiteral(new StackObject(true)));
return;
}
if (args[0]->type == SyntaxTreeNode::TokenNode &&
args[1]->type == SyntaxTreeNode::TokenNode &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1) {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number) {
instructions.push_back(
new Bytecode::LessThanRI(
segment->find_variable(((TokenNode *) args[0])->getName()),
((TokenNode *) args[1])->token.asNumber()));
return;
}
}

args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new Bytecode::LessThan());
Expand Down
1 change: 1 addition & 0 deletions src/bytecode/instructions/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Bytecode {
Decrement,
AddRI,
SubtractRI,
LessThanRI,
};

class Instruction {
Expand Down
18 changes: 18 additions & 0 deletions src/bytecode/instructions/LessThanRI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Instruction.h"
namespace Bytecode {
class LessThanRI final : public Instruction {
public:
uint32_t rg;
double number;
LessThanRI(uint32_t rg, double number)
: rg(rg), number(number) { type = InstructionType::LessThanRI; }
[[nodiscard]] std::string toString() const override {
return "LessThanRI $r" + std::to_string(rg) + ", " + StackObject(number).toString();
}
bool operator==(const Instruction &instruction) const override {
return instruction.type == type;
}
};
}
10 changes: 10 additions & 0 deletions src/bytecode/vm/Interpreter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Interpreter.h"
#include "bytecode/instructions/AddRI.h"
#include "bytecode/instructions/LessThanRI.h"
#include "bytecode/instructions/SubtractRI.h"
#include "exceptions/SyntaxError.h"

Expand Down Expand Up @@ -95,6 +96,15 @@ void Bytecode::Interpreter::execute(const Program &program) {
}
vm.program_stack.push(object1.asNumber() < object2.asNumber());
} break;
case InstructionType::LessThanRI: {
const auto object2 = ((LessThanRI *) currentInstruction)->number;
const auto object1 = vm.call_stack.getLocal(
((LessThanRI *) currentInstruction)->rg);
if (object1.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"-\", Expected number, got string");
}
vm.program_stack.push(object1.asNumber() < object2);
} break;
case InstructionType::GreaterThan: {
const auto object2 = vm.program_stack.pop();
const auto object1 = vm.program_stack.pop();
Expand Down

0 comments on commit 26b87db

Please sign in to comment.