initial support for custom allocators
parent
d80e922913
commit
b8f3e527d5
10
cs_gen.cc
10
cs_gen.cc
|
@ -227,8 +227,8 @@ endblock:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String ListParser::element() {
|
CsString ListParser::element() {
|
||||||
ostd::String s;
|
CsString s;
|
||||||
s.reserve(item.size());
|
s.reserve(item.size());
|
||||||
if (!quote.empty() && (*quote == '"')) {
|
if (!quote.empty() && (*quote == '"')) {
|
||||||
auto writer = s.iter_cap();
|
auto writer = s.iter_cap();
|
||||||
|
@ -251,7 +251,7 @@ endblock:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::Maybe<ostd::String> list_index(
|
ostd::Maybe<CsString> list_index(
|
||||||
ostd::ConstCharRange s, ostd::Size idx
|
ostd::ConstCharRange s, ostd::Size idx
|
||||||
) {
|
) {
|
||||||
ListParser p(s);
|
ListParser p(s);
|
||||||
|
@ -266,10 +266,10 @@ endblock:
|
||||||
return ostd::move(p.element());
|
return ostd::move(p.element());
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::Vector<ostd::String> list_explode(
|
CsVector<CsString> list_explode(
|
||||||
ostd::ConstCharRange s, ostd::Size limit
|
ostd::ConstCharRange s, ostd::Size limit
|
||||||
) {
|
) {
|
||||||
ostd::Vector<ostd::String> ret;
|
CsVector<CsString> ret;
|
||||||
ListParser p(s);
|
ListParser p(s);
|
||||||
while ((ret.size() < limit) && p.parse()) {
|
while ((ret.size() < limit) && p.parse()) {
|
||||||
ret.push(ostd::move(p.element()));
|
ret.push(ostd::move(p.element()));
|
||||||
|
|
24
cs_vm.cc
24
cs_vm.cc
|
@ -408,7 +408,7 @@ static inline void callcommand(
|
||||||
break;
|
break;
|
||||||
case 'C': {
|
case 'C': {
|
||||||
i = ostd::max(i + 1, numargs);
|
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), " ");
|
cscript::util::tvals_concat(buf, ostd::iter(args, i), " ");
|
||||||
CsValue tv;
|
CsValue tv;
|
||||||
tv.set_mstr(buf.get().iter());
|
tv.set_mstr(buf.get().iter());
|
||||||
|
@ -1329,7 +1329,7 @@ static ostd::Uint32 const *runcode(
|
||||||
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
|
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
|
||||||
result.force_null();
|
result.force_null();
|
||||||
{
|
{
|
||||||
auto buf = ostd::appender<ostd::String>();
|
auto buf = ostd::appender<CsString>();
|
||||||
cscript::util::tvals_concat(
|
cscript::util::tvals_concat(
|
||||||
buf, ostd::iter(&args[offset], callargs), " "
|
buf, ostd::iter(&args[offset], callargs), " "
|
||||||
);
|
);
|
||||||
|
@ -1351,7 +1351,7 @@ static ostd::Uint32 const *runcode(
|
||||||
case CODE_CONCW | RET_FLOAT:
|
case CODE_CONCW | RET_FLOAT:
|
||||||
case CODE_CONCW | RET_INT: {
|
case CODE_CONCW | RET_INT: {
|
||||||
int numconc = op >> 8;
|
int numconc = op >> 8;
|
||||||
auto buf = ostd::appender<ostd::String>();
|
auto buf = ostd::appender<CsString>();
|
||||||
cscript::util::tvals_concat(
|
cscript::util::tvals_concat(
|
||||||
buf, ostd::iter(&args[numargs - numconc], numconc),
|
buf, ostd::iter(&args[numargs - numconc], numconc),
|
||||||
((op & CODE_OP_MASK) == CODE_CONC) ? " " : ""
|
((op & CODE_OP_MASK) == CODE_CONC) ? " " : ""
|
||||||
|
@ -1369,7 +1369,7 @@ static ostd::Uint32 const *runcode(
|
||||||
case CODE_CONCM | RET_FLOAT:
|
case CODE_CONCM | RET_FLOAT:
|
||||||
case CODE_CONCM | RET_INT: {
|
case CODE_CONCM | RET_INT: {
|
||||||
int numconc = op >> 8;
|
int numconc = op >> 8;
|
||||||
auto buf = ostd::appender<ostd::String>();
|
auto buf = ostd::appender<CsString>();
|
||||||
cscript::util::tvals_concat(
|
cscript::util::tvals_concat(
|
||||||
buf, ostd::iter(&args[numargs - numconc], numconc)
|
buf, ostd::iter(&args[numargs - numconc], numconc)
|
||||||
);
|
);
|
||||||
|
@ -1653,26 +1653,26 @@ void CsState::run_ret(Ident *id, CsValueRange args, CsValue &ret) {
|
||||||
--rundepth;
|
--rundepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String CsState::run_str(Bytecode const *code) {
|
CsString CsState::run_str(Bytecode const *code) {
|
||||||
CsValue ret;
|
CsValue ret;
|
||||||
run_ret(code, ret);
|
run_ret(code, ret);
|
||||||
ostd::String s = ret.get_str();
|
CsString s = ret.get_str();
|
||||||
ret.cleanup();
|
ret.cleanup();
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String CsState::run_str(ostd::ConstCharRange code) {
|
CsString CsState::run_str(ostd::ConstCharRange code) {
|
||||||
CsValue ret;
|
CsValue ret;
|
||||||
run_ret(code, ret);
|
run_ret(code, ret);
|
||||||
ostd::String s = ret.get_str();
|
CsString s = ret.get_str();
|
||||||
ret.cleanup();
|
ret.cleanup();
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String CsState::run_str(Ident *id, CsValueRange args) {
|
CsString CsState::run_str(Ident *id, CsValueRange args) {
|
||||||
CsValue ret;
|
CsValue ret;
|
||||||
run_ret(id, args, ret);
|
run_ret(id, args, ret);
|
||||||
ostd::String s = ret.get_str();
|
CsString s = ret.get_str();
|
||||||
ret.cleanup();
|
ret.cleanup();
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -1794,12 +1794,12 @@ static bool cs_run_file(
|
||||||
return true;
|
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;
|
CsValue ret;
|
||||||
if (!cs_run_file(*this, fname, ret)) {
|
if (!cs_run_file(*this, fname, ret)) {
|
||||||
return ostd::nothing;
|
return ostd::nothing;
|
||||||
}
|
}
|
||||||
ostd::String s = ret.get_str();
|
CsString s = ret.get_str();
|
||||||
ret.cleanup();
|
ret.cleanup();
|
||||||
return ostd::move(s);
|
return ostd::move(s);
|
||||||
}
|
}
|
||||||
|
|
6
cs_vm.hh
6
cs_vm.hh
|
@ -136,7 +136,7 @@ ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str);
|
||||||
|
|
||||||
struct GenState {
|
struct GenState {
|
||||||
CsState &cs;
|
CsState &cs;
|
||||||
ostd::Vector<ostd::Uint32> code;
|
CsVector<ostd::Uint32> code;
|
||||||
char const *source;
|
char const *source;
|
||||||
|
|
||||||
GenState() = delete;
|
GenState() = delete;
|
||||||
|
@ -235,8 +235,8 @@ struct GenState {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ostd::String intstr(int v);
|
CsString intstr(int v);
|
||||||
ostd::String floatstr(CsFloat v);
|
CsString floatstr(CsFloat v);
|
||||||
|
|
||||||
bool cs_check_num(ostd::ConstCharRange s);
|
bool cs_check_num(ostd::ConstCharRange s);
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,13 @@
|
||||||
|
|
||||||
namespace cscript {
|
namespace cscript {
|
||||||
|
|
||||||
ostd::String intstr(CsInt v) {
|
CsString intstr(CsInt v) {
|
||||||
char buf[256];
|
char buf[256];
|
||||||
snprintf(buf, sizeof(buf), IntFormat, v);
|
snprintf(buf, sizeof(buf), IntFormat, v);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String floatstr(CsFloat v) {
|
CsString floatstr(CsFloat v) {
|
||||||
char buf[256];
|
char buf[256];
|
||||||
snprintf(buf, sizeof(buf), v == CsInt(v) ? RoundFloatFormat : FloatFormat, v);
|
snprintf(buf, sizeof(buf), v == CsInt(v) ? RoundFloatFormat : FloatFormat, v);
|
||||||
return buf;
|
return buf;
|
||||||
|
@ -524,7 +524,7 @@ CsInt CsValue::force_int() {
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::ConstCharRange CsValue::force_str() {
|
ostd::ConstCharRange CsValue::force_str() {
|
||||||
ostd::String rs;
|
CsString rs;
|
||||||
switch (get_type()) {
|
switch (get_type()) {
|
||||||
case VAL_FLOAT:
|
case VAL_FLOAT:
|
||||||
rs = ostd::move(floatstr(f));
|
rs = ostd::move(floatstr(f));
|
||||||
|
@ -586,7 +586,7 @@ Ident *CsValue::get_ident() const {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String CsValue::get_str() const {
|
CsString CsValue::get_str() const {
|
||||||
switch (get_type()) {
|
switch (get_type()) {
|
||||||
case VAL_STR:
|
case VAL_STR:
|
||||||
case VAL_MACRO:
|
case VAL_MACRO:
|
||||||
|
@ -597,7 +597,7 @@ ostd::String CsValue::get_str() const {
|
||||||
case VAL_FLOAT:
|
case VAL_FLOAT:
|
||||||
return floatstr(f);
|
return floatstr(f);
|
||||||
}
|
}
|
||||||
return ostd::String("");
|
return CsString("");
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::ConstCharRange CsValue::get_strr() const {
|
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;
|
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);
|
Ident *id = get_ident(name);
|
||||||
if (!id || id->is_svar()) {
|
if (!id || id->is_svar()) {
|
||||||
return ostd::nothing;
|
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) {
|
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();
|
return static_cast<Fvar *>(id)->get_val_max();
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::Maybe<ostd::String>
|
ostd::Maybe<CsString>
|
||||||
CsState::get_alias_val(ostd::ConstCharRange name) {
|
CsState::get_alias_val(ostd::ConstCharRange name) {
|
||||||
Alias *a = get_alias(name);
|
Alias *a = get_alias(name);
|
||||||
if (!a) {
|
if (!a) {
|
||||||
|
@ -1212,12 +1212,12 @@ static inline void cs_loop_conc(
|
||||||
}
|
}
|
||||||
Alias &a = static_cast<Alias &>(id);
|
Alias &a = static_cast<Alias &>(id);
|
||||||
IdentStack stack;
|
IdentStack stack;
|
||||||
ostd::Vector<char> s;
|
CsVector<char> s;
|
||||||
for (CsInt i = 0; i < n; ++i) {
|
for (CsInt i = 0; i < n; ++i) {
|
||||||
cs_set_iter(a, offset + i * step, stack);
|
cs_set_iter(a, offset + i * step, stack);
|
||||||
CsValue v;
|
CsValue v;
|
||||||
cs.run_ret(body, v);
|
cs.run_ret(body, v);
|
||||||
ostd::String vstr = ostd::move(v.get_str());
|
CsString vstr = ostd::move(v.get_str());
|
||||||
if (space && i) {
|
if (space && i) {
|
||||||
s.push(' ');
|
s.push(' ');
|
||||||
}
|
}
|
||||||
|
@ -1338,7 +1338,7 @@ void cs_init_lib_base(CsState &cs) {
|
||||||
});
|
});
|
||||||
|
|
||||||
cs_add_command(cs, "cases", "ste2V", [&cs](CsValueRange args, CsValue &res) {
|
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) {
|
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
|
||||||
if ((args[i].get_type() == VAL_NULL) || (args[i].get_str() == val)) {
|
if ((args[i].get_type() == VAL_NULL) || (args[i].get_str() == val)) {
|
||||||
cs.run_ret(args[i + 1].code, res);
|
cs.run_ret(args[i + 1].code, res);
|
||||||
|
|
|
@ -89,7 +89,7 @@ struct OSTD_EXPORT CsValue {
|
||||||
p_type = VAL_FLOAT;
|
p_type = VAL_FLOAT;
|
||||||
f = val;
|
f = val;
|
||||||
}
|
}
|
||||||
void set_str(ostd::String val) {
|
void set_str(CsString val) {
|
||||||
if (val.size() == 0) {
|
if (val.size() == 0) {
|
||||||
/* ostd zero length strings cannot be disowned */
|
/* ostd zero length strings cannot be disowned */
|
||||||
char *buf = new char[1];
|
char *buf = new char[1];
|
||||||
|
@ -134,7 +134,7 @@ struct OSTD_EXPORT CsValue {
|
||||||
tv.p_type = VAL_NULL;
|
tv.p_type = VAL_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::String get_str() const;
|
CsString get_str() const;
|
||||||
ostd::ConstCharRange get_strr() const;
|
ostd::ConstCharRange get_strr() const;
|
||||||
CsInt get_int() const;
|
CsInt get_int() const;
|
||||||
CsFloat get_float() const;
|
CsFloat get_float() const;
|
||||||
|
@ -215,7 +215,7 @@ struct OSTD_EXPORT Ident {
|
||||||
protected:
|
protected:
|
||||||
Ident(IdentType tp, ostd::ConstCharRange name, int flags = 0);
|
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
|
/* represents the IdentType above, but internally it has a wider variety
|
||||||
* of values, so it's an int here (maps to an internal enum)
|
* 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);
|
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));
|
val_v.set_str(ostd::move(val));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,8 +369,8 @@ enum {
|
||||||
using CmdFunc = ostd::Function<void(CsValueRange, CsValue &)>;
|
using CmdFunc = ostd::Function<void(CsValueRange, CsValue &)>;
|
||||||
|
|
||||||
struct OSTD_EXPORT CsState {
|
struct OSTD_EXPORT CsState {
|
||||||
ostd::Map<ostd::ConstCharRange, Ident *> idents;
|
CsMap<ostd::ConstCharRange, Ident *> idents;
|
||||||
ostd::Vector<Ident *> identmap;
|
CsVector<Ident *> identmap;
|
||||||
|
|
||||||
Ident *dummy = nullptr;
|
Ident *dummy = nullptr;
|
||||||
|
|
||||||
|
@ -393,7 +393,6 @@ struct OSTD_EXPORT CsState {
|
||||||
void clear_override(Ident &id);
|
void clear_override(Ident &id);
|
||||||
void clear_overrides();
|
void clear_overrides();
|
||||||
|
|
||||||
Ident *add_ident(Ident *id);
|
|
||||||
Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN);
|
Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN);
|
||||||
Ident *force_ident(CsValue &v);
|
Ident *force_ident(CsValue &v);
|
||||||
|
|
||||||
|
@ -429,9 +428,9 @@ struct OSTD_EXPORT CsState {
|
||||||
ostd::ConstCharRange name, ostd::ConstCharRange args, CmdFunc func
|
ostd::ConstCharRange name, ostd::ConstCharRange args, CmdFunc func
|
||||||
);
|
);
|
||||||
|
|
||||||
ostd::String run_str(Bytecode const *code);
|
CsString run_str(Bytecode const *code);
|
||||||
ostd::String run_str(ostd::ConstCharRange code);
|
CsString run_str(ostd::ConstCharRange code);
|
||||||
ostd::String run_str(Ident *id, CsValueRange args);
|
CsString run_str(Ident *id, CsValueRange args);
|
||||||
|
|
||||||
CsInt run_int(Bytecode const *code);
|
CsInt run_int(Bytecode const *code);
|
||||||
CsInt run_int(ostd::ConstCharRange code);
|
CsInt run_int(ostd::ConstCharRange code);
|
||||||
|
@ -453,7 +452,7 @@ struct OSTD_EXPORT CsState {
|
||||||
void run(ostd::ConstCharRange code);
|
void run(ostd::ConstCharRange code);
|
||||||
void run(Ident *id, CsValueRange args);
|
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<CsInt> run_file_int(ostd::ConstCharRange fname);
|
||||||
ostd::Maybe<CsFloat> run_file_float(ostd::ConstCharRange fname);
|
ostd::Maybe<CsFloat> run_file_float(ostd::ConstCharRange fname);
|
||||||
ostd::Maybe<bool> run_file_bool(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<CsInt> get_var_int(ostd::ConstCharRange name);
|
||||||
ostd::Maybe<CsFloat> get_var_float(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_min_int(ostd::ConstCharRange name);
|
||||||
ostd::Maybe<CsInt> get_var_max_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_min_float(ostd::ConstCharRange name);
|
||||||
ostd::Maybe<CsFloat> get_var_max_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(Var *v);
|
||||||
void print_var_int(Ivar *iv, CsInt i);
|
void print_var_int(Ivar *iv, CsInt i);
|
||||||
void print_var_float(Fvar *fv, CsFloat f);
|
void print_var_float(Fvar *fv, CsFloat f);
|
||||||
void print_var_str(Svar *sv, ostd::ConstCharRange s);
|
void print_var_str(Svar *sv, ostd::ConstCharRange s);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ident *add_ident(Ident *id);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct OSTD_EXPORT StackedValue: CsValue {
|
struct OSTD_EXPORT StackedValue: CsValue {
|
||||||
|
@ -626,14 +628,14 @@ namespace util {
|
||||||
void skip();
|
void skip();
|
||||||
bool parse();
|
bool parse();
|
||||||
|
|
||||||
ostd::String element();
|
CsString element();
|
||||||
};
|
};
|
||||||
|
|
||||||
ostd::Size list_length(ostd::ConstCharRange s);
|
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::ConstCharRange s, ostd::Size idx
|
||||||
);
|
);
|
||||||
ostd::Vector<ostd::String> list_explode(
|
CsVector<CsString> list_explode(
|
||||||
ostd::ConstCharRange s, ostd::Size limit = -1
|
ostd::ConstCharRange s, ostd::Size limit = -1
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -657,7 +659,7 @@ namespace util {
|
||||||
) {
|
) {
|
||||||
ostd::Size ret = 0;
|
ostd::Size ret = 0;
|
||||||
for (ostd::Size i = 0; i < vals.size(); ++i) {
|
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()) {
|
switch (vals[i].get_type()) {
|
||||||
case VAL_INT: {
|
case VAL_INT: {
|
||||||
auto r = format_int(ostd::forward<R>(writer), vals[i].i);
|
auto r = format_int(ostd::forward<R>(writer), vals[i].i);
|
||||||
|
|
|
@ -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 */
|
16
lib_list.cc
16
lib_list.cc
|
@ -82,7 +82,7 @@ static void cs_loop_list_conc(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
IdentStack stack;
|
IdentStack stack;
|
||||||
ostd::Vector<char> r;
|
CsVector<char> r;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(list); p.parse(); ++n) {
|
for (util::ListParser p(list); p.parse(); ++n) {
|
||||||
char *val = p.element().disown();
|
char *val = p.element().disown();
|
||||||
|
@ -92,7 +92,7 @@ static void cs_loop_list_conc(
|
||||||
}
|
}
|
||||||
CsValue v;
|
CsValue v;
|
||||||
cs.run_ret(body, 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());
|
r.push_n(vstr.data(), vstr.size());
|
||||||
v.cleanup();
|
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) {
|
static inline void cs_list_merge(CsValueRange args, CsValue &res, F cmp) {
|
||||||
ostd::ConstCharRange list = args[0].get_strr();
|
ostd::ConstCharRange list = args[0].get_strr();
|
||||||
ostd::ConstCharRange elems = args[1].get_strr();
|
ostd::ConstCharRange elems = args[1].get_strr();
|
||||||
ostd::Vector<char> buf;
|
CsVector<char> buf;
|
||||||
if (PushList) {
|
if (PushList) {
|
||||||
buf.push_n(list.data(), list.size());
|
buf.push_n(list.data(), list.size());
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ void cs_init_lib_list(CsState &cs) {
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ostd::String str = ostd::move(args[0].get_str());
|
CsString str = ostd::move(args[0].get_str());
|
||||||
util::ListParser p(str);
|
util::ListParser p(str);
|
||||||
p.item = str;
|
p.item = str;
|
||||||
for (ostd::Size i = 1; i < args.size(); ++i) {
|
for (ostd::Size i = 1; i < args.size(); ++i) {
|
||||||
|
@ -390,7 +390,7 @@ found:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
IdentStack stack;
|
IdentStack stack;
|
||||||
ostd::Vector<char> r;
|
CsVector<char> r;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
||||||
char *val = cs_dup_ostr(p.item);
|
char *val = cs_dup_ostr(p.item);
|
||||||
|
@ -432,7 +432,7 @@ found:
|
||||||
});
|
});
|
||||||
|
|
||||||
cs.add_command("prettylist", "ss", [](CsValueRange args, CsValue &res) {
|
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 s = args[0].get_strr();
|
||||||
ostd::ConstCharRange conj = args[1].get_strr();
|
ostd::ConstCharRange conj = args[1].get_strr();
|
||||||
ostd::Size len = util::list_length(s);
|
ostd::Size len = util::list_length(s);
|
||||||
|
@ -494,7 +494,7 @@ found:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
||||||
ostd::Vector<char> buf;
|
CsVector<char> buf;
|
||||||
if (qend > list) {
|
if (qend > list) {
|
||||||
buf.push_n(list, 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);
|
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 clen = list.size();
|
||||||
ostd::Size total = 0;
|
ostd::Size total = 0;
|
||||||
|
|
||||||
|
|
18
lib_str.cc
18
lib_str.cc
|
@ -77,7 +77,7 @@ void cs_init_lib_string(CsState &cs) {
|
||||||
});
|
});
|
||||||
|
|
||||||
cs.add_command("escape", "s", [](CsValueRange args, CsValue &res) {
|
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());
|
util::escape_string(x, args[0].get_strr());
|
||||||
ostd::Size len = x.size();
|
ostd::Size len = x.size();
|
||||||
res.set_mstr(ostd::CharRange(x.get().disown(), len));
|
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) {
|
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, " ");
|
cscript::util::tvals_concat(s, args, " ");
|
||||||
res.set_mstr(s.get().iter());
|
res.set_mstr(s.get().iter());
|
||||||
s.get().disown();
|
s.get().disown();
|
||||||
});
|
});
|
||||||
|
|
||||||
cs.add_command("concatword", "V", [](CsValueRange args, CsValue &res) {
|
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);
|
cscript::util::tvals_concat(s, args);
|
||||||
res.set_mstr(s.get().iter());
|
res.set_mstr(s.get().iter());
|
||||||
s.get().disown();
|
s.get().disown();
|
||||||
|
@ -110,8 +110,8 @@ void cs_init_lib_string(CsState &cs) {
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ostd::Vector<char> s;
|
CsVector<char> s;
|
||||||
ostd::String fs = ostd::move(args[0].get_str());
|
CsString fs = ostd::move(args[0].get_str());
|
||||||
ostd::ConstCharRange f = fs.iter();
|
ostd::ConstCharRange f = fs.iter();
|
||||||
while (!f.empty()) {
|
while (!f.empty()) {
|
||||||
char c = *f;
|
char c = *f;
|
||||||
|
@ -121,10 +121,10 @@ void cs_init_lib_string(CsState &cs) {
|
||||||
++f;
|
++f;
|
||||||
if (ic >= '1' && ic <= '9') {
|
if (ic >= '1' && ic <= '9') {
|
||||||
int i = ic - '0';
|
int i = ic - '0';
|
||||||
ostd::String sub = ostd::move(
|
CsString sub = ostd::move(
|
||||||
(ostd::Size(i) < args.size())
|
(ostd::Size(i) < args.size())
|
||||||
? args[i].get_str()
|
? args[i].get_str()
|
||||||
: ostd::String("")
|
: CsString("")
|
||||||
);
|
);
|
||||||
s.push_n(sub.data(), sub.size());
|
s.push_n(sub.data(), sub.size());
|
||||||
} else {
|
} else {
|
||||||
|
@ -140,7 +140,7 @@ void cs_init_lib_string(CsState &cs) {
|
||||||
});
|
});
|
||||||
|
|
||||||
cs.add_command("tohex", "ii", [](CsValueRange args, CsValue &res) {
|
cs.add_command("tohex", "ii", [](CsValueRange args, CsValue &res) {
|
||||||
auto r = ostd::appender<ostd::Vector<char>>();
|
auto r = ostd::appender<CsVector<char>>();
|
||||||
ostd::format(
|
ostd::format(
|
||||||
r, "0x%.*X", ostd::max(args[1].get_int(), 1), args[0].get_int()
|
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()) {
|
if (newval2.empty()) {
|
||||||
newval2 = newval;
|
newval2 = newval;
|
||||||
}
|
}
|
||||||
ostd::Vector<char> buf;
|
CsVector<char> buf;
|
||||||
if (!oldval.size()) {
|
if (!oldval.size()) {
|
||||||
res.set_str(s);
|
res.set_str(s);
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue