Skip to content

Commit

Permalink
handle errors in folded programs
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrause committed Oct 29, 2024
1 parent 6003fea commit 242780e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/cmd/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/eval/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <unistd.h>
#endif

const std::string Interpreter::ERROR_SEQ_USING_NEGATIVE_ARG =
"seq using negative argument";

using MemStack = std::stack<Memory>;
using IntStack = std::stack<int64_t>;
using NumStack = std::stack<Number>;
Expand Down Expand Up @@ -395,8 +398,7 @@ std::string getProgramPath(int64_t id) {

std::pair<Number, size_t> 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
Expand Down
2 changes: 2 additions & 0 deletions src/eval/interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 242780e

Please sign in to comment.