From 184cab520587f14b0a4af626a4fb60b458cf63f9 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Tue, 22 Sep 2020 10:51:20 +0200 Subject: [PATCH] improved first pass by using prog vector size instead of pc; added pc offset vector clear to reset --- interpreter.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/interpreter.cpp b/interpreter.cpp index ef3f50e..bd3db0e 100644 --- a/interpreter.cpp +++ b/interpreter.cpp @@ -24,6 +24,7 @@ void interpreter::reset() { _labels.clear(); _vars.clear(); _calls.clear(); + _pc_offsets.clear(); _pc = 0; } @@ -33,14 +34,12 @@ std::string interpreter::eval(std::istream& in, bool& done) { done = false; // first pass: read program & resolve labels - for (std::string line; std::getline(in, line); ++_pc) { + for (std::string line; std::getline(in, line);) { if (line[0] == ':') { // check label definition - _labels.emplace(line.substr(1), _pc); - _pc_offsets.push_back(_pc); - _pc--; // we'll lose this line + _labels.emplace(line.substr(1), _prog.size()); + _pc_offsets.push_back(_prog.size()); } else if (line[0] == '#') { // check comment - _pc_offsets.push_back(_pc); - _pc--; // we'll lose this line + _pc_offsets.push_back(_prog.size()); } else { _prog.push_back(line); }