improved first pass by using prog vector size instead of pc; added pc offset vector clear to reset

This commit is contained in:
Bob Polis 2020-09-22 10:51:20 +02:00
parent 27ad2a900c
commit 184cab5205

View File

@ -24,6 +24,7 @@ void interpreter::reset() {
_labels.clear(); _labels.clear();
_vars.clear(); _vars.clear();
_calls.clear(); _calls.clear();
_pc_offsets.clear();
_pc = 0; _pc = 0;
} }
@ -33,14 +34,12 @@ std::string interpreter::eval(std::istream& in, bool& done) {
done = false; done = false;
// first pass: read program & resolve labels // 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 if (line[0] == ':') { // check label definition
_labels.emplace(line.substr(1), _pc); _labels.emplace(line.substr(1), _prog.size());
_pc_offsets.push_back(_pc); _pc_offsets.push_back(_prog.size());
_pc--; // we'll lose this line
} else if (line[0] == '#') { // check comment } else if (line[0] == '#') { // check comment
_pc_offsets.push_back(_pc); _pc_offsets.push_back(_prog.size());
_pc--; // we'll lose this line
} else { } else {
_prog.push_back(line); _prog.push_back(line);
} }