libcubescript/tools/repl.cc

388 lines
9.9 KiB
C++
Raw Permalink Normal View History

#include <signal.h>
2017-01-30 19:38:11 +01:00
#include <optional>
#include <ostd/platform.hh>
#include <ostd/io.hh>
#include <ostd/string.hh>
2016-09-07 22:57:28 +02:00
#include <cubescript/cubescript.hh>
using namespace cscript;
2017-02-16 19:07:22 +01:00
ostd::string_range version = "CubeScript 0.0.1";
/* util */
#ifdef OSTD_PLATFORM_WIN32
#include <io.h>
static bool stdin_is_tty() {
return _isatty(_fileno(stdin));
}
#else
#include <unistd.h>
static bool stdin_is_tty() {
return isatty(0);
}
#endif
/* line editing support */
2017-06-19 20:13:54 +02:00
inline ostd::string_range get_complete_cmd(ostd::string_range buf) {
2017-02-16 19:07:22 +01:00
ostd::string_range not_allowed = "\"/;()[] \t\r\n\0";
ostd::string_range found = ostd::find_one_of(buf, not_allowed);
while (!found.empty()) {
++found;
buf = found;
found = ostd::find_one_of(found, not_allowed);
}
return buf;
}
2017-06-19 20:13:54 +02:00
inline ostd::string_range get_arg_type(char arg) {
switch (arg) {
case 'i':
return "int";
case 'b':
return "int_min";
case 'f':
return "float";
case 'F':
return "float_prev";
case 't':
return "any";
case 'T':
return "any_m";
case 'E':
return "cond";
case 'N':
return "numargs";
case 'S':
return "str_m";
case 's':
return "str";
case 'e':
return "block";
case 'r':
return "ident";
case '$':
return "self";
}
return "illegal";
}
2017-06-19 20:13:54 +02:00
inline void fill_cmd_args(std::string &writer, ostd::string_range args) {
char variadic = '\0';
int nrep = 0;
if (!args.empty() && ((args.back() == 'V') || (args.back() == 'C'))) {
variadic = args.back();
args.pop_back();
if (!args.empty() && isdigit(args.back())) {
nrep = args.back() - '0';
args.pop_back();
}
}
if (args.empty()) {
if (variadic == 'C') {
writer += "concat(...)";
} else if (variadic == 'V') {
writer += "...";
}
return;
}
int norep = int(args.size()) - nrep;
if (norep > 0) {
for (int i = 0; i < norep; ++i) {
if (i != 0) {
writer += ", ";
}
writer += get_arg_type(*args);
++args;
}
}
if (variadic) {
if (norep > 0) {
writer += ", ";
}
if (variadic == 'C') {
writer += "concat(";
}
if (!args.empty()) {
if (args.size() > 1) {
writer += '{';
}
2017-01-30 19:38:11 +01:00
for (std::size_t i = 0; i < args.size(); ++i) {
if (i) {
writer += ", ";
}
writer += get_arg_type(args[i]);
}
if (args.size() > 1) {
writer += '}';
}
}
writer += "...";
if (variadic == 'C') {
writer += ")";
}
}
}
2017-06-19 20:13:54 +02:00
inline cs_command *get_hint_cmd(cs_state &cs, ostd::string_range buf) {
2017-02-16 19:07:22 +01:00
ostd::string_range nextchars = "([;";
auto lp = ostd::find_one_of(buf, nextchars);
if (!lp.empty()) {
2017-04-01 01:03:22 +02:00
cs_command *cmd = get_hint_cmd(cs, buf.slice(1, buf.size()));
if (cmd) {
return cmd;
}
}
while (!buf.empty() && isspace(buf.front())) {
++buf;
}
2017-02-16 19:07:22 +01:00
ostd::string_range spaces = " \t\r\n";
ostd::string_range s = ostd::find_one_of(buf, spaces);
if (!s.empty()) {
2017-03-31 03:18:26 +02:00
buf = buf.slice(0, &s[0] - &buf[0]);
}
if (!buf.empty()) {
2016-09-11 23:33:02 +02:00
auto cmd = cs.get_ident(buf);
return cmd ? cmd->get_command() : nullptr;
}
return nullptr;
}
2016-09-07 22:57:28 +02:00
#include "edit_linenoise.hh"
#include "edit_readline.hh"
#include "edit_fallback.hh"
2016-09-07 22:46:22 +02:00
/* usage */
2017-02-16 19:07:22 +01:00
void print_usage(ostd::string_range progname, bool err) {
2017-03-10 20:16:20 +01:00
auto &s = err ? ostd::cerr : ostd::cout;
2016-09-07 22:46:22 +02:00
s.writeln(
"Usage: ", progname, " [options] [file]\n"
"Options:\n"
" -e str run string \"str\"\n"
" -i enter interactive mode after the above\n"
" -v show version information\n"
" -h show this message\n"
" -- stop handling options\n"
" - execute stdin and stop handling options"
);
s.flush();
}
void print_version() {
ostd::writeln(version);
}
2017-02-13 18:10:40 +01:00
static cs_state *scs = nullptr;
static void do_sigint(int n) {
/* in case another SIGINT happens, terminate normally */
signal(n, SIG_DFL);
2017-02-13 18:10:40 +01:00
scs->set_call_hook([](cs_state &cs) {
2016-09-11 23:33:02 +02:00
cs.set_call_hook(nullptr);
2017-02-13 18:10:40 +01:00
throw cscript::cs_error(cs, "<execution interrupted>");
2016-09-11 23:33:02 +02:00
});
}
2017-02-16 19:07:22 +01:00
static bool do_call(cs_state &cs, ostd::string_range line, bool file = false) {
2017-02-13 18:10:40 +01:00
cs_value ret;
2016-09-11 23:33:02 +02:00
scs = &cs;
signal(SIGINT, do_sigint);
try {
2016-09-07 22:46:22 +02:00
if (file) {
if (!cs.run_file(line, ret)) {
2017-03-10 20:16:20 +01:00
ostd::cerr.writeln("cannot read file: ", line);
2016-09-07 22:46:22 +02:00
}
} else {
cs.run(line, ret);
}
2017-02-13 18:10:40 +01:00
} catch (cscript::cs_error const &e) {
signal(SIGINT, SIG_DFL);
2016-09-11 23:33:02 +02:00
scs = nullptr;
2017-02-16 19:07:22 +01:00
ostd::string_range terr = e.what();
2016-09-10 16:22:27 +02:00
auto col = ostd::find(terr, ':');
2016-09-15 20:55:58 +02:00
bool is_lnum = false;
2016-09-10 16:22:27 +02:00
if (!col.empty()) {
2016-09-15 20:55:58 +02:00
is_lnum = ostd::find_if(
2017-03-31 03:18:26 +02:00
terr.slice(0, &col[0] - &terr[0]),
[](auto c) { return !isdigit(c); }
2016-09-15 20:55:58 +02:00
).empty();
2017-04-01 01:03:22 +02:00
terr = col.slice(2, col.size());
2016-09-10 16:22:27 +02:00
}
if (!file && ((terr == "missing \"]\"") || (terr == "missing \")\""))) {
return true;
}
ostd::writeln(!is_lnum ? "stdin: " : "stdin:", e.what());
if (e.get_stack().get()) {
2017-03-10 20:16:20 +01:00
cscript::util::print_stack(ostd::cout.iter(), e.get_stack());
ostd::write('\n');
2016-09-10 20:45:04 +02:00
}
return false;
}
signal(SIGINT, SIG_DFL);
2016-09-11 23:33:02 +02:00
scs = nullptr;
2017-02-13 18:10:40 +01:00
if (ret.get_type() != cs_value_type::Null) {
ostd::writeln(ret.get_str());
}
return false;
}
2017-02-13 18:10:40 +01:00
static void do_tty(cs_state &cs) {
2016-09-02 19:01:25 +02:00
auto prompt = cs.new_svar("PROMPT", "> ");
auto prompt2 = cs.new_svar("PROMPT2", ">> ");
bool do_exit = false;
cs.new_command("quit", "", [&do_exit](auto &, auto, auto &) {
do_exit = true;
});
2016-09-07 22:46:22 +02:00
ostd::writeln(version, " (REPL mode)");
for (;;) {
2016-09-11 23:33:02 +02:00
auto line = read_line(cs, prompt);
if (!line) {
return;
}
2017-01-25 01:57:33 +01:00
auto lv = std::move(line.value());
if (lv.empty()) {
continue;
}
while ((lv.back() == '\\') || do_call(cs, lv)) {
bool bsl = (lv.back() == '\\');
if (bsl) {
lv.resize(lv.size() - 1);
}
2016-09-11 23:33:02 +02:00
auto line2 = read_line(cs, prompt2);
if (!line2) {
return;
}
2016-09-10 16:22:27 +02:00
if (!bsl || (line2.value() == "\\")) {
lv += '\n';
}
lv += line2.value();
}
2016-09-11 23:33:02 +02:00
add_history(cs, lv);
if (do_exit) {
return;
}
}
}
2016-09-07 22:46:22 +02:00
int main(int argc, char **argv) {
2017-02-13 18:10:40 +01:00
cs_state gcs;
2016-09-11 23:33:02 +02:00
gcs.init_libs();
2016-09-07 22:46:22 +02:00
2016-09-15 21:27:14 +02:00
gcs.new_command("exec", "s", [](auto &cs, auto args, auto &) {
2016-09-07 22:46:22 +02:00
auto file = args[0].get_strr();
bool ret = cs.run_file(file);
if (!ret) {
2017-02-13 18:10:40 +01:00
throw cscript::cs_error(
2016-09-15 21:27:14 +02:00
cs, "could not run file \"%s\"", file
);
2016-09-07 22:46:22 +02:00
}
});
gcs.new_command("echo", "C", [](auto &, auto args, auto &) {
ostd::writeln(args[0].get_strr());
2016-09-07 22:46:22 +02:00
});
int firstarg = 0;
2016-10-06 20:29:07 +02:00
bool has_inter = false, has_ver = false, has_help = false;
char const *has_str = nullptr;
2016-09-07 22:46:22 +02:00
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
firstarg = i;
goto endargs;
}
switch (argv[i][1]) {
case '-':
if (argv[i][2] != '\0') {
firstarg = -1;
goto endargs;
}
firstarg = (argv[i + 1] != nullptr) ? (i + 1) : 0;
goto endargs;
case '\0':
firstarg = i;
goto endargs;
case 'i':
if (argv[i][2] != '\0') {
firstarg = -1;
goto endargs;
}
has_inter = true;
break;
case 'v':
if (argv[i][2] != '\0') {
firstarg = -1;
goto endargs;
}
has_ver = true;
break;
case 'h':
if (argv[i][2] != '\0') {
firstarg = -1;
goto endargs;
}
has_help = true;
break;
case 'e':
if (argv[i][2] == '\0') {
++i;
if (!argv[i]) {
firstarg = -1;
goto endargs;
2016-10-06 20:29:07 +02:00
} else {
has_str = argv[i];
2016-09-07 22:46:22 +02:00
}
2016-10-06 20:29:07 +02:00
} else {
has_str = argv[i] + 2;
2016-09-07 22:46:22 +02:00
}
break;
default:
firstarg = -1;
goto endargs;
}
}
endargs:
if (firstarg < 0) {
print_usage(argv[0], true);
return 1;
}
if (has_ver && !has_inter) {
print_version();
}
if (has_help) {
print_usage(argv[0], false);
return 0;
}
2016-10-06 20:29:07 +02:00
if (has_str) {
do_call(gcs, has_str);
2016-09-07 22:46:22 +02:00
}
if (firstarg) {
2016-09-11 23:33:02 +02:00
do_call(gcs, argv[firstarg], true);
2016-09-07 22:46:22 +02:00
}
if (!firstarg && !has_str && !has_ver) {
if (stdin_is_tty()) {
2016-09-11 23:33:02 +02:00
init_lineedit(gcs, argv[0]);
do_tty(gcs);
2016-09-07 22:46:22 +02:00
return 0;
} else {
2017-01-30 01:18:55 +01:00
std::string str;
2017-03-10 20:16:20 +01:00
for (char c = '\0'; (c = ostd::cin.get_char()) != EOF;) {
2016-09-07 22:46:22 +02:00
str += c;
}
2016-09-11 23:33:02 +02:00
do_call(gcs, str);
2016-09-07 22:46:22 +02:00
}
}
if (has_inter) {
if (stdin_is_tty()) {
2016-09-11 23:33:02 +02:00
init_lineedit(gcs, argv[0]);
do_tty(gcs);
2016-09-07 22:46:22 +02:00
}
return 0;
}
}