From 083f90591306158134a99b06bb6a4618fd6ceba6 Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 11 Sep 2016 23:13:39 +0200 Subject: [PATCH] explicitly pass CsState to all callbacks (might be a different thread eventually) --- include/cubescript/cubescript.hh | 13 +- src/cs_vm.cc | 21 +-- src/cubescript.cc | 212 ++++++++++++++++++++----------- src/lib_list.cc | 110 +++++++++++----- src/lib_math.cc | 118 +++++++++-------- src/lib_str.cc | 76 +++++++---- tools/repl.cc | 28 ++-- 7 files changed, 368 insertions(+), 210 deletions(-) diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index f0f0c436..b1a95a5b 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -190,7 +190,7 @@ private: int p_index = -1; }; -using CsVarCb = ostd::Function; +using CsVarCb = ostd::Function; struct OSTD_EXPORT CsVar: CsIdent { friend struct CsState; @@ -201,9 +201,9 @@ protected: private: CsVarCb cb_var; - void changed() { + void changed(CsState &cs) { if (cb_var) { - cb_var(*this); + cb_var(cs, *this); } } }; @@ -281,7 +281,7 @@ private: CsValue p_val; }; -using CsCommandCb = ostd::Function; +using CsCommandCb = ostd::Function; struct CsCommand: CsIdent { friend struct CsState; @@ -333,8 +333,9 @@ private: bool p_gap; }; -using CsHookCb = ostd::Function; -using CsPanicCb = ostd::Function; +using CsHookCb = ostd::Function; +using CsPanicCb = + ostd::Function; template struct CsAllocator { diff --git a/src/cs_vm.cc b/src/cs_vm.cc index aa0dee5e..74f457db 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -7,8 +7,10 @@ namespace cscript { struct CsCommandInternal { - static void call(CsCommand *c, CsValueRange args, CsValue &ret) { - c->p_cb_cftv(args, ret); + static void call( + CsState &cs, CsCommand *c, CsValueRange args, CsValue &ret + ) { + c->p_cb_cftv(cs, args, ret); } static bool has_cb(CsIdent *id) { @@ -402,12 +404,12 @@ static inline void callcommand( cscript::util::tvals_concat(buf, ostd::iter(args, i), " "); CsValue tv; tv.set_str(ostd::move(buf.get())); - CsCommandInternal::call(id, CsValueRange(&tv, 1), res); + CsCommandInternal::call(cs, id, CsValueRange(&tv, 1), res); return; } case 'V': i = ostd::max(i + 1, numargs); - CsCommandInternal::call(id, ostd::iter(args, i), res); + CsCommandInternal::call(cs, id, ostd::iter(args, i), res); return; case '1': case '2': @@ -421,7 +423,7 @@ static inline void callcommand( } } ++i; - CsCommandInternal::call(id, CsValueRange(args, i), res); + CsCommandInternal::call(cs, id, CsValueRange(args, i), res); } static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result); @@ -546,7 +548,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { CsValue args[MaxArguments + MaxResults]; auto &chook = cs.get_call_hook(); if (chook) { - chook(); + chook(cs); } for (;;) { ostd::Uint32 op = *code++; @@ -1270,7 +1272,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { int offset = numargs - id->get_num_args(); result.force_null(); CsCommandInternal::call( - id, CsValueRange(args + offset, id->get_num_args()), result + cs, id, CsValueRange(args + offset, id->get_num_args()), + result ); force_arg(result, op & CsCodeRetMask); numargs = offset; @@ -1285,7 +1288,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; result.force_null(); CsCommandInternal::call( - id, ostd::iter(&args[offset], callargs), result + cs, id, ostd::iter(&args[offset], callargs), result ); force_arg(result, op & CsCodeRetMask); numargs = offset; @@ -1305,7 +1308,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { ); CsValue tv; tv.set_str(ostd::move(buf.get())); - CsCommandInternal::call(id, CsValueRange(&tv, 1), result); + CsCommandInternal::call(cs, id, CsValueRange(&tv, 1), result); } force_arg(result, op & CsCodeRetMask); numargs = offset; diff --git a/src/cubescript.cc b/src/cubescript.cc index 70125270..ae6fb7bf 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -256,8 +256,8 @@ CsState::CsState(): p_callhook(), p_panicfunc(), p_out(&ostd::out), p_err(&ostd::err) { /* default panic func */ - p_panicfunc = [this](ostd::ConstCharRange v, CsStackState) { - get_err().writefln( + p_panicfunc = [](CsState &cs, ostd::ConstCharRange v, CsStackState) { + cs.get_err().writefln( "PANIC: unprotected error in call to CubeScript (%s)", v ); }; @@ -276,34 +276,38 @@ CsState::CsState(): id = new_ivar("dbgalias", 0, 1000, 4); assert(id->get_index() == DbgaliasIdx); - new_command("do", "e", [this](CsValueRange args, CsValue &res) { - run(args[0].get_code(), res); + new_command("do", "e", [](CsState &cs, CsValueRange args, CsValue &res) { + cs.run(args[0].get_code(), res); })->p_type = CsIdDo; - new_command("doargs", "e", [this](CsValueRange args, CsValue &res) { - cs_do_args(*this, [&]() { run(args[0].get_code(), res); }); + new_command("doargs", "e", [](CsState &cs, CsValueRange args, CsValue &res) { + cs_do_args(cs, [&cs, &res, &args]() { + cs.run(args[0].get_code(), res); + }); })->p_type = CsIdDoArgs; - new_command("if", "tee", [this](CsValueRange args, CsValue &res) { - run((args[0].get_bool() ? args[1] : args[2]).get_code(), res); + new_command("if", "tee", []( + CsState &cs, CsValueRange args, CsValue &res + ) { + cs.run((args[0].get_bool() ? args[1] : args[2]).get_code(), res); })->p_type = CsIdIf; - new_command("result", "T", [](CsValueRange args, CsValue &res) { + new_command("result", "T", [](CsState &, CsValueRange args, CsValue &res) { res = ostd::move(args[0]); })->p_type = CsIdResult; - new_command("!", "t", [](CsValueRange args, CsValue &res) { + new_command("!", "t", [](CsState &, CsValueRange args, CsValue &res) { res.set_int(!args[0].get_bool()); })->p_type = CsIdNot; - new_command("&&", "E1V", [this](CsValueRange args, CsValue &res) { + new_command("&&", "E1V", [](CsState &cs, CsValueRange args, CsValue &res) { if (args.empty()) { res.set_int(1); } else { for (ostd::Size i = 0; i < args.size(); ++i) { CsBytecode *code = args[i].get_code(); if (code) { - run(code, res); + cs.run(code, res); } else { res = ostd::move(args[i]); } @@ -314,14 +318,14 @@ CsState::CsState(): } })->p_type = CsIdAnd; - new_command("||", "E1V", [this](CsValueRange args, CsValue &res) { + new_command("||", "E1V", [](CsState &cs, CsValueRange args, CsValue &res) { if (args.empty()) { res.set_int(0); } else { for (ostd::Size i = 0; i < args.size(); ++i) { CsBytecode *code = args[i].get_code(); if (code) { - run(code, res); + cs.run(code, res); } else { res = ostd::move(args[i]); } @@ -438,7 +442,7 @@ void CsState::error(ostd::ConstCharRange msg) { throw CsErrorException(msg, cs_save_stack(*this)); } else { if (p_panicfunc) { - p_panicfunc(msg, cs_save_stack(*this)); + p_panicfunc(*this, msg, cs_save_stack(*this)); } exit(EXIT_FAILURE); } @@ -458,19 +462,19 @@ void CsState::clear_override(CsIdent &id) { case CsIdentType::Ivar: { CsIvar &iv = static_cast(id); iv.set_value(iv.p_overrideval); - iv.changed(); + iv.changed(*this); break; } case CsIdentType::Fvar: { CsFvar &fv = static_cast(id); fv.set_value(fv.p_overrideval); - fv.changed(); + fv.changed(*this); break; } case CsIdentType::Svar: { CsSvar &sv = static_cast(id); sv.set_value(sv.p_overrideval); - sv.changed(); + sv.changed(*this); break; } default: @@ -562,7 +566,7 @@ bool CsState::reset_var(ostd::ConstCharRange name) { void CsState::touch_var(ostd::ConstCharRange name) { CsIdent *id = get_ident(name); if (id && id->is_var()) { - static_cast(id)->changed(); + static_cast(id)->changed(*this); } } @@ -760,7 +764,7 @@ void CsState::set_var_int( iv->set_value(v); } if (dofunc) { - iv->changed(); + iv->changed(*this); } } @@ -785,7 +789,7 @@ void CsState::set_var_float( fv->set_value(v); } if (dofunc) { - fv->changed(); + fv->changed(*this); } } @@ -806,7 +810,7 @@ void CsState::set_var_str( } sv->set_value(v); if (dofunc) { - sv->changed(); + sv->changed(*this); } } @@ -916,7 +920,7 @@ void CsState::set_var_int_checked(CsIvar *iv, CsInt v) { v = cs_clamp_var(*this, iv, v); } iv->set_value(v); - iv->changed(); + iv->changed(*this); } void CsState::set_var_int_checked(CsIvar *iv, CsValueRange args) { @@ -961,7 +965,7 @@ void CsState::set_var_float_checked(CsFvar *fv, CsFloat v) { v = cs_clamp_fvar(*this, fv, v); } fv->set_value(v); - fv->changed(); + fv->changed(*this); } void CsState::set_var_str_checked(CsSvar *sv, ostd::ConstCharRange v) { @@ -977,7 +981,7 @@ void CsState::set_var_str_checked(CsSvar *sv, ostd::ConstCharRange v) { return; } sv->set_value(v); - sv->changed(); + sv->changed(*this); } CsCommand *CsState::new_command( @@ -1075,12 +1079,14 @@ static inline void cs_loop_conc( res.set_mstr(ostd::CharRange(s.disown(), len)); } -void cs_init_lib_base(CsState &cs) { - cs.new_command("error", "s", [&cs](CsValueRange args, CsValue &) { +void cs_init_lib_base(CsState &gcs) { + gcs.new_command("error", "s", [](CsState &cs, CsValueRange args, CsValue &) { cs.error(args[0].get_strr()); }); - cs.new_command("pcall", "err", [&cs](CsValueRange args, CsValue &ret) { + gcs.new_command("pcall", "err", []( + CsState &cs, CsValueRange args, CsValue &ret + ) { CsAlias *cret = args[1].get_ident()->get_alias(), *css = args[2].get_ident()->get_alias(); if (!cret || !css) { @@ -1108,7 +1114,7 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("?", "tTT", [](CsValueRange args, CsValue &res) { + gcs.new_command("?", "tTT", [](CsState &, CsValueRange args, CsValue &res) { if (args[0].get_bool()) { res = ostd::move(args[1]); } else { @@ -1116,7 +1122,9 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("cond", "ee2V", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("cond", "ee2V", []( + CsState &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())) { @@ -1130,7 +1138,9 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("case", "ite2V", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("case", "ite2V", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsInt val = args[0].get_int(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ( @@ -1143,7 +1153,9 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("casef", "fte2V", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("casef", "fte2V", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsFloat val = args[0].get_float(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ( @@ -1156,7 +1168,9 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("cases", "ste2V", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("cases", "ste2V", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsString val = args[0].get_str(); for (ostd::Size i = 1; (i + 1) < args.size(); i += 2) { if ( @@ -1169,7 +1183,9 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("pushif", "rTe", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("pushif", "rTe", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { return; @@ -1181,126 +1197,162 @@ void cs_init_lib_base(CsState &cs) { } }); - cs.new_command("loop", "rie", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loop", "rie", []( + CsState &cs, CsValueRange args, CsValue & + ) { cs_do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr, args[2].get_code() ); }); - cs.new_command("loop+", "riie", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loop+", "riie", []( + CsState &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.new_command("loop*", "riie", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loop*", "riie", []( + CsState &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.new_command("loop+*", "riiie", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loop+*", "riiie", []( + CsState &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.new_command("loopwhile", "riee", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loopwhile", "riee", []( + CsState &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.new_command("loopwhile+", "riiee", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loopwhile+", "riiee", []( + CsState &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.new_command("loopwhile*", "riiee", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loopwhile*", "riiee", []( + CsState &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.new_command("loopwhile+*", "riiiee", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("loopwhile+*", "riiiee", []( + CsState &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.new_command("while", "ee", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("while", "ee", []( + CsState &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.new_command("loopconcat", "rie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcat", "rie", []( + CsState &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.new_command("loopconcat+", "riie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcat+", "riie", []( + CsState &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, res, *args[0].get_ident(), args[1].get_int(), + args[2].get_int(), 1, args[3].get_code(), true ); }); - cs.new_command("loopconcat*", "riie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcat*", "riie", []( + CsState &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, res, *args[0].get_ident(), 0, args[2].get_int(), + args[1].get_int(), args[3].get_code(), true ); }); - cs.new_command("loopconcat+*", "riiie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcat+*", "riiie", []( + CsState &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, res, *args[0].get_ident(), args[1].get_int(), + args[3].get_int(), args[2].get_int(), args[4].get_code(), true ); }); - cs.new_command("loopconcatword", "rie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcatword", "rie", []( + CsState &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.new_command("loopconcatword+", "riie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcatword+", "riie", []( + CsState &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, res, *args[0].get_ident(), args[1].get_int(), + args[2].get_int(), 1, args[3].get_code(), false ); }); - cs.new_command("loopconcatword*", "riie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcatword*", "riie", []( + CsState &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, res, *args[0].get_ident(), 0, args[2].get_int(), + args[1].get_int(), args[3].get_code(), false ); }); - cs.new_command("loopconcatword+*", "riiie", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("loopconcatword+*", "riiie", []( + CsState &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.new_command("push", "rTe", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("push", "rTe", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { return; @@ -1310,33 +1362,51 @@ void cs_init_lib_base(CsState &cs) { cs.run(args[2].get_code(), res); }); - cs.new_command("resetvar", "s", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("resetvar", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { res.set_int(cs.reset_var(args[0].get_strr())); }); - cs.new_command("alias", "sT", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("alias", "sT", []( + CsState &cs, CsValueRange args, CsValue & + ) { cs.set_alias(args[0].get_strr(), ostd::move(args[1])); }); - cs.new_command("getvarmin", "s", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("getvarmin", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { res.set_int(cs.get_var_min_int(args[0].get_strr()).value_or(0)); }); - cs.new_command("getvarmax", "s", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("getvarmax", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { res.set_int(cs.get_var_max_int(args[0].get_strr()).value_or(0)); }); - cs.new_command("getfvarmin", "s", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("getfvarmin", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { res.set_float(cs.get_var_min_float(args[0].get_strr()).value_or(0.0f)); }); - cs.new_command("getfvarmax", "s", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("getfvarmax", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { res.set_float(cs.get_var_max_float(args[0].get_strr()).value_or(0.0f)); }); - cs.new_command("identexists", "s", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("identexists", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { res.set_int(cs.have_ident(args[0].get_strr())); }); - 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(""))); + gcs.new_command("getalias", "s", []( + CsState &cs, CsValueRange args, CsValue &res + ) { + res.set_str( + ostd::move(cs.get_alias_val(args[0].get_strr()).value_or("")) + ); }); } diff --git a/src/lib_list.cc b/src/lib_list.cc index 31529e8f..9128c0bb 100644 --- a/src/lib_list.cc +++ b/src/lib_list.cc @@ -132,12 +132,12 @@ 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.new_command("listlen", "s", [](CsValueRange args, CsValue &res) { +void cs_init_lib_list(CsState &gcs) { + gcs.new_command("listlen", "s", [](CsState &, CsValueRange args, CsValue &res) { res.set_int(CsInt(util::list_length(args[0].get_strr()))); }); - cs.new_command("at", "si1V", [](CsValueRange args, CsValue &res) { + gcs.new_command("at", "si1V", [](CsState &, CsValueRange args, CsValue &res) { if (args.empty()) { return; } @@ -162,7 +162,9 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(er); }); - cs.new_command("sublist", "siiN", [](CsValueRange args, CsValue &res) { + gcs.new_command("sublist", "siiN", []( + CsState &, CsValueRange args, CsValue &res + ) { CsInt skip = args[1].get_int(), count = args[2].get_int(), numargs = args[2].get_int(); @@ -191,7 +193,9 @@ void cs_init_lib_list(CsState &cs) { res.set_str(ostd::ConstCharRange(list, qend - list)); }); - cs.new_command("listfind", "rse", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("listfind", "rse", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { res.set_int(-1); @@ -211,7 +215,9 @@ void cs_init_lib_list(CsState &cs) { res.set_int(-1); }); - cs.new_command("listassoc", "rse", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("listassoc", "rse", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -237,21 +243,27 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.new_command("listfind=", "i", [](CsValueRange args, CsValue &res) { + gcs.new_command("listfind=", "i", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_find( args, res, [](const util::ListParser &p, CsInt val) { return cs_parse_int(p.item) == val; } ); }); - cs.new_command("listfind=f", "f", [](CsValueRange args, CsValue &res) { + gcs.new_command("listfind=f", "f", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_find( args, res, [](const util::ListParser &p, CsFloat val) { return cs_parse_float(p.item) == val; } ); }); - cs.new_command("listfind=s", "s", [](CsValueRange args, CsValue &res) { + gcs.new_command("listfind=s", "s", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_find( args, res, [](const util::ListParser &p, ostd::ConstCharRange val) { return p.item == val; @@ -259,21 +271,27 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.new_command("listassoc=", "i", [](CsValueRange args, CsValue &res) { + gcs.new_command("listassoc=", "i", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_assoc( args, res, [](const util::ListParser &p, CsInt val) { return cs_parse_int(p.item) == val; } ); }); - cs.new_command("listassoc=f", "f", [](CsValueRange args, CsValue &res) { + gcs.new_command("listassoc=f", "f", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_assoc( args, res, [](const util::ListParser &p, CsFloat val) { return cs_parse_float(p.item) == val; } ); }); - cs.new_command("listassoc=s", "s", [](CsValueRange args, CsValue &res) { + gcs.new_command("listassoc=s", "s", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_assoc( args, res, [](const util::ListParser &p, ostd::ConstCharRange val) { return p.item == val; @@ -281,7 +299,9 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.new_command("looplist", "rse", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("looplist", "rse", []( + CsState &cs, CsValueRange args, CsValue & + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -295,7 +315,9 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.new_command("looplist2", "rrse", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("looplist2", "rrse", []( + CsState &cs, CsValueRange args, CsValue & + ) { CsStackedValue idv1{args[0].get_ident()}, idv2{args[1].get_ident()}; if (!idv1.has_alias() || !idv2.has_alias()) { return; @@ -311,7 +333,9 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.new_command("looplist3", "rrrse", [&cs](CsValueRange args, CsValue &) { + gcs.new_command("looplist3", "rrrse", []( + CsState &cs, CsValueRange args, CsValue & + ) { CsStackedValue idv1{args[0].get_ident()}; CsStackedValue idv2{args[1].get_ident()}; CsStackedValue idv3{args[2].get_ident()}; @@ -331,8 +355,8 @@ void cs_init_lib_list(CsState &cs) { } }); - cs.new_command("looplistconcat", "rse", [&cs]( - CsValueRange args, CsValue &res + gcs.new_command("looplistconcat", "rse", []( + CsState &cs, CsValueRange args, CsValue &res ) { cs_loop_list_conc( cs, res, args[0].get_ident(), args[1].get_strr(), @@ -340,8 +364,8 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.new_command("looplistconcatword", "rse", [&cs]( - CsValueRange args, CsValue &res + gcs.new_command("looplistconcatword", "rse", []( + CsState &cs, CsValueRange args, CsValue &res ) { cs_loop_list_conc( cs, res, args[0].get_ident(), args[1].get_strr(), @@ -349,7 +373,9 @@ void cs_init_lib_list(CsState &cs) { ); }); - cs.new_command("listfilter", "rse", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("listfilter", "rse", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -373,7 +399,9 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(ostd::CharRange(r.disown(), len)); }); - cs.new_command("listcount", "rse", [&cs](CsValueRange args, CsValue &res) { + gcs.new_command("listcount", "rse", []( + CsState &cs, CsValueRange args, CsValue &res + ) { CsStackedValue idv{args[0].get_ident()}; if (!idv.has_alias()) { return; @@ -391,7 +419,9 @@ void cs_init_lib_list(CsState &cs) { res.set_int(r); }); - cs.new_command("prettylist", "ss", [](CsValueRange args, CsValue &res) { + gcs.new_command("prettylist", "ss", []( + CsState &, CsValueRange args, CsValue &res + ) { CsVector buf; ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange conj = args[1].get_strr(); @@ -425,23 +455,33 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(ostd::CharRange(buf.disown(), slen)); }); - cs.new_command("indexof", "ss", [](CsValueRange args, CsValue &res) { + gcs.new_command("indexof", "ss", []( + CsState &, CsValueRange args, CsValue &res + ) { res.set_int( cs_list_includes(args[0].get_strr(), args[1].get_strr()) ); }); - cs.new_command("listdel", "ss", [](CsValueRange args, CsValue &res) { + gcs.new_command("listdel", "ss", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_merge(args, res, ostd::Less()); }); - cs.new_command("listintersect", "ss", [](CsValueRange args, CsValue &res) { + gcs.new_command("listintersect", "ss", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_merge(args, res, ostd::GreaterEqual()); }); - cs.new_command("listunion", "ss", [](CsValueRange args, CsValue &res) { + gcs.new_command("listunion", "ss", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_list_merge(args, res, ostd::Less()); }); - cs.new_command("listsplice", "ssii", [](CsValueRange args, CsValue &res) { + gcs.new_command("listsplice", "ssii", []( + CsState &, CsValueRange args, CsValue &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(); @@ -488,7 +528,7 @@ void cs_init_lib_list(CsState &cs) { res.set_mstr(ostd::CharRange(buf.disown(), slen)); }); - cs_init_lib_list_sort(cs); + cs_init_lib_list_sort(gcs); } struct ListSortItem { @@ -511,8 +551,8 @@ struct ListSortFun { }; static void cs_list_sort( - CsState &cs, CsValue &res, ostd::ConstCharRange list, CsIdent *x, CsIdent *y, - CsBytecode *body, CsBytecode *unique + CsState &cs, CsValue &res, ostd::ConstCharRange list, + CsIdent *x, CsIdent *y, CsBytecode *body, CsBytecode *unique ) { if (x == y || !x->is_alias() || !y->is_alias()) { return; @@ -609,17 +649,17 @@ static void cs_list_sort( res.set_mstr(sorted); } -static void cs_init_lib_list_sort(CsState &cs) { - cs.new_command("sortlist", "srree", [&cs]( - CsValueRange args, CsValue &res +static void cs_init_lib_list_sort(CsState &gcs) { + gcs.new_command("sortlist", "srree", []( + CsState &cs, CsValueRange args, CsValue &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() ); }); - cs.new_command("uniquelist", "srre", [&cs]( - CsValueRange args, CsValue &res + gcs.new_command("uniquelist", "srre", []( + CsState &cs, CsValueRange args, CsValue &res ) { cs_list_sort( cs, res, args[0].get_strr(), args[1].get_ident(), diff --git a/src/lib_math.cc b/src/lib_math.cc index 8b5887ae..3e238d65 100644 --- a/src/lib_math.cc +++ b/src/lib_math.cc @@ -138,68 +138,74 @@ 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", [](CsValueRange args, CsValue &res) { + cs.new_command("sin", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_sin(args[0].get_float() * RAD)); }); - cs.new_command("cos", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("cos", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_cos(args[0].get_float() * RAD)); }); - cs.new_command("tan", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("tan", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_tan(args[0].get_float() * RAD)); }); - cs.new_command("asin", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("asin", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_asin(args[0].get_float()) / RAD); }); - cs.new_command("acos", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("acos", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_acos(args[0].get_float()) / RAD); }); - cs.new_command("atan", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("atan", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_atan(args[0].get_float()) / RAD); }); - cs.new_command("atan2", "ff", [](CsValueRange args, CsValue &res) { + cs.new_command("atan2", "ff", []( + CsState &, CsValueRange args, CsValue &res + ) { res.set_float(cs_atan2(args[0].get_float(), args[1].get_float()) / RAD); }); - cs.new_command("sqrt", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("sqrt", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_sqrt(args[0].get_float())); }); - cs.new_command("loge", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("loge", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_log(args[0].get_float())); }); - cs.new_command("log2", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("log2", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_log(args[0].get_float()) / M_LN2); }); - cs.new_command("log10", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("log10", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_log10(args[0].get_float())); }); - cs.new_command("exp", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("exp", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_exp(args[0].get_float())); }); - cs.new_command("min", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("min", "i1V", [](CsState &, 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.new_command("max", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("max", "i1V", [](CsState &, 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.new_command("minf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("minf", "f1V", []( + CsState &, 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.new_command("maxf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("maxf", "f1V", []( + CsState &, 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()); @@ -207,21 +213,23 @@ void cs_init_lib_math(CsState &cs) { res.set_float(v); }); - cs.new_command("abs", "i", [](CsValueRange args, CsValue &res) { + cs.new_command("abs", "i", [](CsState &, CsValueRange args, CsValue &res) { res.set_int(cs_abs(args[0].get_int())); }); - cs.new_command("absf", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("absf", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_abs(args[0].get_float())); }); - cs.new_command("floor", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("floor", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_floor(args[0].get_float())); }); - cs.new_command("ceil", "f", [](CsValueRange args, CsValue &res) { + cs.new_command("ceil", "f", [](CsState &, CsValueRange args, CsValue &res) { res.set_float(cs_ceil(args[0].get_float())); }); - cs.new_command("round", "ff", [](CsValueRange args, CsValue &res) { + cs.new_command("round", "ff", []( + CsState &, CsValueRange args, CsValue &res + ) { CsFloat step = args[1].get_float(); CsFloat r = args[0].get_float(); if (step > 0) { @@ -233,43 +241,43 @@ void cs_init_lib_math(CsState &cs) { res.set_float(r); }); - cs.new_command("+", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("+", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop(args, res, 0, ostd::Add(), CsMathNoop()); }); - cs.new_command("*", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("*", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 1, ostd::Multiply(), CsMathNoop() ); }); - cs.new_command("-", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("-", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::Subtract(), ostd::Negate() ); }); - cs.new_command("^", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("^", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitXor(), [](CsInt val) { return ~val; } ); }); - cs.new_command("~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitXor(), [](CsInt val) { return ~val; } ); }); - cs.new_command("&", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("&", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitAnd(), CsMathNoop() ); }); - cs.new_command("|", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("|", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::BitOr(), CsMathNoop() ); }); /* special combined cases */ - cs.new_command("^~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("^~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { CsInt val; if (args.size() >= 2) { val = args[0].get_int() ^ ~args[1].get_int(); @@ -281,7 +289,7 @@ void cs_init_lib_math(CsState &cs) { } res.set_int(val); }); - cs.new_command("&~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("&~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { CsInt val; if (args.size() >= 2) { val = args[0].get_int() & ~args[1].get_int(); @@ -293,7 +301,7 @@ void cs_init_lib_math(CsState &cs) { } res.set_int(val); }); - cs.new_command("|~", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("|~", "i1V", [](CsState &, CsValueRange args, CsValue &res) { CsInt val; if (args.size() >= 2) { val = args[0].get_int() | ~args[1].get_int(); @@ -306,7 +314,7 @@ void cs_init_lib_math(CsState &cs) { res.set_int(val); }); - cs.new_command("<<", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<<", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { return (val2 < CsInt(sizeof(CsInt) * CHAR_BIT)) @@ -315,7 +323,7 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.new_command(">>", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">>", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { return val1 >> ostd::clamp( @@ -325,23 +333,23 @@ void cs_init_lib_math(CsState &cs) { ); }); - cs.new_command("+f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("+f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::Add(), CsMathNoop() ); }); - cs.new_command("*f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("*f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 1, ostd::Multiply(), CsMathNoop() ); }); - cs.new_command("-f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("-f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, ostd::Subtract(), ostd::Negate() ); }); - cs.new_command("div", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("div", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { if (val2) { @@ -351,7 +359,7 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.new_command("mod", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("mod", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsInt val1, CsInt val2) { if (val2) { @@ -361,7 +369,9 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.new_command("divf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("divf", "f1V", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_mathop( args, res, 0, [](CsFloat val1, CsFloat val2) { if (val2) { @@ -371,7 +381,9 @@ void cs_init_lib_math(CsState &cs) { }, CsMathNoop() ); }); - cs.new_command("modf", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("modf", "f1V", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_mathop( args, res, 0, [](CsFloat val1, CsFloat val2) { if (val2) { @@ -382,7 +394,7 @@ void cs_init_lib_math(CsState &cs) { ); }); - cs.new_command("pow", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("pow", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_mathop( args, res, 0, [](CsFloat val1, CsFloat val2) { return CsFloat(pow(val1, val2)); @@ -390,41 +402,41 @@ void cs_init_lib_math(CsState &cs) { ); }); - cs.new_command("=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Equal()); }); - cs.new_command("!=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("!=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::NotEqual()); }); - cs.new_command("<", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Less()); }); - cs.new_command(">", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Greater()); }); - cs.new_command("<=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::LessEqual()); }); - cs.new_command(">=", "i1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">=", "i1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::GreaterEqual()); }); - cs.new_command("=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Equal()); }); - cs.new_command("!=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("!=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::NotEqual()); }); - cs.new_command("(args, res, ostd::Less()); }); - cs.new_command(">f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::Greater()); }); - cs.new_command("<=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::LessEqual()); }); - cs.new_command(">=f", "f1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">=f", "f1V", [](CsState &, CsValueRange args, CsValue &res) { cs_cmpop(args, res, ostd::GreaterEqual()); }); } diff --git a/src/lib_str.cc b/src/lib_str.cc index 689d507b..5f77a47b 100644 --- a/src/lib_str.cc +++ b/src/lib_str.cc @@ -22,7 +22,9 @@ static inline void cs_strgcmp(CsValueRange args, CsValue &res, F cfunc) { }; void cs_init_lib_string(CsState &cs) { - cs.new_command("strstr", "ss", [](CsValueRange args, CsValue &res) { + cs.new_command("strstr", "ss", []( + CsState &, 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 +37,15 @@ void cs_init_lib_string(CsState &cs) { res.set_int(-1); }); - cs.new_command("strlen", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("strlen", "s", []( + CsState &, CsValueRange args, CsValue &res + ) { res.set_int(CsInt(args[0].get_strr().size())); }); - cs.new_command("strcode", "si", [](CsValueRange args, CsValue &res) { + cs.new_command("strcode", "si", []( + CsState &, CsValueRange args, CsValue &res + ) { ostd::ConstCharRange str = args[0].get_strr(); CsInt i = args[1].get_int(); if (i >= CsInt(str.size())) { @@ -49,14 +55,18 @@ void cs_init_lib_string(CsState &cs) { } }); - cs.new_command("codestr", "i", [](CsValueRange args, CsValue &res) { + cs.new_command("codestr", "i", []( + CsState &, CsValueRange args, CsValue &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", [](CsValueRange args, CsValue &res) { + cs.new_command("strlower", "s", []( + CsState &, 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 +76,9 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(buf, s.size())); }); - cs.new_command("strupper", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("strupper", "s", []( + CsState &, 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 +88,18 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(buf, s.size())); }); - cs.new_command("escape", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("escape", "s", []( + CsState &, 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.new_command("unescape", "s", [](CsValueRange args, CsValue &res) { + cs.new_command("unescape", "s", []( + CsState &, 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 +108,27 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(buf, s.size())); }); - cs.new_command("concat", "V", [](CsValueRange args, CsValue &res) { + cs.new_command("concat", "V", []( + CsState &, CsValueRange args, CsValue &res + ) { auto s = ostd::appender(); cscript::util::tvals_concat(s, args, " "); res.set_mstr(s.get().iter()); s.get().disown(); }); - cs.new_command("concatword", "V", [](CsValueRange args, CsValue &res) { + cs.new_command("concatword", "V", []( + CsState &, CsValueRange args, CsValue &res + ) { auto s = ostd::appender(); cscript::util::tvals_concat(s, args); res.set_mstr(s.get().iter()); s.get().disown(); }); - cs.new_command("format", "V", [](CsValueRange args, CsValue &res) { + cs.new_command("format", "V", []( + CsState &, CsValueRange args, CsValue &res + ) { if (args.empty()) { return; } @@ -139,7 +161,9 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(s.disown(), len)); }); - cs.new_command("tohex", "ii", [](CsValueRange args, CsValue &res) { + cs.new_command("tohex", "ii", []( + CsState &, CsValueRange args, CsValue &res + ) { auto r = ostd::appender>(); ostd::format( r, "0x%.*X", ostd::max(args[1].get_int(), CsInt(1)), @@ -150,7 +174,9 @@ void cs_init_lib_string(CsState &cs) { res.set_mstr(ostd::CharRange(r.get().disown(), len)); }); - cs.new_command("substr", "siiN", [](CsValueRange args, CsValue &res) { + cs.new_command("substr", "siiN", []( + CsState &, 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(); @@ -163,29 +189,33 @@ void cs_init_lib_string(CsState &cs) { )); }); - cs.new_command("strcmp", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("strcmp", "s1V", []( + CsState &, CsValueRange args, CsValue &res + ) { cs_strgcmp(args, res, ostd::Equal()); }); - cs.new_command("=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::Equal()); }); - cs.new_command("!=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("!=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::NotEqual()); }); - cs.new_command("()); }); - cs.new_command(">s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::Greater()); }); - cs.new_command("<=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command("<=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::LessEqual()); }); - cs.new_command(">=s", "s1V", [](CsValueRange args, CsValue &res) { + cs.new_command(">=s", "s1V", [](CsState &, CsValueRange args, CsValue &res) { cs_strgcmp(args, res, ostd::GreaterEqual()); }); - cs.new_command("strreplace", "ssss", [](CsValueRange args, CsValue &res) { + cs.new_command("strreplace", "ssss", []( + CsState &, CsValueRange args, CsValue &res + ) { ostd::ConstCharRange s = args[0].get_strr(); ostd::ConstCharRange oldval = args[1].get_strr(), newval = args[2].get_strr(), @@ -229,7 +259,9 @@ void cs_init_lib_string(CsState &cs) { } }); - cs.new_command("strsplice", "ssii", [](CsValueRange args, CsValue &res) { + cs.new_command("strsplice", "ssii", []( + CsState &, 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 2d2b4a7e..a3b61e71 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -186,9 +186,9 @@ static void do_sigint(int n) { /* in case another SIGINT happens, terminate normally */ signal(n, SIG_DFL); if (gcs) { - gcs->set_call_hook([]() { - gcs->set_call_hook(nullptr); - gcs->error(""); + gcs->set_call_hook([](CsState &cs) { + cs.set_call_hook(nullptr); + cs.error(""); }); } } @@ -236,7 +236,7 @@ static void do_tty(CsState &cs) { auto prompt2 = cs.new_svar("PROMPT2", ">> "); bool do_exit = false; - cs.new_command("quit", "", [&do_exit](auto, auto &) { + cs.new_command("quit", "", [&do_exit](CsState &, auto, auto &) { do_exit = true; }); @@ -272,11 +272,11 @@ static void do_tty(CsState &cs) { } int main(int argc, char **argv) { - CsState cs; - gcs = &cs; - cs.init_libs(); + CsState csg; + gcs = &csg; + csg.init_libs(); - cs.new_command("exec", "sb", [&cs](auto args, auto &res) { + csg.new_command("exec", "sb", [](CsState &cs, auto args, auto &res) { auto file = args[0].get_strr(); bool ret = cs.run_file(file); if (!ret) { @@ -289,7 +289,7 @@ int main(int argc, char **argv) { } }); - cs.new_command("echo", "C", [&cs](auto args, auto &) { + csg.new_command("echo", "C", [](CsState &cs, auto args, auto &) { cs.get_out().writeln(args[0].get_strr()); }); @@ -366,31 +366,31 @@ endargs: if (*str == '\0') { str = argv[++i]; } - do_call(cs, str); + do_call(csg, str); break; } } } if (firstarg) { - do_call(cs, argv[firstarg], true); + do_call(csg, argv[firstarg], true); } if (!firstarg && !has_str && !has_ver) { if (stdin_is_tty()) { init_lineedit(argv[0]); - do_tty(cs); + do_tty(csg); return 0; } else { ostd::String str; for (char c = '\0'; (c = ostd::in.getchar()) != EOF;) { str += c; } - do_call(cs, str); + do_call(csg, str); } } if (has_inter) { if (stdin_is_tty()) { init_lineedit(argv[0]); - do_tty(cs); + do_tty(csg); } return 0; }