From 372bc976a004456b3259cdc8a3906856b5235bcd Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Mon, 21 Sep 2020 12:25:40 +0200 Subject: [PATCH] improved syntax_error to hold line number as well --- interpreter.cpp | 4 ++-- interpreter.hpp | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/interpreter.cpp b/interpreter.cpp index 1b1b71f..4f3615c 100644 --- a/interpreter.cpp +++ b/interpreter.cpp @@ -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 ----------------------------------------------------- diff --git a/interpreter.hpp b/interpreter.hpp index c5b5f77..01a3abd 100644 --- a/interpreter.hpp +++ b/interpreter.hpp @@ -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 {