cpp-peglib/language/main.cc

78 lines
1.7 KiB
C++
Raw Normal View History

2015-05-28 23:19:46 +00:00
#include "repl.hpp"
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
bool read_file(const char* path, vector<char>& buff)
{
ifstream ifs(path, ios::in|ios::binary);
if (ifs.fail()) {
return false;
}
auto size = static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg());
if (size > 0) {
buff.resize(size);
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
}
return true;
}
int main(int argc, const char** argv)
{
auto print_ast = false;
auto shell = false;
vector<const char*> path_list;
int argi = 1;
while (argi < argc) {
auto arg = argv[argi++];
if (string("--shell") == arg) {
shell = true;
} else if (string("--ast") == arg) {
print_ast = true;
} else {
path_list.push_back(arg);
}
}
if (!shell) {
shell = path_list.empty();
}
2015-05-29 01:36:29 +00:00
try {
auto env = make_shared<Environment>();
env->setup_built_in_functions();
2015-05-28 23:19:46 +00:00
2015-05-29 01:36:29 +00:00
for (auto path: path_list) {
vector<char> buff;
if (!read_file(path, buff)) {
cerr << "can't open '" << path << "'." << endl;
return -1;
}
2015-05-28 23:19:46 +00:00
2015-05-29 01:36:29 +00:00
Value val;
string msg;
if (!run(path, env, buff.data(), buff.size(), val, msg, print_ast)) {
2015-06-03 21:36:56 +00:00
cerr << msg << endl;
2015-05-29 01:36:29 +00:00
return -1;
}
2015-05-28 23:19:46 +00:00
}
2015-05-29 01:36:29 +00:00
if (shell) {
repl(env, print_ast);
}
} catch (exception& e) {
cerr << e.what() << endl;
return -1;
2015-05-28 23:19:46 +00:00
}
return 0;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix