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);
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);
})->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.run(args[0].get_code(), res);
});
})->p_type = CsIdDoArgs;
new_command("if", "tee", [](
CsState &cs, CsValueRange args, CsValue &res
) {
new_command("if", "tee", [](auto &cs, auto args, auto &res) {
cs.run((args[0].get_bool() ? args[1] : args[2]).get_code(), res);
})->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]);
})->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());
})->p_type = CsIdNot;
new_command("&&", "E1V", [](CsState &cs, CsValueRange args, CsValue &res) {
new_command("&&", "E1V", [](auto &cs, auto args, auto &res) {
if (args.empty()) {
res.set_int(1);
} else {
@ -314,7 +312,7 @@ CsState::CsState(CsAllocCb func, void *data):
}
})->p_type = CsIdAnd;
new_command("||", "E1V", [](CsState &cs, CsValueRange args, CsValue &res) {
new_command("||", "E1V", [](auto &cs, auto args, auto &res) {
if (args.empty()) {
res.set_int(0);
} else {
@ -1084,13 +1082,11 @@ end:
}
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());
});
gcs.new_command("pcall", "err", [](
CsState &cs, CsValueRange args, CsValue &ret
) {
gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) {
CsAlias *cret = args[1].get_ident()->get_alias(),
*css = args[2].get_ident()->get_alias();
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()) {
throw CsBreakException();
} 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()) {
throw CsContinueException();
} 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()) {
res = ostd::move(args[1]);
} else {
@ -1141,9 +1137,7 @@ void cs_init_lib_base(CsState &gcs) {
}
});
gcs.new_command("cond", "ee2V", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("cond", "ee2V", [](auto &cs, auto args, auto &res) {
for (ostd::Size i = 0; i < args.size(); i += 2) {
if ((i + 1) < args.size()) {
if (cs.run_bool(args[i].get_code())) {
@ -1157,9 +1151,7 @@ void cs_init_lib_base(CsState &gcs) {
}
});
gcs.new_command("case", "ite2V", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("case", "ite2V", [](auto &cs, auto args, auto &res) {
CsInt val = args[0].get_int();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if (
@ -1172,9 +1164,7 @@ void cs_init_lib_base(CsState &gcs) {
}
});
gcs.new_command("casef", "fte2V", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("casef", "fte2V", [](auto &cs, auto args, auto &res) {
CsFloat val = args[0].get_float();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if (
@ -1187,9 +1177,7 @@ void cs_init_lib_base(CsState &gcs) {
}
});
gcs.new_command("cases", "ste2V", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("cases", "ste2V", [](auto &cs, auto args, auto &res) {
CsString val = args[0].get_str();
for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) {
if (
@ -1202,9 +1190,7 @@ void cs_init_lib_base(CsState &gcs) {
}
});
gcs.new_command("pushif", "rTe", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("pushif", "rTe", [](auto &cs, auto args, auto &res) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) {
return;
@ -1216,81 +1202,63 @@ void cs_init_lib_base(CsState &gcs) {
}
});
gcs.new_command("loop", "rie", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loop", "rie", [](auto &cs, auto args, auto &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr,
args[2].get_code()
);
});
gcs.new_command("loop+", "riie", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loop+", "riie", [](auto &cs, auto args, auto &) {
cs_do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1,
nullptr, args[3].get_code()
);
});
gcs.new_command("loop*", "riie", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loop*", "riie", [](auto &cs, auto args, auto &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(),
nullptr, args[3].get_code()
);
});
gcs.new_command("loop+*", "riiie", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loop+*", "riiie", [](auto &cs, auto args, auto &) {
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()
);
});
gcs.new_command("loopwhile", "riee", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loopwhile", "riee", [](auto &cs, auto args, auto &) {
cs_do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), args[3].get_code()
);
});
gcs.new_command("loopwhile+", "riiee", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loopwhile+", "riiee", [](auto &cs, auto args, auto &) {
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()
);
});
gcs.new_command("loopwhile*", "riiee", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loopwhile*", "riiee", [](auto &cs, auto args, auto &) {
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()
);
});
gcs.new_command("loopwhile+*", "riiiee", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("loopwhile+*", "riiiee", [](auto &cs, auto args, auto &) {
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()
);
});
gcs.new_command("while", "ee", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("while", "ee", [](auto &cs, auto args, auto &) {
CsBytecode *cond = args[0].get_code(), *body = args[1].get_code();
while (cs.run_bool(cond)) {
switch (cs.run_loop(body)) {
@ -1304,45 +1272,35 @@ end:
return;
});
gcs.new_command("loopconcat", "rie", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("loopconcat", "rie", [](auto &cs, auto args, auto &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), true
);
});
gcs.new_command("loopconcat+", "riie", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("loopconcat+", "riie", [](auto &cs, auto args, auto &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(),
args[2].get_int(), 1, args[3].get_code(), true
);
});
gcs.new_command("loopconcat*", "riie", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("loopconcat*", "riie", [](auto &cs, auto args, auto &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(),
args[1].get_int(), args[3].get_code(), true
);
});
gcs.new_command("loopconcat+*", "riiie", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("loopconcat+*", "riiie", [](auto &cs, auto args, auto &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
);
});
gcs.new_command("loopconcatword", "rie", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("loopconcatword", "rie", [](auto &cs, auto args, auto &res) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1,
args[2].get_code(), false
@ -1350,7 +1308,7 @@ end:
});
gcs.new_command("loopconcatword+", "riie", [](
CsState &cs, CsValueRange args, CsValue &res
auto &cs, auto args, auto &res
) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(),
@ -1359,7 +1317,7 @@ end:
});
gcs.new_command("loopconcatword*", "riie", [](
CsState &cs, CsValueRange args, CsValue &res
auto &cs, auto args, auto &res
) {
cs_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(),
@ -1368,7 +1326,7 @@ end:
});
gcs.new_command("loopconcatword+*", "riiie", [](
CsState &cs, CsValueRange args, CsValue &res
auto &cs, auto args, auto &res
) {
cs_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(),
@ -1376,9 +1334,7 @@ end:
);
});
gcs.new_command("push", "rTe", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("push", "rTe", [](auto &cs, auto args, auto &res) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) {
return;
@ -1388,48 +1344,32 @@ end:
cs.run(args[2].get_code(), res);
});
gcs.new_command("resetvar", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("resetvar", "s", [](auto &cs, auto args, auto &res) {
res.set_int(cs.reset_var(args[0].get_strr()));
});
gcs.new_command("alias", "sT", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("alias", "sT", [](auto &cs, auto args, auto &) {
cs.set_alias(args[0].get_strr(), ostd::move(args[1]));
});
gcs.new_command("getvarmin", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("getvarmin", "s", [](auto &cs, auto args, auto &res) {
res.set_int(cs.get_var_min_int(args[0].get_strr()).value_or(0));
});
gcs.new_command("getvarmax", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("getvarmax", "s", [](auto &cs, auto args, auto &res) {
res.set_int(cs.get_var_max_int(args[0].get_strr()).value_or(0));
});
gcs.new_command("getfvarmin", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("getfvarmin", "s", [](auto &cs, auto args, auto &res) {
res.set_float(cs.get_var_min_float(args[0].get_strr()).value_or(0.0f));
});
gcs.new_command("getfvarmax", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("getfvarmax", "s", [](auto &cs, auto args, auto &res) {
res.set_float(cs.get_var_max_float(args[0].get_strr()).value_or(0.0f));
});
gcs.new_command("identexists", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("identexists", "s", [](auto &cs, auto args, auto &res) {
res.set_int(cs.have_ident(args[0].get_strr()));
});
gcs.new_command("getalias", "s", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("getalias", "s", [](auto &cs, auto args, auto &res) {
res.set_str(
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);
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())));
});
gcs.new_command("at", "si1V", [](CsState &, CsValueRange args, CsValue &res) {
gcs.new_command("at", "si1V", [](auto &, auto args, auto &res) {
if (args.empty()) {
return;
}
@ -170,9 +170,7 @@ void cs_init_lib_list(CsState &gcs) {
res.set_mstr(er);
});
gcs.new_command("sublist", "siiN", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("sublist", "siiN", [](auto &, auto args, auto &res) {
CsInt skip = args[1].get_int(),
count = 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));
});
gcs.new_command("listfind", "rse", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) {
res.set_int(-1);
@ -223,9 +219,7 @@ void cs_init_lib_list(CsState &gcs) {
res.set_int(-1);
});
gcs.new_command("listassoc", "rse", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("listassoc", "rse", [](auto &cs, auto args, auto &res) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) {
return;
@ -251,27 +245,21 @@ void cs_init_lib_list(CsState &gcs) {
}
});
gcs.new_command("listfind=", "i", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listfind=", "i", [](auto &, auto args, auto &res) {
cs_list_find<CsInt>(
args, res, [](const util::ListParser &p, CsInt val) {
return cs_parse_int(p.item) == val;
}
);
});
gcs.new_command("listfind=f", "f", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listfind=f", "f", [](auto &, auto args, auto &res) {
cs_list_find<CsFloat>(
args, res, [](const util::ListParser &p, CsFloat val) {
return cs_parse_float(p.item) == val;
}
);
});
gcs.new_command("listfind=s", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listfind=s", "s", [](auto &, auto args, auto &res) {
cs_list_find<ostd::ConstCharRange>(
args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
return p.item == val;
@ -279,27 +267,21 @@ void cs_init_lib_list(CsState &gcs) {
);
});
gcs.new_command("listassoc=", "i", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listassoc=", "i", [](auto &, auto args, auto &res) {
cs_list_assoc<CsInt>(
args, res, [](const util::ListParser &p, CsInt val) {
return cs_parse_int(p.item) == val;
}
);
});
gcs.new_command("listassoc=f", "f", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listassoc=f", "f", [](auto &, auto args, auto &res) {
cs_list_assoc<CsFloat>(
args, res, [](const util::ListParser &p, CsFloat val) {
return cs_parse_float(p.item) == val;
}
);
});
gcs.new_command("listassoc=s", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listassoc=s", "s", [](auto &, auto args, auto &res) {
cs_list_assoc<ostd::ConstCharRange>(
args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
return p.item == val;
@ -307,9 +289,7 @@ void cs_init_lib_list(CsState &gcs) {
);
});
gcs.new_command("looplist", "rse", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("looplist", "rse", [](auto &cs, auto args, auto &) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) {
return;
@ -330,9 +310,7 @@ end:
return;
});
gcs.new_command("looplist2", "rrse", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("looplist2", "rrse", [](auto &cs, auto args, auto &) {
CsStackedValue idv1{args[0].get_ident()}, idv2{args[1].get_ident()};
if (!idv1.has_alias() || !idv2.has_alias()) {
return;
@ -355,9 +333,7 @@ end:
return;
});
gcs.new_command("looplist3", "rrrse", [](
CsState &cs, CsValueRange args, CsValue &
) {
gcs.new_command("looplist3", "rrrse", [](auto &cs, auto args, auto &) {
CsStackedValue idv1{args[0].get_ident()};
CsStackedValue idv2{args[1].get_ident()};
CsStackedValue idv3{args[2].get_ident()};
@ -384,9 +360,7 @@ end:
return;
});
gcs.new_command("looplistconcat", "rse", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("looplistconcat", "rse", [](auto &cs, auto args, auto &res) {
cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(),
args[2].get_code(), true
@ -394,7 +368,7 @@ end:
});
gcs.new_command("looplistconcatword", "rse", [](
CsState &cs, CsValueRange args, CsValue &res
auto &cs, auto args, auto &res
) {
cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(),
@ -402,9 +376,7 @@ end:
);
});
gcs.new_command("listfilter", "rse", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("listfilter", "rse", [](auto &cs, auto args, auto &res) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) {
return;
@ -428,9 +400,7 @@ end:
res.set_mstr(ostd::CharRange(r.disown(), len));
});
gcs.new_command("listcount", "rse", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) {
CsStackedValue idv{args[0].get_ident()};
if (!idv.has_alias()) {
return;
@ -448,9 +418,7 @@ end:
res.set_int(r);
});
gcs.new_command("prettylist", "ss", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("prettylist", "ss", [](auto &, auto args, auto &res) {
CsVector<char> buf;
ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange conj = args[1].get_strr();
@ -484,33 +452,23 @@ end:
res.set_mstr(ostd::CharRange(buf.disown(), slen));
});
gcs.new_command("indexof", "ss", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("indexof", "ss", [](auto &, auto args, auto &res) {
res.set_int(
cs_list_includes(args[0].get_strr(), args[1].get_strr())
);
});
gcs.new_command("listdel", "ss", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listdel", "ss", [](auto &, auto args, auto &res) {
cs_list_merge<false, false>(args, res, ostd::Less<int>());
});
gcs.new_command("listintersect", "ss", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listintersect", "ss", [](auto &, auto args, auto &res) {
cs_list_merge<false, false>(args, res, ostd::GreaterEqual<int>());
});
gcs.new_command("listunion", "ss", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listunion", "ss", [](auto &, auto args, auto &res) {
cs_list_merge<true, true>(args, res, ostd::Less<int>());
});
gcs.new_command("listsplice", "ssii", [](
CsState &, CsValueRange args, CsValue &res
) {
gcs.new_command("listsplice", "ssii", [](auto &, auto args, auto &res) {
CsInt offset = ostd::max(args[2].get_int(), CsInt(0));
CsInt len = ostd::max(args[3].get_int(), CsInt(0));
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) {
gcs.new_command("sortlist", "srree", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) {
cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(),
args[2].get_ident(), args[3].get_code(), args[4].get_code()
);
});
gcs.new_command("uniquelist", "srre", [](
CsState &cs, CsValueRange args, CsValue &res
) {
gcs.new_command("uniquelist", "srre", [](auto &cs, auto args, auto &res) {
cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(),
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); }
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));
});
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));
});
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));
});
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);
});
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);
});
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);
});
cs.new_command("atan2", "ff", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("atan2", "ff", [](auto &, auto args, auto &res) {
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()));
});
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()));
});
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);
});
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()));
});
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()));
});
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);
for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::min(v, args[i].get_int());
}
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);
for (ostd::Size i = 1; i < args.size(); ++i) {
v = ostd::max(v, args[i].get_int());
}
res.set_int(v);
});
cs.new_command("minf", "f1V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("minf", "f1V", [](auto &, auto args, auto &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.new_command("maxf", "f1V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("maxf", "f1V", [](auto &, auto args, auto &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());
@ -213,23 +207,21 @@ void cs_init_lib_math(CsState &cs) {
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()));
});
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()));
});
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()));
});
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()));
});
cs.new_command("round", "ff", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("round", "ff", [](auto &, auto args, auto &res) {
CsFloat step = args[1].get_float();
CsFloat r = args[0].get_float();
if (step > 0) {
@ -241,43 +233,43 @@ void cs_init_lib_math(CsState &cs) {
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.new_command("*", "i1V", [](CsState &, CsValueRange args, CsValue &res) {
cs.new_command("*", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<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>(
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>(
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>(
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>(
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>(
args, res, 0, ostd::BitOr<CsInt>(), CsMathNoop<CsInt>()
);
});
/* special combined cases */
cs.new_command("^~", "i1V", [](CsState &, CsValueRange args, CsValue &res) {
cs.new_command("^~", "i1V", [](auto &, auto args, auto &res) {
CsInt val;
if (args.size() >= 2) {
val = args[0].get_int() ^ ~args[1].get_int();
@ -289,7 +281,7 @@ void cs_init_lib_math(CsState &cs) {
}
res.set_int(val);
});
cs.new_command("&~", "i1V", [](CsState &, CsValueRange args, CsValue &res) {
cs.new_command("&~", "i1V", [](auto &, auto args, auto &res) {
CsInt val;
if (args.size() >= 2) {
val = args[0].get_int() & ~args[1].get_int();
@ -301,7 +293,7 @@ void cs_init_lib_math(CsState &cs) {
}
res.set_int(val);
});
cs.new_command("|~", "i1V", [](CsState &, CsValueRange args, CsValue &res) {
cs.new_command("|~", "i1V", [](auto &, auto args, auto &res) {
CsInt val;
if (args.size() >= 2) {
val = args[0].get_int() | ~args[1].get_int();
@ -314,7 +306,7 @@ void cs_init_lib_math(CsState &cs) {
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>(
args, res, 0, [](CsInt val1, CsInt val2) {
return (val2 < CsInt(sizeof(CsInt) * CHAR_BIT))
@ -323,7 +315,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>()
);
});
cs.new_command(">>", "i1V", [](CsState &, CsValueRange args, CsValue &res) {
cs.new_command(">>", "i1V", [](auto &, auto args, auto &res) {
cs_mathop<CsInt>(
args, res, 0, [](CsInt val1, CsInt val2) {
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>(
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>(
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>(
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>(
args, res, 0, [](CsInt val1, CsInt val2) {
if (val2) {
@ -359,7 +351,7 @@ void cs_init_lib_math(CsState &cs) {
}, 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>(
args, res, 0, [](CsInt val1, CsInt val2) {
if (val2) {
@ -369,9 +361,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsInt>()
);
});
cs.new_command("divf", "f1V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("divf", "f1V", [](auto &, auto args, auto &res) {
cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat val2) {
if (val2) {
@ -381,9 +371,7 @@ void cs_init_lib_math(CsState &cs) {
}, CsMathNoop<CsFloat>()
);
});
cs.new_command("modf", "f1V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("modf", "f1V", [](auto &, auto args, auto &res) {
cs_mathop<CsFloat>(
args, res, 0, [](CsFloat val1, CsFloat 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>(
args, res, 0, [](CsFloat val1, CsFloat 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.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.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.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.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.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.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.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.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.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.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.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>());
});
}

View File

@ -22,9 +22,7 @@ static inline void cs_strgcmp(CsValueRange args, CsValue &res, F cfunc) {
};
void cs_init_lib_string(CsState &cs) {
cs.new_command("strstr", "ss", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strstr", "ss", [](auto &, auto args, auto &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) {
@ -37,15 +35,11 @@ void cs_init_lib_string(CsState &cs) {
res.set_int(-1);
});
cs.new_command("strlen", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strlen", "s", [](auto &, auto args, auto &res) {
res.set_int(CsInt(args[0].get_strr().size()));
});
cs.new_command("strcode", "si", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strcode", "si", [](auto &, auto args, auto &res) {
ostd::ConstCharRange str = args[0].get_strr();
CsInt i = args[1].get_int();
if (i >= CsInt(str.size())) {
@ -55,18 +49,14 @@ void cs_init_lib_string(CsState &cs) {
}
});
cs.new_command("codestr", "i", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("codestr", "i", [](auto &, auto args, auto &res) {
char *s = new char[2];
s[0] = char(args[0].get_int());
s[1] = '\0';
res.set_mstr(s);
});
cs.new_command("strlower", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strlower", "s", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1];
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()));
});
cs.new_command("strupper", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strupper", "s", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[s.size() + 1];
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()));
});
cs.new_command("escape", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("escape", "s", [](auto &, auto args, auto &res) {
auto x = ostd::appender<CsString>();
util::escape_string(x, args[0].get_strr());
ostd::Size len = x.size();
res.set_mstr(ostd::CharRange(x.get().disown(), len));
});
cs.new_command("unescape", "s", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("unescape", "s", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr();
char *buf = new char[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()));
});
cs.new_command("concat", "V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("concat", "V", [](auto &, auto args, auto &res) {
auto s = ostd::appender<CsString>();
cscript::util::tvals_concat(s, args, " ");
res.set_mstr(s.get().iter());
s.get().disown();
});
cs.new_command("concatword", "V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("concatword", "V", [](auto &, auto args, auto &res) {
auto s = ostd::appender<CsString>();
cscript::util::tvals_concat(s, args);
res.set_mstr(s.get().iter());
s.get().disown();
});
cs.new_command("format", "V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("format", "V", [](auto &, auto args, auto &res) {
if (args.empty()) {
return;
}
@ -161,9 +139,7 @@ void cs_init_lib_string(CsState &cs) {
res.set_mstr(ostd::CharRange(s.disown(), len));
});
cs.new_command("tohex", "ii", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("tohex", "ii", [](auto &, auto args, auto &res) {
auto r = ostd::appender<CsVector<char>>();
ostd::format(
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));
});
cs.new_command("substr", "siiN", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("substr", "siiN", [](auto &, auto args, auto &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();
@ -189,33 +163,29 @@ void cs_init_lib_string(CsState &cs) {
));
});
cs.new_command("strcmp", "s1V", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strcmp", "s1V", [](auto &, auto args, auto &res) {
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.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.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.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.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.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.new_command("strreplace", "ssss", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strreplace", "ssss", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange oldval = args[1].get_strr(),
newval = args[2].get_strr(),
@ -259,9 +229,7 @@ void cs_init_lib_string(CsState &cs) {
}
});
cs.new_command("strsplice", "ssii", [](
CsState &, CsValueRange args, CsValue &res
) {
cs.new_command("strsplice", "ssii", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr();
ostd::ConstCharRange vals = args[1].get_strr();
CsInt skip = args[2].get_int(),

View File

@ -233,7 +233,7 @@ static void do_tty(CsState &cs) {
auto prompt2 = cs.new_svar("PROMPT2", ">> ");
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;
});
@ -272,7 +272,7 @@ int main(int argc, char **argv) {
CsState gcs;
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();
bool ret = cs.run_file(file);
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());
});