moved comment parsing to first pass, to save program space

This commit is contained in:
Bob Polis 2020-09-21 12:41:29 +02:00
parent 9c4bd366b6
commit eeeaf894e0

View File

@ -36,6 +36,8 @@ std::string interpreter::eval(std::istream& in, bool& done) {
for (std::string line; std::getline(in, line); ++_pc) { for (std::string line; std::getline(in, line); ++_pc) {
if (line[0] == ':') { if (line[0] == ':') {
_labels.emplace(line.substr(1), _pc--); _labels.emplace(line.substr(1), _pc--);
} else if (line[0] == '#') { // check comment
_pc--;
} else { } else {
_prog.push_back(line); _prog.push_back(line);
} }
@ -47,12 +49,6 @@ std::string interpreter::eval(std::istream& in, bool& done) {
// fetch next instruction // fetch next instruction
std::string code {_prog[_pc]}; std::string code {_prog[_pc]};
// check comment
if (code[0] == '#') {
_pc++;
continue;
}
// check literal int // check literal int
if (std::isdigit(code[0])) { if (std::isdigit(code[0])) {
_stack.push_back(code); _stack.push_back(code);