2015-07-25 02:28:35 +00:00
|
|
|
#include "culebra.h"
|
2015-07-25 02:18:03 +00:00
|
|
|
#include "linenoise.hpp"
|
2015-05-28 23:19:46 +00:00
|
|
|
#include <fstream>
|
2015-07-28 21:30:41 +00:00
|
|
|
#include <iomanip>
|
2015-05-28 23:19:46 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-07-25 02:23:40 +00:00
|
|
|
using namespace culebra;
|
2015-08-10 20:37:56 +00:00
|
|
|
using namespace peg;
|
2015-05-28 23:19:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-28 10:47:18 +00:00
|
|
|
struct CommandLineDebugger
|
|
|
|
{
|
2015-07-29 22:37:42 +00:00
|
|
|
void operator()(const Ast& ast, Environment& env, bool force_to_break) {
|
2015-07-29 21:58:20 +00:00
|
|
|
if (quit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-29 22:37:42 +00:00
|
|
|
if ((command_ == "n" && env.level <= level_) ||
|
2015-07-29 21:58:20 +00:00
|
|
|
(command_ == "s") ||
|
2015-07-29 22:37:42 +00:00
|
|
|
(command_ == "o" && env.level < level_)) {
|
2015-07-28 10:47:18 +00:00
|
|
|
force_to_break = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (force_to_break) {
|
2015-08-04 22:02:42 +00:00
|
|
|
static auto show_initial_usage = true;
|
|
|
|
if (show_initial_usage) {
|
|
|
|
show_initial_usage = false;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2015-07-28 21:30:41 +00:00
|
|
|
show_lines(ast);
|
|
|
|
|
2015-07-28 10:47:18 +00:00
|
|
|
for (;;) {
|
2015-08-04 22:02:42 +00:00
|
|
|
cout << endl << "debug> ";
|
|
|
|
|
2015-07-29 21:58:20 +00:00
|
|
|
string s;
|
|
|
|
std::getline(cin, s);
|
|
|
|
|
|
|
|
istringstream is(s);
|
|
|
|
is >> command_;
|
|
|
|
|
|
|
|
if (command_ == "h") {
|
2015-08-04 22:02:42 +00:00
|
|
|
usage();
|
2015-07-29 21:58:20 +00:00
|
|
|
} else if (command_ == "l") {
|
|
|
|
is >> display_lines_;
|
|
|
|
show_lines(ast);
|
|
|
|
} else if (command_ == "p") {
|
|
|
|
string symbol;
|
|
|
|
is >> symbol;
|
|
|
|
print(ast, env, symbol);
|
|
|
|
} else if (command_ == "c") {
|
2015-07-28 10:47:18 +00:00
|
|
|
break;
|
2015-07-29 21:58:20 +00:00
|
|
|
} else if (command_ == "n") {
|
2015-07-28 10:47:18 +00:00
|
|
|
break;
|
2015-07-29 21:58:20 +00:00
|
|
|
} else if (command_ == "s") {
|
2015-07-28 10:47:18 +00:00
|
|
|
break;
|
2015-07-29 21:58:20 +00:00
|
|
|
} else if (command_ == "o") {
|
|
|
|
break;
|
|
|
|
} else if (command_ == "q") {
|
|
|
|
quit = true;
|
2015-07-28 10:47:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 22:37:42 +00:00
|
|
|
level_ = env.level;;
|
2015-07-28 10:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:58:20 +00:00
|
|
|
void show_lines(const Ast& ast) {
|
|
|
|
prepare_cache(ast.path);
|
|
|
|
|
2015-08-04 22:02:42 +00:00
|
|
|
cout << endl << "Break in " << ast.path << ":" << ast.line << endl;
|
2015-07-29 21:58:20 +00:00
|
|
|
|
2015-07-28 21:30:41 +00:00
|
|
|
auto count = get_line_count(ast.path);
|
2015-07-29 21:58:20 +00:00
|
|
|
|
|
|
|
auto lines_ahead = (size_t)((display_lines_ - .5) / 2);
|
|
|
|
auto start = (size_t)max((int)ast.line - (int)lines_ahead, 1);
|
|
|
|
auto end = min(start + display_lines_, count);
|
|
|
|
|
|
|
|
auto needed_digits = to_string(count).length();
|
|
|
|
|
2015-07-28 21:30:41 +00:00
|
|
|
for (auto l = start; l < end; l++) {
|
|
|
|
auto s = get_line(ast.path, l);
|
|
|
|
if (l == ast.line) {
|
|
|
|
cout << "> ";
|
|
|
|
} else {
|
|
|
|
cout << " ";
|
|
|
|
}
|
2015-07-29 21:58:20 +00:00
|
|
|
cout << setw(needed_digits) << l << " " << s << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<Ast> find_function_node(const Ast& ast) {
|
2015-08-04 17:04:58 +00:00
|
|
|
auto node = ast.parent;
|
|
|
|
while (node->parent && node->tag != "FUNCTION"_) {
|
|
|
|
node = node->parent;
|
2015-07-29 21:58:20 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enum_identifiers(const Ast& ast, set<string>& references) {
|
|
|
|
for (auto node: ast.nodes) {
|
|
|
|
switch (node->tag) {
|
|
|
|
case "IDENTIFIER"_:
|
|
|
|
references.insert(node->token);
|
|
|
|
break;
|
|
|
|
case "FUNCTION"_:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
enum_identifiers(*node, references);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 22:37:42 +00:00
|
|
|
void print(const Ast& ast, Environment& env, const string& symbol) {
|
2015-07-29 21:58:20 +00:00
|
|
|
if (symbol.empty()) {
|
|
|
|
print_all(ast, env);
|
2015-07-29 22:37:42 +00:00
|
|
|
} else if (env.has(symbol)) {
|
|
|
|
cout << symbol << ": " << env.get(symbol).str() << endl;
|
2015-07-29 21:58:20 +00:00
|
|
|
} else {
|
|
|
|
cout << "'" << symbol << "'" << "is not undefined." << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 22:37:42 +00:00
|
|
|
void print_all(const Ast& ast, Environment& env) {
|
2015-07-29 21:58:20 +00:00
|
|
|
auto node = find_function_node(ast);
|
|
|
|
set<string> references;
|
|
|
|
enum_identifiers(*node, references);
|
|
|
|
for (const auto& symbol: references) {
|
2015-08-04 17:04:58 +00:00
|
|
|
if (env.has(symbol)) {
|
|
|
|
const auto& val = env.get(symbol);
|
|
|
|
if (val.type != Value::Function) {
|
|
|
|
cout << symbol << ": " << val.str() << endl;
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 21:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_line_count(const string& path) {
|
2015-07-29 21:58:20 +00:00
|
|
|
return sources_[path].size();
|
2015-07-28 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string get_line(const string& path, size_t line) {
|
2015-07-29 21:58:20 +00:00
|
|
|
const auto& positions = sources_[path];
|
2015-07-28 21:30:41 +00:00
|
|
|
auto idx = line - 1;
|
|
|
|
auto first = idx > 0 ? positions[idx - 1] : 0;
|
|
|
|
auto last = positions[idx];
|
|
|
|
auto size = last - first;
|
|
|
|
|
|
|
|
string s(size, 0);
|
|
|
|
ifstream ifs(path, ios::in | ios::binary);
|
|
|
|
ifs.seekg(first, ios::beg).read((char*)s.data(), static_cast<streamsize>(s.size()));
|
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
auto rit = s.rbegin();
|
|
|
|
while (rit != s.rend()) {
|
|
|
|
if (*rit == '\n') {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
++rit;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s.substr(0, s.size() - count);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void prepare_cache(const string& path) {
|
2015-07-29 21:58:20 +00:00
|
|
|
auto it = sources_.find(path);
|
|
|
|
if (it == sources_.end()) {
|
2015-07-28 21:30:41 +00:00
|
|
|
vector<char> buff;
|
|
|
|
read_file(path.c_str(), buff);
|
|
|
|
|
2015-07-29 21:58:20 +00:00
|
|
|
auto& positions = sources_[path];
|
2015-07-28 21:30:41 +00:00
|
|
|
|
|
|
|
auto i = 0u;
|
|
|
|
for (; i < buff.size(); i++) {
|
|
|
|
if (buff[i] == '\n') {
|
|
|
|
positions.push_back(i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
positions.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 22:02:42 +00:00
|
|
|
void usage() {
|
|
|
|
cout << "Usage: (c)ontinue, (n)ext, (s)tep in, step (o)out, (p)ring, (l)ist, (q)uit" << endl;
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:58:20 +00:00
|
|
|
bool quit = false;
|
|
|
|
string command_;
|
|
|
|
size_t level_ = 0;
|
|
|
|
size_t display_lines_ = 4;
|
|
|
|
map<string, vector<size_t>> sources_;
|
2015-07-28 10:47:18 +00:00
|
|
|
};
|
|
|
|
|
2015-07-25 02:18:03 +00:00
|
|
|
int repl(shared_ptr<Environment> env, bool print_ast)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
auto line = linenoise::Readline("cul> ");
|
|
|
|
|
|
|
|
if (line == "exit" || line == "quit") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!line.empty()) {
|
2015-07-29 21:58:20 +00:00
|
|
|
vector<string> msgs;
|
2015-09-16 15:59:19 +00:00
|
|
|
auto ast = parse("(repl)", line.data(), line.size(), msgs);
|
|
|
|
if (ast) {
|
2015-07-28 10:47:18 +00:00
|
|
|
if (print_ast) {
|
2015-08-28 02:26:34 +00:00
|
|
|
peg::print_ast(ast);
|
2015-07-28 10:47:18 +00:00
|
|
|
}
|
2015-09-16 15:59:19 +00:00
|
|
|
|
|
|
|
Value val;
|
|
|
|
if (interpret(ast, env, val, msgs)) {
|
|
|
|
cout << val << endl;
|
|
|
|
linenoise::AddHistory(line.c_str());
|
|
|
|
continue;
|
2015-07-28 10:47:18 +00:00
|
|
|
}
|
2015-07-25 02:18:03 +00:00
|
|
|
}
|
2015-09-16 15:59:19 +00:00
|
|
|
|
|
|
|
for (const auto& msg : msgs) {
|
|
|
|
cout << msg << endl;;
|
|
|
|
}
|
2015-07-25 02:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-28 23:19:46 +00:00
|
|
|
int main(int argc, const char** argv)
|
|
|
|
{
|
|
|
|
auto print_ast = false;
|
|
|
|
auto shell = false;
|
2015-07-28 10:47:18 +00:00
|
|
|
auto debug = false;
|
2015-05-28 23:19:46 +00:00
|
|
|
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;
|
2015-07-28 10:47:18 +00:00
|
|
|
} else if (string("--debug") == arg) {
|
|
|
|
debug = true;
|
2015-05-28 23:19:46 +00:00
|
|
|
} else {
|
|
|
|
path_list.push_back(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shell) {
|
|
|
|
shell = path_list.empty();
|
|
|
|
}
|
|
|
|
|
2015-05-29 01:36:29 +00:00
|
|
|
try {
|
2015-06-01 20:19:30 +00:00
|
|
|
auto env = make_shared<Environment>();
|
2015-07-23 01:14:55 +00:00
|
|
|
setup_built_in_functions(*env);
|
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-07-29 21:58:20 +00:00
|
|
|
vector<string> msgs;
|
2015-09-16 15:59:19 +00:00
|
|
|
auto ast = parse(path, buff.data(), buff.size(), msgs);
|
|
|
|
if (ast) {
|
|
|
|
if (print_ast) {
|
|
|
|
peg::print_ast(ast);
|
|
|
|
}
|
2015-07-28 10:47:18 +00:00
|
|
|
|
2015-09-16 15:59:19 +00:00
|
|
|
Value val;
|
|
|
|
auto dbg = debug ? CommandLineDebugger() : Debugger();
|
|
|
|
if (interpret(ast, env, val, msgs, dbg)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-28 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:59:19 +00:00
|
|
|
for (const auto& msg : msgs) {
|
|
|
|
cerr << msg << endl;
|
2015-05-29 01:36:29 +00:00
|
|
|
}
|
2015-09-16 15:59:19 +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
|