use type inference for shorter command funcs

master
Daniel Kolesa 2016-09-15 00:42:19 +02:00
parent c5772f0720
commit b0917ade03
5 changed files with 147 additions and 297 deletions

View File

@ -272,31 +272,29 @@ CsState::CsState(CsAllocCb func, void *data):
id = new_ivar("dbgalias", 0, 1000, 4); id = new_ivar("dbgalias", 0, 1000, 4);
assert(id->get_index() == DbgaliasIdx); assert(id->get_index() == DbgaliasIdx);
new_command("do", "e", [](CsState &cs, CsValueRange args, CsValue &res) { new_command("do", "e", [](auto &cs, auto args, auto &res) {
cs.run(args[0].get_code(), res); cs.run(args[0].get_code(), res);
})->p_type = CsIdDo; })->p_type = CsIdDo;
new_command("doargs", "e", [](CsState &cs, CsValueRange args, CsValue &res) { new_command("doargs", "e", [](auto &cs, auto args, auto &res) {
cs_do_args(cs, [&cs, &res, &args]() { cs_do_args(cs, [&cs, &res, &args]() {
cs.run(args[0].get_code(), res); cs.run(args[0].get_code(), res);
}); });
})->p_type = CsIdDoArgs; })->p_type = CsIdDoArgs;
new_command("if", "tee", []( new_command("if", "tee", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs.run((args[0].get_bool() ? args[1] : args[2]).get_code(), res); cs.run((args[0].get_bool() ? args[1] : args[2]).get_code(), res);
})->p_type = CsIdIf; })->p_type = CsIdIf;
new_command("result", "T", [](CsState &, CsValueRange args, CsValue &res) { new_command("result", "T", [](auto &, auto args, auto &res) {
res = ostd::move(args[0]); res = ostd::move(args[0]);
})->p_type = CsIdResult; })->p_type = CsIdResult;
new_command("!", "t", [](CsState &, CsValueRange args, CsValue &res) { new_command("!", "t", [](auto &, auto args, auto &res) {
res.set_int(!args[0].get_bool()); res.set_int(!args[0].get_bool());
})->p_type = CsIdNot; })->p_type = CsIdNot;
new_command("&&", "E1V", [](CsState &cs, CsValueRange args, CsValue &res) { new_command("&&", "E1V", [](auto &cs, auto args, auto &res) {
if (args.empty()) { if (args.empty()) {
res.set_int(1); res.set_int(1);
} else { } else {
@ -314,7 +312,7 @@ CsState::CsState(CsAllocCb func, void *data):
} }
})->p_type = CsIdAnd; })->p_type = CsIdAnd;
new_command("||", "E1V", [](CsState &cs, CsValueRange args, CsValue &res) { new_command("||", "E1V", [](auto &cs, auto args, auto &res) {
if (args.empty()) { if (args.empty()) {
res.set_int(0); res.set_int(0);
} else { } else {
@ -1084,13 +1082,11 @@ end:
} }
void cs_init_lib_base(CsState &gcs) { void cs_init_lib_base(CsState &gcs) {
gcs.new_command("error", "s", [](CsState &cs, CsValueRange args, CsValue &) { gcs.new_command("error", "s", [](auto &cs, auto args, auto &) {
throw CsErrorException(cs, args[0].get_strr()); throw CsErrorException(cs, args[0].get_strr());
}); });
gcs.new_command("pcall", "err", []( gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) {
CsState &cs, CsValueRange args, CsValue &ret
) {
CsAlias *cret = args[1].get_ident()->get_alias(), CsAlias *cret = args[1].get_ident()->get_alias(),
*css = args[2].get_ident()->get_alias(); *css = args[2].get_ident()->get_alias();
if (!cret || !css) { if (!cret || !css) {
@ -1117,7 +1113,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("break", "", [](CsState &cs, auto, auto &) { gcs.new_command("break", "", [](auto &cs, auto, auto &) {
if (cs.is_in_loop()) { if (cs.is_in_loop()) {
throw CsBreakException(); throw CsBreakException();
} else { } else {
@ -1125,7 +1121,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("continue", "", [](CsState &cs, auto, auto &) { gcs.new_command("continue", "", [](auto &cs, auto, auto &) {
if (cs.is_in_loop()) { if (cs.is_in_loop()) {
throw CsContinueException(); throw CsContinueException();
} else { } else {
@ -1133,7 +1129,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("?", "tTT", [](CsState &, CsValueRange args, CsValue &res) { gcs.new_command("?", "tTT", [](auto &, auto args, auto &res) {
if (args[0].get_bool()) { if (args[0].get_bool()) {
res = ostd::move(args[1]); res = ostd::move(args[1]);
} else { } else {
@ -1141,9 +1137,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("cond", "ee2V", []( gcs.new_command("cond", "ee2V", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
for (ostd::Size i = 0; i < args.size(); i += 2) { for (ostd::Size i = 0; i < args.size(); i += 2) {
if ((i + 1) < args.size()) { if ((i + 1) < args.size()) {
if (cs.run_bool(args[i].get_code())) { if (cs.run_bool(args[i].get_code())) {
@ -1157,9 +1151,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("case", "ite2V", []( gcs.new_command("case", "ite2V", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsInt val = args[0].get_int(); CsInt val = args[0].get_int();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ( if (
@ -1172,9 +1164,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("casef", "fte2V", []( gcs.new_command("casef", "fte2V", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsFloat val = args[0].get_float(); CsFloat val = args[0].get_float();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if ( if (
@ -1187,9 +1177,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("cases", "ste2V", []( gcs.new_command("cases", "ste2V", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsString 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 ( if (
@ -1202,9 +1190,7 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("pushif", "rTe", []( gcs.new_command("pushif", "rTe", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) {
return; return;
@ -1216,81 +1202,63 @@ void cs_init_lib_base(CsState &gcs) {
} }
}); });
gcs.new_command("loop", "rie", []( gcs.new_command("loop", "rie", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr, cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr,
args[2].get_code() args[2].get_code()
); );
}); });
gcs.new_command("loop+", "riie", []( gcs.new_command("loop+", "riie", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
nullptr, args[3].get_code() nullptr, args[3].get_code()
); );
}); });
gcs.new_command("loop*", "riie", []( gcs.new_command("loop*", "riie", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(), cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(),
nullptr, args[3].get_code() nullptr, args[3].get_code()
); );
}); });
gcs.new_command("loop+*", "riiie", []( gcs.new_command("loop+*", "riiie", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
args[2].get_int(), nullptr, args[4].get_code() args[2].get_int(), nullptr, args[4].get_code()
); );
}); });
gcs.new_command("loopwhile", "riee", []( gcs.new_command("loopwhile", "riee", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1, cs, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), args[3].get_code() args[2].get_code(), args[3].get_code()
); );
}); });
gcs.new_command("loopwhile+", "riiee", []( gcs.new_command("loopwhile+", "riiee", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
args[3].get_code(), args[4].get_code() args[3].get_code(), args[4].get_code()
); );
}); });
gcs.new_command("loopwhile*", "riiee", []( gcs.new_command("loopwhile*", "riiee", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), cs, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(),
args[3].get_code(), args[4].get_code() args[3].get_code(), args[4].get_code()
); );
}); });
gcs.new_command("loopwhile+*", "riiiee", []( gcs.new_command("loopwhile+*", "riiiee", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs_do_loop( cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), 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() args[2].get_int(), args[4].get_code(), args[5].get_code()
); );
}); });
gcs.new_command("while", "ee", []( gcs.new_command("while", "ee", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
CsBytecode *cond = args[0].get_code(), *body = args[1].get_code(); CsBytecode *cond = args[0].get_code(), *body = args[1].get_code();
while (cs.run_bool(cond)) { while (cs.run_bool(cond)) {
switch (cs.run_loop(body)) { switch (cs.run_loop(body)) {
@ -1304,45 +1272,35 @@ end:
return; return;
}); });
gcs.new_command("loopconcat", "rie", []( gcs.new_command("loopconcat", "rie", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), true args[2].get_code(), true
); );
}); });
gcs.new_command("loopconcat+", "riie", []( gcs.new_command("loopconcat+", "riie", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), cs, res, *args[0].get_ident(), args[1].get_int(),
args[2].get_int(), 1, args[3].get_code(), true args[2].get_int(), 1, args[3].get_code(), true
); );
}); });
gcs.new_command("loopconcat*", "riie", []( gcs.new_command("loopconcat*", "riie", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(), cs, res, *args[0].get_ident(), 0, args[2].get_int(),
args[1].get_int(), args[3].get_code(), true args[1].get_int(), args[3].get_code(), true
); );
}); });
gcs.new_command("loopconcat+*", "riiie", []( gcs.new_command("loopconcat+*", "riiie", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), cs, res, *args[0].get_ident(), args[1].get_int(),
args[3].get_int(), args[2].get_int(), args[4].get_code(), true args[3].get_int(), args[2].get_int(), args[4].get_code(), true
); );
}); });
gcs.new_command("loopconcatword", "rie", []( gcs.new_command("loopconcatword", "rie", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), false args[2].get_code(), false
@ -1350,7 +1308,7 @@ end:
}); });
gcs.new_command("loopconcatword+", "riie", []( gcs.new_command("loopconcatword+", "riie", [](
CsState &cs, CsValueRange args, CsValue &res auto &cs, auto args, auto &res
) { ) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), cs, res, *args[0].get_ident(), args[1].get_int(),
@ -1359,7 +1317,7 @@ end:
}); });
gcs.new_command("loopconcatword*", "riie", []( gcs.new_command("loopconcatword*", "riie", [](
CsState &cs, CsValueRange args, CsValue &res auto &cs, auto args, auto &res
) { ) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(), cs, res, *args[0].get_ident(), 0, args[2].get_int(),
@ -1368,7 +1326,7 @@ end:
}); });
gcs.new_command("loopconcatword+*", "riiie", []( gcs.new_command("loopconcatword+*", "riiie", [](
CsState &cs, CsValueRange args, CsValue &res auto &cs, auto args, auto &res
) { ) {
cs_loop_conc( cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
@ -1376,9 +1334,7 @@ end:
); );
}); });
gcs.new_command("push", "rTe", []( gcs.new_command("push", "rTe", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) {
return; return;
@ -1388,48 +1344,32 @@ end:
cs.run(args[2].get_code(), res); cs.run(args[2].get_code(), res);
}); });
gcs.new_command("resetvar", "s", []( gcs.new_command("resetvar", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_int(cs.reset_var(args[0].get_strr())); res.set_int(cs.reset_var(args[0].get_strr()));
}); });
gcs.new_command("alias", "sT", []( gcs.new_command("alias", "sT", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
cs.set_alias(args[0].get_strr(), ostd::move(args[1])); cs.set_alias(args[0].get_strr(), ostd::move(args[1]));
}); });
gcs.new_command("getvarmin", "s", []( gcs.new_command("getvarmin", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_int(cs.get_var_min_int(args[0].get_strr()).value_or(0)); res.set_int(cs.get_var_min_int(args[0].get_strr()).value_or(0));
}); });
gcs.new_command("getvarmax", "s", []( gcs.new_command("getvarmax", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_int(cs.get_var_max_int(args[0].get_strr()).value_or(0)); res.set_int(cs.get_var_max_int(args[0].get_strr()).value_or(0));
}); });
gcs.new_command("getfvarmin", "s", []( gcs.new_command("getfvarmin", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_float(cs.get_var_min_float(args[0].get_strr()).value_or(0.0f)); res.set_float(cs.get_var_min_float(args[0].get_strr()).value_or(0.0f));
}); });
gcs.new_command("getfvarmax", "s", []( gcs.new_command("getfvarmax", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_float(cs.get_var_max_float(args[0].get_strr()).value_or(0.0f)); res.set_float(cs.get_var_max_float(args[0].get_strr()).value_or(0.0f));
}); });
gcs.new_command("identexists", "s", []( gcs.new_command("identexists", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_int(cs.have_ident(args[0].get_strr())); res.set_int(cs.have_ident(args[0].get_strr()));
}); });
gcs.new_command("getalias", "s", []( gcs.new_command("getalias", "s", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
res.set_str( res.set_str(
ostd::move(cs.get_alias_val(args[0].get_strr()).value_or("")) ostd::move(cs.get_alias_val(args[0].get_strr()).value_or(""))
); );

View File

@ -141,11 +141,11 @@ static inline void cs_list_merge(CsValueRange args, CsValue &res, F cmp) {
static void cs_init_lib_list_sort(CsState &cs); static void cs_init_lib_list_sort(CsState &cs);
void cs_init_lib_list(CsState &gcs) { void cs_init_lib_list(CsState &gcs) {
gcs.new_command("listlen", "s", [](CsState &, CsValueRange args, CsValue &res) { gcs.new_command("listlen", "s", [](auto &, auto args, auto &res) {
res.set_int(CsInt(util::list_length(args[0].get_strr()))); res.set_int(CsInt(util::list_length(args[0].get_strr())));
}); });
gcs.new_command("at", "si1V", [](CsState &, CsValueRange args, CsValue &res) { gcs.new_command("at", "si1V", [](auto &, auto args, auto &res) {
if (args.empty()) { if (args.empty()) {
return; return;
} }
@ -170,9 +170,7 @@ void cs_init_lib_list(CsState &gcs) {
res.set_mstr(er); res.set_mstr(er);
}); });
gcs.new_command("sublist", "siiN", []( gcs.new_command("sublist", "siiN", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
CsInt skip = args[1].get_int(), CsInt skip = args[1].get_int(),
count = args[2].get_int(), count = args[2].get_int(),
numargs = args[2].get_int(); numargs = args[2].get_int();
@ -201,9 +199,7 @@ void cs_init_lib_list(CsState &gcs) {
res.set_str(ostd::ConstCharRange(list, qend - list)); res.set_str(ostd::ConstCharRange(list, qend - list));
}); });
gcs.new_command("listfind", "rse", []( gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) { if (!idv.has_alias()) {
res.set_int(-1); res.set_int(-1);
@ -223,9 +219,7 @@ void cs_init_lib_list(CsState &gcs) {
res.set_int(-1); res.set_int(-1);
}); });
gcs.new_command("listassoc", "rse", []( gcs.new_command("listassoc", "rse", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) { if (!idv.has_alias()) {
return; return;
@ -251,27 +245,21 @@ void cs_init_lib_list(CsState &gcs) {
} }
}); });
gcs.new_command("listfind=", "i", []( gcs.new_command("listfind=", "i", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_find<CsInt>( cs_list_find<CsInt>(
args, res, [](const util::ListParser &p, CsInt val) { args, res, [](const util::ListParser &p, CsInt val) {
return cs_parse_int(p.item) == val; return cs_parse_int(p.item) == val;
} }
); );
}); });
gcs.new_command("listfind=f", "f", []( gcs.new_command("listfind=f", "f", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_find<CsFloat>( cs_list_find<CsFloat>(
args, res, [](const util::ListParser &p, CsFloat val) { args, res, [](const util::ListParser &p, CsFloat val) {
return cs_parse_float(p.item) == val; return cs_parse_float(p.item) == val;
} }
); );
}); });
gcs.new_command("listfind=s", "s", []( gcs.new_command("listfind=s", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_find<ostd::ConstCharRange>( cs_list_find<ostd::ConstCharRange>(
args, res, [](const util::ListParser &p, ostd::ConstCharRange val) { args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
return p.item == val; return p.item == val;
@ -279,27 +267,21 @@ void cs_init_lib_list(CsState &gcs) {
); );
}); });
gcs.new_command("listassoc=", "i", []( gcs.new_command("listassoc=", "i", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_assoc<CsInt>( cs_list_assoc<CsInt>(
args, res, [](const util::ListParser &p, CsInt val) { args, res, [](const util::ListParser &p, CsInt val) {
return cs_parse_int(p.item) == val; return cs_parse_int(p.item) == val;
} }
); );
}); });
gcs.new_command("listassoc=f", "f", []( gcs.new_command("listassoc=f", "f", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_assoc<CsFloat>( cs_list_assoc<CsFloat>(
args, res, [](const util::ListParser &p, CsFloat val) { args, res, [](const util::ListParser &p, CsFloat val) {
return cs_parse_float(p.item) == val; return cs_parse_float(p.item) == val;
} }
); );
}); });
gcs.new_command("listassoc=s", "s", []( gcs.new_command("listassoc=s", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_assoc<ostd::ConstCharRange>( cs_list_assoc<ostd::ConstCharRange>(
args, res, [](const util::ListParser &p, ostd::ConstCharRange val) { args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
return p.item == val; return p.item == val;
@ -307,9 +289,7 @@ void cs_init_lib_list(CsState &gcs) {
); );
}); });
gcs.new_command("looplist", "rse", []( gcs.new_command("looplist", "rse", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) { if (!idv.has_alias()) {
return; return;
@ -330,9 +310,7 @@ end:
return; return;
}); });
gcs.new_command("looplist2", "rrse", []( gcs.new_command("looplist2", "rrse", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
CsStackedValue idv1{args[0].get_ident()}, idv2{args[1].get_ident()}; CsStackedValue idv1{args[0].get_ident()}, idv2{args[1].get_ident()};
if (!idv1.has_alias() || !idv2.has_alias()) { if (!idv1.has_alias() || !idv2.has_alias()) {
return; return;
@ -355,9 +333,7 @@ end:
return; return;
}); });
gcs.new_command("looplist3", "rrrse", []( gcs.new_command("looplist3", "rrrse", [](auto &cs, auto args, auto &) {
CsState &cs, CsValueRange args, CsValue &
) {
CsStackedValue idv1{args[0].get_ident()}; CsStackedValue idv1{args[0].get_ident()};
CsStackedValue idv2{args[1].get_ident()}; CsStackedValue idv2{args[1].get_ident()};
CsStackedValue idv3{args[2].get_ident()}; CsStackedValue idv3{args[2].get_ident()};
@ -384,9 +360,7 @@ end:
return; return;
}); });
gcs.new_command("looplistconcat", "rse", []( gcs.new_command("looplistconcat", "rse", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_loop_list_conc( cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(), cs, res, args[0].get_ident(), args[1].get_strr(),
args[2].get_code(), true args[2].get_code(), true
@ -394,7 +368,7 @@ end:
}); });
gcs.new_command("looplistconcatword", "rse", []( gcs.new_command("looplistconcatword", "rse", [](
CsState &cs, CsValueRange args, CsValue &res auto &cs, auto args, auto &res
) { ) {
cs_loop_list_conc( cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(), cs, res, args[0].get_ident(), args[1].get_strr(),
@ -402,9 +376,7 @@ end:
); );
}); });
gcs.new_command("listfilter", "rse", []( gcs.new_command("listfilter", "rse", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) { if (!idv.has_alias()) {
return; return;
@ -428,9 +400,7 @@ end:
res.set_mstr(ostd::CharRange(r.disown(), len)); res.set_mstr(ostd::CharRange(r.disown(), len));
}); });
gcs.new_command("listcount", "rse", []( gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
CsStackedValue idv{args[0].get_ident()}; CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) { if (!idv.has_alias()) {
return; return;
@ -448,9 +418,7 @@ end:
res.set_int(r); res.set_int(r);
}); });
gcs.new_command("prettylist", "ss", []( gcs.new_command("prettylist", "ss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
CsVector<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();
@ -484,33 +452,23 @@ end:
res.set_mstr(ostd::CharRange(buf.disown(), slen)); res.set_mstr(ostd::CharRange(buf.disown(), slen));
}); });
gcs.new_command("indexof", "ss", []( gcs.new_command("indexof", "ss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
res.set_int( res.set_int(
cs_list_includes(args[0].get_strr(), args[1].get_strr()) cs_list_includes(args[0].get_strr(), args[1].get_strr())
); );
}); });
gcs.new_command("listdel", "ss", []( gcs.new_command("listdel", "ss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_merge<false, false>(args, res, ostd::Less<int>()); cs_list_merge<false, false>(args, res, ostd::Less<int>());
}); });
gcs.new_command("listintersect", "ss", []( gcs.new_command("listintersect", "ss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_merge<false, false>(args, res, ostd::GreaterEqual<int>()); cs_list_merge<false, false>(args, res, ostd::GreaterEqual<int>());
}); });
gcs.new_command("listunion", "ss", []( gcs.new_command("listunion", "ss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_list_merge<true, true>(args, res, ostd::Less<int>()); cs_list_merge<true, true>(args, res, ostd::Less<int>());
}); });
gcs.new_command("listsplice", "ssii", []( gcs.new_command("listsplice", "ssii", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
CsInt offset = ostd::max(args[2].get_int(), CsInt(0)); CsInt offset = ostd::max(args[2].get_int(), CsInt(0));
CsInt len = ostd::max(args[3].get_int(), CsInt(0)); CsInt len = ostd::max(args[3].get_int(), CsInt(0));
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
@ -679,17 +637,13 @@ static void cs_list_sort(
} }
static void cs_init_lib_list_sort(CsState &gcs) { static void cs_init_lib_list_sort(CsState &gcs) {
gcs.new_command("sortlist", "srree", []( gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_list_sort( cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(), cs, res, args[0].get_strr(), args[1].get_ident(),
args[2].get_ident(), args[3].get_code(), args[4].get_code() args[2].get_ident(), args[3].get_code(), args[4].get_code()
); );
}); });
gcs.new_command("uniquelist", "srre", []( gcs.new_command("uniquelist", "srre", [](auto &cs, auto args, auto &res) {
CsState &cs, CsValueRange args, CsValue &res
) {
cs_list_sort( cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(), cs, res, args[0].get_strr(), args[1].get_ident(),
args[2].get_ident(), nullptr, args[3].get_code() args[2].get_ident(), nullptr, args[3].get_code()

View File

@ -138,74 +138,68 @@ inline auto cs_fmod(double f, double f2) { return fmod(f, f2); }
inline auto cs_fmod(long double f, long double f2) { return fmodl(f, f2); } inline auto cs_fmod(long double f, long double f2) { return fmodl(f, f2); }
void cs_init_lib_math(CsState &cs) { void cs_init_lib_math(CsState &cs) {
cs.new_command("sin", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("sin", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_sin(args[0].get_float() * RAD)); res.set_float(cs_sin(args[0].get_float() * RAD));
}); });
cs.new_command("cos", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("cos", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_cos(args[0].get_float() * RAD)); res.set_float(cs_cos(args[0].get_float() * RAD));
}); });
cs.new_command("tan", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("tan", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_tan(args[0].get_float() * RAD)); res.set_float(cs_tan(args[0].get_float() * RAD));
}); });
cs.new_command("asin", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("asin", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_asin(args[0].get_float()) / RAD); res.set_float(cs_asin(args[0].get_float()) / RAD);
}); });
cs.new_command("acos", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("acos", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_acos(args[0].get_float()) / RAD); res.set_float(cs_acos(args[0].get_float()) / RAD);
}); });
cs.new_command("atan", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("atan", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_atan(args[0].get_float()) / RAD); res.set_float(cs_atan(args[0].get_float()) / RAD);
}); });
cs.new_command("atan2", "ff", []( cs.new_command("atan2", "ff", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
res.set_float(cs_atan2(args[0].get_float(), args[1].get_float()) / RAD); res.set_float(cs_atan2(args[0].get_float(), args[1].get_float()) / RAD);
}); });
cs.new_command("sqrt", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("sqrt", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_sqrt(args[0].get_float())); res.set_float(cs_sqrt(args[0].get_float()));
}); });
cs.new_command("loge", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("loge", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_log(args[0].get_float())); res.set_float(cs_log(args[0].get_float()));
}); });
cs.new_command("log2", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("log2", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_log(args[0].get_float()) / M_LN2); res.set_float(cs_log(args[0].get_float()) / M_LN2);
}); });
cs.new_command("log10", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("log10", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_log10(args[0].get_float())); res.set_float(cs_log10(args[0].get_float()));
}); });
cs.new_command("exp", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("exp", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_exp(args[0].get_float())); res.set_float(cs_exp(args[0].get_float()));
}); });
cs.new_command("min", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("min", "i1V", [](auto &, auto args, auto &res) {
CsInt v = (!args.empty() ? args[0].get_int() : 0); CsInt v = (!args.empty() ? args[0].get_int() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) { for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::min(v, args[i].get_int()); v = ostd::min(v, args[i].get_int());
} }
res.set_int(v); res.set_int(v);
}); });
cs.new_command("max", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("max", "i1V", [](auto &, auto args, auto &res) {
CsInt v = (!args.empty() ? args[0].get_int() : 0); CsInt v = (!args.empty() ? args[0].get_int() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) { for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::max(v, args[i].get_int()); v = ostd::max(v, args[i].get_int());
} }
res.set_int(v); res.set_int(v);
}); });
cs.new_command("minf", "f1V", []( cs.new_command("minf", "f1V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
CsFloat v = (!args.empty() ? args[0].get_float() : 0); CsFloat v = (!args.empty() ? args[0].get_float() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) { for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::min(v, args[i].get_float()); v = ostd::min(v, args[i].get_float());
} }
res.set_float(v); res.set_float(v);
}); });
cs.new_command("maxf", "f1V", []( cs.new_command("maxf", "f1V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
CsFloat v = (!args.empty() ? args[0].get_float() : 0); CsFloat v = (!args.empty() ? args[0].get_float() : 0);
for (ostd::Size i = 1; i < args.size(); ++i) { for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::max(v, args[i].get_float()); v = ostd::max(v, args[i].get_float());
@ -213,23 +207,21 @@ void cs_init_lib_math(CsState &cs) {
res.set_float(v); res.set_float(v);
}); });
cs.new_command("abs", "i", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("abs", "i", [](auto &, auto args, auto &res) {
res.set_int(cs_abs(args[0].get_int())); res.set_int(cs_abs(args[0].get_int()));
}); });
cs.new_command("absf", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("absf", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_abs(args[0].get_float())); res.set_float(cs_abs(args[0].get_float()));
}); });
cs.new_command("floor", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("floor", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_floor(args[0].get_float())); res.set_float(cs_floor(args[0].get_float()));
}); });
cs.new_command("ceil", "f", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("ceil", "f", [](auto &, auto args, auto &res) {
res.set_float(cs_ceil(args[0].get_float())); res.set_float(cs_ceil(args[0].get_float()));
}); });
cs.new_command("round", "ff", []( cs.new_command("round", "ff", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
CsFloat step = args[1].get_float(); CsFloat step = args[1].get_float();
CsFloat r = args[0].get_float(); CsFloat r = args[0].get_float();
if (step > 0) { if (step > 0) {
@ -241,43 +233,43 @@ void cs_init_lib_math(CsState &cs) {
res.set_float(r); res.set_float(r);
}); });
cs.new_command("+", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("+", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>(args, res, 0, ostd::Add<CsInt>(), CsMathNoop<CsInt>()); cs_mathop<CsInt>(args, res, 0, ostd::Add<CsInt>(), CsMathNoop<CsInt>());
}); });
cs.new_command("*", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("*", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 1, ostd::Multiply<CsInt>(), CsMathNoop<CsInt>() args, res, 1, ostd::Multiply<CsInt>(), CsMathNoop<CsInt>()
); );
}); });
cs.new_command("-", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("-", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, ostd::Subtract<CsInt>(), ostd::Negate<CsInt>() args, res, 0, ostd::Subtract<CsInt>(), ostd::Negate<CsInt>()
); );
}); });
cs.new_command("^", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("^", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, ostd::BitXor<CsInt>(), [](CsInt val) { return ~val; } args, res, 0, ostd::BitXor<CsInt>(), [](CsInt val) { return ~val; }
); );
}); });
cs.new_command("~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("~", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, ostd::BitXor<CsInt>(), [](CsInt val) { return ~val; } args, res, 0, ostd::BitXor<CsInt>(), [](CsInt val) { return ~val; }
); );
}); });
cs.new_command("&", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("&", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, ostd::BitAnd<CsInt>(), CsMathNoop<CsInt>() args, res, 0, ostd::BitAnd<CsInt>(), CsMathNoop<CsInt>()
); );
}); });
cs.new_command("|", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("|", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, ostd::BitOr<CsInt>(), CsMathNoop<CsInt>() args, res, 0, ostd::BitOr<CsInt>(), CsMathNoop<CsInt>()
); );
}); });
/* special combined cases */ /* special combined cases */
cs.new_command("^~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("^~", "i1V", [](auto &, auto args, auto &res) {
CsInt val; CsInt val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = args[0].get_int() ^ ~args[1].get_int(); val = args[0].get_int() ^ ~args[1].get_int();
@ -289,7 +281,7 @@ void cs_init_lib_math(CsState &cs) {
} }
res.set_int(val); res.set_int(val);
}); });
cs.new_command("&~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("&~", "i1V", [](auto &, auto args, auto &res) {
CsInt val; CsInt val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = args[0].get_int() & ~args[1].get_int(); val = args[0].get_int() & ~args[1].get_int();
@ -301,7 +293,7 @@ void cs_init_lib_math(CsState &cs) {
} }
res.set_int(val); res.set_int(val);
}); });
cs.new_command("|~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("|~", "i1V", [](auto &, auto args, auto &res) {
CsInt val; CsInt val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = args[0].get_int() | ~args[1].get_int(); val = args[0].get_int() | ~args[1].get_int();
@ -314,7 +306,7 @@ void cs_init_lib_math(CsState &cs) {
res.set_int(val); res.set_int(val);
}); });
cs.new_command("<<", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<<", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) { args, res, 0, [](CsInt val1, CsInt val2) {
return (val2 < CsInt(sizeof(CsInt) * CHAR_BIT)) return (val2 < CsInt(sizeof(CsInt) * CHAR_BIT))
@ -323,7 +315,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>() }, CsMathNoop<CsInt>()
); );
}); });
cs.new_command(">>", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">>", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) { args, res, 0, [](CsInt val1, CsInt val2) {
return val1 >> ostd::clamp( return val1 >> ostd::clamp(
@ -333,23 +325,23 @@ void cs_init_lib_math(CsState &cs) {
); );
}); });
cs.new_command("+f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("+f", "f1V", [](auto &, auto args, auto &res) {
cs_mathop<CsFloat>( cs_mathop<CsFloat>(
args, res, 0, ostd::Add<CsFloat>(), CsMathNoop<CsFloat>() args, res, 0, ostd::Add<CsFloat>(), CsMathNoop<CsFloat>()
); );
}); });
cs.new_command("*f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("*f", "f1V", [](auto &, auto args, auto &res) {
cs_mathop<CsFloat>( cs_mathop<CsFloat>(
args, res, 1, ostd::Multiply<CsFloat>(), CsMathNoop<CsFloat>() args, res, 1, ostd::Multiply<CsFloat>(), CsMathNoop<CsFloat>()
); );
}); });
cs.new_command("-f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("-f", "f1V", [](auto &, auto args, auto &res) {
cs_mathop<CsFloat>( cs_mathop<CsFloat>(
args, res, 0, ostd::Subtract<CsFloat>(), ostd::Negate<CsFloat>() args, res, 0, ostd::Subtract<CsFloat>(), ostd::Negate<CsFloat>()
); );
}); });
cs.new_command("div", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("div", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) { args, res, 0, [](CsInt val1, CsInt val2) {
if (val2) { if (val2) {
@ -359,7 +351,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>() }, CsMathNoop<CsInt>()
); );
}); });
cs.new_command("mod", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("mod", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>( cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) { args, res, 0, [](CsInt val1, CsInt val2) {
if (val2) { if (val2) {
@ -369,9 +361,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>() }, CsMathNoop<CsInt>()
); );
}); });
cs.new_command("divf", "f1V", []( cs.new_command("divf", "f1V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_mathop<CsFloat>( cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) { args, res, 0, [](CsFloat val1, CsFloat val2) {
if (val2) { if (val2) {
@ -381,9 +371,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsFloat>() }, CsMathNoop<CsFloat>()
); );
}); });
cs.new_command("modf", "f1V", []( cs.new_command("modf", "f1V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_mathop<CsFloat>( cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) { args, res, 0, [](CsFloat val1, CsFloat val2) {
if (val2) { if (val2) {
@ -394,7 +382,7 @@ void cs_init_lib_math(CsState &cs) {
); );
}); });
cs.new_command("pow", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("pow", "f1V", [](auto &, auto args, auto &res) {
cs_mathop<CsFloat>( cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) { args, res, 0, [](CsFloat val1, CsFloat val2) {
return CsFloat(pow(val1, val2)); return CsFloat(pow(val1, val2));
@ -402,41 +390,41 @@ void cs_init_lib_math(CsState &cs) {
); );
}); });
cs.new_command("=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("=", "i1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsInt>(args, res, ostd::Equal<CsInt>()); cs_cmpop<CsInt>(args, res, ostd::Equal<CsInt>());
}); });
cs.new_command("!=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("!=", "i1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsInt>(args, res, ostd::NotEqual<CsInt>()); cs_cmpop<CsInt>(args, res, ostd::NotEqual<CsInt>());
}); });
cs.new_command("<", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<", "i1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsInt>(args, res, ostd::Less<CsInt>()); cs_cmpop<CsInt>(args, res, ostd::Less<CsInt>());
}); });
cs.new_command(">", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">", "i1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsInt>(args, res, ostd::Greater<CsInt>()); cs_cmpop<CsInt>(args, res, ostd::Greater<CsInt>());
}); });
cs.new_command("<=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<=", "i1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsInt>(args, res, ostd::LessEqual<CsInt>()); cs_cmpop<CsInt>(args, res, ostd::LessEqual<CsInt>());
}); });
cs.new_command(">=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">=", "i1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsInt>(args, res, ostd::GreaterEqual<CsInt>()); cs_cmpop<CsInt>(args, res, ostd::GreaterEqual<CsInt>());
}); });
cs.new_command("=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("=f", "f1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsFloat>(args, res, ostd::Equal<CsFloat>()); cs_cmpop<CsFloat>(args, res, ostd::Equal<CsFloat>());
}); });
cs.new_command("!=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("!=f", "f1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsFloat>(args, res, ostd::NotEqual<CsFloat>()); cs_cmpop<CsFloat>(args, res, ostd::NotEqual<CsFloat>());
}); });
cs.new_command("<f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<f", "f1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsFloat>(args, res, ostd::Less<CsFloat>()); cs_cmpop<CsFloat>(args, res, ostd::Less<CsFloat>());
}); });
cs.new_command(">f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">f", "f1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsFloat>(args, res, ostd::Greater<CsFloat>()); cs_cmpop<CsFloat>(args, res, ostd::Greater<CsFloat>());
}); });
cs.new_command("<=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<=f", "f1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsFloat>(args, res, ostd::LessEqual<CsFloat>()); cs_cmpop<CsFloat>(args, res, ostd::LessEqual<CsFloat>());
}); });
cs.new_command(">=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">=f", "f1V", [](auto &, auto args, auto &res) {
cs_cmpop<CsFloat>(args, res, ostd::GreaterEqual<CsFloat>()); cs_cmpop<CsFloat>(args, res, ostd::GreaterEqual<CsFloat>());
}); });
} }

View File

@ -22,9 +22,7 @@ static inline void cs_strgcmp(CsValueRange args, CsValue &res, F cfunc) {
}; };
void cs_init_lib_string(CsState &cs) { void cs_init_lib_string(CsState &cs) {
cs.new_command("strstr", "ss", []( cs.new_command("strstr", "ss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange a = args[0].get_strr(), b = args[1].get_strr(); ostd::ConstCharRange a = args[0].get_strr(), b = args[1].get_strr();
ostd::ConstCharRange s = a; ostd::ConstCharRange s = a;
for (CsInt i = 0; b.size() <= s.size(); ++i) { for (CsInt i = 0; b.size() <= s.size(); ++i) {
@ -37,15 +35,11 @@ void cs_init_lib_string(CsState &cs) {
res.set_int(-1); res.set_int(-1);
}); });
cs.new_command("strlen", "s", []( cs.new_command("strlen", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
res.set_int(CsInt(args[0].get_strr().size())); res.set_int(CsInt(args[0].get_strr().size()));
}); });
cs.new_command("strcode", "si", []( cs.new_command("strcode", "si", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange str = args[0].get_strr(); ostd::ConstCharRange str = args[0].get_strr();
CsInt i = args[1].get_int(); CsInt i = args[1].get_int();
if (i >= CsInt(str.size())) { if (i >= CsInt(str.size())) {
@ -55,18 +49,14 @@ void cs_init_lib_string(CsState &cs) {
} }
}); });
cs.new_command("codestr", "i", []( cs.new_command("codestr", "i", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
char *s = new char[2]; char *s = new char[2];
s[0] = char(args[0].get_int()); s[0] = char(args[0].get_int());
s[1] = '\0'; s[1] = '\0';
res.set_mstr(s); res.set_mstr(s);
}); });
cs.new_command("strlower", "s", []( cs.new_command("strlower", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1]; char *buf = new char[s.size() + 1];
for (auto i: ostd::range(s.size())) { for (auto i: ostd::range(s.size())) {
@ -76,9 +66,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(buf, s.size())); res.set_mstr(ostd::CharRange(buf, s.size()));
}); });
cs.new_command("strupper", "s", []( cs.new_command("strupper", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1]; char *buf = new char[s.size() + 1];
for (auto i: ostd::range(s.size())) { for (auto i: ostd::range(s.size())) {
@ -88,18 +76,14 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(buf, s.size())); res.set_mstr(ostd::CharRange(buf, s.size()));
}); });
cs.new_command("escape", "s", []( cs.new_command("escape", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
auto x = ostd::appender<CsString>(); 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));
}); });
cs.new_command("unescape", "s", []( cs.new_command("unescape", "s", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1]; char *buf = new char[s.size() + 1];
auto writer = ostd::CharRange(buf, s.size() + 1); auto writer = ostd::CharRange(buf, s.size() + 1);
@ -108,27 +92,21 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(buf, s.size())); res.set_mstr(ostd::CharRange(buf, s.size()));
}); });
cs.new_command("concat", "V", []( cs.new_command("concat", "V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
auto s = ostd::appender<CsString>(); 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.new_command("concatword", "V", []( cs.new_command("concatword", "V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
auto s = ostd::appender<CsString>(); 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.new_command("format", "V", []( cs.new_command("format", "V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
if (args.empty()) { if (args.empty()) {
return; return;
} }
@ -161,9 +139,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(s.disown(), len)); res.set_mstr(ostd::CharRange(s.disown(), len));
}); });
cs.new_command("tohex", "ii", []( cs.new_command("tohex", "ii", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
auto r = ostd::appender<CsVector<char>>(); auto r = ostd::appender<CsVector<char>>();
ostd::format( ostd::format(
r, "0x%.*X", ostd::max(args[1].get_int(), CsInt(1)), r, "0x%.*X", ostd::max(args[1].get_int(), CsInt(1)),
@ -174,9 +150,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(r.get().disown(), len)); res.set_mstr(ostd::CharRange(r.get().disown(), len));
}); });
cs.new_command("substr", "siiN", []( cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
CsInt start = args[1].get_int(), count = args[2].get_int(); CsInt start = args[1].get_int(), count = args[2].get_int();
CsInt numargs = args[3].get_int(); CsInt numargs = args[3].get_int();
@ -189,33 +163,29 @@ void cs_init_lib_string(CsState &cs) {
)); ));
}); });
cs.new_command("strcmp", "s1V", []( cs.new_command("strcmp", "s1V", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>());
}); });
cs.new_command("=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("=s", "s1V", [](auto &, auto args, auto &res) {
cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>());
}); });
cs.new_command("!=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("!=s", "s1V", [](auto &, auto args, auto &res) {
cs_strgcmp(args, res, ostd::NotEqual<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::NotEqual<ostd::ConstCharRange>());
}); });
cs.new_command("<s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<s", "s1V", [](auto &, auto args, auto &res) {
cs_strgcmp(args, res, ostd::Less<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::Less<ostd::ConstCharRange>());
}); });
cs.new_command(">s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">s", "s1V", [](auto &, auto args, auto &res) {
cs_strgcmp(args, res, ostd::Greater<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::Greater<ostd::ConstCharRange>());
}); });
cs.new_command("<=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command("<=s", "s1V", [](auto &, auto args, auto &res) {
cs_strgcmp(args, res, ostd::LessEqual<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::LessEqual<ostd::ConstCharRange>());
}); });
cs.new_command(">=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs.new_command(">=s", "s1V", [](auto &, auto args, auto &res) {
cs_strgcmp(args, res, ostd::GreaterEqual<ostd::ConstCharRange>()); cs_strgcmp(args, res, ostd::GreaterEqual<ostd::ConstCharRange>());
}); });
cs.new_command("strreplace", "ssss", []( cs.new_command("strreplace", "ssss", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange oldval = args[1].get_strr(), ostd::ConstCharRange oldval = args[1].get_strr(),
newval = args[2].get_strr(), newval = args[2].get_strr(),
@ -259,9 +229,7 @@ void cs_init_lib_string(CsState &cs) {
} }
}); });
cs.new_command("strsplice", "ssii", []( cs.new_command("strsplice", "ssii", [](auto &, auto args, auto &res) {
CsState &, CsValueRange args, CsValue &res
) {
ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange vals = args[1].get_strr(); ostd::ConstCharRange vals = args[1].get_strr();
CsInt skip = args[2].get_int(), CsInt skip = args[2].get_int(),

View File

@ -233,7 +233,7 @@ static void do_tty(CsState &cs) {
auto prompt2 = cs.new_svar("PROMPT2", ">> "); auto prompt2 = cs.new_svar("PROMPT2", ">> ");
bool do_exit = false; bool do_exit = false;
cs.new_command("quit", "", [&do_exit](CsState &, auto, auto &) { cs.new_command("quit", "", [&do_exit](auto &, auto, auto &) {
do_exit = true; do_exit = true;
}); });
@ -272,7 +272,7 @@ int main(int argc, char **argv) {
CsState gcs; CsState gcs;
gcs.init_libs(); gcs.init_libs();
gcs.new_command("exec", "sb", [](CsState &cs, auto args, auto &res) { gcs.new_command("exec", "sb", [](auto &cs, auto args, auto &res) {
auto file = args[0].get_strr(); auto file = args[0].get_strr();
bool ret = cs.run_file(file); bool ret = cs.run_file(file);
if (!ret) { if (!ret) {
@ -285,7 +285,7 @@ int main(int argc, char **argv) {
} }
}); });
gcs.new_command("echo", "C", [](CsState &cs, auto args, auto &) { gcs.new_command("echo", "C", [](auto &cs, auto args, auto &) {
cs.get_out().writeln(args[0].get_strr()); cs.get_out().writeln(args[0].get_strr());
}); });