diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index 39ee987..7e241e7 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -481,11 +481,13 @@ struct OSTD_EXPORT cs_state { void run(cs_bcode *code, cs_value &ret); void run(std::string_view code, cs_value &ret); + void run(std::string_view code, cs_value &ret, std::string_view source); void run(cs_ident *id, cs_value_r args, cs_value &ret); - void run(cs_bcode *code); - void run(std::string_view code); - void run(cs_ident *id, cs_value_r args); + cs_value run(cs_bcode *code); + cs_value run(std::string_view code); + cs_value run(std::string_view code, std::string_view source); + cs_value run(cs_ident *id, cs_value_r args); cs_loop_state run_loop(cs_bcode *code, cs_value &ret); cs_loop_state run_loop(cs_bcode *code); @@ -494,13 +496,6 @@ struct OSTD_EXPORT cs_state { return p_inloop; } - std::optional run_file_str(std::string_view fname); - std::optional run_file_int(std::string_view fname); - std::optional run_file_float(std::string_view fname); - std::optional run_file_bool(std::string_view fname); - bool run_file(std::string_view fname, cs_value &ret); - bool run_file(std::string_view fname); - void set_alias(std::string_view name, cs_value v); void set_var_int( diff --git a/src/cs_vm.cc b/src/cs_vm.cc index a72ba27..dff7acc 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -1582,6 +1582,12 @@ void cs_state::run(std::string_view code, cs_value &ret) { cs_run(*this, std::string_view{}, code, ret); } +void cs_state::run( + std::string_view code, cs_value &ret, std::string_view source +) { + cs_run(*this, source, code, ret); +} + void cs_state::run(cs_ident *id, cs_value_r args, cs_value &ret) { int nargs = int(args.size()); ret.set_none(); @@ -1727,19 +1733,28 @@ bool cs_state::run_bool(cs_ident *id, cs_value_r args) { return ret.get_bool(); } -void cs_state::run(cs_bcode *code) { +cs_value cs_state::run(cs_bcode *code) { cs_value ret{*this}; run(code, ret); + return ret; } -void cs_state::run(std::string_view code) { +cs_value cs_state::run(std::string_view code) { cs_value ret{*this}; run(code, ret); + return ret; } -void cs_state::run(cs_ident *id, cs_value_r args) { +cs_value cs_state::run(std::string_view code, std::string_view source) { + cs_value ret{*this}; + run(code, ret, source); + return ret; +} + +cs_value cs_state::run(cs_ident *id, cs_value_r args) { cs_value ret{*this}; run(id, args, ret); + return ret; } cs_loop_state cs_state::run_loop(cs_bcode *code, cs_value &ret) { @@ -1764,75 +1779,4 @@ cs_loop_state cs_state::run_loop(cs_bcode *code) { return run_loop(code, ret); } -static bool cs_run_file( - cs_state &cs, std::string_view fname, cs_value &ret -) { - std::unique_ptr buf; - size_t len; - - ostd::file_stream f(fname, ostd::stream_mode::READ); - if (!f.is_open()) { - return false; - } - - len = f.size(); - buf = std::make_unique(len + 1); - if (!buf) { - return false; - } - try { - f.get(buf.get(), len); - } catch (...) { - return false; - } - buf[len] = '\0'; - - cs_run(cs, fname, std::string_view{buf.get(), len}, ret); - return true; -} - -std::optional cs_state::run_file_str(std::string_view fname) { - cs_value ret{*this}; - if (!cs_run_file(*this, fname, ret)) { - return std::nullopt; - } - return ret.get_str(); -} - -std::optional cs_state::run_file_int(std::string_view fname) { - cs_value ret{*this}; - if (!cs_run_file(*this, fname, ret)) { - return std::nullopt; - } - return ret.get_int(); -} - -std::optional cs_state::run_file_float(std::string_view fname) { - cs_value ret{*this}; - if (!cs_run_file(*this, fname, ret)) { - return std::nullopt; - } - return ret.get_float(); -} - -std::optional cs_state::run_file_bool(std::string_view fname) { - cs_value ret{*this}; - if (!cs_run_file(*this, fname, ret)) { - return std::nullopt; - } - return ret.get_bool(); -} - -bool cs_state::run_file(std::string_view fname, cs_value &ret) { - return cs_run_file(*this, fname, ret); -} - -bool cs_state::run_file(std::string_view fname) { - cs_value ret{*this}; - if (!cs_run_file(*this, fname, ret)) { - return false; - } - return true; -} - } /* namespace cscript */ diff --git a/tools/repl.cc b/tools/repl.cc index 30e09ba..44708a1 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -233,13 +234,39 @@ static void repl_print_var(cs_state const &cs, cs_var const &var) { } } +static bool do_run_file(cs_state &cs, std::string_view fname, cs_value &ret) { + std::unique_ptr buf; + std::size_t len; + + ostd::file_stream f{fname, ostd::stream_mode::READ}; + if (!f.is_open()) { + return false; + } + + len = f.size(); + buf = std::make_unique(len + 1); + if (!buf) { + return false; + } + + try { + f.get(buf.get(), len); + } catch (...) { + return false; + } + buf[len] = '\0'; + + cs.run(std::string_view{buf.get(), len}, ret, fname); + return true; +} + static bool do_call(cs_state &cs, std::string_view line, bool file = false) { cs_value ret{cs}; scs = &cs; signal(SIGINT, do_sigint); try { if (file) { - if (!cs.run_file(line, ret)) { + if (!do_run_file(cs, line, ret)) { ostd::cerr.writeln("cannot read file: ", line); } } else { @@ -325,7 +352,8 @@ int main(int argc, char **argv) { gcs.new_command("exec", "s", [](auto &cs, auto args, auto &) { auto file = args[0].get_str(); - bool ret = cs.run_file(file); + cs_value val{cs}; + bool ret = do_run_file(cs, file, val); if (!ret) { throw cscript::cs_error( cs, "could not run file \"%s\"", file