From 242780e451ca4db5f4956413bade605dfc7fb5c7 Mon Sep 17 00:00:00 2001 From: Christian Krause Date: Tue, 29 Oct 2024 21:51:42 +0100 Subject: [PATCH] handle errors in folded programs --- src/cmd/commands.cpp | 19 ++++++++++++++++--- src/eval/interpreter.cpp | 6 ++++-- src/eval/interpreter.hpp | 2 ++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/cmd/commands.cpp b/src/cmd/commands.cpp index 8e388618..4e4fa531 100644 --- a/src/cmd/commands.cpp +++ b/src/cmd/commands.cpp @@ -357,10 +357,23 @@ void Commands::autoFold() { auto terms = seq.getTerms(OeisSequence::DEFAULT_SEQ_LENGTH); auto result = evaluator.check(main, terms, -1, main_id); if (result.first == status_t::ERROR) { - Log::get().error("Error in folded program", true); + Sequence tmp; + std::string error_msg; + try { + evaluator.eval(main, tmp, terms.size(), true); + } catch (std::exception& e) { + error_msg = e.what(); + } + if (error_msg.find(Interpreter::ERROR_SEQ_USING_NEGATIVE_ARG) != + std::string::npos) { + Log::get().warn("Ignoring invalid folded program"); + } else { + Log::get().error("Unknown error in folded program", true); + } + } else { + auto path = ProgramUtil::getProgramPath(main_id); + manager.dumpProgram(main_id, main, path, submitted_by); } - auto path = ProgramUtil::getProgramPath(main_id); - manager.dumpProgram(main_id, main, path, submitted_by); } if (log_scheduler.isTargetReached()) { log_scheduler.reset(); diff --git a/src/eval/interpreter.cpp b/src/eval/interpreter.cpp index be2722ae..fa28ab7f 100644 --- a/src/eval/interpreter.cpp +++ b/src/eval/interpreter.cpp @@ -21,6 +21,9 @@ #include #endif +const std::string Interpreter::ERROR_SEQ_USING_NEGATIVE_ARG = + "seq using negative argument"; + using MemStack = std::stack; using IntStack = std::stack; using NumStack = std::stack; @@ -395,8 +398,7 @@ std::string getProgramPath(int64_t id) { std::pair Interpreter::callSeq(int64_t id, const Number& arg) { if (arg < 0) { - throw std::runtime_error("seq using negative argument: " + - std::to_string(id)); + throw std::runtime_error(ERROR_SEQ_USING_NEGATIVE_ARG); } // check if already cached diff --git a/src/eval/interpreter.hpp b/src/eval/interpreter.hpp index a47d25b0..234692bb 100644 --- a/src/eval/interpreter.hpp +++ b/src/eval/interpreter.hpp @@ -9,6 +9,8 @@ class Interpreter { public: + static const std::string ERROR_SEQ_USING_NEGATIVE_ARG; + explicit Interpreter(const Settings &settings); static Number calc(const Operation::Type type, const Number &target,