1
0
Fork 0
mirror of https://gitlab.com/niansa/anyproc.git synced 2025-03-06 20:49:24 +01:00
anyproc/examples/repl.cpp
2023-11-26 22:06:56 +01:00

65 lines
1.8 KiB
C++

#include "anyproc.hpp"
#include <iostream>
#include <string>
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <model file>" << std::endl;
return -1;
}
PyEval eval(argv[1]);
const auto progress_indicator = [] (float progress) {
if (progress < 2.5f || progress > 90.0f) {
std::cout << "\r \r";
return true;
}
std::cout << unsigned(progress) << "% \r" << std::flush;
return true;
};
bool had_nl = false,
had_begin = false,
first_token;
const auto result_printer = [&] (const char *token) {
std::cout << token << std::flush;
if (token[0] == '\n')
had_nl = true;
if ((had_nl || first_token) && token[0] == '>' && token[1] == '>') {
had_begin = true;
return false;
}
first_token = false;
return true;
};
for (;;) {
std::string expr;
std::cout << ">>> " << std::flush;
std::getline(std::cin, expr);
eval.begin();
if (expr.empty())
continue;
bool no_value = expr[0] == '$';
if (expr.find("import ") == 0) {
eval.load_module(expr.substr(7, expr.size()-7)).finish();
} else if (no_value) {
eval.expression(expr.substr(1, expr.size()-1)).finish();
} else {
first_token = true;
had_begin = false;
eval.expression(expr).run(progress_indicator, result_printer);
if (had_begin) {
eval.get_inference().append("> \n");
std::cout << "\r \r";
} else if (!had_nl) {
std::cout << '\n';
}
had_nl = false;
}
}
}