diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index 54877943..58989e33 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -204,6 +204,8 @@ protected: private: CsVarCb cb_var; + virtual CsString to_printable() const = 0; + void changed(CsState &cs) { if (cb_var) { cb_var(cs, *this); @@ -220,6 +222,8 @@ struct OSTD_EXPORT CsIvar: CsVar { CsInt get_value() const; void set_value(CsInt val); + CsString to_printable() const final; + private: CsIvar( ostd::ConstCharRange n, CsInt m, CsInt x, CsInt v, CsVarCb f, int flags @@ -237,6 +241,8 @@ struct OSTD_EXPORT CsFvar: CsVar { CsFloat get_value() const; void set_value(CsFloat val); + CsString to_printable() const final; + private: CsFvar( ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat v, @@ -252,6 +258,8 @@ struct OSTD_EXPORT CsSvar: CsVar { ostd::ConstCharRange get_value() const; void set_value(CsString val); + CsString to_printable() const final; + private: CsSvar(ostd::ConstCharRange n, CsString v, CsVarCb f, int flags); @@ -366,10 +374,6 @@ struct OSTD_EXPORT CsState { CsState(CsAllocCb func = nullptr, void *data = nullptr); virtual ~CsState(); - CsStream const &get_out() const; - CsStream &get_out(); - void set_out(CsStream &s); - CsHookCb set_call_hook(CsHookCb func); CsHookCb const &get_call_hook() const; CsHookCb &get_call_hook(); @@ -550,10 +554,7 @@ struct OSTD_EXPORT CsState { ostd::Maybe get_alias_val(ostd::ConstCharRange name); - void print_var(CsVar *v); - virtual void print_var(CsIvar *iv); - virtual void print_var(CsFvar *fv); - virtual void print_var(CsSvar *sv); + virtual void print_var(CsVar *v); private: CsIdent *add_ident(CsIdent *id); @@ -566,8 +567,6 @@ private: char p_errbuf[512]; CsHookCb p_callhook; - - CsStream *p_out; }; struct CsStackStateNode { diff --git a/include/cubescript/cubescript_conf.hh b/include/cubescript/cubescript_conf.hh index 4f978d64..d6a5b61f 100644 --- a/include/cubescript/cubescript_conf.hh +++ b/include/cubescript/cubescript_conf.hh @@ -28,6 +28,14 @@ namespace cscript { constexpr auto const IntFormat = "%d"; constexpr auto const FloatFormat = "%.7g"; constexpr auto const RoundFloatFormat = "%.1f"; + + constexpr auto const IvarFormat = "%s = %d"; + constexpr auto const IvarHexFormat = "%s = 0x%X"; + constexpr auto const IvarHexColorFormat = "%s = 0x%.6X (%d, %d, %d)"; + constexpr auto const FvarFormat = "%s = %.7g"; + constexpr auto const FvarRoundFormat = "%s = %.1f"; + constexpr auto const SvarFormat = "%s = \"%s\""; + constexpr auto const SvarQuotedFormat = "%s = [%s]"; } /* namespace cscript */ #endif /* LIBCUBESCRIPT_CUBESCRIPT_CONF_HH */ \ No newline at end of file diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 9e13d5ab..d034a7a7 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -1506,7 +1506,7 @@ noid: } case CsIdIvar: if (callargs <= 0) { - cs.print_var(static_cast(id)); + cs.print_var(static_cast(id)); } else { cs.set_var_int_checked( static_cast(id), @@ -1518,7 +1518,7 @@ noid: continue; case CsIdFvar: if (callargs <= 0) { - cs.print_var(static_cast(id)); + cs.print_var(static_cast(id)); } else { cs.set_var_float_checked( static_cast(id), @@ -1530,7 +1530,7 @@ noid: continue; case CsIdSvar: if (callargs <= 0) { - cs.print_var(static_cast(id)); + cs.print_var(static_cast(id)); } else { cs.set_var_str_checked( static_cast(id), @@ -1623,14 +1623,14 @@ void CsState::run(CsIdent *id, CsValueRange args, CsValue &ret) { break; case CsIdentType::Ivar: if (args.empty()) { - print_var(static_cast(id)); + print_var(static_cast(id)); } else { set_var_int_checked(static_cast(id), args); } break; case CsIdentType::Fvar: if (args.empty()) { - print_var(static_cast(id)); + print_var(static_cast(id)); } else { set_var_float_checked( static_cast(id), args[0].force_float() @@ -1639,7 +1639,7 @@ void CsState::run(CsIdent *id, CsValueRange args, CsValue &ret) { break; case CsIdentType::Svar: if (args.empty()) { - print_var(static_cast(id)); + print_var(static_cast(id)); } else { set_var_str_checked( static_cast(id), args[0].force_str() diff --git a/src/cubescript.cc b/src/cubescript.cc index 9ffcd9fc..f6b1a73a 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -221,6 +221,22 @@ void CsIvar::set_value(CsInt val) { p_storage = val; } +CsString CsIvar::to_printable() const { + CsInt i = p_storage; + auto app = ostd::appender(); + if (!(get_flags() & CsIdfHex) || (i < 0)) { + format(app, IvarFormat, get_name(), i); + } else if (p_maxval == 0xFFFFFF) { + format( + app, IvarHexColorFormat, get_name(), + i, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF + ); + } else { + format(app, IvarHexFormat, get_name(), i); + } + return ostd::move(app.get()); +} + CsFloat CsFvar::get_val_min() const { return p_minval; } @@ -235,6 +251,13 @@ void CsFvar::set_value(CsFloat val) { p_storage = val; } +CsString CsFvar::to_printable() const { + CsFloat f = p_storage; + auto app = ostd::appender(); + format(app, (f == CsInt(f)) ? FvarRoundFormat : FvarFormat, get_name(), f); + return ostd::move(app.get()); +} + ostd::ConstCharRange CsSvar::get_value() const { return p_storage.iter(); } @@ -242,6 +265,17 @@ void CsSvar::set_value(CsString val) { p_storage = ostd::move(val); } +CsString CsSvar::to_printable() const { + ostd::ConstCharRange s = p_storage; + auto app = ostd::appender(); + if (ostd::find(s, '"').empty()) { + format(app, SvarFormat, get_name(), s); + } else { + format(app, SvarQuotedFormat, get_name(), s); + } + return ostd::move(app.get()); +} + ostd::ConstCharRange CsCommand::get_args() const { return p_cargs; } @@ -254,8 +288,7 @@ void cs_init_lib_base(CsState &cs); CsState::CsState(CsAllocCb func, void *data): p_state(nullptr), - p_allocf(func), p_aptr(data), p_callhook(), - p_out(&ostd::out) + p_allocf(func), p_aptr(data), p_callhook() { p_state = create(); for (int i = 0; i < MaxArguments; ++i) { @@ -367,18 +400,6 @@ CsState::~CsState() { destroy(p_state); } -CsStream const &CsState::get_out() const { - return *p_out; -} - -CsStream &CsState::get_out() { - return *p_out; -} - -void CsState::set_out(CsStream &s) { - p_out = &s; -} - CsHookCb CsState::set_call_hook(CsHookCb func) { auto hk = ostd::move(p_callhook); p_callhook = ostd::move(func); @@ -593,59 +614,8 @@ void CsState::set_alias(ostd::ConstCharRange name, CsValue v) { } } -void CsState::print_var(CsIvar *iv) { - CsInt i = iv->get_value(); - if (i < 0) { - get_out().writefln("%s = %d", iv->get_name(), i); - return; - } - if (iv->get_flags() & CsIdfHex) { - if (iv->get_val_max() == 0xFFFFFF) { - get_out().writefln( - "%s = 0x%.6X (%d, %d, %d)", iv->get_name(), - i, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF - ); - } else { - get_out().writefln("%s = 0x%X", iv->get_name(), i); - } - } else { - get_out().writefln("%s = %d", iv->get_name(), i); - } -} - -void CsState::print_var(CsFvar *fv) { - get_out().writefln("%s = %s", fv->get_name(), floatstr(fv->get_value())); -} - -void CsState::print_var(CsSvar *sv) { - ostd::ConstCharRange sval = sv->get_value(); - if (ostd::find(sval, '"').empty()) { - get_out().writefln("%s = \"%s\"", sv->get_name(), sval); - } else { - get_out().writefln("%s = [%s]", sv->get_name(), sval); - } -} - void CsState::print_var(CsVar *v) { - switch (v->get_type()) { - case CsIdentType::Ivar: { - CsIvar *iv = static_cast(v); - print_var(iv); - break; - } - case CsIdentType::Fvar: { - CsFvar *fv = static_cast(v); - print_var(fv); - break; - } - case CsIdentType::Svar: { - CsSvar *sv = static_cast(v); - print_var(sv); - break; - } - default: - break; - } + writeln(v->to_printable()); } void CsAlias::get_cstr(CsValue &v) const { diff --git a/tools/repl.cc b/tools/repl.cc index 0346d69a..d4d8f23f 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -217,10 +217,10 @@ static bool do_call(CsState &cs, ostd::ConstCharRange line, bool file = false) { if (!file && ((terr == "missing \"]\"") || (terr == "missing \")\""))) { return true; } - cs.get_out().writeln(!is_lnum ? "stdin: " : "stdin:", e.what()); + ostd::writeln(!is_lnum ? "stdin: " : "stdin:", e.what()); if (e.get_stack().get()) { - cscript::util::print_stack(cs.get_out().iter(), e.get_stack()); - cs.get_out().write('\n'); + cscript::util::print_stack(ostd::out.iter(), e.get_stack()); + ostd::write('\n'); } return false; } @@ -286,8 +286,8 @@ int main(int argc, char **argv) { } }); - gcs.new_command("echo", "C", [](auto &cs, auto args, auto &) { - cs.get_out().writeln(args[0].get_strr()); + gcs.new_command("echo", "C", [](auto &, auto args, auto &) { + ostd::writeln(args[0].get_strr()); }); int firstarg = 0;