Skip to content

Commit

Permalink
VM: Only update segment when pushing to/popping from the call stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Jul 29, 2024
1 parent 36b0e0d commit 85e06d1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,21 @@ void VM::run(const Program &program) {
callStack.front().locals = newPtr;
}
}
auto *segment = &program.segments[callStack.back().segmentIndex];
for (;;) {
auto &segment = program.segments[callStack.back().segmentIndex];
auto &instruction = segment.instructions[callStack.back().currentInstruction];
auto &instruction = segment->instructions[callStack.back().currentInstruction];
switch (instruction.type) {
case Instruction::InstructionType::Invalid:
throw std::runtime_error("[VM::run] Invalid instruction!");
case Instruction::InstructionType::Return: {
popStackFrame();
segment = &program.segments[callStack.back().segmentIndex];
continue;
}
case Instruction::InstructionType::Call: {
callStack.back().currentInstruction++;
newStackFrame(program.segments[instruction.params.index]);
segment = &program.segments[instruction.params.index];
continue;
}
case Instruction::InstructionType::JumpIfFalse: {
Expand Down

0 comments on commit 85e06d1

Please sign in to comment.