initial support for custom allocators

master
Daniel Kolesa 2016-08-21 01:34:03 +01:00
parent d80e922913
commit b8f3e527d5
8 changed files with 103 additions and 65 deletions

View File

@ -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<ostd::String> list_index(
ostd::Maybe<CsString> list_index(
ostd::ConstCharRange s, ostd::Size idx
) {
ListParser p(s);
@ -266,10 +266,10 @@ endblock:
return ostd::move(p.element());
}
ostd::Vector<ostd::String> list_explode(
CsVector<CsString> list_explode(
ostd::ConstCharRange s, ostd::Size limit
) {
ostd::Vector<ostd::String> ret;
CsVector<CsString> ret;
ListParser p(s);
while ((ret.size() < limit) && p.parse()) {
ret.push(ostd::move(p.element()));

View File

@ -408,7 +408,7 @@ static inline void callcommand(
break;
case 'C': {
i = ostd::max(i + 1, numargs);
auto buf = ostd::appender<ostd::String>();
auto buf = ostd::appender<CsString>();
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<ostd::String>();
auto buf = ostd::appender<CsString>();
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<ostd::String>();
auto buf = ostd::appender<CsString>();
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<ostd::String>();
auto buf = ostd::appender<CsString>();
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<ostd::String> CsState::run_file_str(ostd::ConstCharRange fname) {
ostd::Maybe<CsString> 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);
}

View File

@ -136,7 +136,7 @@ ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str);
struct GenState {
CsState &cs;
ostd::Vector<ostd::Uint32> code;
CsVector<ostd::Uint32> 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);

View File

@ -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<CsFloat> CsState::get_var_float(ostd::ConstCharRange name) {
return *static_cast<Fvar *>(id)->p_storage;
}
ostd::Maybe<ostd::String> CsState::get_var_str(ostd::ConstCharRange name) {
ostd::Maybe<CsString> 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<Svar *>(id)->p_storage);
return CsString(*static_cast<Svar *>(id)->p_storage);
}
ostd::Maybe<CsInt> CsState::get_var_min_int(ostd::ConstCharRange name) {
@ -978,7 +978,7 @@ ostd::Maybe<CsFloat> CsState::get_var_max_float(ostd::ConstCharRange name) {
return static_cast<Fvar *>(id)->get_val_max();
}
ostd::Maybe<ostd::String>
ostd::Maybe<CsString>
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<Alias &>(id);
IdentStack stack;
ostd::Vector<char> s;
CsVector<char> 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);

View File

@ -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<void(CsValueRange, CsValue &)>;
struct OSTD_EXPORT CsState {
ostd::Map<ostd::ConstCharRange, Ident *> idents;
ostd::Vector<Ident *> identmap;
CsMap<ostd::ConstCharRange, Ident *> idents;
CsVector<Ident *> 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<ostd::String> run_file_str(ostd::ConstCharRange fname);
ostd::Maybe<CsString> run_file_str(ostd::ConstCharRange fname);
ostd::Maybe<CsInt> run_file_int(ostd::ConstCharRange fname);
ostd::Maybe<CsFloat> run_file_float(ostd::ConstCharRange fname);
ostd::Maybe<bool> run_file_bool(ostd::ConstCharRange fname);
@ -481,7 +480,7 @@ struct OSTD_EXPORT CsState {
ostd::Maybe<CsInt> get_var_int(ostd::ConstCharRange name);
ostd::Maybe<CsFloat> get_var_float(ostd::ConstCharRange name);
ostd::Maybe<ostd::String> get_var_str(ostd::ConstCharRange name);
ostd::Maybe<CsString> get_var_str(ostd::ConstCharRange name);
ostd::Maybe<CsInt> get_var_min_int(ostd::ConstCharRange name);
ostd::Maybe<CsInt> get_var_max_int(ostd::ConstCharRange name);
@ -489,12 +488,15 @@ struct OSTD_EXPORT CsState {
ostd::Maybe<CsFloat> get_var_min_float(ostd::ConstCharRange name);
ostd::Maybe<CsFloat> get_var_max_float(ostd::ConstCharRange name);
ostd::Maybe<ostd::String> get_alias_val(ostd::ConstCharRange name);
ostd::Maybe<CsString> 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<ostd::String> list_index(
ostd::Maybe<CsString> list_index(
ostd::ConstCharRange s, ostd::Size idx
);
ostd::Vector<ostd::String> list_explode(
CsVector<CsString> 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<ostd::String>();
auto s = ostd::appender<CsString>();
switch (vals[i].get_type()) {
case VAL_INT: {
auto r = format_int(ostd::forward<R>(writer), vals[i].i);

36
cubescript_conf.hh 100644
View File

@ -0,0 +1,36 @@
#ifndef LIBCUBESCRIPT_CUBESCRIPT_CONF_HH
#define LIBCUBESCRIPT_CUBESCRIPT_CONF_HH
#include <limits.h>
#include <ostd/types.hh>
#include <ostd/memory.hh>
#include <ostd/string.hh>
#include <ostd/vector.hh>
#include <ostd/map.hh>
namespace cscript {
template<typename T>
using CsAllocator = ostd::Allocator<T>;
using CsInt = int;
using CsFloat = float;
using CsString = ostd::StringBase<char, CsAllocator<char>>;
template<typename K, typename V>
using CsMap = ostd::Map<
K, V, ostd::ToHash<K>, ostd::EqualWithCstr<K>,
CsAllocator<ostd::Pair<K const, V const>>
>;
template<typename T>
using CsVector = ostd::Vector<T, CsAllocator<T>>;
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 */

View File

@ -82,7 +82,7 @@ static void cs_loop_list_conc(
return;
}
IdentStack stack;
ostd::Vector<char> r;
CsVector<char> 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<bool PushList, bool Swap, typename F>
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<char> buf;
CsVector<char> 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<char> r;
CsVector<char> 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<char> buf;
CsVector<char> 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<char> buf;
CsVector<char> buf;
if (qend > list) {
buf.push_n(list, qend - list);
}
@ -560,7 +560,7 @@ static void cs_list_sort(
Alias *xa = static_cast<Alias *>(x), *ya = static_cast<Alias *>(y);
ostd::Vector<ListSortItem> items;
CsVector<ListSortItem> items;
ostd::Size clen = list.size();
ostd::Size total = 0;

View File

@ -77,7 +77,7 @@ void cs_init_lib_string(CsState &cs) {
});
cs.add_command("escape", "s", [](CsValueRange args, CsValue &res) {
auto x = ostd::appender<ostd::String>();
auto x = ostd::appender<CsString>();
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<ostd::String>();
auto s = ostd::appender<CsString>();
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<ostd::String>();
auto s = ostd::appender<CsString>();
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<char> s;
ostd::String fs = ostd::move(args[0].get_str());
CsVector<char> 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<ostd::Vector<char>>();
auto r = ostd::appender<CsVector<char>>();
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<char> buf;
CsVector<char> buf;
if (!oldval.size()) {
res.set_str(s);
return;