Skip to content

Commit

Permalink
Refactor: remove DecrementR instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed May 23, 2024
1 parent 016cfdf commit e091e66
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 37 deletions.
32 changes: 6 additions & 26 deletions src/bytecode/builtin_functions/SubtractFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,21 @@ namespace Bytecode::BuiltinFunctions {
return;
}

if (args[1]->type == SyntaxTreeNode::TokenNode &&
((TokenNode *) args[1])->getName() == "1") {
if (args[0]->type == SyntaxTreeNode::TokenNode &&
result->find_variable(((TokenNode *) args[0])->getName()) != -1) {
instructions.push_back(new (Instruction){
Instruction::DecrementR,
{.r_param = {result->find_variable(((TokenNode *) args[0])->getName())}},
});
return;
}
args[0]->compile(result, program, instructions);
instructions.push_back(new (Instruction){Instruction::Decrement});
return;
}

if (args[0]->type == SyntaxTreeNode::TokenNode &&
((TokenNode *) args[0])->getName() == "1") {
args[1]->compile(result, program, instructions);
instructions.push_back(new (Instruction){Instruction::Decrement});
return;
}


if (args[0]->type == SyntaxTreeNode::TokenNode &&
args[1]->type == SyntaxTreeNode::TokenNode &&
result->find_variable(((TokenNode *) args[0])->getName()) != -1) {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number) {
((TokenNode *) args[1])->token.type == Token::Number) {
if (((TokenNode *) args[0])->token.type == Token::Symbol) {
instructions.push_back(new (Instruction){
Instruction::SubtractRI,
{.ri_params = {
result->find_variable(((TokenNode *) args[0])->getName()),
StackObject(((TokenNode *) args[1])->token)}},
});
return;
} else if (((TokenNode *) args[1])->getName() == "1"){
args[0]->compile(result, program, instructions);
instructions.push_back(new (Instruction){Instruction::Decrement});
return;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/bytecode/compiler/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Bytecode {
DivideRR,
Increment,
Decrement,
DecrementR,
Equals,
EqualsRI,
LessThan,
Expand Down
7 changes: 0 additions & 7 deletions src/bytecode/vm/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ void Bytecode::Interpreter::execute(const Program &program) {
case Instruction::Decrement:
vm.program_stack.push(vm.program_stack.pop().data.number - 1);
break;
case Instruction::DecrementR: {
const auto number = vm.call_stack.getLocal(currentInstruction->params.r_param.reg);
if (number.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"-\", Expected number, got string");
}
vm.program_stack.push(number.data.number - 1);
} break;
case Instruction::Divide: {
const auto object2 = vm.program_stack.pop();
const auto object1 = vm.program_stack.pop();
Expand Down
3 changes: 0 additions & 3 deletions src/utils/dump_bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ void print_instruction(const Bytecode::Instruction &instruction) {
case Bytecode::Instruction::Decrement:
std::cout << "Decrement\n";
break;
case Bytecode::Instruction::DecrementR:
std::cout << "DecrementR $r" << instruction.params.r_param.reg << '\n';
break;
case Bytecode::Instruction::Equals:
std::cout << "Equals\n";
break;
Expand Down

0 comments on commit e091e66

Please sign in to comment.