rename TaggedValue

master
Daniel Kolesa 2016-08-18 19:38:30 +01:00
parent 84f2cb16e0
commit 303e219ad0
7 changed files with 283 additions and 283 deletions

122
cs_vm.cc
View File

@ -145,7 +145,7 @@ BytecodeRef &BytecodeRef::operator=(BytecodeRef &&v) {
return *this;
}
static inline ostd::Uint32 const *forcecode(CsState &cs, TaggedValue &v) {
static inline ostd::Uint32 const *forcecode(CsState &cs, CsValue &v) {
if (v.get_type() != VAL_CODE) {
GenState gs(cs);
gs.code.reserve(64);
@ -156,7 +156,7 @@ static inline ostd::Uint32 const *forcecode(CsState &cs, TaggedValue &v) {
return reinterpret_cast<ostd::Uint32 const *>(v.code);
}
static inline void forcecond(CsState &cs, TaggedValue &v) {
static inline void forcecond(CsState &cs, CsValue &v) {
switch (v.get_type()) {
case VAL_STR:
case VAL_MACRO:
@ -177,7 +177,7 @@ static ostd::Uint32 emptyblock[VAL_ANY][2] = {
{ CODE_START + 0x100, CODE_EXIT | RET_STR }
};
static inline void force_arg(TaggedValue &v, int type) {
static inline void force_arg(CsValue &v, int type) {
switch (type) {
case RET_STR:
if (v.get_type() != VAL_STR) {
@ -197,7 +197,7 @@ static inline void force_arg(TaggedValue &v, int type) {
}
}
static inline void free_args(TaggedValue *args, int &oldnum, int newnum) {
static inline void free_args(CsValue *args, int &oldnum, int newnum) {
for (int i = newnum; i < oldnum; i++) {
args[i].cleanup();
}
@ -205,7 +205,7 @@ static inline void free_args(TaggedValue *args, int &oldnum, int newnum) {
}
static ostd::Uint32 const *skipcode(
ostd::Uint32 const *code, TaggedValue *result = nullptr
ostd::Uint32 const *code, CsValue *result = nullptr
) {
int depth = 0;
for (;;) {
@ -247,7 +247,7 @@ static ostd::Uint32 const *skipcode(
}
}
void TaggedValue::copy_arg(TaggedValue &r) const {
void CsValue::copy_arg(CsValue &r) const {
r.cleanup();
switch (get_type()) {
case VAL_INT:
@ -276,7 +276,7 @@ void TaggedValue::copy_arg(TaggedValue &r) const {
}
static inline void callcommand(
CsState &cs, Command *id, TaggedValue *args, TaggedValue &res, int numargs,
CsState &cs, Command *id, CsValue *args, CsValue &res, int numargs,
bool lookup = false
) {
int i = -1, fakeargs = 0;
@ -410,9 +410,9 @@ static inline void callcommand(
i = ostd::max(i + 1, numargs);
auto buf = ostd::appender<ostd::String>();
cscript::util::tvals_concat(buf, ostd::iter(args, i), " ");
TaggedValue tv;
CsValue tv;
tv.set_mstr(buf.get().iter());
id->cb_cftv(TvalRange(&tv, 1), res);
id->cb_cftv(CsValueRange(&tv, 1), res);
goto cleanup;
}
case 'V':
@ -431,7 +431,7 @@ static inline void callcommand(
}
}
++i;
id->cb_cftv(TvalRange(args, i), res);
id->cb_cftv(CsValueRange(args, i), res);
cleanup:
for (ostd::Size k = 0; k < ostd::Size(i); ++k) {
args[k].cleanup();
@ -442,11 +442,11 @@ cleanup:
}
static ostd::Uint32 const *runcode(
CsState &cs, ostd::Uint32 const *code, TaggedValue &result
CsState &cs, ostd::Uint32 const *code, CsValue &result
);
static inline void cs_call_alias(
CsState &cs, Alias *a, TaggedValue *args, TaggedValue &result,
CsState &cs, Alias *a, CsValue *args, CsValue &result,
int callargs, int &nargs, int offset, int skip, ostd::Uint32 op
) {
IdentStack argstack[MaxArguments];
@ -509,7 +509,7 @@ static inline Alias *cs_get_lookuparg_id(CsState &cs, ostd::Uint32 op) {
}
static inline int cs_get_lookupu_type(
CsState &cs, TaggedValue &arg, Ident *&id, ostd::Uint32 op
CsState &cs, CsValue &arg, Ident *&id, ostd::Uint32 op
) {
if (
arg.get_type() != VAL_STR &&
@ -545,7 +545,7 @@ static inline int cs_get_lookupu_type(
case IdentType::command: {
arg.cleanup();
arg.set_null();
TaggedValue buf[MaxArguments];
CsValue buf[MaxArguments];
callcommand(cs, static_cast<Command *>(id), buf, arg, 0, true);
force_arg(arg, op & CODE_RET_MASK);
return -2; /* ignore */
@ -561,7 +561,7 @@ static inline int cs_get_lookupu_type(
}
static ostd::Uint32 const *runcode(
CsState &cs, ostd::Uint32 const *code, TaggedValue &result
CsState &cs, ostd::Uint32 const *code, CsValue &result
) {
result.set_null();
if (rundepth >= MaxRunDepth) {
@ -570,7 +570,7 @@ static ostd::Uint32 const *runcode(
}
++rundepth;
int numargs = 0;
TaggedValue args[MaxArguments + MaxResults];
CsValue args[MaxArguments + MaxResults];
for (;;) {
ostd::Uint32 op = *code++;
switch (op & 0xFF) {
@ -876,7 +876,7 @@ static ostd::Uint32 const *runcode(
continue;
}
case CODE_COMPILE: {
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
GenState gs(cs);
switch (arg.get_type()) {
case VAL_INT:
@ -914,7 +914,7 @@ static ostd::Uint32 const *runcode(
continue;
}
case CODE_COND: {
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (arg.get_type()) {
case VAL_STR:
case VAL_MACRO:
@ -950,7 +950,7 @@ static ostd::Uint32 const *runcode(
continue;
}
case CODE_IDENTU: {
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
Ident *id = cs.dummy;
if (
arg.get_type() == VAL_STR ||
@ -975,7 +975,7 @@ static ostd::Uint32 const *runcode(
case CODE_LOOKUPU | RET_STR: {
Ident *id = nullptr;
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_str(ostd::move(
@ -1018,7 +1018,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUPU | RET_INT: {
Ident *id = nullptr;
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_int(static_cast<Alias *>(id)->val_v.get_int());
@ -1059,7 +1059,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUPU | RET_FLOAT: {
Ident *id = nullptr;
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_float(
@ -1104,7 +1104,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUPU | RET_NULL: {
Ident *id = nullptr;
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
static_cast<Alias *>(id)->val_v.get_val(arg);
@ -1142,7 +1142,7 @@ static ostd::Uint32 const *runcode(
case CODE_LOOKUPMU | RET_STR: {
Ident *id = nullptr;
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
static_cast<Alias *>(id)->get_cstr(arg);
@ -1181,7 +1181,7 @@ static ostd::Uint32 const *runcode(
}
case CODE_LOOKUPMU | RET_NULL: {
Ident *id = nullptr;
TaggedValue &arg = args[numargs - 1];
CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
static_cast<Alias *>(id)->get_cval(arg);
@ -1308,7 +1308,7 @@ static ostd::Uint32 const *runcode(
Command *id = static_cast<Command *>(cs.identmap[op >> 8]);
int offset = numargs - id->numargs;
result.force_null();
id->cb_cftv(TvalRange(args + offset, id->numargs), result);
id->cb_cftv(CsValueRange(args + offset, id->numargs), result);
force_arg(result, op & CODE_RET_MASK);
free_args(args, numargs, offset);
continue;
@ -1338,9 +1338,9 @@ static ostd::Uint32 const *runcode(
cscript::util::tvals_concat(
buf, ostd::iter(&args[offset], callargs), " "
);
TaggedValue tv;
CsValue tv;
tv.set_mstr(buf.get().iter());
id->cb_cftv(TvalRange(&tv, 1), result);
id->cb_cftv(CsValueRange(&tv, 1), result);
}
force_arg(result, op & CODE_RET_MASK);
free_args(args, numargs, offset);
@ -1444,7 +1444,7 @@ static ostd::Uint32 const *runcode(
case CODE_CALLU | RET_FLOAT:
case CODE_CALLU | RET_INT: {
int callargs = op >> 8, offset = numargs - callargs;
TaggedValue &idarg = args[offset - 1];
CsValue &idarg = args[offset - 1];
if (
idarg.get_type() != VAL_STR &&
idarg.get_type() != VAL_MACRO &&
@ -1568,11 +1568,11 @@ exit:
return code;
}
void CsState::run_ret(Bytecode const *code, TaggedValue &ret) {
void CsState::run_ret(Bytecode const *code, CsValue &ret) {
runcode(*this, reinterpret_cast<ostd::Uint32 const *>(code), ret);
}
void CsState::run_ret(ostd::ConstCharRange code, TaggedValue &ret) {
void CsState::run_ret(ostd::ConstCharRange code, CsValue &ret) {
GenState gs(*this);
gs.code.reserve(64);
/* FIXME range */
@ -1583,7 +1583,7 @@ void CsState::run_ret(ostd::ConstCharRange code, TaggedValue &ret) {
}
}
void CsState::run_ret(Ident *id, TvalRange args, TaggedValue &ret) {
void CsState::run_ret(Ident *id, CsValueRange args, CsValue &ret) {
int nargs = int(args.size());
ret.set_null();
++rundepth;
@ -1598,8 +1598,8 @@ void CsState::run_ret(Ident *id, TvalRange args, TaggedValue &ret) {
/* fallthrough */
case IdentType::command:
if (nargs < static_cast<Command *>(id)->numargs) {
TaggedValue buf[MaxArguments];
memcpy(buf, args.data(), args.size() * sizeof(TaggedValue));
CsValue buf[MaxArguments];
memcpy(buf, args.data(), args.size() * sizeof(CsValue));
callcommand(
*this, static_cast<Command *>(id), buf, ret,
nargs, false
@ -1659,7 +1659,7 @@ void CsState::run_ret(Ident *id, TvalRange args, TaggedValue &ret) {
}
ostd::String CsState::run_str(Bytecode const *code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
ostd::String s = ret.get_str();
ret.cleanup();
@ -1667,15 +1667,15 @@ ostd::String CsState::run_str(Bytecode const *code) {
}
ostd::String CsState::run_str(ostd::ConstCharRange code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
ostd::String s = ret.get_str();
ret.cleanup();
return s;
}
ostd::String CsState::run_str(Ident *id, TvalRange args) {
TaggedValue ret;
ostd::String CsState::run_str(Ident *id, CsValueRange args) {
CsValue ret;
run_ret(id, args, ret);
ostd::String s = ret.get_str();
ret.cleanup();
@ -1683,7 +1683,7 @@ ostd::String CsState::run_str(Ident *id, TvalRange args) {
}
CsInt CsState::run_int(Bytecode const *code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
CsInt i = ret.get_int();
ret.cleanup();
@ -1691,15 +1691,15 @@ CsInt CsState::run_int(Bytecode const *code) {
}
CsInt CsState::run_int(ostd::ConstCharRange code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
CsInt i = ret.get_int();
ret.cleanup();
return i;
}
CsInt CsState::run_int(Ident *id, TvalRange args) {
TaggedValue ret;
CsInt CsState::run_int(Ident *id, CsValueRange args) {
CsValue ret;
run_ret(id, args, ret);
CsInt i = ret.get_int();
ret.cleanup();
@ -1707,7 +1707,7 @@ CsInt CsState::run_int(Ident *id, TvalRange args) {
}
CsFloat CsState::run_float(Bytecode const *code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
CsFloat f = ret.get_float();
ret.cleanup();
@ -1715,15 +1715,15 @@ CsFloat CsState::run_float(Bytecode const *code) {
}
CsFloat CsState::run_float(ostd::ConstCharRange code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
CsFloat f = ret.get_float();
ret.cleanup();
return f;
}
CsFloat CsState::run_float(Ident *id, TvalRange args) {
TaggedValue ret;
CsFloat CsState::run_float(Ident *id, CsValueRange args) {
CsValue ret;
run_ret(id, args, ret);
CsFloat f = ret.get_float();
ret.cleanup();
@ -1731,7 +1731,7 @@ CsFloat CsState::run_float(Ident *id, TvalRange args) {
}
bool CsState::run_bool(Bytecode const *code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
bool b = ret.get_bool();
ret.cleanup();
@ -1739,15 +1739,15 @@ bool CsState::run_bool(Bytecode const *code) {
}
bool CsState::run_bool(ostd::ConstCharRange code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
bool b = ret.get_bool();
ret.cleanup();
return b;
}
bool CsState::run_bool(Ident *id, TvalRange args) {
TaggedValue ret;
bool CsState::run_bool(Ident *id, CsValueRange args) {
CsValue ret;
run_ret(id, args, ret);
bool b = ret.get_bool();
ret.cleanup();
@ -1755,25 +1755,25 @@ bool CsState::run_bool(Ident *id, TvalRange args) {
}
void CsState::run(Bytecode const *code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
ret.cleanup();
}
void CsState::run(ostd::ConstCharRange code) {
TaggedValue ret;
CsValue ret;
run_ret(code, ret);
ret.cleanup();
}
void CsState::run(Ident *id, TvalRange args) {
TaggedValue ret;
void CsState::run(Ident *id, CsValueRange args) {
CsValue ret;
run_ret(id, args, ret);
ret.cleanup();
}
static bool cs_run_file(
CsState &cs, ostd::ConstCharRange fname, TaggedValue &ret
CsState &cs, ostd::ConstCharRange fname, CsValue &ret
) {
ostd::ConstCharRange oldsrcfile = cs.src_file, oldsrcstr = cs.src_str;
ostd::Box<char[]> buf;
@ -1800,7 +1800,7 @@ static bool cs_run_file(
}
ostd::Maybe<ostd::String> CsState::run_file_str(ostd::ConstCharRange fname) {
TaggedValue ret;
CsValue ret;
if (!cs_run_file(*this, fname, ret)) {
return ostd::nothing;
}
@ -1810,7 +1810,7 @@ ostd::Maybe<ostd::String> CsState::run_file_str(ostd::ConstCharRange fname) {
}
ostd::Maybe<CsInt> CsState::run_file_int(ostd::ConstCharRange fname) {
TaggedValue ret;
CsValue ret;
if (!cs_run_file(*this, fname, ret)) {
return ostd::nothing;
}
@ -1820,7 +1820,7 @@ ostd::Maybe<CsInt> CsState::run_file_int(ostd::ConstCharRange fname) {
}
ostd::Maybe<CsFloat> CsState::run_file_float(ostd::ConstCharRange fname) {
TaggedValue ret;
CsValue ret;
if (!cs_run_file(*this, fname, ret)) {
return ostd::nothing;
}
@ -1830,7 +1830,7 @@ ostd::Maybe<CsFloat> CsState::run_file_float(ostd::ConstCharRange fname) {
}
ostd::Maybe<bool> CsState::run_file_bool(ostd::ConstCharRange fname) {
TaggedValue ret;
CsValue ret;
if (!cs_run_file(*this, fname, ret)) {
return ostd::nothing;
}
@ -1839,12 +1839,12 @@ ostd::Maybe<bool> CsState::run_file_bool(ostd::ConstCharRange fname) {
return i;
}
bool CsState::run_file_ret(ostd::ConstCharRange fname, TaggedValue &ret) {
bool CsState::run_file_ret(ostd::ConstCharRange fname, CsValue &ret) {
return cs_run_file(*this, fname, ret);
}
bool CsState::run_file(ostd::ConstCharRange fname) {
TaggedValue ret;
CsValue ret;
if (!cs_run_file(*this, fname, ret)) {
return false;
}

View File

@ -72,7 +72,7 @@ enum {
RET_FLOAT = VAL_FLOAT << CODE_RET,
};
struct NullValue: TaggedValue {
struct NullValue: CsValue {
NullValue() { set_null(); }
} const null_value;

View File

@ -89,7 +89,7 @@ Alias::Alias(ostd::ConstCharRange name, int fl):
{
val_v.set_null();
}
Alias::Alias(ostd::ConstCharRange name, TaggedValue const &v, int fl):
Alias::Alias(ostd::ConstCharRange name, CsValue const &v, int fl):
Ident(IdentType::alias, name, fl),
code(nullptr), stack(nullptr), val_v(v)
{}
@ -328,7 +328,7 @@ Ident *CsState::new_ident(ostd::ConstCharRange name, int flags) {
return id;
}
Ident *CsState::force_ident(TaggedValue &v) {
Ident *CsState::force_ident(CsValue &v) {
switch (v.get_type()) {
case VAL_IDENT:
return v.id;
@ -370,7 +370,7 @@ void CsState::touch_var(ostd::ConstCharRange name) {
}
}
void CsState::set_alias(ostd::ConstCharRange name, TaggedValue &v) {
void CsState::set_alias(ostd::ConstCharRange name, CsValue &v) {
Ident *id = get_ident(name);
if (id) {
switch (id->get_type()) {
@ -461,7 +461,7 @@ void CsState::print_var(Var *v) {
}
}
void TaggedValue::cleanup() {
void CsValue::cleanup() {
switch (get_type()) {
case VAL_STR:
delete[] s;
@ -477,7 +477,7 @@ void TaggedValue::cleanup() {
}
}
void TaggedValue::force_null() {
void CsValue::force_null() {
if (get_type() == VAL_NULL) {
return;
}
@ -485,7 +485,7 @@ void TaggedValue::force_null() {
set_null();
}
CsFloat TaggedValue::force_float() {
CsFloat CsValue::force_float() {
CsFloat rf = 0.0f;
switch (get_type()) {
case VAL_INT:
@ -504,7 +504,7 @@ CsFloat TaggedValue::force_float() {
return rf;
}
CsInt TaggedValue::force_int() {
CsInt CsValue::force_int() {
CsInt ri = 0;
switch (get_type()) {
case VAL_FLOAT:
@ -523,7 +523,7 @@ CsInt TaggedValue::force_int() {
return ri;
}
ostd::ConstCharRange TaggedValue::force_str() {
ostd::ConstCharRange CsValue::force_str() {
ostd::String rs;
switch (get_type()) {
case VAL_FLOAT:
@ -544,7 +544,7 @@ ostd::ConstCharRange TaggedValue::force_str() {
return s;
}
CsInt TaggedValue::get_int() const {
CsInt CsValue::get_int() const {
switch (get_type()) {
case VAL_FLOAT:
return CsInt(f);
@ -558,7 +558,7 @@ CsInt TaggedValue::get_int() const {
return 0;
}
CsFloat TaggedValue::get_float() const {
CsFloat CsValue::get_float() const {
switch (get_type()) {
case VAL_FLOAT:
return f;
@ -572,21 +572,21 @@ CsFloat TaggedValue::get_float() const {
return 0.0f;
}
Bytecode *TaggedValue::get_code() const {
Bytecode *CsValue::get_code() const {
if (get_type() != VAL_CODE) {
return nullptr;
}
return const_cast<Bytecode *>(code);
}
Ident *TaggedValue::get_ident() const {
Ident *CsValue::get_ident() const {
if (get_type() != VAL_IDENT) {
return nullptr;
}
return id;
}
ostd::String TaggedValue::get_str() const {
ostd::String CsValue::get_str() const {
switch (get_type()) {
case VAL_STR:
case VAL_MACRO:
@ -600,7 +600,7 @@ ostd::String TaggedValue::get_str() const {
return ostd::String("");
}
ostd::ConstCharRange TaggedValue::get_strr() const {
ostd::ConstCharRange CsValue::get_strr() const {
switch (get_type()) {
case VAL_STR:
case VAL_MACRO:
@ -612,7 +612,7 @@ ostd::ConstCharRange TaggedValue::get_strr() const {
return ostd::ConstCharRange();
}
void TaggedValue::get_val(TaggedValue &r) const {
void CsValue::get_val(CsValue &r) const {
switch (get_type()) {
case VAL_STR:
case VAL_MACRO:
@ -641,7 +641,7 @@ OSTD_EXPORT bool code_is_empty(Bytecode const *code) {
) == CODE_EXIT;
}
bool TaggedValue::code_is_empty() const {
bool CsValue::code_is_empty() const {
if (get_type() != VAL_CODE) {
return true;
}
@ -665,7 +665,7 @@ static inline bool cs_get_bool(ostd::ConstCharRange s) {
return true;
}
bool TaggedValue::get_bool() const {
bool CsValue::get_bool() const {
switch (get_type()) {
case VAL_FLOAT:
return f != 0;
@ -680,7 +680,7 @@ bool TaggedValue::get_bool() const {
}
}
void Alias::get_cstr(TaggedValue &v) const {
void Alias::get_cstr(CsValue &v) const {
switch (val_v.get_type()) {
case VAL_MACRO:
v.set_macro(val_v.code, val_v.len);
@ -701,7 +701,7 @@ void Alias::get_cstr(TaggedValue &v) const {
}
}
void Alias::get_cval(TaggedValue &v) const {
void Alias::get_cval(CsValue &v) const {
switch (val_v.get_type()) {
case VAL_MACRO:
v.set_macro(val_v.code, val_v.len);
@ -730,7 +730,7 @@ void Alias::clean_code() {
}
}
void Alias::push_arg(TaggedValue const &v, IdentStack &st, bool um) {
void Alias::push_arg(CsValue const &v, IdentStack &st, bool um) {
st.val_s = val_v;
st.next = stack;
stack = &st;
@ -769,7 +769,7 @@ void Alias::redo_arg(IdentStack const &st) {
clean_code();
}
void Alias::set_arg(CsState &cs, TaggedValue &v) {
void Alias::set_arg(CsState &cs, CsValue &v) {
if (cs.stack->usedargs & (1 << get_index())) {
val_v.cleanup();
set_value(v);
@ -780,7 +780,7 @@ void Alias::set_arg(CsState &cs, TaggedValue &v) {
}
}
void Alias::set_alias(CsState &cs, TaggedValue &v) {
void Alias::set_alias(CsState &cs, CsValue &v) {
val_v.cleanup();
set_value(v);
clean_code();
@ -1021,7 +1021,7 @@ void CsState::set_var_int_checked(Ivar *iv, CsInt v) {
iv->changed();
}
void CsState::set_var_int_checked(Ivar *iv, TvalRange args) {
void CsState::set_var_int_checked(Ivar *iv, CsValueRange args) {
CsInt v = args[0].force_int();
if ((iv->get_flags() & IDF_HEX) && (args.size() > 1)) {
v = (v << 16) | (args[1].force_int() << 8);
@ -1146,7 +1146,7 @@ bool CsState::add_command(
}
void cs_init_lib_io(CsState &cs) {
cs_add_command(cs, "exec", "sb", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "exec", "sb", [&cs](CsValueRange args, CsValue &res) {
auto file = args[0].get_strr();
bool ret = cs.run_file(file);
if (!ret) {
@ -1159,7 +1159,7 @@ void cs_init_lib_io(CsState &cs) {
}
});
cs_add_command(cs, "echo", "C", [](TvalRange args, TaggedValue &) {
cs_add_command(cs, "echo", "C", [](CsValueRange args, CsValue &) {
ostd::writeln(args[0].get_strr());
});
}
@ -1170,7 +1170,7 @@ static inline void cs_set_iter(Alias &a, CsInt i, IdentStack &stack) {
a.val_v.set_int(i);
return;
}
TaggedValue v;
CsValue v;
v.set_int(i);
a.push_arg(v, stack);
}
@ -1195,7 +1195,7 @@ static inline void cs_do_loop(
}
static inline void cs_loop_conc(
CsState &cs, TaggedValue &res, Ident &id, CsInt offset, CsInt n,
CsState &cs, CsValue &res, Ident &id, CsInt offset, CsInt n,
CsInt step, Bytecode *body, bool space
) {
if (n <= 0 || !id.is_alias()) {
@ -1206,7 +1206,7 @@ static inline void cs_loop_conc(
ostd::Vector<char> s;
for (CsInt i = 0; i < n; ++i) {
cs_set_iter(a, offset + i * step, stack);
TaggedValue v;
CsValue v;
cs.run_ret(body, v);
ostd::String vstr = ostd::move(v.get_str());
if (space && i) {
@ -1224,11 +1224,11 @@ static inline void cs_loop_conc(
}
void cs_init_lib_base(CsState &cs) {
cs_add_command(cs, "do", "e", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "do", "e", [&cs](CsValueRange args, CsValue &res) {
cs.run_ret(args[0].get_code(), res);
}, ID_DO);
cs_add_command(cs, "doargs", "e", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "doargs", "e", [&cs](CsValueRange args, CsValue &res) {
if (cs.stack != &cs.noalias) {
cs_do_args(cs, [&]() { cs.run_ret(args[0].get_code(), res); });
} else {
@ -1236,21 +1236,21 @@ void cs_init_lib_base(CsState &cs) {
}
}, ID_DOARGS);
cs_add_command(cs, "if", "tee", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "if", "tee", [&cs](CsValueRange args, CsValue &res) {
cs.run_ret((args[0].get_bool() ? args[1] : args[2]).get_code(), res);
}, ID_IF);
cs_add_command(cs, "result", "T", [](TvalRange args, TaggedValue &res) {
TaggedValue &v = args[0];
cs_add_command(cs, "result", "T", [](CsValueRange args, CsValue &res) {
CsValue &v = args[0];
res = v;
v.set_null();
}, ID_RESULT);
cs_add_command(cs, "!", "t", [](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "!", "t", [](CsValueRange args, CsValue &res) {
res.set_int(!args[0].get_bool());
}, ID_NOT);
cs_add_command(cs, "&&", "E1V", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "&&", "E1V", [&cs](CsValueRange args, CsValue &res) {
if (args.empty()) {
res.set_int(1);
} else {
@ -1270,7 +1270,7 @@ void cs_init_lib_base(CsState &cs) {
}
}, ID_AND);
cs_add_command(cs, "||", "E1V", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "||", "E1V", [&cs](CsValueRange args, CsValue &res) {
if (args.empty()) {
res.set_int(0);
} else {
@ -1290,11 +1290,11 @@ void cs_init_lib_base(CsState &cs) {
}
}, ID_OR);
cs_add_command(cs, "?", "tTT", [](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "?", "tTT", [](CsValueRange args, CsValue &res) {
res.set(args[0].get_bool() ? args[1] : args[2]);
});
cs_add_command(cs, "cond", "ee2V", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "cond", "ee2V", [&cs](CsValueRange args, CsValue &res) {
for (ostd::Size i = 0; i < args.size(); i += 2) {
if ((i + 1) < args.size()) {
if (cs.run_bool(args[i].code)) {
@ -1308,7 +1308,7 @@ void cs_init_lib_base(CsState &cs) {
}
});
cs_add_command(cs, "case", "ite2V", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "case", "ite2V", [&cs](CsValueRange args, CsValue &res) {
CsInt val = args[0].get_int();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ((args[i].get_type() == VAL_NULL) || (args[i].get_int() == val)) {
@ -1318,7 +1318,7 @@ void cs_init_lib_base(CsState &cs) {
}
});
cs_add_command(cs, "casef", "fte2V", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "casef", "fte2V", [&cs](CsValueRange args, CsValue &res) {
CsFloat val = args[0].get_float();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ((args[i].get_type() == VAL_NULL) || (args[i].get_float() == val)) {
@ -1328,7 +1328,7 @@ void cs_init_lib_base(CsState &cs) {
}
});
cs_add_command(cs, "cases", "ste2V", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "cases", "ste2V", [&cs](CsValueRange args, CsValue &res) {
ostd::String 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)) {
@ -1338,9 +1338,9 @@ void cs_init_lib_base(CsState &cs) {
}
});
cs_add_command(cs, "pushif", "rTe", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "pushif", "rTe", [&cs](CsValueRange args, CsValue &res) {
Ident *id = args[0].get_ident();
TaggedValue &v = args[1];
CsValue &v = args[1];
Bytecode *code = args[2].get_code();
if (!id->is_alias() || (id->get_index() < MaxArguments)) {
return;
@ -1355,139 +1355,139 @@ void cs_init_lib_base(CsState &cs) {
}
});
cs_add_command(cs, "loop", "rie", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loop", "rie", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr,
args[2].get_code()
);
});
cs_add_command(cs, "loop+", "riie", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loop+", "riie", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
nullptr, args[3].get_code()
);
});
cs_add_command(cs, "loop*", "riie", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loop*", "riie", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(),
nullptr, args[3].get_code()
);
});
cs_add_command(cs, "loop+*", "riiie", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loop+*", "riiie", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
args[2].get_int(), nullptr, args[4].get_code()
);
});
cs_add_command(cs, "loopwhile", "riee", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loopwhile", "riee", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), args[3].get_code()
);
});
cs_add_command(cs, "loopwhile+", "riiee", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loopwhile+", "riiee", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
args[3].get_code(), args[4].get_code()
);
});
cs_add_command(cs, "loopwhile*", "riiee", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loopwhile*", "riiee", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(),
args[3].get_code(), args[4].get_code()
);
});
cs_add_command(cs, "loopwhile+*", "riiiee", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "loopwhile+*", "riiiee", [&cs](CsValueRange args, CsValue &) {
cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
args[2].get_int(), args[4].get_code(), args[5].get_code()
);
});
cs_add_command(cs, "while", "ee", [&cs](TvalRange args, TaggedValue &) {
cs_add_command(cs, "while", "ee", [&cs](CsValueRange args, CsValue &) {
Bytecode *cond = args[0].get_code(), *body = args[1].get_code();
while (cs.run_bool(cond)) {
cs.run_int(body);
}
});
cs_add_command(cs, "loopconcat", "rie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcat", "rie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), true
);
});
cs_add_command(cs, "loopconcat+", "riie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcat+", "riie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
args[3].get_code(), true
);
});
cs_add_command(cs, "loopconcat*", "riie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcat*", "riie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(),
args[3].get_code(), true
);
});
cs_add_command(cs, "loopconcat+*", "riiie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcat+*", "riiie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
args[2].get_int(), args[4].get_code(), true
);
});
cs_add_command(cs, "loopconcatword", "rie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcatword", "rie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), false
);
});
cs_add_command(cs, "loopconcatword+", "riie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcatword+", "riie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
args[3].get_code(), false
);
});
cs_add_command(cs, "loopconcatword*", "riie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcatword*", "riie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(),
args[3].get_code(), false
);
});
cs_add_command(cs, "loopconcatword+*", "riiie", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "loopconcatword+*", "riiie", [&cs](CsValueRange args, CsValue &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
args[2].get_int(), args[4].get_code(), false
);
});
cs_add_command(cs, "nodebug", "e", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "nodebug", "e", [&cs](CsValueRange args, CsValue &res) {
++cs.nodebug;
cs.run_ret(args[0].get_code(), res);
--cs.nodebug;
});
cs_add_command(cs, "push", "rTe", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "push", "rTe", [&cs](CsValueRange args, CsValue &res) {
Ident *id = args[0].get_ident();
if (!id->is_alias() || (id->get_index() < MaxArguments)) {
return;
}
Alias *a = static_cast<Alias *>(id);
IdentStack stack;
TaggedValue &v = args[1];
CsValue &v = args[1];
a->push_arg(v, stack);
v.set_null();
cs.run_ret(args[2].get_code(), res);
@ -1496,34 +1496,34 @@ void cs_init_lib_base(CsState &cs) {
cs_add_command(cs, "local", nullptr, nullptr, ID_LOCAL);
cs_add_command(cs, "resetvar", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "resetvar", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_int(cs.reset_var(args[0].get_strr()));
});
cs_add_command(cs, "alias", "sT", [&cs](TvalRange args, TaggedValue &) {
TaggedValue &v = args[1];
cs_add_command(cs, "alias", "sT", [&cs](CsValueRange args, CsValue &) {
CsValue &v = args[1];
cs.set_alias(args[0].get_strr(), v);
v.set_null();
});
cs_add_command(cs, "getvarmin", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "getvarmin", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_int(cs.get_var_min_int(args[0].get_strr()).value_or(0));
});
cs_add_command(cs, "getvarmax", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "getvarmax", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_int(cs.get_var_max_int(args[0].get_strr()).value_or(0));
});
cs_add_command(cs, "getfvarmin", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "getfvarmin", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_float(cs.get_var_min_float(args[0].get_strr()).value_or(0.0f));
});
cs_add_command(cs, "getfvarmax", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "getfvarmax", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_float(cs.get_var_max_float(args[0].get_strr()).value_or(0.0f));
});
cs_add_command(cs, "identexists", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "identexists", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_int(cs.have_ident(args[0].get_strr()));
});
cs_add_command(cs, "getalias", "s", [&cs](TvalRange args, TaggedValue &res) {
cs_add_command(cs, "getalias", "s", [&cs](CsValueRange args, CsValue &res) {
res.set_str(ostd::move(cs.get_alias_val(args[0].get_strr()).value_or("")));
});
}

View File

@ -66,7 +66,7 @@ OSTD_EXPORT bool code_is_empty(Bytecode const *code);
struct Ident;
struct OSTD_EXPORT TaggedValue {
struct OSTD_EXPORT CsValue {
union {
CsInt i; /* ID_IVAR, VAL_INT */
CsFloat f; /* ID_FVAR, VAL_FLOAT */
@ -129,7 +129,7 @@ struct OSTD_EXPORT TaggedValue {
code = val;
}
void set(TaggedValue &tv) {
void set(CsValue &tv) {
*this = tv;
tv.p_type = VAL_NULL;
}
@ -140,7 +140,7 @@ struct OSTD_EXPORT TaggedValue {
CsFloat get_float() const;
Bytecode *get_code() const;
Ident *get_ident() const;
void get_val(TaggedValue &r) const;
void get_val(CsValue &r) const;
bool get_bool() const;
@ -152,16 +152,16 @@ struct OSTD_EXPORT TaggedValue {
bool code_is_empty() const;
void cleanup();
void copy_arg(TaggedValue &r) const;
void copy_arg(CsValue &r) const;
private:
int p_type;
};
using TvalRange = ostd::PointerRange<TaggedValue>;
using CsValueRange = ostd::PointerRange<CsValue>;
struct IdentStack {
TaggedValue val_s;
CsValue val_s;
IdentStack *next;
};
@ -297,15 +297,15 @@ private:
struct OSTD_EXPORT Alias: Ident {
Bytecode *code;
IdentStack *stack;
TaggedValue val_v;
CsValue val_v;
Alias(ostd::ConstCharRange n, char *a, int flags);
Alias(ostd::ConstCharRange n, CsInt a, int flags);
Alias(ostd::ConstCharRange n, CsFloat a, int flags);
Alias(ostd::ConstCharRange n, int flags);
Alias(ostd::ConstCharRange n, TaggedValue const &v, int flags);
Alias(ostd::ConstCharRange n, CsValue const &v, int flags);
void set_value(TaggedValue const &v) {
void set_value(CsValue const &v) {
val_v = v;
}
@ -313,16 +313,16 @@ struct OSTD_EXPORT Alias: Ident {
val_v = v.val_s;
}
void get_cstr(TaggedValue &v) const;
void get_cval(TaggedValue &v) const;
void get_cstr(CsValue &v) const;
void get_cval(CsValue &v) const;
void push_arg(TaggedValue const &v, IdentStack &st, bool um = true);
void push_arg(CsValue const &v, IdentStack &st, bool um = true);
void pop_arg();
void undo_arg(IdentStack &st);
void redo_arg(IdentStack const &st);
void set_arg(CsState &cs, TaggedValue &v);
void set_alias(CsState &cs, TaggedValue &v);
void set_arg(CsState &cs, CsValue &v);
void set_alias(CsState &cs, CsValue &v);
void clean_code();
@ -339,7 +339,7 @@ struct IdentLink {
IdentStack *argstack;
};
using CmdFunc = ostd::Function<void(TvalRange, TaggedValue &)>;
using CmdFunc = ostd::Function<void(CsValueRange, CsValue &)>;
struct OSTD_EXPORT CsState {
ostd::Map<ostd::ConstCharRange, Ident *> idents;
@ -366,7 +366,7 @@ struct OSTD_EXPORT CsState {
Ident *add_ident(Ident *id);
Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN);
Ident *force_ident(TaggedValue &v);
Ident *force_ident(CsValue &v);
Ident *get_ident(ostd::ConstCharRange name) {
Ident **id = idents.at(name);
@ -397,36 +397,36 @@ struct OSTD_EXPORT CsState {
ostd::String run_str(Bytecode const *code);
ostd::String run_str(ostd::ConstCharRange code);
ostd::String run_str(Ident *id, TvalRange args);
ostd::String run_str(Ident *id, CsValueRange args);
CsInt run_int(Bytecode const *code);
CsInt run_int(ostd::ConstCharRange code);
CsInt run_int(Ident *id, TvalRange args);
CsInt run_int(Ident *id, CsValueRange args);
CsFloat run_float(Bytecode const *code);
CsFloat run_float(ostd::ConstCharRange code);
CsFloat run_float(Ident *id, TvalRange args);
CsFloat run_float(Ident *id, CsValueRange args);
bool run_bool(Bytecode const *code);
bool run_bool(ostd::ConstCharRange code);
bool run_bool(Ident *id, TvalRange args);
bool run_bool(Ident *id, CsValueRange args);
void run_ret(Bytecode const *code, TaggedValue &ret);
void run_ret(ostd::ConstCharRange code, TaggedValue &ret);
void run_ret(Ident *id, TvalRange args, TaggedValue &ret);
void run_ret(Bytecode const *code, CsValue &ret);
void run_ret(ostd::ConstCharRange code, CsValue &ret);
void run_ret(Ident *id, CsValueRange args, CsValue &ret);
void run(Bytecode const *code);
void run(ostd::ConstCharRange code);
void run(Ident *id, TvalRange args);
void run(Ident *id, CsValueRange args);
ostd::Maybe<ostd::String> 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);
bool run_file_ret(ostd::ConstCharRange fname, TaggedValue &ret);
bool run_file_ret(ostd::ConstCharRange fname, CsValue &ret);
bool run_file(ostd::ConstCharRange fname);
void set_alias(ostd::ConstCharRange name, TaggedValue &v);
void set_alias(ostd::ConstCharRange name, CsValue &v);
void set_var_int(
ostd::ConstCharRange name, CsInt v,
@ -441,7 +441,7 @@ struct OSTD_EXPORT CsState {
);
void set_var_int_checked(Ivar *iv, CsInt v);
void set_var_int_checked(Ivar *iv, TvalRange args);
void set_var_int_checked(Ivar *iv, CsValueRange args);
void set_var_float_checked(Fvar *fv, CsFloat v);
void set_var_str_checked(Svar *fv, ostd::ConstCharRange v);
@ -473,9 +473,9 @@ enum {
OSTD_EXPORT void init_libs(CsState &cs, int libs = CS_LIB_ALL);
struct OSTD_EXPORT StackedValue: TaggedValue {
struct OSTD_EXPORT StackedValue: CsValue {
StackedValue(Ident *id = nullptr):
TaggedValue(), p_a(nullptr), p_stack(), p_pushed(false)
CsValue(), p_a(nullptr), p_stack(), p_pushed(false)
{
set_alias(id);
}
@ -628,7 +628,7 @@ namespace util {
template<typename R>
inline ostd::Size tvals_concat(
R &&writer, TvalRange vals,
R &&writer, CsValueRange vals,
ostd::ConstCharRange sep = ostd::ConstCharRange()
) {
ostd::Size ret = 0;

View File

@ -10,27 +10,27 @@ struct CsArgVal;
template<>
struct CsArgVal<CsInt> {
static CsInt get(TaggedValue &tv) {
static CsInt get(CsValue &tv) {
return tv.get_int();
}
};
template<>
struct CsArgVal<CsFloat> {
static CsFloat get(TaggedValue &tv) {
static CsFloat get(CsValue &tv) {
return tv.get_float();
}
};
template<>
struct CsArgVal<ostd::ConstCharRange> {
static ostd::ConstCharRange get(TaggedValue &tv) {
static ostd::ConstCharRange get(CsValue &tv) {
return tv.get_strr();
}
};
template<typename T, typename F>
static inline void cs_list_find(TvalRange args, TaggedValue &res, F cmp) {
static inline void cs_list_find(CsValueRange args, CsValue &res, F cmp) {
CsInt n = 0, skip = args[2].get_int();
T val = CsArgVal<T>::get(args[1]);
for (util::ListParser p(args[0].get_strr()); p.parse(); ++n) {
@ -50,7 +50,7 @@ notfound:
}
template<typename T, typename F>
static inline void cs_list_assoc(TvalRange args, TaggedValue &res, F cmp) {
static inline void cs_list_assoc(CsValueRange args, CsValue &res, F cmp) {
T val = CsArgVal<T>::get(args[1]);
for (util::ListParser p(args[0].get_strr()); p.parse();) {
if (cmp(p, val)) {
@ -75,13 +75,13 @@ static inline void cs_set_iter(Alias &a, char *val, IdentStack &stack) {
a.val_v.set_mstr(val);
return;
}
TaggedValue v;
CsValue v;
v.set_mstr(val);
a.push_arg(v, stack);
}
static void cs_loop_list_conc(
CsState &cs, TaggedValue &res, Ident *id, ostd::ConstCharRange list,
CsState &cs, CsValue &res, Ident *id, ostd::ConstCharRange list,
Bytecode const *body, bool space
) {
if (!id->is_alias()) {
@ -96,7 +96,7 @@ static void cs_loop_list_conc(
if (n && space) {
r.push(' ');
}
TaggedValue v;
CsValue v;
cs.run_ret(body, v);
ostd::String vstr = ostd::move(v.get_str());
r.push_n(vstr.data(), vstr.size());
@ -122,7 +122,7 @@ int cs_list_includes(ostd::ConstCharRange list, ostd::ConstCharRange needle) {
}
template<bool PushList, bool Swap, typename F>
static inline void cs_list_merge(TvalRange args, TaggedValue &res, F cmp) {
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;
@ -148,11 +148,11 @@ static inline void cs_list_merge(TvalRange args, TaggedValue &res, F cmp) {
static void cs_init_lib_list_sort(CsState &cs);
void cs_init_lib_list(CsState &cs) {
cs.add_command("listlen", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("listlen", "s", [](CsValueRange args, CsValue &res) {
res.set_int(CsInt(util::list_length(args[0].get_strr())));
});
cs.add_command("at", "si1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("at", "si1V", [](CsValueRange args, CsValue &res) {
if (args.empty()) {
return;
}
@ -177,7 +177,7 @@ void cs_init_lib_list(CsState &cs) {
res.set_mstr(er);
});
cs.add_command("sublist", "siiN", [](TvalRange args, TaggedValue &res) {
cs.add_command("sublist", "siiN", [](CsValueRange args, CsValue &res) {
CsInt skip = args[1].get_int(),
count = args[2].get_int(),
numargs = args[2].get_int();
@ -206,7 +206,7 @@ void cs_init_lib_list(CsState &cs) {
res.set_str(ostd::ConstCharRange(list, qend - list));
});
cs.add_command("listfind", "rse", [&cs](TvalRange args, TaggedValue &res) {
cs.add_command("listfind", "rse", [&cs](CsValueRange args, CsValue &res) {
Ident *id = args[0].get_ident();
auto body = args[2].get_code();
if (!id->is_alias()) {
@ -230,7 +230,7 @@ found:
}
});
cs.add_command("listassoc", "rse", [&cs](TvalRange args, TaggedValue &res) {
cs.add_command("listassoc", "rse", [&cs](CsValueRange args, CsValue &res) {
Ident *id = args[0].get_ident();
auto body = args[2].get_code();
if (!id->is_alias()) {
@ -259,21 +259,21 @@ found:
}
});
cs.add_command("listfind=", "i", [](TvalRange args, TaggedValue &res) {
cs.add_command("listfind=", "i", [](CsValueRange args, CsValue &res) {
cs_list_find<CsInt>(
args, res, [](const util::ListParser &p, CsInt val) {
return cs_parse_int(p.item) == val;
}
);
});
cs.add_command("listfind=f", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("listfind=f", "f", [](CsValueRange args, CsValue &res) {
cs_list_find<CsFloat>(
args, res, [](const util::ListParser &p, CsFloat val) {
return cs_parse_float(p.item) == val;
}
);
});
cs.add_command("listfind=s", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("listfind=s", "s", [](CsValueRange args, CsValue &res) {
cs_list_find<ostd::ConstCharRange>(
args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
return p.item == val;
@ -281,21 +281,21 @@ found:
);
});
cs.add_command("listassoc=", "i", [](TvalRange args, TaggedValue &res) {
cs.add_command("listassoc=", "i", [](CsValueRange args, CsValue &res) {
cs_list_assoc<CsInt>(
args, res, [](const util::ListParser &p, CsInt val) {
return cs_parse_int(p.item) == val;
}
);
});
cs.add_command("listassoc=f", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("listassoc=f", "f", [](CsValueRange args, CsValue &res) {
cs_list_assoc<CsFloat>(
args, res, [](const util::ListParser &p, CsFloat val) {
return cs_parse_float(p.item) == val;
}
);
});
cs.add_command("listassoc=s", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("listassoc=s", "s", [](CsValueRange args, CsValue &res) {
cs_list_assoc<ostd::ConstCharRange>(
args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
return p.item == val;
@ -303,7 +303,7 @@ found:
);
});
cs.add_command("looplist", "rse", [&cs](TvalRange args, TaggedValue &) {
cs.add_command("looplist", "rse", [&cs](CsValueRange args, CsValue &) {
Ident *id = args[0].get_ident();
auto body = args[2].get_code();
if (!id->is_alias()) {
@ -320,7 +320,7 @@ found:
}
});
cs.add_command("looplist2", "rrse", [&cs](TvalRange args, TaggedValue &) {
cs.add_command("looplist2", "rrse", [&cs](CsValueRange args, CsValue &) {
Ident *id = args[0].get_ident(), *id2 = args[1].get_ident();
auto body = args[3].get_code();
if (!id->is_alias() || !id2->is_alias()) {
@ -342,7 +342,7 @@ found:
}
});
cs.add_command("looplist3", "rrrse", [&cs](TvalRange args, TaggedValue &) {
cs.add_command("looplist3", "rrrse", [&cs](CsValueRange args, CsValue &) {
Ident *id = args[0].get_ident();
Ident *id2 = args[1].get_ident();
Ident *id3 = args[2].get_ident();
@ -372,7 +372,7 @@ found:
});
cs.add_command("looplistconcat", "rse", [&cs](
TvalRange args, TaggedValue &res
CsValueRange args, CsValue &res
) {
cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(),
@ -381,7 +381,7 @@ found:
});
cs.add_command("looplistconcatword", "rse", [&cs](
TvalRange args, TaggedValue &res
CsValueRange args, CsValue &res
) {
cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(),
@ -389,7 +389,7 @@ found:
);
});
cs.add_command("listfilter", "rse", [&cs](TvalRange args, TaggedValue &res) {
cs.add_command("listfilter", "rse", [&cs](CsValueRange args, CsValue &res) {
Ident *id = args[0].get_ident();
auto body = args[2].get_code();
if (!id->is_alias()) {
@ -416,7 +416,7 @@ found:
res.set_mstr(ostd::CharRange(r.disown(), len));
});
cs.add_command("listcount", "rse", [&cs](TvalRange args, TaggedValue &res) {
cs.add_command("listcount", "rse", [&cs](CsValueRange args, CsValue &res) {
Ident *id = args[0].get_ident();
auto body = args[2].get_code();
if (!id->is_alias()) {
@ -437,7 +437,7 @@ found:
res.set_int(r);
});
cs.add_command("prettylist", "ss", [](TvalRange args, TaggedValue &res) {
cs.add_command("prettylist", "ss", [](CsValueRange args, CsValue &res) {
ostd::Vector<char> buf;
ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange conj = args[1].get_strr();
@ -471,23 +471,23 @@ found:
res.set_mstr(ostd::CharRange(buf.disown(), slen));
});
cs.add_command("indexof", "ss", [](TvalRange args, TaggedValue &res) {
cs.add_command("indexof", "ss", [](CsValueRange args, CsValue &res) {
res.set_int(
cs_list_includes(args[0].get_strr(), args[1].get_strr())
);
});
cs.add_command("listdel", "ss", [](TvalRange args, TaggedValue &res) {
cs.add_command("listdel", "ss", [](CsValueRange args, CsValue &res) {
cs_list_merge<false, false>(args, res, ostd::Less<int>());
});
cs.add_command("listintersect", "ss", [](TvalRange args, TaggedValue &res) {
cs.add_command("listintersect", "ss", [](CsValueRange args, CsValue &res) {
cs_list_merge<false, false>(args, res, ostd::GreaterEqual<int>());
});
cs.add_command("listunion", "ss", [](TvalRange args, TaggedValue &res) {
cs.add_command("listunion", "ss", [](CsValueRange args, CsValue &res) {
cs_list_merge<true, true>(args, res, ostd::Less<int>());
});
cs.add_command("listsplice", "ssii", [](TvalRange args, TaggedValue &res) {
cs.add_command("listsplice", "ssii", [](CsValueRange args, CsValue &res) {
CsInt offset = ostd::max(args[2].get_int(), 0);
CsInt len = ostd::max(args[3].get_int(), 0);
ostd::ConstCharRange s = args[0].get_strr();
@ -557,7 +557,7 @@ struct ListSortFun {
};
static void cs_list_sort(
CsState &cs, TaggedValue &res, ostd::ConstCharRange list, Ident *x, Ident *y,
CsState &cs, CsValue &res, ostd::ConstCharRange list, Ident *x, Ident *y,
Bytecode *body, Bytecode *unique
) {
if (x == y || !x->is_alias() || !y->is_alias()) {
@ -584,7 +584,7 @@ static void cs_list_sort(
}
/* default null value, set later from callback */
TaggedValue nv;
CsValue nv;
nv.set_null();
IdentStack xstack, ystack;
@ -659,7 +659,7 @@ static void cs_list_sort(
static void cs_init_lib_list_sort(CsState &cs) {
cs.add_command("sortlist", "srree", [&cs](
TvalRange args, TaggedValue &res
CsValueRange args, CsValue &res
) {
cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(),
@ -667,7 +667,7 @@ static void cs_init_lib_list_sort(CsState &cs) {
);
});
cs.add_command("uniquelist", "srre", [&cs](
TvalRange args, TaggedValue &res
CsValueRange args, CsValue &res
) {
cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(),

View File

@ -15,20 +15,20 @@ struct CsMathVal;
template<>
struct CsMathVal<CsInt> {
static CsInt get(TaggedValue &tv) {
static CsInt get(CsValue &tv) {
return tv.get_int();
}
static void set(TaggedValue &res, CsInt val) {
static void set(CsValue &res, CsInt val) {
res.set_int(val);
}
};
template<>
struct CsMathVal<CsFloat> {
static CsFloat get(TaggedValue &tv) {
static CsFloat get(CsValue &tv) {
return tv.get_float();
}
static void set(TaggedValue &res, CsFloat val) {
static void set(CsValue &res, CsFloat val) {
res.set_float(val);
}
};
@ -42,7 +42,7 @@ struct CsMathNoop {
template<typename T, typename F1, typename F2>
static inline void cs_mathop(
TvalRange args, TaggedValue &res, T initval,
CsValueRange args, CsValue &res, T initval,
F1 binop, F2 unop
) {
T val;
@ -58,7 +58,7 @@ static inline void cs_mathop(
}
template<typename T, typename F>
static inline void cs_cmpop(TvalRange args, TaggedValue &res, F cmp) {
static inline void cs_cmpop(CsValueRange args, CsValue &res, F cmp) {
bool val;
if (args.size() >= 2) {
val = cmp(CsMathVal<T>::get(args[0]), CsMathVal<T>::get(args[1]));
@ -72,68 +72,68 @@ static inline void cs_cmpop(TvalRange args, TaggedValue &res, F cmp) {
}
void cs_init_lib_math(CsState &cs) {
cs.add_command("sin", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("sin", "f", [](CsValueRange args, CsValue &res) {
res.set_float(sin(args[0].get_float() * RAD));
});
cs.add_command("cos", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("cos", "f", [](CsValueRange args, CsValue &res) {
res.set_float(cos(args[0].get_float() * RAD));
});
cs.add_command("tan", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("tan", "f", [](CsValueRange args, CsValue &res) {
res.set_float(tan(args[0].get_float() * RAD));
});
cs.add_command("asin", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("asin", "f", [](CsValueRange args, CsValue &res) {
res.set_float(asin(args[0].get_float()) / RAD);
});
cs.add_command("acos", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("acos", "f", [](CsValueRange args, CsValue &res) {
res.set_float(acos(args[0].get_float()) / RAD);
});
cs.add_command("atan", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("atan", "f", [](CsValueRange args, CsValue &res) {
res.set_float(atan(args[0].get_float()) / RAD);
});
cs.add_command("atan2", "ff", [](TvalRange args, TaggedValue &res) {
cs.add_command("atan2", "ff", [](CsValueRange args, CsValue &res) {
res.set_float(atan2(args[0].get_float(), args[1].get_float()) / RAD);
});
cs.add_command("sqrt", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("sqrt", "f", [](CsValueRange args, CsValue &res) {
res.set_float(sqrt(args[0].get_float()));
});
cs.add_command("loge", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("loge", "f", [](CsValueRange args, CsValue &res) {
res.set_float(log(args[0].get_float()));
});
cs.add_command("log2", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("log2", "f", [](CsValueRange args, CsValue &res) {
res.set_float(log(args[0].get_float()) / M_LN2);
});
cs.add_command("log10", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("log10", "f", [](CsValueRange args, CsValue &res) {
res.set_float(log10(args[0].get_float()));
});
cs.add_command("exp", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("exp", "f", [](CsValueRange args, CsValue &res) {
res.set_float(exp(args[0].get_float()));
});
cs.add_command("min", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("min", "i1V", [](CsValueRange args, CsValue &res) {
CsInt v = (!args.empty() ? args[0].get_int() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::min(v, args[i].get_int());
}
res.set_int(v);
});
cs.add_command("max", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("max", "i1V", [](CsValueRange args, CsValue &res) {
CsInt v = (!args.empty() ? args[0].get_int() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::max(v, args[i].get_int());
}
res.set_int(v);
});
cs.add_command("minf", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("minf", "f1V", [](CsValueRange args, CsValue &res) {
CsFloat v = (!args.empty() ? args[0].get_float() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::min(v, args[i].get_float());
}
res.set_float(v);
});
cs.add_command("maxf", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("maxf", "f1V", [](CsValueRange args, CsValue &res) {
CsFloat v = (!args.empty() ? args[0].get_float() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::max(v, args[i].get_float());
@ -141,21 +141,21 @@ void cs_init_lib_math(CsState &cs) {
res.set_float(v);
});
cs.add_command("abs", "i", [](TvalRange args, TaggedValue &res) {
cs.add_command("abs", "i", [](CsValueRange args, CsValue &res) {
res.set_int(abs(args[0].get_int()));
});
cs.add_command("absf", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("absf", "f", [](CsValueRange args, CsValue &res) {
res.set_float(fabs(args[0].get_float()));
});
cs.add_command("floor", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("floor", "f", [](CsValueRange args, CsValue &res) {
res.set_float(floor(args[0].get_float()));
});
cs.add_command("ceil", "f", [](TvalRange args, TaggedValue &res) {
cs.add_command("ceil", "f", [](CsValueRange args, CsValue &res) {
res.set_float(ceil(args[0].get_float()));
});
cs.add_command("round", "ff", [](TvalRange args, TaggedValue &res) {
cs.add_command("round", "ff", [](CsValueRange args, CsValue &res) {
double step = args[1].get_float();
double r = args[0].get_float();
if (step > 0) {
@ -167,43 +167,43 @@ void cs_init_lib_math(CsState &cs) {
res.set_float(CsFloat(r));
});
cs.add_command("+", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("+", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(args, res, 0, ostd::Add<CsInt>(), CsMathNoop<CsInt>());
});
cs.add_command("*", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("*", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 1, ostd::Multiply<CsInt>(), CsMathNoop<CsInt>()
);
});
cs.add_command("-", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("-", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, ostd::Subtract<CsInt>(), ostd::Negate<CsInt>()
);
});
cs.add_command("^", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("^", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, ostd::BitXor<CsInt>(), [](CsInt val) { return ~val; }
);
});
cs.add_command("~", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("~", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, ostd::BitXor<CsInt>(), [](CsInt val) { return ~val; }
);
});
cs.add_command("&", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("&", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, ostd::BitAnd<CsInt>(), CsMathNoop<CsInt>()
);
});
cs.add_command("|", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("|", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, ostd::BitOr<CsInt>(), CsMathNoop<CsInt>()
);
});
/* special combined cases */
cs.add_command("^~", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("^~", "i1V", [](CsValueRange args, CsValue &res) {
CsInt val;
if (args.size() >= 2) {
val = args[0].get_int() ^ ~args[1].get_int();
@ -215,7 +215,7 @@ void cs_init_lib_math(CsState &cs) {
}
res.set_int(val);
});
cs.add_command("&~", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("&~", "i1V", [](CsValueRange args, CsValue &res) {
CsInt val;
if (args.size() >= 2) {
val = args[0].get_int() & ~args[1].get_int();
@ -227,7 +227,7 @@ void cs_init_lib_math(CsState &cs) {
}
res.set_int(val);
});
cs.add_command("|~", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("|~", "i1V", [](CsValueRange args, CsValue &res) {
CsInt val;
if (args.size() >= 2) {
val = args[0].get_int() | ~args[1].get_int();
@ -240,7 +240,7 @@ void cs_init_lib_math(CsState &cs) {
res.set_int(val);
});
cs.add_command("<<", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<<", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) {
return (val2 < CsInt(sizeof(CsInt) * CHAR_BIT))
@ -249,7 +249,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>()
);
});
cs.add_command(">>", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">>", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) {
return val1 >> ostd::clamp(
@ -259,23 +259,23 @@ void cs_init_lib_math(CsState &cs) {
);
});
cs.add_command("+f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("+f", "f1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsFloat>(
args, res, 0, ostd::Add<CsFloat>(), CsMathNoop<CsFloat>()
);
});
cs.add_command("*f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("*f", "f1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsFloat>(
args, res, 1, ostd::Multiply<CsFloat>(), CsMathNoop<CsFloat>()
);
});
cs.add_command("-f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("-f", "f1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsFloat>(
args, res, 0, ostd::Subtract<CsFloat>(), ostd::Negate<CsFloat>()
);
});
cs.add_command("div", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("div", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) {
if (val2) {
@ -285,7 +285,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>()
);
});
cs.add_command("mod", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("mod", "i1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) {
if (val2) {
@ -295,7 +295,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>()
);
});
cs.add_command("divf", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("divf", "f1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) {
if (val2) {
@ -305,7 +305,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsFloat>()
);
});
cs.add_command("modf", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("modf", "f1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) {
if (val2) {
@ -316,7 +316,7 @@ void cs_init_lib_math(CsState &cs) {
);
});
cs.add_command("pow", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("pow", "f1V", [](CsValueRange args, CsValue &res) {
cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) {
return CsFloat(pow(val1, val2));
@ -324,41 +324,41 @@ void cs_init_lib_math(CsState &cs) {
);
});
cs.add_command("=", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("=", "i1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsInt>(args, res, ostd::Equal<CsInt>());
});
cs.add_command("!=", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("!=", "i1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsInt>(args, res, ostd::NotEqual<CsInt>());
});
cs.add_command("<", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<", "i1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsInt>(args, res, ostd::Less<CsInt>());
});
cs.add_command(">", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">", "i1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsInt>(args, res, ostd::Greater<CsInt>());
});
cs.add_command("<=", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<=", "i1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsInt>(args, res, ostd::LessEqual<CsInt>());
});
cs.add_command(">=", "i1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">=", "i1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsInt>(args, res, ostd::GreaterEqual<CsInt>());
});
cs.add_command("=f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("=f", "f1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsFloat>(args, res, ostd::Equal<CsFloat>());
});
cs.add_command("!=f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("!=f", "f1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsFloat>(args, res, ostd::NotEqual<CsFloat>());
});
cs.add_command("<f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<f", "f1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsFloat>(args, res, ostd::Less<CsFloat>());
});
cs.add_command(">f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">f", "f1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsFloat>(args, res, ostd::Greater<CsFloat>());
});
cs.add_command("<=f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<=f", "f1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsFloat>(args, res, ostd::LessEqual<CsFloat>());
});
cs.add_command(">=f", "f1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">=f", "f1V", [](CsValueRange args, CsValue &res) {
cs_cmpop<CsFloat>(args, res, ostd::GreaterEqual<CsFloat>());
});
}

View File

@ -5,7 +5,7 @@
namespace cscript {
template<typename F>
static inline void cs_strgcmp(TvalRange args, TaggedValue &res, F cfunc) {
static inline void cs_strgcmp(CsValueRange args, CsValue &res, F cfunc) {
bool val;
if (args.size() >= 2) {
val = cfunc(args[0].get_strr(), args[1].get_strr());
@ -22,7 +22,7 @@ static inline void cs_strgcmp(TvalRange args, TaggedValue &res, F cfunc) {
};
void cs_init_lib_string(CsState &cs) {
cs.add_command("strstr", "ss", [](TvalRange args, TaggedValue &res) {
cs.add_command("strstr", "ss", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange a = args[0].get_strr(), b = args[1].get_strr();
ostd::ConstCharRange s = a;
for (CsInt i = 0; b.size() <= s.size(); ++i) {
@ -35,11 +35,11 @@ void cs_init_lib_string(CsState &cs) {
res.set_int(-1);
});
cs.add_command("strlen", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("strlen", "s", [](CsValueRange args, CsValue &res) {
res.set_int(CsInt(args[0].get_strr().size()));
});
cs.add_command("strcode", "si", [](TvalRange args, TaggedValue &res) {
cs.add_command("strcode", "si", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange str = args[0].get_strr();
CsInt i = args[1].get_int();
if (i >= CsInt(str.size())) {
@ -49,14 +49,14 @@ void cs_init_lib_string(CsState &cs) {
}
});
cs.add_command("codestr", "i", [](TvalRange args, TaggedValue &res) {
cs.add_command("codestr", "i", [](CsValueRange args, CsValue &res) {
char *s = new char[2];
s[0] = char(args[0].get_int());
s[1] = '\0';
res.set_mstr(s);
});
cs.add_command("strlower", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("strlower", "s", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1];
for (auto i: ostd::range(s.size())) {
@ -66,7 +66,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(buf, s.size()));
});
cs.add_command("strupper", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("strupper", "s", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1];
for (auto i: ostd::range(s.size())) {
@ -76,14 +76,14 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(buf, s.size()));
});
cs.add_command("escape", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("escape", "s", [](CsValueRange args, CsValue &res) {
auto x = ostd::appender<ostd::String>();
util::escape_string(x, args[0].get_strr());
ostd::Size len = x.size();
res.set_mstr(ostd::CharRange(x.get().disown(), len));
});
cs.add_command("unescape", "s", [](TvalRange args, TaggedValue &res) {
cs.add_command("unescape", "s", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1];
auto writer = ostd::CharRange(buf, s.size() + 1);
@ -92,21 +92,21 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(buf, s.size()));
});
cs.add_command("concat", "V", [](TvalRange args, TaggedValue &res) {
cs.add_command("concat", "V", [](CsValueRange args, CsValue &res) {
auto s = ostd::appender<ostd::String>();
cscript::util::tvals_concat(s, args, " ");
res.set_mstr(s.get().iter());
s.get().disown();
});
cs.add_command("concatword", "V", [](TvalRange args, TaggedValue &res) {
cs.add_command("concatword", "V", [](CsValueRange args, CsValue &res) {
auto s = ostd::appender<ostd::String>();
cscript::util::tvals_concat(s, args);
res.set_mstr(s.get().iter());
s.get().disown();
});
cs.add_command("format", "V", [](TvalRange args, TaggedValue &res) {
cs.add_command("format", "V", [](CsValueRange args, CsValue &res) {
if (args.empty()) {
return;
}
@ -139,7 +139,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(s.disown(), len));
});
cs.add_command("tohex", "ii", [](TvalRange args, TaggedValue &res) {
cs.add_command("tohex", "ii", [](CsValueRange args, CsValue &res) {
auto r = ostd::appender<ostd::Vector<char>>();
ostd::format(
r, "0x%.*X", ostd::max(args[1].get_int(), 1), args[0].get_int()
@ -149,7 +149,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(r.get().disown(), len));
});
cs.add_command("substr", "siiN", [](TvalRange args, TaggedValue &res) {
cs.add_command("substr", "siiN", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange s = args[0].get_strr();
CsInt start = args[1].get_int(), count = args[2].get_int();
CsInt numargs = args[3].get_int();
@ -160,29 +160,29 @@ void cs_init_lib_string(CsState &cs) {
));
});
cs.add_command("strcmp", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("strcmp", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>());
});
cs.add_command("=s", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("=s", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>());
});
cs.add_command("!=s", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("!=s", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::NotEqual<ostd::ConstCharRange>());
});
cs.add_command("<s", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<s", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::Less<ostd::ConstCharRange>());
});
cs.add_command(">s", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">s", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::Greater<ostd::ConstCharRange>());
});
cs.add_command("<=s", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command("<=s", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::LessEqual<ostd::ConstCharRange>());
});
cs.add_command(">=s", "s1V", [](TvalRange args, TaggedValue &res) {
cs.add_command(">=s", "s1V", [](CsValueRange args, CsValue &res) {
cs_strgcmp(args, res, ostd::GreaterEqual<ostd::ConstCharRange>());
});
cs.add_command("strreplace", "ssss", [](TvalRange args, TaggedValue &res) {
cs.add_command("strreplace", "ssss", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange oldval = args[1].get_strr(),
newval = args[2].get_strr(),
@ -226,7 +226,7 @@ void cs_init_lib_string(CsState &cs) {
}
});
cs.add_command("strsplice", "ssii", [](TvalRange args, TaggedValue &res) {
cs.add_command("strsplice", "ssii", [](CsValueRange args, CsValue &res) {
ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange vals = args[1].get_strr();
CsInt skip = args[2].get_int(),