diff --git a/cubescript.cc b/cubescript.cc index 88b50883..546d650b 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -273,11 +273,11 @@ CsState::CsState() { id = new_ivar("dbgalias", 0, 1000, 4); assert(id->get_index() == DbgaliasIdx); - add_command("do", "e", [this](CsValueRange args, CsValue &res) { + new_command("do", "e", [this](CsValueRange args, CsValue &res) { run_ret(args[0].get_code(), res); })->p_type = ID_DO; - add_command("doargs", "e", [this](CsValueRange args, CsValue &res) { + new_command("doargs", "e", [this](CsValueRange args, CsValue &res) { if (p_stack != &noalias) { cs_do_args(*this, [&]() { run_ret(args[0].get_code(), res); }); } else { @@ -285,21 +285,21 @@ CsState::CsState() { } })->p_type = ID_DOARGS; - add_command("if", "tee", [this](CsValueRange args, CsValue &res) { + new_command("if", "tee", [this](CsValueRange args, CsValue &res) { run_ret((args[0].get_bool() ? args[1] : args[2]).get_code(), res); })->p_type = ID_IF; - add_command("result", "T", [](CsValueRange args, CsValue &res) { + new_command("result", "T", [](CsValueRange args, CsValue &res) { CsValue &v = args[0]; res = v; v.set_null(); })->p_type = ID_RESULT; - add_command("!", "t", [](CsValueRange args, CsValue &res) { + new_command("!", "t", [](CsValueRange args, CsValue &res) { res.set_int(!args[0].get_bool()); })->p_type = ID_NOT; - add_command("&&", "E1V", [this](CsValueRange args, CsValue &res) { + new_command("&&", "E1V", [this](CsValueRange args, CsValue &res) { if (args.empty()) { res.set_int(1); } else { @@ -320,7 +320,7 @@ CsState::CsState() { } })->p_type = ID_AND; - add_command("||", "E1V", [this](CsValueRange args, CsValue &res) { + new_command("||", "E1V", [this](CsValueRange args, CsValue &res) { if (args.empty()) { res.set_int(0); } else { @@ -341,7 +341,7 @@ CsState::CsState() { } })->p_type = ID_OR; - add_command("local", nullptr, nullptr)->p_type = ID_LOCAL; + new_command("local", nullptr, nullptr)->p_type = ID_LOCAL; cs_init_lib_base(*this); } @@ -1199,7 +1199,7 @@ void CsState::set_var_str_checked(CsSvar *sv, ostd::ConstCharRange v) { sv->changed(); } -CsCommand *CsState::add_command( +CsCommand *CsState::new_command( ostd::ConstCharRange name, ostd::ConstCharRange args, CsCommandCb func ) { int nargs = 0; @@ -1247,7 +1247,7 @@ CsCommand *CsState::add_command( } void cs_init_lib_io(CsState &cs) { - cs.add_command("exec", "sb", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("exec", "sb", [&cs](CsValueRange args, CsValue &res) { auto file = args[0].get_strr(); bool ret = cs.run_file(file); if (!ret) { @@ -1260,7 +1260,7 @@ void cs_init_lib_io(CsState &cs) { } }); - cs.add_command("echo", "C", [](CsValueRange args, CsValue &) { + cs.new_command("echo", "C", [](CsValueRange args, CsValue &) { ostd::writeln(args[0].get_strr()); }); } @@ -1310,11 +1310,11 @@ static inline void cs_loop_conc( } void cs_init_lib_base(CsState &cs) { - cs.add_command("?", "tTT", [](CsValueRange args, CsValue &res) { + cs.new_command("?", "tTT", [](CsValueRange args, CsValue &res) { res.set(args[0].get_bool() ? args[1] : args[2]); }); - cs.add_command("cond", "ee2V", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("cond", "ee2V", [&cs](CsValueRange args, CsValue &res) { for (ostd::Size i = 0; i < args.size(); i += 2) { if ((i + 1) < args.size()) { if (cs.run_bool(args[i].get_code())) { @@ -1328,7 +1328,7 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.add_command("case", "ite2V", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("case", "ite2V", [&cs](CsValueRange args, CsValue &res) { CsInt val = args[0].get_int(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ( @@ -1341,7 +1341,7 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.add_command("casef", "fte2V", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("casef", "fte2V", [&cs](CsValueRange args, CsValue &res) { CsFloat val = args[0].get_float(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ( @@ -1354,7 +1354,7 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.add_command("cases", "ste2V", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("cases", "ste2V", [&cs](CsValueRange args, CsValue &res) { CsString val = args[0].get_str(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ( @@ -1367,7 +1367,7 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.add_command("pushif", "rTe", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("pushif", "rTe", [&cs](CsValueRange args, CsValue &res) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { return; @@ -1379,132 +1379,132 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.add_command("loop", "rie", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loop", "rie", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr, args[2].get_code() ); }); - cs.add_command("loop+", "riie", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loop+", "riie", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, nullptr, args[3].get_code() ); }); - cs.add_command("loop*", "riie", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loop*", "riie", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(), nullptr, args[3].get_code() ); }); - cs.add_command("loop+*", "riiie", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loop+*", "riiie", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), nullptr, args[4].get_code() ); }); - cs.add_command("loopwhile", "riee", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loopwhile", "riee", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), 1, args[2].get_code(), args[3].get_code() ); }); - cs.add_command("loopwhile+", "riiee", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loopwhile+", "riiee", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, args[3].get_code(), args[4].get_code() ); }); - cs.add_command("loopwhile*", "riiee", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loopwhile*", "riiee", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), args[3].get_code(), args[4].get_code() ); }); - cs.add_command("loopwhile+*", "riiiee", [&cs](CsValueRange args, CsValue &) { + cs.new_command("loopwhile+*", "riiiee", [&cs](CsValueRange args, CsValue &) { cs_do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), args[4].get_code(), args[5].get_code() ); }); - cs.add_command("while", "ee", [&cs](CsValueRange args, CsValue &) { + cs.new_command("while", "ee", [&cs](CsValueRange args, CsValue &) { CsBytecode *cond = args[0].get_code(), *body = args[1].get_code(); while (cs.run_bool(cond)) { cs.run_int(body); } }); - cs.add_command("loopconcat", "rie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcat", "rie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, args[2].get_code(), true ); }); - cs.add_command("loopconcat+", "riie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcat+", "riie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, args[3].get_code(), true ); }); - cs.add_command("loopconcat*", "riie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcat*", "riie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), args[3].get_code(), true ); }); - cs.add_command("loopconcat+*", "riiie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcat+*", "riiie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), args[4].get_code(), true ); }); - cs.add_command("loopconcatword", "rie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcatword", "rie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, args[2].get_code(), false ); }); - cs.add_command("loopconcatword+", "riie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcatword+", "riie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, args[3].get_code(), false ); }); - cs.add_command("loopconcatword*", "riie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcatword*", "riie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), args[3].get_code(), false ); }); - cs.add_command("loopconcatword+*", "riiie", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("loopconcatword+*", "riiie", [&cs](CsValueRange args, CsValue &res) { cs_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), args[4].get_code(), false ); }); - cs.add_command("nodebug", "e", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("nodebug", "e", [&cs](CsValueRange args, CsValue &res) { ++cs.nodebug; cs.run_ret(args[0].get_code(), res); --cs.nodebug; }); - cs.add_command("push", "rTe", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("push", "rTe", [&cs](CsValueRange args, CsValue &res) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { return; @@ -1514,34 +1514,34 @@ void cs_init_lib_base(CsState &cs) { cs.run_ret(args[2].get_code(), res); }); - cs.add_command("resetvar", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("resetvar", "s", [&cs](CsValueRange args, CsValue &res) { res.set_int(cs.reset_var(args[0].get_strr())); }); - cs.add_command("alias", "sT", [&cs](CsValueRange args, CsValue &) { + cs.new_command("alias", "sT", [&cs](CsValueRange args, CsValue &) { CsValue &v = args[1]; cs.set_alias(args[0].get_strr(), v); v.set_null(); }); - cs.add_command("getvarmin", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("getvarmin", "s", [&cs](CsValueRange args, CsValue &res) { res.set_int(cs.get_var_min_int(args[0].get_strr()).value_or(0)); }); - cs.add_command("getvarmax", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("getvarmax", "s", [&cs](CsValueRange args, CsValue &res) { res.set_int(cs.get_var_max_int(args[0].get_strr()).value_or(0)); }); - cs.add_command("getfvarmin", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("getfvarmin", "s", [&cs](CsValueRange args, CsValue &res) { res.set_float(cs.get_var_min_float(args[0].get_strr()).value_or(0.0f)); }); - cs.add_command("getfvarmax", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("getfvarmax", "s", [&cs](CsValueRange args, CsValue &res) { res.set_float(cs.get_var_max_float(args[0].get_strr()).value_or(0.0f)); }); - cs.add_command("identexists", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("identexists", "s", [&cs](CsValueRange args, CsValue &res) { res.set_int(cs.have_ident(args[0].get_strr())); }); - cs.add_command("getalias", "s", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("getalias", "s", [&cs](CsValueRange args, CsValue &res) { res.set_str(ostd::move(cs.get_alias_val(args[0].get_strr()).value_or(""))); }); } diff --git a/cubescript.hh b/cubescript.hh index 7a7345ef..39c05357 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -364,6 +364,10 @@ struct OSTD_EXPORT CsState { CsVarCb f = CsVarCb(), int flags = 0 ); + CsCommand *new_command( + ostd::ConstCharRange name, ostd::ConstCharRange args, CsCommandCb func + ); + CsIdent *get_ident(ostd::ConstCharRange name) { CsIdent **id = idents.at(name); if (!id) { @@ -387,10 +391,6 @@ struct OSTD_EXPORT CsState { bool reset_var(ostd::ConstCharRange name); void touch_var(ostd::ConstCharRange name); - CsCommand *add_command( - ostd::ConstCharRange name, ostd::ConstCharRange args, CsCommandCb func - ); - CsString run_str(CsBytecode *code); CsString run_str(ostd::ConstCharRange code); CsString run_str(CsIdent *id, CsValueRange args); diff --git a/lib_list.cc b/lib_list.cc index 5aba9fad..82564d74 100644 --- a/lib_list.cc +++ b/lib_list.cc @@ -134,11 +134,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 &cs) { - cs.add_command("listlen", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("listlen", "s", [](CsValueRange args, CsValue &res) { res.set_int(CsInt(util::list_length(args[0].get_strr()))); }); - cs.add_command("at", "si1V", [](CsValueRange args, CsValue &res) { + cs.new_command("at", "si1V", [](CsValueRange args, CsValue &res) { if (args.empty()) { return; } @@ -163,7 +163,7 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(er); }); - cs.add_command("sublist", "siiN", [](CsValueRange args, CsValue &res) { + cs.new_command("sublist", "siiN", [](CsValueRange args, CsValue &res) { CsInt skip = args[1].get_int(), count = args[2].get_int(), numargs = args[2].get_int(); @@ -192,7 +192,7 @@ void cs_init_lib_list(CsState &cs) { res.set_str(ostd::ConstCharRange(list, qend - list)); }); - cs.add_command("listfind", "rse", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("listfind", "rse", [&cs](CsValueRange args, CsValue &res) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { res.set_int(-1); @@ -212,7 +212,7 @@ void cs_init_lib_list(CsState &cs) { res.set_int(-1); }); - cs.add_command("listassoc", "rse", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("listassoc", "rse", [&cs](CsValueRange args, CsValue &res) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -238,21 +238,21 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.add_command("listfind=", "i", [](CsValueRange args, CsValue &res) { + cs.new_command("listfind=", "i", [](CsValueRange args, CsValue &res) { cs_list_find( args, res, [](const util::ListParser &p, CsInt val) { return cs_parse_int(p.item) == val; } ); }); - cs.add_command("listfind=f", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("listfind=f", "f", [](CsValueRange args, CsValue &res) { cs_list_find( args, res, [](const util::ListParser &p, CsFloat val) { return cs_parse_float(p.item) == val; } ); }); - cs.add_command("listfind=s", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("listfind=s", "s", [](CsValueRange args, CsValue &res) { cs_list_find( args, res, [](const util::ListParser &p, ostd::ConstCharRange val) { return p.item == val; @@ -260,21 +260,21 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.add_command("listassoc=", "i", [](CsValueRange args, CsValue &res) { + cs.new_command("listassoc=", "i", [](CsValueRange args, CsValue &res) { cs_list_assoc( args, res, [](const util::ListParser &p, CsInt val) { return cs_parse_int(p.item) == val; } ); }); - cs.add_command("listassoc=f", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("listassoc=f", "f", [](CsValueRange args, CsValue &res) { cs_list_assoc( args, res, [](const util::ListParser &p, CsFloat val) { return cs_parse_float(p.item) == val; } ); }); - cs.add_command("listassoc=s", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("listassoc=s", "s", [](CsValueRange args, CsValue &res) { cs_list_assoc( args, res, [](const util::ListParser &p, ostd::ConstCharRange val) { return p.item == val; @@ -282,7 +282,7 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.add_command("looplist", "rse", [&cs](CsValueRange args, CsValue &) { + cs.new_command("looplist", "rse", [&cs](CsValueRange args, CsValue &) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -296,7 +296,7 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.add_command("looplist2", "rrse", [&cs](CsValueRange args, CsValue &) { + cs.new_command("looplist2", "rrse", [&cs](CsValueRange args, CsValue &) { CsStackedValue idv1{args[0].get_ident()}, idv2{args[1].get_ident()}; if (!idv1.has_alias() || !idv2.has_alias()) { return; @@ -312,7 +312,7 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.add_command("looplist3", "rrrse", [&cs](CsValueRange args, CsValue &) { + cs.new_command("looplist3", "rrrse", [&cs](CsValueRange args, CsValue &) { CsStackedValue idv1{args[0].get_ident()}; CsStackedValue idv2{args[1].get_ident()}; CsStackedValue idv3{args[2].get_ident()}; @@ -332,7 +332,7 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.add_command("looplistconcat", "rse", [&cs]( + cs.new_command("looplistconcat", "rse", [&cs]( CsValueRange args, CsValue &res ) { cs_loop_list_conc( @@ -341,7 +341,7 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.add_command("looplistconcatword", "rse", [&cs]( + cs.new_command("looplistconcatword", "rse", [&cs]( CsValueRange args, CsValue &res ) { cs_loop_list_conc( @@ -350,7 +350,7 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.add_command("listfilter", "rse", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("listfilter", "rse", [&cs](CsValueRange args, CsValue &res) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -374,7 +374,7 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(ostd::CharRange(r.disown(), len)); }); - cs.add_command("listcount", "rse", [&cs](CsValueRange args, CsValue &res) { + cs.new_command("listcount", "rse", [&cs](CsValueRange args, CsValue &res) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -392,7 +392,7 @@ void cs_init_lib_list(CsState &cs) { res.set_int(r); }); - cs.add_command("prettylist", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("prettylist", "ss", [](CsValueRange args, CsValue &res) { CsVector buf; ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange conj = args[1].get_strr(); @@ -426,23 +426,23 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(ostd::CharRange(buf.disown(), slen)); }); - cs.add_command("indexof", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("indexof", "ss", [](CsValueRange args, CsValue &res) { res.set_int( cs_list_includes(args[0].get_strr(), args[1].get_strr()) ); }); - cs.add_command("listdel", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("listdel", "ss", [](CsValueRange args, CsValue &res) { cs_list_merge(args, res, ostd::Less()); }); - cs.add_command("listintersect", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("listintersect", "ss", [](CsValueRange args, CsValue &res) { cs_list_merge(args, res, ostd::GreaterEqual()); }); - cs.add_command("listunion", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("listunion", "ss", [](CsValueRange args, CsValue &res) { cs_list_merge(args, res, ostd::Less()); }); - cs.add_command("listsplice", "ssii", [](CsValueRange args, CsValue &res) { + cs.new_command("listsplice", "ssii", [](CsValueRange args, CsValue &res) { CsInt offset = ostd::max(args[2].get_int(), 0); CsInt len = ostd::max(args[3].get_int(), 0); ostd::ConstCharRange s = args[0].get_strr(); @@ -611,7 +611,7 @@ static void cs_list_sort( } static void cs_init_lib_list_sort(CsState &cs) { - cs.add_command("sortlist", "srree", [&cs]( + cs.new_command("sortlist", "srree", [&cs]( CsValueRange args, CsValue &res ) { cs_list_sort( @@ -619,7 +619,7 @@ static void cs_init_lib_list_sort(CsState &cs) { args[2].get_ident(), args[3].get_code(), args[4].get_code() ); }); - cs.add_command("uniquelist", "srre", [&cs]( + cs.new_command("uniquelist", "srre", [&cs]( CsValueRange args, CsValue &res ) { cs_list_sort( diff --git a/lib_math.cc b/lib_math.cc index 6fb965af..0a79a02a 100644 --- a/lib_math.cc +++ b/lib_math.cc @@ -72,68 +72,68 @@ static inline void cs_cmpop(CsValueRange args, CsValue &res, F cmp) { } void cs_init_lib_math(CsState &cs) { - cs.add_command("sin", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("sin", "f", [](CsValueRange args, CsValue &res) { res.set_float(sin(args[0].get_float() * RAD)); }); - cs.add_command("cos", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("cos", "f", [](CsValueRange args, CsValue &res) { res.set_float(cos(args[0].get_float() * RAD)); }); - cs.add_command("tan", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("tan", "f", [](CsValueRange args, CsValue &res) { res.set_float(tan(args[0].get_float() * RAD)); }); - cs.add_command("asin", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("asin", "f", [](CsValueRange args, CsValue &res) { res.set_float(asin(args[0].get_float()) / RAD); }); - cs.add_command("acos", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("acos", "f", [](CsValueRange args, CsValue &res) { res.set_float(acos(args[0].get_float()) / RAD); }); - cs.add_command("atan", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("atan", "f", [](CsValueRange args, CsValue &res) { res.set_float(atan(args[0].get_float()) / RAD); }); - cs.add_command("atan2", "ff", [](CsValueRange args, CsValue &res) { + cs.new_command("atan2", "ff", [](CsValueRange args, CsValue &res) { res.set_float(atan2(args[0].get_float(), args[1].get_float()) / RAD); }); - cs.add_command("sqrt", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("sqrt", "f", [](CsValueRange args, CsValue &res) { res.set_float(sqrt(args[0].get_float())); }); - cs.add_command("loge", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("loge", "f", [](CsValueRange args, CsValue &res) { res.set_float(log(args[0].get_float())); }); - cs.add_command("log2", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("log2", "f", [](CsValueRange args, CsValue &res) { res.set_float(log(args[0].get_float()) / M_LN2); }); - cs.add_command("log10", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("log10", "f", [](CsValueRange args, CsValue &res) { res.set_float(log10(args[0].get_float())); }); - cs.add_command("exp", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("exp", "f", [](CsValueRange args, CsValue &res) { res.set_float(exp(args[0].get_float())); }); - cs.add_command("min", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("min", "i1V", [](CsValueRange args, CsValue &res) { CsInt v = (!args.empty() ? args[0].get_int() : 0); for (ostd::Size i = 1; i < args.size(); ++i) { v = ostd::min(v, args[i].get_int()); } res.set_int(v); }); - cs.add_command("max", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("max", "i1V", [](CsValueRange args, CsValue &res) { CsInt v = (!args.empty() ? args[0].get_int() : 0); for (ostd::Size i = 1; i < args.size(); ++i) { v = ostd::max(v, args[i].get_int()); } res.set_int(v); }); - cs.add_command("minf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("minf", "f1V", [](CsValueRange args, CsValue &res) { CsFloat v = (!args.empty() ? args[0].get_float() : 0); for (ostd::Size i = 1; i < args.size(); ++i) { v = ostd::min(v, args[i].get_float()); } res.set_float(v); }); - cs.add_command("maxf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("maxf", "f1V", [](CsValueRange args, CsValue &res) { CsFloat v = (!args.empty() ? args[0].get_float() : 0); for (ostd::Size i = 1; i < args.size(); ++i) { v = ostd::max(v, args[i].get_float()); @@ -141,21 +141,21 @@ void cs_init_lib_math(CsState &cs) { res.set_float(v); }); - cs.add_command("abs", "i", [](CsValueRange args, CsValue &res) { + cs.new_command("abs", "i", [](CsValueRange args, CsValue &res) { res.set_int(abs(args[0].get_int())); }); - cs.add_command("absf", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("absf", "f", [](CsValueRange args, CsValue &res) { res.set_float(fabs(args[0].get_float())); }); - cs.add_command("floor", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("floor", "f", [](CsValueRange args, CsValue &res) { res.set_float(floor(args[0].get_float())); }); - cs.add_command("ceil", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("ceil", "f", [](CsValueRange args, CsValue &res) { res.set_float(ceil(args[0].get_float())); }); - cs.add_command("round", "ff", [](CsValueRange args, CsValue &res) { + cs.new_command("round", "ff", [](CsValueRange args, CsValue &res) { double step = args[1].get_float(); double r = args[0].get_float(); if (step > 0) { @@ -167,43 +167,43 @@ void cs_init_lib_math(CsState &cs) { res.set_float(CsFloat(r)); }); - cs.add_command("+", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("+", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop(args, res, 0, ostd::Add(), CsMathNoop()); }); - cs.add_command("*", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("*", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 1, ostd::Multiply(), CsMathNoop() ); }); - cs.add_command("-", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("-", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::Subtract(), ostd::Negate() ); }); - cs.add_command("^", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("^", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitXor(), [](CsInt val) { return ~val; } ); }); - cs.add_command("~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("~", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitXor(), [](CsInt val) { return ~val; } ); }); - cs.add_command("&", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("&", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitAnd(), CsMathNoop() ); }); - cs.add_command("|", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("|", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitOr(), CsMathNoop() ); }); /* special combined cases */ - cs.add_command("^~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("^~", "i1V", [](CsValueRange args, CsValue &res) { CsInt val; if (args.size() >= 2) { val = args[0].get_int() ^ ~args[1].get_int(); @@ -215,7 +215,7 @@ void cs_init_lib_math(CsState &cs) { } res.set_int(val); }); - cs.add_command("&~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("&~", "i1V", [](CsValueRange args, CsValue &res) { CsInt val; if (args.size() >= 2) { val = args[0].get_int() & ~args[1].get_int(); @@ -227,7 +227,7 @@ void cs_init_lib_math(CsState &cs) { } res.set_int(val); }); - cs.add_command("|~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("|~", "i1V", [](CsValueRange args, CsValue &res) { CsInt val; if (args.size() >= 2) { val = args[0].get_int() | ~args[1].get_int(); @@ -240,7 +240,7 @@ void cs_init_lib_math(CsState &cs) { res.set_int(val); }); - cs.add_command("<<", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<<", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { return (val2 < CsInt(sizeof(CsInt) * CHAR_BIT)) @@ -249,7 +249,7 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.add_command(">>", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">>", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { return val1 >> ostd::clamp( @@ -259,23 +259,23 @@ void cs_init_lib_math(CsState &cs) { ); }); - cs.add_command("+f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("+f", "f1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::Add(), CsMathNoop() ); }); - cs.add_command("*f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("*f", "f1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 1, ostd::Multiply(), CsMathNoop() ); }); - cs.add_command("-f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("-f", "f1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::Subtract(), ostd::Negate() ); }); - cs.add_command("div", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("div", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { if (val2) { @@ -285,7 +285,7 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.add_command("mod", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("mod", "i1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { if (val2) { @@ -295,7 +295,7 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.add_command("divf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("divf", "f1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsFloat val1, CsFloat val2) { if (val2) { @@ -305,7 +305,7 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.add_command("modf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("modf", "f1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsFloat val1, CsFloat val2) { if (val2) { @@ -316,7 +316,7 @@ void cs_init_lib_math(CsState &cs) { ); }); - cs.add_command("pow", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("pow", "f1V", [](CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsFloat val1, CsFloat val2) { return CsFloat(pow(val1, val2)); @@ -324,41 +324,41 @@ void cs_init_lib_math(CsState &cs) { ); }); - cs.add_command("=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("=", "i1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Equal()); }); - cs.add_command("!=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("!=", "i1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::NotEqual()); }); - cs.add_command("<", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<", "i1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Less()); }); - cs.add_command(">", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">", "i1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Greater()); }); - cs.add_command("<=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<=", "i1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::LessEqual()); }); - cs.add_command(">=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">=", "i1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::GreaterEqual()); }); - cs.add_command("=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("=f", "f1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Equal()); }); - cs.add_command("!=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("!=f", "f1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::NotEqual()); }); - cs.add_command("(args, res, ostd::Less()); }); - cs.add_command(">f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">f", "f1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Greater()); }); - cs.add_command("<=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<=f", "f1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::LessEqual()); }); - cs.add_command(">=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">=f", "f1V", [](CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::GreaterEqual()); }); } diff --git a/lib_str.cc b/lib_str.cc index 59cc5c4c..89bad7b4 100644 --- a/lib_str.cc +++ b/lib_str.cc @@ -22,7 +22,7 @@ static inline void cs_strgcmp(CsValueRange args, CsValue &res, F cfunc) { }; void cs_init_lib_string(CsState &cs) { - cs.add_command("strstr", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("strstr", "ss", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange a = args[0].get_strr(), b = args[1].get_strr(); ostd::ConstCharRange s = a; for (CsInt i = 0; b.size() <= s.size(); ++i) { @@ -35,11 +35,11 @@ void cs_init_lib_string(CsState &cs) { res.set_int(-1); }); - cs.add_command("strlen", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("strlen", "s", [](CsValueRange args, CsValue &res) { res.set_int(CsInt(args[0].get_strr().size())); }); - cs.add_command("strcode", "si", [](CsValueRange args, CsValue &res) { + cs.new_command("strcode", "si", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange str = args[0].get_strr(); CsInt i = args[1].get_int(); if (i >= CsInt(str.size())) { @@ -49,14 +49,14 @@ void cs_init_lib_string(CsState &cs) { } }); - cs.add_command("codestr", "i", [](CsValueRange args, CsValue &res) { + cs.new_command("codestr", "i", [](CsValueRange args, CsValue &res) { char *s = new char[2]; s[0] = char(args[0].get_int()); s[1] = '\0'; res.set_mstr(s); }); - cs.add_command("strlower", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("strlower", "s", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange s = args[0].get_strr(); char *buf = new char[s.size() + 1]; for (auto i: ostd::range(s.size())) { @@ -66,7 +66,7 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(buf, s.size())); }); - cs.add_command("strupper", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("strupper", "s", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange s = args[0].get_strr(); char *buf = new char[s.size() + 1]; for (auto i: ostd::range(s.size())) { @@ -76,14 +76,14 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(buf, s.size())); }); - cs.add_command("escape", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("escape", "s", [](CsValueRange args, CsValue &res) { auto x = ostd::appender(); util::escape_string(x, args[0].get_strr()); ostd::Size len = x.size(); res.set_mstr(ostd::CharRange(x.get().disown(), len)); }); - cs.add_command("unescape", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("unescape", "s", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange s = args[0].get_strr(); char *buf = new char[s.size() + 1]; auto writer = ostd::CharRange(buf, s.size() + 1); @@ -92,21 +92,21 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(buf, s.size())); }); - cs.add_command("concat", "V", [](CsValueRange args, CsValue &res) { + cs.new_command("concat", "V", [](CsValueRange args, CsValue &res) { auto s = ostd::appender(); cscript::util::tvals_concat(s, args, " "); res.set_mstr(s.get().iter()); s.get().disown(); }); - cs.add_command("concatword", "V", [](CsValueRange args, CsValue &res) { + cs.new_command("concatword", "V", [](CsValueRange args, CsValue &res) { auto s = ostd::appender(); cscript::util::tvals_concat(s, args); res.set_mstr(s.get().iter()); s.get().disown(); }); - cs.add_command("format", "V", [](CsValueRange args, CsValue &res) { + cs.new_command("format", "V", [](CsValueRange args, CsValue &res) { if (args.empty()) { return; } @@ -139,7 +139,7 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(s.disown(), len)); }); - cs.add_command("tohex", "ii", [](CsValueRange args, CsValue &res) { + cs.new_command("tohex", "ii", [](CsValueRange args, CsValue &res) { auto r = ostd::appender>(); ostd::format( r, "0x%.*X", ostd::max(args[1].get_int(), 1), args[0].get_int() @@ -149,7 +149,7 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(r.get().disown(), len)); }); - cs.add_command("substr", "siiN", [](CsValueRange args, CsValue &res) { + cs.new_command("substr", "siiN", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange s = args[0].get_strr(); CsInt start = args[1].get_int(), count = args[2].get_int(); CsInt numargs = args[3].get_int(); @@ -160,29 +160,29 @@ void cs_init_lib_string(CsState &cs) { )); }); - cs.add_command("strcmp", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("strcmp", "s1V", [](CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::Equal()); }); - cs.add_command("=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("=s", "s1V", [](CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::Equal()); }); - cs.add_command("!=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("!=s", "s1V", [](CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::NotEqual()); }); - cs.add_command("()); }); - cs.add_command(">s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">s", "s1V", [](CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::Greater()); }); - cs.add_command("<=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<=s", "s1V", [](CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::LessEqual()); }); - cs.add_command(">=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">=s", "s1V", [](CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::GreaterEqual()); }); - cs.add_command("strreplace", "ssss", [](CsValueRange args, CsValue &res) { + cs.new_command("strreplace", "ssss", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange oldval = args[1].get_strr(), newval = args[2].get_strr(), @@ -226,7 +226,7 @@ void cs_init_lib_string(CsState &cs) { } }); - cs.add_command("strsplice", "ssii", [](CsValueRange args, CsValue &res) { + cs.new_command("strsplice", "ssii", [](CsValueRange args, CsValue &res) { ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange vals = args[1].get_strr(); CsInt skip = args[2].get_int(), diff --git a/tools/repl.cc b/tools/repl.cc index 9d2eaf83..c388994d 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -167,7 +167,7 @@ static void do_tty(CsState &cs) { auto prompt2 = cs.new_svar("PROMPT2", ">> "); bool do_exit = false; - cs.add_command("quit", "", [&do_exit](auto, auto &) { + cs.new_command("quit", "", [&do_exit](auto, auto &) { do_exit = true; });