Compare commits

...

2 Commits

Author SHA1 Message Date
vaclavt
9966c961d1 autocompletion update 2022-03-01 20:29:18 +01:00
vaclavt
830361050c doc update 2022-03-01 20:28:36 +01:00
6 changed files with 25 additions and 33 deletions

View File

@@ -70,7 +70,6 @@ utils/local_install.sh
### TODO
- add test for >>> (range 1.5 (inc 5.5)) => (1.500000 2.500000 3.500000 4.500000 5.500000)
- ml_utils.cpp add all commands into const std::vector<std::string> commands
- unify -f and -run options
- add debug support, at least call stack
- multiline editing (see kilocpp editor)

View File

@@ -79,8 +79,8 @@
|`(reduce lambda acumulator list)`|Reduces list|`>>> (reduce (lambda (x y) (+ (* x 10) y)) 0 '(1 2 3 4)) => 1234`|List manipulation|
|`(exit code)`|Exit the program with an integer code||System|
|`(quit code)`|Same as (exit ..)||System|
|`(print ..)`|Print several values and return the last one||IO|
|`(random low high)`|Get a random number between two numbers inclusively||System|
|`(print ..)`|Print one or several values separated by space and return the last one|`>>> (print "pi" "is" 3.14)\npi is 3.140000 => 3.140000`|IO|
|`(random low high)`|Get a random number between two numbers inclusively|`>>> (random 1 6) => 5`|System|
|`(include file)`|Read a file and execute its code||IO|
|`(input [prompt])`|Get user input with an optional prompt||IO|
|`(read)`|Reads in the printed representation of a Lisp object from input-stream, builds a corresponding Lisp object, and returns the object||IO|

12
ml.cpp
View File

@@ -2287,14 +2287,12 @@ MlValue MlEnvironment::get(const std::string &name) const {
}
// Get vector of executables in this scope
std::vector<std::string> MlEnvironment::get_lambdas_list() const {
std::vector<std::string> lambdas{128};
std::vector<std::string> MlEnvironment::get_lambdas_list(const std::string &token) const {
std::vector<std::string> lambdas{};
for (auto it = defs.begin(); it != defs.end(); it++) {
if (it->second.get_type_name() == FUNCTION_TYPE) {
for (auto it = defs.begin(); it != defs.end(); it++)
if (it->second.get_type_name() == FUNCTION_TYPE && (token.empty() || it->first.find(token) == 0))
lambdas.push_back(it->first);
}
}
return lambdas;
}
@@ -2306,7 +2304,7 @@ void repl(MlEnvironment &env) {
MlValue tmp;
std::vector<MlValue> parsed;
setup_linenoise(env);
setup_linenoise(env, builtin_funcs);
while (true) {
char *line = linenoise(">>> ");

2
ml.h
View File

@@ -50,7 +50,7 @@ public:
void setX(const std::string &name, const MlValue& value);
// Get vector of executables in this scope
std::vector<std::string> get_lambdas_list() const;
std::vector<std::string> get_lambdas_list(const std::string &token) const;
void combine(MlEnvironment const &other);

View File

@@ -3,19 +3,11 @@
#include <string>
#include <vector>
#include <algorithm>
#include <filesystem>
#include <iostream>
const std::vector<std::string> commands {
"eval", "type", "parse", "do", "if", "for", "while", "scope", "quote", "defn",
"def", "lambda", "benchmark", "=", "!=", ">", "<", ">=", "<=", "+", "-", "*", "/", "%",
"list", "insert", "index", "remove", "len", "push", "pop", "head", "tail", "first", "last",
"range", "map", "filter", "reduce", "exit", "quit", "print", "input", "random", "include",
"read-file", "write-file", "read-url", "system-cmd", "ls-dir", "is-file?", "is-dir?",
"parse-csv", "parse-json", "get-universal-time", "date-to-str", "str-to-date", "date-add", "debug",
"sprintf", "display", "string-replace", "string-regex?", "string-pad", "int", "float", "string",
"benchmark", "thread-create", "thread-under-lock", "thread-sleep", "threads-join", "try", "throw",
"usql"
};
std::vector<std::string> commands {};
std::string get_history_file_dir() {
std::string filename{".ml_history.txt"};
@@ -27,7 +19,7 @@ std::string get_history_file_dir() {
MlEnvironment * repl_env = nullptr;
void setup_linenoise(const MlEnvironment &env) {
void setup_linenoise(const MlEnvironment &env, const std::map<const std::string, Builtin> &builtins) {
repl_env = (MlEnvironment*) &env;
std::string history_file = get_history_file_dir();
@@ -37,6 +29,10 @@ void setup_linenoise(const MlEnvironment &env) {
linenoiseSetHintsCallback(hints);
linenoiseSetMultiLine(1);
linenoiseHistoryLoad(history_file.c_str());
commands.reserve(builtins.size());
for(auto it = builtins.begin(); it != builtins.end(); ++it)
commands.push_back(it->first);
}
void linenoise_line_read(char *line) {
@@ -68,15 +64,14 @@ void completion(const char *buf, linenoiseCompletions *lc) {
std::string token = str.substr(pos+1);
std::string begining = str.substr(0, pos+1);
// PERF optimize not to get all lambdas, but those beginning with token
std::vector<std::string> lambdas = repl_env->get_lambdas_list();
lambdas.insert(end(lambdas), begin(commands), end(commands));
auto suggestions = repl_env->get_lambdas_list(token);
std::copy_if(commands.begin(), commands.end(), std::back_inserter(suggestions),
[&token] (const std::string &cmd) { return token.empty() || cmd.find(token)==0; });
for (const auto & lambda : lambdas) {
if(lambda.find(token) == 0) {
std::string completion_string = begining + lambda;
linenoiseAddCompletion(lc, completion_string.c_str());
}
std::sort(suggestions.begin(), suggestions.end());
for (const auto &suggestion : suggestions) {
std::string completion = begining + suggestion;
linenoiseAddCompletion(lc, completion.c_str());
}
}
}

View File

@@ -4,7 +4,7 @@
#include "linenoise.h"
void setup_linenoise(const MlEnvironment &env);
void setup_linenoise(const MlEnvironment &env, const std::map<const std::string, Builtin> &builtins);
void linenoise_line_read(char *line);
void close_linenoise();