overridable print_var stuff and overridable out/err streams

master
Daniel Kolesa 2016-09-02 19:22:19 +01:00
parent 8f1be31ba2
commit 6c0b6da94d
6 changed files with 65 additions and 26 deletions

View File

@ -139,9 +139,9 @@ void cs_debug_alias(CsState &cs) {
CsIdent *id = l->id; CsIdent *id = l->id;
++depth; ++depth;
if (depth < dalias->get_value()) { if (depth < dalias->get_value()) {
ostd::err.writefln(" %d) %s", total - depth + 1, id->get_name()); cs.get_err().writefln(" %d) %s", total - depth + 1, id->get_name());
} else if (l->next == &cs.noalias) { } else if (l->next == &cs.noalias) {
ostd::err.writefln( cs.get_err().writefln(
depth == dalias->get_value() ? " %d) %s" : " ..%d) %s", depth == dalias->get_value() ? " %d) %s" : " ..%d) %s",
total - depth + 1, id->get_name() total - depth + 1, id->get_name()
); );

View File

@ -119,7 +119,7 @@ void cs_debug_code(CsState &cs, ostd::ConstCharRange fmt, A &&...args) {
if (cs.nodebug) { if (cs.nodebug) {
return; return;
} }
ostd::err.writefln(fmt, ostd::forward<A>(args)...); cs.get_err().writefln(fmt, ostd::forward<A>(args)...);
cs_debug_alias(cs); cs_debug_alias(cs);
} }
@ -131,7 +131,7 @@ void cs_debug_code_line(
return; return;
} }
ostd::Array<char, 256> buf; ostd::Array<char, 256> buf;
ostd::err.writefln( cs.get_err().writefln(
cs_debug_line(p, fmt, ostd::CharRange(buf.data(), buf.size())), cs_debug_line(p, fmt, ostd::CharRange(buf.data(), buf.size())),
ostd::forward<A>(args)... ostd::forward<A>(args)...
); );

View File

@ -254,7 +254,7 @@ void CsSvar::set_value(CsString val) {
void cs_init_lib_base(CsState &cs); void cs_init_lib_base(CsState &cs);
CsState::CsState() { CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) {
noalias.id = nullptr; noalias.id = nullptr;
noalias.next = nullptr; noalias.next = nullptr;
noalias.usedargs = (1 << MaxArguments) - 1; noalias.usedargs = (1 << MaxArguments) - 1;
@ -360,6 +360,30 @@ CsState::~CsState() {
} }
} }
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;
}
CsStream const &CsState::get_err() const {
return *p_err;
}
CsStream &CsState::get_err() {
return *p_err;
}
void CsState::set_err(CsStream &s) {
p_err = &s;
}
void CsState::clear_override(CsIdent &id) { void CsState::clear_override(CsIdent &id) {
if (!(id.get_flags() & IDF_OVERRIDDEN)) { if (!(id.get_flags() & IDF_OVERRIDDEN)) {
return; return;
@ -523,34 +547,36 @@ void CsState::set_alias(ostd::ConstCharRange name, CsValue &v) {
} }
} }
void CsState::print_var_int(CsIvar *iv, CsInt i) { void CsState::print_var(CsIvar *iv) {
CsInt i = iv->get_value();
if (i < 0) { if (i < 0) {
writefln("%s = %d", iv->get_name(), i); get_out().writefln("%s = %d", iv->get_name(), i);
return; return;
} }
if (iv->get_flags() & IDF_HEX) { if (iv->get_flags() & IDF_HEX) {
if (iv->get_val_max() == 0xFFFFFF) { if (iv->get_val_max() == 0xFFFFFF) {
writefln( get_out().writefln(
"%s = 0x%.6X (%d, %d, %d)", iv->get_name(), "%s = 0x%.6X (%d, %d, %d)", iv->get_name(),
i, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF i, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF
); );
} else { } else {
writefln("%s = 0x%X", iv->get_name(), i); get_out().writefln("%s = 0x%X", iv->get_name(), i);
} }
} else { } else {
writefln("%s = %d", iv->get_name(), i); get_out().writefln("%s = %d", iv->get_name(), i);
} }
} }
void CsState::print_var_float(CsFvar *fv, CsFloat f) { void CsState::print_var(CsFvar *fv) {
writefln("%s = %s", fv->get_name(), floatstr(f)); get_out().writefln("%s = %s", fv->get_name(), floatstr(fv->get_value()));
} }
void CsState::print_var_str(CsSvar *sv, ostd::ConstCharRange s) { void CsState::print_var(CsSvar *sv) {
if (ostd::find(s, '"').empty()) { ostd::ConstCharRange sval = sv->get_value();
writefln("%s = \"%s\"", sv->get_name(), s); if (ostd::find(sval, '"').empty()) {
get_out().writefln("%s = \"%s\"", sv->get_name(), sval);
} else { } else {
writefln("%s = [%s]", sv->get_name(), s); get_out().writefln("%s = [%s]", sv->get_name(), sval);
} }
} }
@ -558,17 +584,17 @@ void CsState::print_var(CsVar *v) {
switch (v->get_type()) { switch (v->get_type()) {
case CsIdentType::ivar: { case CsIdentType::ivar: {
CsIvar *iv = static_cast<CsIvar *>(v); CsIvar *iv = static_cast<CsIvar *>(v);
print_var_int(iv, iv->get_value()); print_var(iv);
break; break;
} }
case CsIdentType::fvar: { case CsIdentType::fvar: {
CsFvar *fv = static_cast<CsFvar *>(v); CsFvar *fv = static_cast<CsFvar *>(v);
print_var_float(fv, fv->get_value()); print_var(fv);
break; break;
} }
case CsIdentType::svar: { case CsIdentType::svar: {
CsSvar *sv = static_cast<CsSvar *>(v); CsSvar *sv = static_cast<CsSvar *>(v);
print_var_str(sv, sv->get_value()); print_var(sv);
break; break;
} }
default: default:
@ -1234,7 +1260,7 @@ CsCommand *CsState::new_command(
case 'V': case 'V':
break; break;
default: default:
ostd::err.writefln( get_err().writefln(
"builtin %s declared with illegal type: %c", "builtin %s declared with illegal type: %c",
name, fmt.front() name, fmt.front()
); );

View File

@ -342,6 +342,14 @@ struct OSTD_EXPORT CsState {
CsState(); CsState();
~CsState(); ~CsState();
CsStream const &get_out() const;
CsStream &get_out();
void set_out(CsStream &s);
CsStream const &get_err() const;
CsStream &get_err();
void set_err(CsStream &s);
void init_libs(int libs = CS_LIB_ALL); void init_libs(int libs = CS_LIB_ALL);
void clear_override(CsIdent &id); void clear_override(CsIdent &id);
@ -453,12 +461,14 @@ 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); void print_var(CsVar *v);
void print_var_int(CsIvar *iv, CsInt i); virtual void print_var(CsIvar *iv);
void print_var_float(CsFvar *fv, CsFloat f); virtual void print_var(CsFvar *fv);
void print_var_str(CsSvar *sv, ostd::ConstCharRange s); virtual void print_var(CsSvar *sv);
private: private:
CsIdent *add_ident(CsIdent *id); CsIdent *add_ident(CsIdent *id);
CsStream *p_out, *p_err;
}; };
struct OSTD_EXPORT CsStackedValue: CsValue { struct OSTD_EXPORT CsStackedValue: CsValue {

View File

@ -7,6 +7,7 @@
#include <ostd/string.hh> #include <ostd/string.hh>
#include <ostd/vector.hh> #include <ostd/vector.hh>
#include <ostd/map.hh> #include <ostd/map.hh>
#include <ostd/stream.hh>
namespace cscript { namespace cscript {
template<typename T> template<typename T>
@ -25,6 +26,8 @@ namespace cscript {
template<typename T> template<typename T>
using CsVector = ostd::Vector<T, CsAllocator<T>>; using CsVector = ostd::Vector<T, CsAllocator<T>>;
using CsStream = ostd::Stream;
constexpr CsInt const CsIntMin = INT_MIN; constexpr CsInt const CsIntMin = INT_MIN;
constexpr CsInt const CsIntMax = INT_MAX; constexpr CsInt const CsIntMax = INT_MAX;

View File

@ -176,7 +176,7 @@ static void do_tty(CsState &cs) {
bool ret = cs.run_file(file); bool ret = cs.run_file(file);
if (!ret) { if (!ret) {
if (args[1].get_int()) { if (args[1].get_int()) {
ostd::err.writefln("could not run file \"%s\"", file); cs.get_err().writefln("could not run file \"%s\"", file);
} }
res.set_int(0); res.set_int(0);
} else { } else {
@ -184,8 +184,8 @@ static void do_tty(CsState &cs) {
} }
}); });
cs.new_command("echo", "C", [](CsValueRange args, CsValue &) { cs.new_command("echo", "C", [&cs](CsValueRange args, CsValue &) {
ostd::writeln(args[0].get_strr()); cs.get_out().writeln(args[0].get_strr());
}); });
ostd::writeln(version); ostd::writeln(version);