83 lines
2.0 KiB
C++
83 lines
2.0 KiB
C++
#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;
|
|
}
|