From c9f6ac8be9685bdd916619742ae54a25976548d9 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 31 Aug 2016 22:00:13 +0100 Subject: [PATCH] add initial simplistic REPL --- Makefile | 5 ++++- repl.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 repl.cc diff --git a/Makefile b/Makefile index 5df5e8cc..b8b37d3f 100644 --- a/Makefile +++ b/Makefile @@ -20,13 +20,16 @@ LIBCS_LIB = libcubescript.a .cc.o: $(CXX) $(CXXFLAGS) $(LIBCS_CXXFLAGS) -c -o $@ $< -all: library +all: library repl library: $(LIBCS_LIB) $(LIBCS_LIB): $(LIBCS_OBJ) ar rcs $(LIBCS_LIB) $(LIBCS_OBJ) +repl: $(LIBCS_LIB) + $(CXX) $(CXXFLAGS) $(LIBCS_CXXFLAGS) $(LDFLAGS) repl.cc -o repl $(LIBCS_LIB) + clean: rm -f $(LIBCS_LIB) $(LIBCS_OBJ) diff --git a/repl.cc b/repl.cc new file mode 100644 index 00000000..37e45fe5 --- /dev/null +++ b/repl.cc @@ -0,0 +1,43 @@ +#include +#include +#include + +#include + +using namespace cscript; + +ostd::ConstCharRange version = + "CubeScript 0.0.1 (REPL mode) Copyright (C) 2016 Daniel \"q66\" Kolesa"; +CsSvar *prompt = nullptr; + +static ostd::Maybe read_line() { + ostd::write(prompt->get_value()); + char buf[512]; + if (fgets(buf, sizeof(buf), stdin)) { + return ostd::String(buf); + } + return ostd::nothing; +} + +static void do_tty(CsState &cs) { + ostd::writeln(version); + for (;;) { + auto line = read_line(); + if (!line) { + continue; + } + CsValue ret; + ret.set_null(); + cs.run_ret(line.value(), ret); + if (ret.get_type() != CsValueType::null) { + ostd::writeln(ret.get_str()); + } + } +} + +int main() { + CsState cs; + cs.init_libs(); + prompt = cs.add_ident("PROMPT", "> "); + do_tty(cs); +}