restore back before introducing main.cpp

This commit is contained in:
2021-03-12 20:14:13 +01:00
parent 55d39ef321
commit de7c39d52f
6 changed files with 1352 additions and 16 deletions

59
ml.cpp
View File

@@ -9,6 +9,7 @@
#include "clib/json11.h"
#include "clib/printf.h"
#include "linenoise.h"
#include <cmath>
#include <map>
@@ -1609,14 +1610,54 @@ namespace builtin {
}
}
// void completion(const char *buf, linenoiseCompletions *lc) {
// if (buf[0] == 'h') {
// linenoiseAddCompletion(lc,"hello");
// linenoiseAddCompletion(lc,"hello there");
// }
// }
// char *hints(const char *buf, int *color, int *bold) {
// if (!strcasecmp(buf,"hello")) {
// *color = 35;
// *bold = 0;
// return " World";
// }
// return NULL;
// }
void repl(MlEnvironment &env) {
std::string code;
std::string input;
MlValue tmp;
std::vector<MlValue> parsed;
char *line;
// TODO not portable and in function
std::string history_file;
std::string file{"/.ml_history.txt"};
const char *t = std::getenv("HOME");
if (t == nullptr)
history_file = "/tmp/" + file;
else
history_file = std::string{t} + "/" + file;
// linenoiseHistorySetMaxLen(500);
// linenoiseSetCompletionCallback(completion);
// linenoiseSetHintsCallback(hints);
linenoiseSetMultiLine(1);
linenoiseHistoryLoad(history_file.c_str());
while (true) {
std::cout << ">>> ";
std::getline(std::cin, input);
// std::cout << ">>> ";
// std::getline(std::cin, input);
line = linenoise(">>> ");
linenoiseHistoryAdd(line);
input = std::string(line);
if (input == "!quit" || input == "!q")
break;
else if (input == "!env" || input == "!e")
@@ -1638,6 +1679,8 @@ void repl(MlEnvironment &env) {
}
}
}
linenoiseHistorySave(history_file.c_str());
}
void load_std_lib(MlEnvironment &env) {
@@ -1773,16 +1816,6 @@ MlValue MlEnvironment::get(const std::string &name) const {
throw MlError(MlValue::atom(name), *this, ATOM_NOT_DEFINED);
}
std::vector<std::string> getCmdOption(char *argv[], int argc, const std::string &option) {
std::vector<std::string> tokens;
for (int i = 1; i < argc; ++i) {
if (option == argv[i] && i + 1 < argc) {
i++;
tokens.push_back(std::string(argv[i]));
}
}
return tokens;
}
bool cmdOptionExists(char **begin, char **end, const std::string &option) { return std::find(begin, end, option) != end; }
@@ -1811,7 +1844,7 @@ int main(int argc, char *argv[]) {
return 0;
}
if (argc == 1 || (argc == 2 && std::string(argv[1]) == "-i"))
if (argc == 1 || (argc == 2 && std::string(argv[1]) == "-i"))
repl(env);
else if (argc == 3 && std::string(argv[1]) == "-c")
run(argv[2], env);