input2 added

This commit is contained in:
vaclavt
2022-05-19 18:38:52 +02:00
parent aec06b5f52
commit 6506494750
5 changed files with 72 additions and 24 deletions

37
ml.cpp
View File

@@ -1135,6 +1135,24 @@ MlValue input(std::vector<MlValue> args, MlEnvironment &env) {
return MlValue::string(s);
}
// Get user input with an optional prompt using line noise
MlValue input2(std::vector<MlValue> args, MlEnvironment &env) {
// TODO add setup etc
eval_args(args, env);
if (args.size() > 1)
throw MlError(MlValue("input2", input2), env, TOO_MANY_ARGS);
char *line = linenoise(args.empty() ? ">>> " : args[0].as_string().c_str());
if (line == nullptr) throw std::runtime_error("date_to_string, cannot initialise linenoise");
std::string input{line};
linenoise_add_to_history(input);
return MlValue::string(input);
}
// Get a random number between two numbers inclusively
MlValue random(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
@@ -2252,6 +2270,7 @@ std::map<const std::string, Builtin> builtin_funcs
std::make_pair("random", builtin::random),
std::make_pair("include", builtin::include),
std::make_pair("input", builtin::input),
std::make_pair("input2", builtin::input2),
std::make_pair("read", builtin::read),
std::make_pair("read-file", builtin::read_file),
std::make_pair("read-file-lines", builtin::read_file_lines),
@@ -2361,15 +2380,14 @@ void repl(MlEnvironment &env) {
MlValue tmp;
std::vector<MlValue> parsed;
setup_linenoise(env, builtin_funcs);
setup_linenoise_repl(env, builtin_funcs);
while (true) {
char *line = linenoise(">>> ");
if (line == nullptr) break;
linenoise_line_read(line);
input = std::string(line);
linenoise_add_to_history(input);
if (input == "!quit" || input == "!q")
break;
@@ -2393,8 +2411,6 @@ void repl(MlEnvironment &env) {
}
}
}
close_linenoise();
}
@@ -2425,11 +2441,13 @@ int main(int argc, char *argv[]) {
srand(time(NULL));
try {
// performance monitor on
if (cmdOptionExists(argv, argv + argc, "-p"))
if (cmdOptionExists(argv, argv + argc, "-p")) {
MlPerfMon::instance().turnOn();
}
// better stacktrace
if (cmdOptionExists(argv, argv + argc, "-d"))
if (cmdOptionExists(argv, argv + argc, "-d")) {
MlPerfMon::instance().debugOn();
}
// skip loading std lib
if (!cmdOptionExists(argv, argv + argc, "-b")) {
load_std_lib(env);
@@ -2444,6 +2462,9 @@ int main(int argc, char *argv[]) {
std::cout << VERSION << std::endl;
return 0;
}
setup_linenoise();
// passed code
if (cmdOptionExists(argv, argv + argc, "-c")) {
std::vector<std::string> codes = getCmdOption(argv, argc, "-c");
@@ -2467,6 +2488,8 @@ int main(int argc, char *argv[]) {
repl(env);
}
close_linenoise();
MlPerfMon::instance().print_results();
return 0;