From b8f3e527d5cff5555dd3bd95acca7e56a20e6653 Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 21 Aug 2016 01:34:03 +0100 Subject: [PATCH] initial support for custom allocators --- cs_gen.cc | 10 +++++----- cs_vm.cc | 24 ++++++++++++------------ cs_vm.hh | 6 +++--- cubescript.cc | 22 +++++++++++----------- cubescript.hh | 36 +++++++++++++++++++----------------- cubescript_conf.hh | 36 ++++++++++++++++++++++++++++++++++++ lib_list.cc | 16 ++++++++-------- lib_str.cc | 18 +++++++++--------- 8 files changed, 103 insertions(+), 65 deletions(-) create mode 100644 cubescript_conf.hh diff --git a/cs_gen.cc b/cs_gen.cc index ada1275..f1c45c4 100644 --- a/cs_gen.cc +++ b/cs_gen.cc @@ -227,8 +227,8 @@ endblock: return true; } - ostd::String ListParser::element() { - ostd::String s; + CsString ListParser::element() { + CsString s; s.reserve(item.size()); if (!quote.empty() && (*quote == '"')) { auto writer = s.iter_cap(); @@ -251,7 +251,7 @@ endblock: return ret; } - ostd::Maybe list_index( + ostd::Maybe list_index( ostd::ConstCharRange s, ostd::Size idx ) { ListParser p(s); @@ -266,10 +266,10 @@ endblock: return ostd::move(p.element()); } - ostd::Vector list_explode( + CsVector list_explode( ostd::ConstCharRange s, ostd::Size limit ) { - ostd::Vector ret; + CsVector ret; ListParser p(s); while ((ret.size() < limit) && p.parse()) { ret.push(ostd::move(p.element())); diff --git a/cs_vm.cc b/cs_vm.cc index 11ea396..14f9965 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -408,7 +408,7 @@ static inline void callcommand( break; case 'C': { i = ostd::max(i + 1, numargs); - auto buf = ostd::appender(); + auto buf = ostd::appender(); cscript::util::tvals_concat(buf, ostd::iter(args, i), " "); CsValue tv; tv.set_mstr(buf.get().iter()); @@ -1329,7 +1329,7 @@ static ostd::Uint32 const *runcode( int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; result.force_null(); { - auto buf = ostd::appender(); + auto buf = ostd::appender(); cscript::util::tvals_concat( buf, ostd::iter(&args[offset], callargs), " " ); @@ -1351,7 +1351,7 @@ static ostd::Uint32 const *runcode( case CODE_CONCW | RET_FLOAT: case CODE_CONCW | RET_INT: { int numconc = op >> 8; - auto buf = ostd::appender(); + auto buf = ostd::appender(); cscript::util::tvals_concat( buf, ostd::iter(&args[numargs - numconc], numconc), ((op & CODE_OP_MASK) == CODE_CONC) ? " " : "" @@ -1369,7 +1369,7 @@ static ostd::Uint32 const *runcode( case CODE_CONCM | RET_FLOAT: case CODE_CONCM | RET_INT: { int numconc = op >> 8; - auto buf = ostd::appender(); + auto buf = ostd::appender(); cscript::util::tvals_concat( buf, ostd::iter(&args[numargs - numconc], numconc) ); @@ -1653,26 +1653,26 @@ void CsState::run_ret(Ident *id, CsValueRange args, CsValue &ret) { --rundepth; } -ostd::String CsState::run_str(Bytecode const *code) { +CsString CsState::run_str(Bytecode const *code) { CsValue ret; run_ret(code, ret); - ostd::String s = ret.get_str(); + CsString s = ret.get_str(); ret.cleanup(); return s; } -ostd::String CsState::run_str(ostd::ConstCharRange code) { +CsString CsState::run_str(ostd::ConstCharRange code) { CsValue ret; run_ret(code, ret); - ostd::String s = ret.get_str(); + CsString s = ret.get_str(); ret.cleanup(); return s; } -ostd::String CsState::run_str(Ident *id, CsValueRange args) { +CsString CsState::run_str(Ident *id, CsValueRange args) { CsValue ret; run_ret(id, args, ret); - ostd::String s = ret.get_str(); + CsString s = ret.get_str(); ret.cleanup(); return s; } @@ -1794,12 +1794,12 @@ static bool cs_run_file( return true; } -ostd::Maybe CsState::run_file_str(ostd::ConstCharRange fname) { +ostd::Maybe CsState::run_file_str(ostd::ConstCharRange fname) { CsValue ret; if (!cs_run_file(*this, fname, ret)) { return ostd::nothing; } - ostd::String s = ret.get_str(); + CsString s = ret.get_str(); ret.cleanup(); return ostd::move(s); } diff --git a/cs_vm.hh b/cs_vm.hh index b9a345d..0da3aee 100644 --- a/cs_vm.hh +++ b/cs_vm.hh @@ -136,7 +136,7 @@ ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str); struct GenState { CsState &cs; - ostd::Vector code; + CsVector code; char const *source; GenState() = delete; @@ -235,8 +235,8 @@ struct GenState { } }; -ostd::String intstr(int v); -ostd::String floatstr(CsFloat v); +CsString intstr(int v); +CsString floatstr(CsFloat v); bool cs_check_num(ostd::ConstCharRange s); diff --git a/cubescript.cc b/cubescript.cc index 7577122..fb37e2e 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -4,13 +4,13 @@ namespace cscript { -ostd::String intstr(CsInt v) { +CsString intstr(CsInt v) { char buf[256]; snprintf(buf, sizeof(buf), IntFormat, v); return buf; } -ostd::String floatstr(CsFloat v) { +CsString floatstr(CsFloat v) { char buf[256]; snprintf(buf, sizeof(buf), v == CsInt(v) ? RoundFloatFormat : FloatFormat, v); return buf; @@ -524,7 +524,7 @@ CsInt CsValue::force_int() { } ostd::ConstCharRange CsValue::force_str() { - ostd::String rs; + CsString rs; switch (get_type()) { case VAL_FLOAT: rs = ostd::move(floatstr(f)); @@ -586,7 +586,7 @@ Ident *CsValue::get_ident() const { return id; } -ostd::String CsValue::get_str() const { +CsString CsValue::get_str() const { switch (get_type()) { case VAL_STR: case VAL_MACRO: @@ -597,7 +597,7 @@ ostd::String CsValue::get_str() const { case VAL_FLOAT: return floatstr(f); } - return ostd::String(""); + return CsString(""); } ostd::ConstCharRange CsValue::get_strr() const { @@ -938,12 +938,12 @@ ostd::Maybe CsState::get_var_float(ostd::ConstCharRange name) { return *static_cast(id)->p_storage; } -ostd::Maybe CsState::get_var_str(ostd::ConstCharRange name) { +ostd::Maybe CsState::get_var_str(ostd::ConstCharRange name) { Ident *id = get_ident(name); if (!id || id->is_svar()) { return ostd::nothing; } - return ostd::String(*static_cast(id)->p_storage); + return CsString(*static_cast(id)->p_storage); } ostd::Maybe CsState::get_var_min_int(ostd::ConstCharRange name) { @@ -978,7 +978,7 @@ ostd::Maybe CsState::get_var_max_float(ostd::ConstCharRange name) { return static_cast(id)->get_val_max(); } -ostd::Maybe +ostd::Maybe CsState::get_alias_val(ostd::ConstCharRange name) { Alias *a = get_alias(name); if (!a) { @@ -1212,12 +1212,12 @@ static inline void cs_loop_conc( } Alias &a = static_cast(id); IdentStack stack; - ostd::Vector s; + CsVector s; for (CsInt i = 0; i < n; ++i) { cs_set_iter(a, offset + i * step, stack); CsValue v; cs.run_ret(body, v); - ostd::String vstr = ostd::move(v.get_str()); + CsString vstr = ostd::move(v.get_str()); if (space && i) { s.push(' '); } @@ -1338,7 +1338,7 @@ void cs_init_lib_base(CsState &cs) { }); cs_add_command(cs, "cases", "ste2V", [&cs](CsValueRange args, CsValue &res) { - ostd::String val = args[0].get_str(); + CsString val = args[0].get_str(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ((args[i].get_type() == VAL_NULL) || (args[i].get_str() == val)) { cs.run_ret(args[i + 1].code, res); diff --git a/cubescript.hh b/cubescript.hh index f26ceb2..d006efe 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -89,7 +89,7 @@ struct OSTD_EXPORT CsValue { p_type = VAL_FLOAT; f = val; } - void set_str(ostd::String val) { + void set_str(CsString val) { if (val.size() == 0) { /* ostd zero length strings cannot be disowned */ char *buf = new char[1]; @@ -134,7 +134,7 @@ struct OSTD_EXPORT CsValue { tv.p_type = VAL_NULL; } - ostd::String get_str() const; + CsString get_str() const; ostd::ConstCharRange get_strr() const; CsInt get_int() const; CsFloat get_float() const; @@ -215,7 +215,7 @@ struct OSTD_EXPORT Ident { protected: Ident(IdentType tp, ostd::ConstCharRange name, int flags = 0); - ostd::String p_name; + CsString p_name; /* represents the IdentType above, but internally it has a wider variety * of values, so it's an int here (maps to an internal enum) */ @@ -319,7 +319,7 @@ struct OSTD_EXPORT Alias: Ident { val_v.set_mstr(val); } - void set_value_str(ostd::String val) { + void set_value_str(CsString val) { val_v.set_str(ostd::move(val)); } @@ -369,8 +369,8 @@ enum { using CmdFunc = ostd::Function; struct OSTD_EXPORT CsState { - ostd::Map idents; - ostd::Vector identmap; + CsMap idents; + CsVector identmap; Ident *dummy = nullptr; @@ -393,7 +393,6 @@ struct OSTD_EXPORT CsState { void clear_override(Ident &id); void clear_overrides(); - Ident *add_ident(Ident *id); Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN); Ident *force_ident(CsValue &v); @@ -429,9 +428,9 @@ struct OSTD_EXPORT CsState { ostd::ConstCharRange name, ostd::ConstCharRange args, CmdFunc func ); - ostd::String run_str(Bytecode const *code); - ostd::String run_str(ostd::ConstCharRange code); - ostd::String run_str(Ident *id, CsValueRange args); + CsString run_str(Bytecode const *code); + CsString run_str(ostd::ConstCharRange code); + CsString run_str(Ident *id, CsValueRange args); CsInt run_int(Bytecode const *code); CsInt run_int(ostd::ConstCharRange code); @@ -453,7 +452,7 @@ struct OSTD_EXPORT CsState { void run(ostd::ConstCharRange code); void run(Ident *id, CsValueRange args); - ostd::Maybe run_file_str(ostd::ConstCharRange fname); + ostd::Maybe run_file_str(ostd::ConstCharRange fname); ostd::Maybe run_file_int(ostd::ConstCharRange fname); ostd::Maybe run_file_float(ostd::ConstCharRange fname); ostd::Maybe run_file_bool(ostd::ConstCharRange fname); @@ -481,7 +480,7 @@ struct OSTD_EXPORT CsState { ostd::Maybe get_var_int(ostd::ConstCharRange name); ostd::Maybe get_var_float(ostd::ConstCharRange name); - ostd::Maybe get_var_str(ostd::ConstCharRange name); + ostd::Maybe get_var_str(ostd::ConstCharRange name); ostd::Maybe get_var_min_int(ostd::ConstCharRange name); ostd::Maybe get_var_max_int(ostd::ConstCharRange name); @@ -489,12 +488,15 @@ struct OSTD_EXPORT CsState { ostd::Maybe get_var_min_float(ostd::ConstCharRange name); ostd::Maybe get_var_max_float(ostd::ConstCharRange name); - ostd::Maybe get_alias_val(ostd::ConstCharRange name); + ostd::Maybe get_alias_val(ostd::ConstCharRange name); void print_var(Var *v); void print_var_int(Ivar *iv, CsInt i); void print_var_float(Fvar *fv, CsFloat f); void print_var_str(Svar *sv, ostd::ConstCharRange s); + +private: + Ident *add_ident(Ident *id); }; struct OSTD_EXPORT StackedValue: CsValue { @@ -626,14 +628,14 @@ namespace util { void skip(); bool parse(); - ostd::String element(); + CsString element(); }; ostd::Size list_length(ostd::ConstCharRange s); - ostd::Maybe list_index( + ostd::Maybe list_index( ostd::ConstCharRange s, ostd::Size idx ); - ostd::Vector list_explode( + CsVector list_explode( ostd::ConstCharRange s, ostd::Size limit = -1 ); @@ -657,7 +659,7 @@ namespace util { ) { ostd::Size ret = 0; for (ostd::Size i = 0; i < vals.size(); ++i) { - auto s = ostd::appender(); + auto s = ostd::appender(); switch (vals[i].get_type()) { case VAL_INT: { auto r = format_int(ostd::forward(writer), vals[i].i); diff --git a/cubescript_conf.hh b/cubescript_conf.hh new file mode 100644 index 0000000..1017e8b --- /dev/null +++ b/cubescript_conf.hh @@ -0,0 +1,36 @@ +#ifndef LIBCUBESCRIPT_CUBESCRIPT_CONF_HH +#define LIBCUBESCRIPT_CUBESCRIPT_CONF_HH + +#include +#include +#include +#include +#include +#include + +namespace cscript { + template + using CsAllocator = ostd::Allocator; + + using CsInt = int; + using CsFloat = float; + using CsString = ostd::StringBase>; + + template + using CsMap = ostd::Map< + K, V, ostd::ToHash, ostd::EqualWithCstr, + CsAllocator> + >; + + template + using CsVector = ostd::Vector>; + + constexpr CsInt const CsIntMin = INT_MIN; + constexpr CsInt const CsIntMax = INT_MAX; + + constexpr auto const IntFormat = "%d"; + constexpr auto const FloatFormat = "%.7g"; + constexpr auto const RoundFloatFormat = "%.1f"; +} /* namespace cscript */ + +#endif /* LIBCUBESCRIPT_CUBESCRIPT_CONF_HH */ \ No newline at end of file diff --git a/lib_list.cc b/lib_list.cc index e978467..f2a4669 100644 --- a/lib_list.cc +++ b/lib_list.cc @@ -82,7 +82,7 @@ static void cs_loop_list_conc( return; } IdentStack stack; - ostd::Vector r; + CsVector r; int n = 0; for (util::ListParser p(list); p.parse(); ++n) { char *val = p.element().disown(); @@ -92,7 +92,7 @@ static void cs_loop_list_conc( } CsValue v; cs.run_ret(body, v); - ostd::String vstr = ostd::move(v.get_str()); + CsString vstr = ostd::move(v.get_str()); r.push_n(vstr.data(), vstr.size()); v.cleanup(); } @@ -119,7 +119,7 @@ template static inline void cs_list_merge(CsValueRange args, CsValue &res, F cmp) { ostd::ConstCharRange list = args[0].get_strr(); ostd::ConstCharRange elems = args[1].get_strr(); - ostd::Vector buf; + CsVector buf; if (PushList) { buf.push_n(list.data(), list.size()); } @@ -150,7 +150,7 @@ void cs_init_lib_list(CsState &cs) { if (args.empty()) { return; } - ostd::String str = ostd::move(args[0].get_str()); + CsString str = ostd::move(args[0].get_str()); util::ListParser p(str); p.item = str; for (ostd::Size i = 1; i < args.size(); ++i) { @@ -390,7 +390,7 @@ found: return; } IdentStack stack; - ostd::Vector r; + CsVector r; int n = 0; for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) { char *val = cs_dup_ostr(p.item); @@ -432,7 +432,7 @@ found: }); cs.add_command("prettylist", "ss", [](CsValueRange args, CsValue &res) { - ostd::Vector buf; + CsVector buf; ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange conj = args[1].get_strr(); ostd::Size len = util::list_length(s); @@ -494,7 +494,7 @@ found: } } char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list; - ostd::Vector buf; + CsVector buf; if (qend > list) { buf.push_n(list, qend - list); } @@ -560,7 +560,7 @@ static void cs_list_sort( Alias *xa = static_cast(x), *ya = static_cast(y); - ostd::Vector items; + CsVector items; ostd::Size clen = list.size(); ostd::Size total = 0; diff --git a/lib_str.cc b/lib_str.cc index 4b157b8..59cc5c4 100644 --- a/lib_str.cc +++ b/lib_str.cc @@ -77,7 +77,7 @@ void cs_init_lib_string(CsState &cs) { }); cs.add_command("escape", "s", [](CsValueRange args, CsValue &res) { - auto x = ostd::appender(); + auto x = ostd::appender(); util::escape_string(x, args[0].get_strr()); ostd::Size len = x.size(); res.set_mstr(ostd::CharRange(x.get().disown(), len)); @@ -93,14 +93,14 @@ void cs_init_lib_string(CsState &cs) { }); cs.add_command("concat", "V", [](CsValueRange args, CsValue &res) { - auto s = ostd::appender(); + auto s = ostd::appender(); cscript::util::tvals_concat(s, args, " "); res.set_mstr(s.get().iter()); s.get().disown(); }); cs.add_command("concatword", "V", [](CsValueRange args, CsValue &res) { - auto s = ostd::appender(); + auto s = ostd::appender(); cscript::util::tvals_concat(s, args); res.set_mstr(s.get().iter()); s.get().disown(); @@ -110,8 +110,8 @@ void cs_init_lib_string(CsState &cs) { if (args.empty()) { return; } - ostd::Vector s; - ostd::String fs = ostd::move(args[0].get_str()); + CsVector s; + CsString fs = ostd::move(args[0].get_str()); ostd::ConstCharRange f = fs.iter(); while (!f.empty()) { char c = *f; @@ -121,10 +121,10 @@ void cs_init_lib_string(CsState &cs) { ++f; if (ic >= '1' && ic <= '9') { int i = ic - '0'; - ostd::String sub = ostd::move( + CsString sub = ostd::move( (ostd::Size(i) < args.size()) ? args[i].get_str() - : ostd::String("") + : CsString("") ); s.push_n(sub.data(), sub.size()); } else { @@ -140,7 +140,7 @@ void cs_init_lib_string(CsState &cs) { }); cs.add_command("tohex", "ii", [](CsValueRange args, CsValue &res) { - auto r = ostd::appender>(); + auto r = ostd::appender>(); ostd::format( r, "0x%.*X", ostd::max(args[1].get_int(), 1), args[0].get_int() ); @@ -190,7 +190,7 @@ void cs_init_lib_string(CsState &cs) { if (newval2.empty()) { newval2 = newval; } - ostd::Vector buf; + CsVector buf; if (!oldval.size()) { res.set_str(s); return;