forked from OctaForge/libcubescript
remove the out stream and re-do the var printing system
parent
192ce615d8
commit
845232605d
|
@ -204,6 +204,8 @@ protected:
|
||||||
private:
|
private:
|
||||||
CsVarCb cb_var;
|
CsVarCb cb_var;
|
||||||
|
|
||||||
|
virtual CsString to_printable() const = 0;
|
||||||
|
|
||||||
void changed(CsState &cs) {
|
void changed(CsState &cs) {
|
||||||
if (cb_var) {
|
if (cb_var) {
|
||||||
cb_var(cs, *this);
|
cb_var(cs, *this);
|
||||||
|
@ -220,6 +222,8 @@ struct OSTD_EXPORT CsIvar: CsVar {
|
||||||
CsInt get_value() const;
|
CsInt get_value() const;
|
||||||
void set_value(CsInt val);
|
void set_value(CsInt val);
|
||||||
|
|
||||||
|
CsString to_printable() const final;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CsIvar(
|
CsIvar(
|
||||||
ostd::ConstCharRange n, CsInt m, CsInt x, CsInt v, CsVarCb f, int flags
|
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;
|
CsFloat get_value() const;
|
||||||
void set_value(CsFloat val);
|
void set_value(CsFloat val);
|
||||||
|
|
||||||
|
CsString to_printable() const final;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CsFvar(
|
CsFvar(
|
||||||
ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat v,
|
ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat v,
|
||||||
|
@ -252,6 +258,8 @@ struct OSTD_EXPORT CsSvar: CsVar {
|
||||||
ostd::ConstCharRange get_value() const;
|
ostd::ConstCharRange get_value() const;
|
||||||
void set_value(CsString val);
|
void set_value(CsString val);
|
||||||
|
|
||||||
|
CsString to_printable() const final;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CsSvar(ostd::ConstCharRange n, CsString v, CsVarCb f, int flags);
|
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);
|
CsState(CsAllocCb func = nullptr, void *data = nullptr);
|
||||||
virtual ~CsState();
|
virtual ~CsState();
|
||||||
|
|
||||||
CsStream const &get_out() const;
|
|
||||||
CsStream &get_out();
|
|
||||||
void set_out(CsStream &s);
|
|
||||||
|
|
||||||
CsHookCb set_call_hook(CsHookCb func);
|
CsHookCb set_call_hook(CsHookCb func);
|
||||||
CsHookCb const &get_call_hook() const;
|
CsHookCb const &get_call_hook() const;
|
||||||
CsHookCb &get_call_hook();
|
CsHookCb &get_call_hook();
|
||||||
|
@ -550,10 +554,7 @@ struct OSTD_EXPORT CsState {
|
||||||
|
|
||||||
ostd::Maybe<CsString> get_alias_val(ostd::ConstCharRange name);
|
ostd::Maybe<CsString> get_alias_val(ostd::ConstCharRange name);
|
||||||
|
|
||||||
void print_var(CsVar *v);
|
virtual void print_var(CsVar *v);
|
||||||
virtual void print_var(CsIvar *iv);
|
|
||||||
virtual void print_var(CsFvar *fv);
|
|
||||||
virtual void print_var(CsSvar *sv);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CsIdent *add_ident(CsIdent *id);
|
CsIdent *add_ident(CsIdent *id);
|
||||||
|
@ -566,8 +567,6 @@ private:
|
||||||
char p_errbuf[512];
|
char p_errbuf[512];
|
||||||
|
|
||||||
CsHookCb p_callhook;
|
CsHookCb p_callhook;
|
||||||
|
|
||||||
CsStream *p_out;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CsStackStateNode {
|
struct CsStackStateNode {
|
||||||
|
|
|
@ -28,6 +28,14 @@ namespace cscript {
|
||||||
constexpr auto const IntFormat = "%d";
|
constexpr auto const IntFormat = "%d";
|
||||||
constexpr auto const FloatFormat = "%.7g";
|
constexpr auto const FloatFormat = "%.7g";
|
||||||
constexpr auto const RoundFloatFormat = "%.1f";
|
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 */
|
} /* namespace cscript */
|
||||||
|
|
||||||
#endif /* LIBCUBESCRIPT_CUBESCRIPT_CONF_HH */
|
#endif /* LIBCUBESCRIPT_CUBESCRIPT_CONF_HH */
|
12
src/cs_vm.cc
12
src/cs_vm.cc
|
@ -1506,7 +1506,7 @@ noid:
|
||||||
}
|
}
|
||||||
case CsIdIvar:
|
case CsIdIvar:
|
||||||
if (callargs <= 0) {
|
if (callargs <= 0) {
|
||||||
cs.print_var(static_cast<CsIvar *>(id));
|
cs.print_var(static_cast<CsVar *>(id));
|
||||||
} else {
|
} else {
|
||||||
cs.set_var_int_checked(
|
cs.set_var_int_checked(
|
||||||
static_cast<CsIvar *>(id),
|
static_cast<CsIvar *>(id),
|
||||||
|
@ -1518,7 +1518,7 @@ noid:
|
||||||
continue;
|
continue;
|
||||||
case CsIdFvar:
|
case CsIdFvar:
|
||||||
if (callargs <= 0) {
|
if (callargs <= 0) {
|
||||||
cs.print_var(static_cast<CsFvar *>(id));
|
cs.print_var(static_cast<CsVar *>(id));
|
||||||
} else {
|
} else {
|
||||||
cs.set_var_float_checked(
|
cs.set_var_float_checked(
|
||||||
static_cast<CsFvar *>(id),
|
static_cast<CsFvar *>(id),
|
||||||
|
@ -1530,7 +1530,7 @@ noid:
|
||||||
continue;
|
continue;
|
||||||
case CsIdSvar:
|
case CsIdSvar:
|
||||||
if (callargs <= 0) {
|
if (callargs <= 0) {
|
||||||
cs.print_var(static_cast<CsSvar *>(id));
|
cs.print_var(static_cast<CsVar *>(id));
|
||||||
} else {
|
} else {
|
||||||
cs.set_var_str_checked(
|
cs.set_var_str_checked(
|
||||||
static_cast<CsSvar *>(id),
|
static_cast<CsSvar *>(id),
|
||||||
|
@ -1623,14 +1623,14 @@ void CsState::run(CsIdent *id, CsValueRange args, CsValue &ret) {
|
||||||
break;
|
break;
|
||||||
case CsIdentType::Ivar:
|
case CsIdentType::Ivar:
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
print_var(static_cast<CsIvar *>(id));
|
print_var(static_cast<CsVar *>(id));
|
||||||
} else {
|
} else {
|
||||||
set_var_int_checked(static_cast<CsIvar *>(id), args);
|
set_var_int_checked(static_cast<CsIvar *>(id), args);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CsIdentType::Fvar:
|
case CsIdentType::Fvar:
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
print_var(static_cast<CsFvar *>(id));
|
print_var(static_cast<CsVar *>(id));
|
||||||
} else {
|
} else {
|
||||||
set_var_float_checked(
|
set_var_float_checked(
|
||||||
static_cast<CsFvar *>(id), args[0].force_float()
|
static_cast<CsFvar *>(id), args[0].force_float()
|
||||||
|
@ -1639,7 +1639,7 @@ void CsState::run(CsIdent *id, CsValueRange args, CsValue &ret) {
|
||||||
break;
|
break;
|
||||||
case CsIdentType::Svar:
|
case CsIdentType::Svar:
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
print_var(static_cast<CsSvar *>(id));
|
print_var(static_cast<CsVar *>(id));
|
||||||
} else {
|
} else {
|
||||||
set_var_str_checked(
|
set_var_str_checked(
|
||||||
static_cast<CsSvar *>(id), args[0].force_str()
|
static_cast<CsSvar *>(id), args[0].force_str()
|
||||||
|
|
|
@ -221,6 +221,22 @@ void CsIvar::set_value(CsInt val) {
|
||||||
p_storage = val;
|
p_storage = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CsString CsIvar::to_printable() const {
|
||||||
|
CsInt i = p_storage;
|
||||||
|
auto app = ostd::appender<CsString>();
|
||||||
|
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 {
|
CsFloat CsFvar::get_val_min() const {
|
||||||
return p_minval;
|
return p_minval;
|
||||||
}
|
}
|
||||||
|
@ -235,6 +251,13 @@ void CsFvar::set_value(CsFloat val) {
|
||||||
p_storage = val;
|
p_storage = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CsString CsFvar::to_printable() const {
|
||||||
|
CsFloat f = p_storage;
|
||||||
|
auto app = ostd::appender<CsString>();
|
||||||
|
format(app, (f == CsInt(f)) ? FvarRoundFormat : FvarFormat, get_name(), f);
|
||||||
|
return ostd::move(app.get());
|
||||||
|
}
|
||||||
|
|
||||||
ostd::ConstCharRange CsSvar::get_value() const {
|
ostd::ConstCharRange CsSvar::get_value() const {
|
||||||
return p_storage.iter();
|
return p_storage.iter();
|
||||||
}
|
}
|
||||||
|
@ -242,6 +265,17 @@ void CsSvar::set_value(CsString val) {
|
||||||
p_storage = ostd::move(val);
|
p_storage = ostd::move(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CsString CsSvar::to_printable() const {
|
||||||
|
ostd::ConstCharRange s = p_storage;
|
||||||
|
auto app = ostd::appender<CsString>();
|
||||||
|
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 {
|
ostd::ConstCharRange CsCommand::get_args() const {
|
||||||
return p_cargs;
|
return p_cargs;
|
||||||
}
|
}
|
||||||
|
@ -254,8 +288,7 @@ void cs_init_lib_base(CsState &cs);
|
||||||
|
|
||||||
CsState::CsState(CsAllocCb func, void *data):
|
CsState::CsState(CsAllocCb func, void *data):
|
||||||
p_state(nullptr),
|
p_state(nullptr),
|
||||||
p_allocf(func), p_aptr(data), p_callhook(),
|
p_allocf(func), p_aptr(data), p_callhook()
|
||||||
p_out(&ostd::out)
|
|
||||||
{
|
{
|
||||||
p_state = create<CsSharedState>();
|
p_state = create<CsSharedState>();
|
||||||
for (int i = 0; i < MaxArguments; ++i) {
|
for (int i = 0; i < MaxArguments; ++i) {
|
||||||
|
@ -367,18 +400,6 @@ CsState::~CsState() {
|
||||||
destroy(p_state);
|
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) {
|
CsHookCb CsState::set_call_hook(CsHookCb func) {
|
||||||
auto hk = ostd::move(p_callhook);
|
auto hk = ostd::move(p_callhook);
|
||||||
p_callhook = ostd::move(func);
|
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) {
|
void CsState::print_var(CsVar *v) {
|
||||||
switch (v->get_type()) {
|
writeln(v->to_printable());
|
||||||
case CsIdentType::Ivar: {
|
|
||||||
CsIvar *iv = static_cast<CsIvar *>(v);
|
|
||||||
print_var(iv);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CsIdentType::Fvar: {
|
|
||||||
CsFvar *fv = static_cast<CsFvar *>(v);
|
|
||||||
print_var(fv);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CsIdentType::Svar: {
|
|
||||||
CsSvar *sv = static_cast<CsSvar *>(v);
|
|
||||||
print_var(sv);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CsAlias::get_cstr(CsValue &v) const {
|
void CsAlias::get_cstr(CsValue &v) const {
|
||||||
|
|
|
@ -217,10 +217,10 @@ static bool do_call(CsState &cs, ostd::ConstCharRange line, bool file = false) {
|
||||||
if (!file && ((terr == "missing \"]\"") || (terr == "missing \")\""))) {
|
if (!file && ((terr == "missing \"]\"") || (terr == "missing \")\""))) {
|
||||||
return true;
|
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()) {
|
if (e.get_stack().get()) {
|
||||||
cscript::util::print_stack(cs.get_out().iter(), e.get_stack());
|
cscript::util::print_stack(ostd::out.iter(), e.get_stack());
|
||||||
cs.get_out().write('\n');
|
ostd::write('\n');
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -286,8 +286,8 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
gcs.new_command("echo", "C", [](auto &cs, auto args, auto &) {
|
gcs.new_command("echo", "C", [](auto &, auto args, auto &) {
|
||||||
cs.get_out().writeln(args[0].get_strr());
|
ostd::writeln(args[0].get_strr());
|
||||||
});
|
});
|
||||||
|
|
||||||
int firstarg = 0;
|
int firstarg = 0;
|
||||||
|
|
Loading…
Reference in New Issue