improved syntax_error to hold line number as well

This commit is contained in:
Bob Polis 2020-09-21 12:25:40 +02:00
parent 27b798a83e
commit 372bc976a0
2 changed files with 7 additions and 3 deletions

View File

@ -52,7 +52,7 @@ std::string interpreter::eval(std::istream& in, bool& done) {
_pc++;
continue;
}
// check literal int
if (std::isdigit(code[0])) {
_stack.push_back(code);
@ -130,7 +130,7 @@ void interpreter::exec_instruction(const std::string& code, bool& done) {
else if (code == "inp") inp();
else if (code == "out") out();
else if (code == "err") err();
else throw syntax_error {code};
else throw syntax_error {code, _pc + 1};
}
// integer operations -----------------------------------------------------

View File

@ -18,7 +18,11 @@
class syntax_error : public std::runtime_error {
public:
syntax_error(const std::string& what_arg) : std::runtime_error(what_arg) {}
syntax_error(const std::string& what_arg, size_t lineno)
: std::runtime_error {what_arg}, _lineno {lineno} {}
size_t lineno() const { return _lineno; }
private:
size_t _lineno {0};
};
class interpreter {