Merge branch 'master' of https://bitbucket.org/vaclavt/mlisp into HEAD
# Conflicts: # Readme.md # ml.cpp
This commit is contained in:
commit
dc61c74992
|
|
@ -22,6 +22,7 @@ project(ml)
|
||||||
set(PROJECT_NAME ml)
|
set(PROJECT_NAME ml)
|
||||||
|
|
||||||
set(SOURCE
|
set(SOURCE
|
||||||
|
main.cpp
|
||||||
ml.cpp
|
ml.cpp
|
||||||
ml_io.cpp
|
ml_io.cpp
|
||||||
ml_date.cpp
|
ml_date.cpp
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,3 @@
|
||||||
(term-blue (sprintf "%.2f" (list 1.11))) " "
|
(term-blue (sprintf "%.2f" (list 1.11))) " "
|
||||||
(term-yellow (sprintf "%.2f" (list 1.11))) " "
|
(term-yellow (sprintf "%.2f" (list 1.11))) " "
|
||||||
))
|
))
|
||||||
|
|
||||||
;; (define q_change_str (sprintf "%+.2f %%" (list q)))
|
|
||||||
;; (if (>= q_change 0.0)
|
|
||||||
;; (define q_change_str (term-green q_change_str))
|
|
||||||
;; (define q_change_str (term-red q_change_str)))
|
|
||||||
|
|
||||||
|
|
|
||||||
32
doc/Doc.md
32
doc/Doc.md
|
|
@ -43,27 +43,27 @@
|
||||||
|`(member list item)`|Returns true (1) when list contains item||
|
|`(member list item)`|Returns true (1) when list contains item||
|
||||||
|`(uniq list)`|Returns list with removed duplicates ||
|
|`(uniq list)`|Returns list with removed duplicates ||
|
||||||
|`(make-list len)`|Return list with len nil elements|`(make-list 3) => (nil nil nil)`|
|
|`(make-list len)`|Return list with len nil elements|`(make-list 3) => (nil nil nil)`|
|
||||||
|`(make-list-of len value)`|||
|
|`(make-list-of len value)`|Return list with len value elements||
|
||||||
|`(map ..)`||`(map (lambda (x) (+ x 10)) '(1 2 3 4 5 6)) => (11 12 13 14 15 16)`|
|
|`(map ..)`||`(map (lambda (x) (+ x 10)) '(1 2 3 4 5 6)) => (11 12 13 14 15 16)`|
|
||||||
|`(filter lambda list)`||`(filter (lambda (x) (> x 2)) '(1 2 3 4 5)) => (3 4 5)`|
|
|`(filter lambda list)`||`(filter (lambda (x) (> x 2)) '(1 2 3 4 5)) => (3 4 5)`|
|
||||||
|`(reduce lambda acumulator list)`||`>>> (reduce (lambda (x y) (+ (* x 10) y)) 0 '(1 2 3 4)) => 1234`|
|
|`(reduce lambda acumulator list)`||`>>> (reduce (lambda (x y) (+ (* x 10) y)) 0 '(1 2 3 4)) => 1234`|
|
||||||
|`(exit ..)`|||
|
|`(exit code)`|Exit the program with an integer code||
|
||||||
|`(quit ..)`|||
|
|`(quit code)`|Same as (exit ..)||
|
||||||
|`(print ..)`|||
|
|`(print ..)`|Print several values and return the last one||
|
||||||
|`(input ..)`|||
|
|`(input [prompt])`|Get user input with an optional prompt||
|
||||||
|`(random ..)`|||
|
|`(random low high)`|Get a random number between two numbers inclusively||
|
||||||
|`(include ..)`|||
|
|`(include file)`|Read a file and execute its code||
|
||||||
|`(read-file ..)`|||
|
|`(read-file filename)`|Get the contents of a file||
|
||||||
|`(write-file ..)`|||
|
|`(write-file filename)`|Write a string to a file||
|
||||||
|`(read-url ..)`|||
|
|`(read-url url [headers])`|Reads URL|Returnd list (status_code content)|
|
||||||
|`(system-cmd ..)`|||
|
|`(system-cmd command_str)`|Execute system command||
|
||||||
|`(ls-dir ..)`|||
|
|`(ls-dir ..)`|List a dir|List of directory entries|
|
||||||
|`(is-file? ..)`|Returns true if passed filename is a file||
|
|`(is-file? ..)`|Returns true if passed filename is a file||
|
||||||
|`(is-dir? ..)`|Returns true if passed filename is a directory||
|
|`(is-dir? ..)`|Returns true if passed filename is a directory||
|
||||||
|`(parse-csv ..)`|||
|
|`(parse-csv string)`|Parse CSV string||
|
||||||
|`(parse-json ..)`|||
|
|`(parse-json json_string)`|Parse JSON string||
|
||||||
|`(save-csv ..)`|||
|
|`(save-csv ..)`|||
|
||||||
|`(get-universal-time ..)`|||
|
|`(get-universal-time)`|Get current time as secs from epoch||
|
||||||
|`(date-to-str ..)`|||
|
|`(date-to-str ..)`|||
|
||||||
|`(str-to-date ..)`|||
|
|`(str-to-date ..)`|||
|
||||||
|`(date-add ..)`|||
|
|`(date-add ..)`|||
|
||||||
|
|
@ -77,4 +77,6 @@
|
||||||
|`(eval ..)`|||
|
|`(eval ..)`|||
|
||||||
|`(type ..)`|||
|
|`(type ..)`|||
|
||||||
|`(parse ..)`|||
|
|`(parse ..)`|||
|
||||||
|
|`(make-list size)`|||
|
||||||
|
|`(make-list-of size value)`|||
|
||||||
|`(xx ..)`|||
|
|`(xx ..)`|||
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,82 @@
|
||||||
|
#include "ml.h"
|
||||||
|
#include "ml_io.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
MlValue run(const std::string &code, MlEnvironment &env);
|
||||||
|
MlValue repl(MlEnvironment &env);
|
||||||
|
|
||||||
|
|
||||||
|
void load_std_lib(MlEnvironment &env) {
|
||||||
|
std::string loader =
|
||||||
|
R"( (define ___lib_path '("/usr/local/var/mlisp"))
|
||||||
|
(for d ___lib_path
|
||||||
|
(if (is-dir? d)
|
||||||
|
(for f (ls-dir d)
|
||||||
|
(if (regex-search? f "^.*\.l(i)?sp$")
|
||||||
|
(include (+ d "/" f))
|
||||||
|
'())
|
||||||
|
)
|
||||||
|
'()))
|
||||||
|
)";
|
||||||
|
|
||||||
|
run(loader, env);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool cmdOptionExists(char **begin, char **end, const std::string &option) { return std::find(begin, end, option) != end; }
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
MlEnvironment env;
|
||||||
|
std::vector<MlValue> args;
|
||||||
|
for (int i = 0; i < argc; i++)
|
||||||
|
args.push_back(MlValue::string(argv[i]));
|
||||||
|
env.set("cmd-args", MlValue(args));
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
try {
|
||||||
|
load_std_lib(env);
|
||||||
|
// for xcode profiling
|
||||||
|
// run(read_file_contents("/Users/vaclavt/Development/mlisp/tests/test.lsp"), env);
|
||||||
|
|
||||||
|
// help
|
||||||
|
if (cmdOptionExists(argv, argv + argc, "-h")) {
|
||||||
|
std::cout << "Usage:\n\t-h print this help\n\t-f source_file - executes code in file\n\t-c code - runs passed code\n\t-i runs repl\n\t-v prints version string\n\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// version
|
||||||
|
if (cmdOptionExists(argv, argv + argc, "-v")) {
|
||||||
|
std::cout << VERSION << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
else if (argc == 3 && std::string(argv[1]) == "-f")
|
||||||
|
run(read_file_contents(argv[2]), env);
|
||||||
|
else std::cerr << "invalid arguments" << std::endl;
|
||||||
|
} catch (MlError &e) {
|
||||||
|
std::cerr << e.description() << std::endl;
|
||||||
|
} catch (std::runtime_error &e) {
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue