libcubescript/repl.cc

45 lines
1.1 KiB
C++
Raw Normal View History

2016-08-31 23:00:13 +02:00
#include <ostd/io.hh>
#include <ostd/string.hh>
#include <ostd/maybe.hh>
#include <cubescript.hh>
using namespace cscript;
ostd::ConstCharRange version =
"CubeScript 0.0.1 (REPL mode) Copyright (C) 2016 Daniel \"q66\" Kolesa";
CsSvar *prompt = nullptr;
2016-08-31 23:21:00 +02:00
static ostd::String read_line() {
2016-08-31 23:00:13 +02:00
ostd::write(prompt->get_value());
2016-08-31 23:21:00 +02:00
auto app = ostd::appender<ostd::String>();
/* i really need to implement some sort of get_line for ostd streams */
for (char c = ostd::in.getchar(); c && (c != '\n'); c = ostd::in.getchar()) {
app.put(c);
2016-08-31 23:00:13 +02:00
}
2016-08-31 23:21:00 +02:00
return ostd::move(app.get());
2016-08-31 23:00:13 +02:00
}
static void do_tty(CsState &cs) {
ostd::writeln(version);
for (;;) {
auto line = read_line();
2016-08-31 23:21:00 +02:00
if (line.empty()) {
2016-08-31 23:00:13 +02:00
continue;
}
CsValue ret;
ret.set_null();
2016-08-31 23:21:00 +02:00
cs.run_ret(line, ret);
2016-08-31 23:00:13 +02:00
if (ret.get_type() != CsValueType::null) {
ostd::writeln(ret.get_str());
}
}
}
int main() {
CsState cs;
cs.init_libs();
prompt = cs.add_ident<CsSvar>("PROMPT", "> ");
do_tty(cs);
}