restore back before introducing main.cpp

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

View File

@ -29,7 +29,8 @@ set(SOURCE
clib/csvparser.cpp
clib/sslclient.cpp
clib/json11.cpp
clib/printf.cpp)
clib/printf.cpp
clib/linenoise.c)
add_executable(${PROJECT_NAME} ${SOURCE})

View File

@ -4,6 +4,7 @@
### TODO
- support for (), nil, t, in lsp code replace 0 by nil in logicals
- casting to string (like ti int and to float)
- some performance functionality (at least counting how many times symbol was evaluated)
- documentation
- add url of source/inspiration to clib/*.cpp
@ -40,6 +41,7 @@
- format (sprintf)
- setq
- mapcar (funcall, apply)
- syntax highlighting do VS Code
#### Performance
- push_back - repeatedly without reserving size
@ -53,7 +55,7 @@ cp stdlib/*.lsp /usr/local/var/mlisp/
#### Compile
```
gcc -o ml -I/usr/local/opt/openssl/include -Iclib -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl -Wl,-stack_size -Wl,0x1000000 --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp
gcc -o ml -I/usr/local/opt/openssl/include -Iclib -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl -Wl,-stack_size -Wl,0x1000000 --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp clib/linenoise.c
```
or
cmake

1225
clib/linenoise.c Normal file

File diff suppressed because it is too large Load Diff

75
clib/linenoise.h Normal file
View File

@ -0,0 +1,75 @@
/* linenoise.h -- VERSION 1.0
*
* Guerrilla line editing library against the idea that a line editing lib
* needs to be 20,000 lines of C code.
*
* See linenoise.c for more information.
*
* ------------------------------------------------------------------------
*
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __LINENOISE_H
#define __LINENOISE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
typedef void(linenoiseFreeHintsCallback)(void *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */

View File

@ -67,7 +67,7 @@ std::string mini_sprintf(const std::string &format_str, const std::vector<MlValu
for (si = format_str.begin(); si != format_str.end(); ++si) {
c = *si;
// formating directives
// formatting directives
if (c == '%') {
if (++si >= format_str.end()) {
return output_str; // end of string, invalid % on last pos

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);