From dd8502231dbba1f323e6c22cb4117fab5799890e Mon Sep 17 00:00:00 2001 From: VaclavT Date: Wed, 10 Mar 2021 23:52:55 +0100 Subject: [PATCH] main.cpp introduced --- CMakeLists.txt | 1 + Readme.md | 2 +- main.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ ml.cpp | 71 +------------------------------------------ ml.h | 2 +- 5 files changed, 86 insertions(+), 72 deletions(-) create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1123fc8..cd6a7bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ project(ml) set(PROJECT_NAME ml) set(SOURCE + main.cpp ml.cpp ml_io.cpp ml_date.cpp diff --git a/Readme.md b/Readme.md index 160eb26..3bd9a2a 100644 --- a/Readme.md +++ b/Readme.md @@ -53,7 +53,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 main.cpp ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp ``` or cmake diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..773c0c7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,82 @@ +#include "ml.h" +#include "ml_io.h" + +#include + + +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 getCmdOption(char *argv[], int argc, const std::string &option) { + std::vector 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 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; +} diff --git a/ml.cpp b/ml.cpp index 8797ed5..91273db 100644 --- a/ml.cpp +++ b/ml.cpp @@ -1609,6 +1609,7 @@ namespace builtin { } } + void repl(MlEnvironment &env) { std::string code; std::string input; @@ -1640,22 +1641,6 @@ void 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); -} - // Does this environment, or its parent environment, have a variable? bool MlEnvironment::has(std::string name) const { // Find the value in the map @@ -1772,57 +1757,3 @@ MlValue MlEnvironment::get(const std::string &name) const { throw MlError(MlValue::atom(name), *this, ATOM_NOT_DEFINED); } - -std::vector getCmdOption(char *argv[], int argc, const std::string &option) { - std::vector 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; } - -int main(int argc, char *argv[]) { - MlEnvironment env; - std::vector 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; -} diff --git a/ml.h b/ml.h index 4910d97..e827d49 100644 --- a/ml.h +++ b/ml.h @@ -214,4 +214,4 @@ private: std::string str; std::vector list; MlEnvironment lambda_scope; -}; \ No newline at end of file +};