main.cpp introduced

This commit is contained in:
VaclavT 2021-03-10 23:52:55 +01:00
parent 55d39ef321
commit dd8502231d
5 changed files with 86 additions and 72 deletions

View File

@ -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

View File

@ -53,7 +53,7 @@ cp stdlib/*.lsp /usr/local/var/mlisp/
#### Compile #### 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 or
cmake cmake

82
main.cpp Normal file
View File

@ -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;
}

71
ml.cpp
View File

@ -1609,6 +1609,7 @@ namespace builtin {
} }
} }
void repl(MlEnvironment &env) { void repl(MlEnvironment &env) {
std::string code; std::string code;
std::string input; 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? // Does this environment, or its parent environment, have a variable?
bool MlEnvironment::has(std::string name) const { bool MlEnvironment::has(std::string name) const {
// Find the value in the map // 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); 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; }
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;
}

2
ml.h
View File

@ -214,4 +214,4 @@ private:
std::string str; std::string str;
std::vector<MlValue> list; std::vector<MlValue> list;
MlEnvironment lambda_scope; MlEnvironment lambda_scope;
}; };