2021-03-23 02:43:00 +01:00
|
|
|
#include <cubescript/cubescript.hh>
|
|
|
|
|
2021-03-31 01:10:58 +02:00
|
|
|
#include <iterator>
|
|
|
|
|
2021-03-23 02:43:00 +01:00
|
|
|
#include "cs_std.hh"
|
|
|
|
#include "cs_ident.hh"
|
2021-04-02 05:47:49 +02:00
|
|
|
#include "cs_thread.hh"
|
2021-03-23 02:43:00 +01:00
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
namespace cubescript {
|
2021-03-23 02:43:00 +01:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static inline void do_loop(
|
|
|
|
state &cs, ident &id, integer_type offset, integer_type n, integer_type step,
|
2021-04-01 05:05:43 +02:00
|
|
|
bcode_ref &&cond, bcode_ref &&body
|
2021-03-23 02:43:00 +01:00
|
|
|
) {
|
2021-03-30 03:57:43 +02:00
|
|
|
if (n <= 0) {
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-04-02 04:43:57 +02:00
|
|
|
if (alias_local st{cs, &id}; st) {
|
2021-03-30 03:57:43 +02:00
|
|
|
any_value idv{cs};
|
|
|
|
for (integer_type i = 0; i < n; ++i) {
|
2021-04-06 00:54:46 +02:00
|
|
|
idv.set_integer(offset + i * step);
|
2021-03-30 20:37:50 +02:00
|
|
|
st.set(idv);
|
2021-03-30 03:57:43 +02:00
|
|
|
if (cond && !cs.run(cond).get_bool()) {
|
2021-03-23 02:43:00 +01:00
|
|
|
break;
|
2021-03-30 03:57:43 +02:00
|
|
|
}
|
|
|
|
switch (cs.run_loop(body)) {
|
|
|
|
case loop_state::BREAK:
|
|
|
|
return;
|
|
|
|
default: /* continue and normal */
|
|
|
|
break;
|
|
|
|
}
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
static inline void do_loop_conc(
|
|
|
|
state &cs, any_value &res, ident &id, integer_type offset, integer_type n,
|
2021-04-01 05:05:43 +02:00
|
|
|
integer_type step, bcode_ref &&body, bool space
|
2021-03-23 02:43:00 +01:00
|
|
|
) {
|
2021-03-30 03:57:43 +02:00
|
|
|
if (n <= 0) {
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-04-02 04:43:57 +02:00
|
|
|
if (alias_local st{cs, &id}; st) {
|
2021-03-30 03:57:43 +02:00
|
|
|
charbuf s{cs};
|
|
|
|
any_value idv{cs};
|
|
|
|
for (integer_type i = 0; i < n; ++i) {
|
2021-04-06 00:54:46 +02:00
|
|
|
idv.set_integer(offset + i * step);
|
2021-03-30 20:37:50 +02:00
|
|
|
st.set(idv);
|
2021-03-30 03:57:43 +02:00
|
|
|
any_value v{cs};
|
|
|
|
switch (cs.run_loop(body, v)) {
|
|
|
|
case loop_state::BREAK:
|
|
|
|
goto end;
|
|
|
|
case loop_state::CONTINUE:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (space && i) {
|
|
|
|
s.push_back(' ');
|
|
|
|
}
|
2021-04-06 00:54:46 +02:00
|
|
|
s.append(v.get_string());
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
end:
|
2021-04-06 00:54:46 +02:00
|
|
|
res.set_string(s.str());
|
2021-03-30 03:57:43 +02:00
|
|
|
}
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
void init_lib_base(state &gcs) {
|
2021-03-23 02:43:00 +01:00
|
|
|
gcs.new_command("error", "s", [](auto &cs, auto args, auto &) {
|
2021-04-06 00:54:46 +02:00
|
|
|
throw error{cs, args[0].get_string()};
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) {
|
2021-03-30 03:57:43 +02:00
|
|
|
alias *cret = args[1].get_ident()->get_alias();
|
|
|
|
alias *css = args[2].get_ident()->get_alias();
|
2021-03-23 02:43:00 +01:00
|
|
|
if (!cret || !css) {
|
2021-04-06 00:54:46 +02:00
|
|
|
ret.set_integer(0);
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-03-23 23:29:32 +01:00
|
|
|
any_value result{cs}, tback{cs};
|
2021-03-23 02:43:00 +01:00
|
|
|
bool rc = true;
|
|
|
|
try {
|
2021-04-05 18:32:45 +02:00
|
|
|
result = cs.run(args[0].get_code());
|
2021-03-23 23:29:32 +01:00
|
|
|
} catch (error const &e) {
|
2021-04-06 00:54:46 +02:00
|
|
|
result.set_string(e.what());
|
2021-03-23 02:43:00 +01:00
|
|
|
if (e.get_stack().get()) {
|
2021-03-23 23:29:32 +01:00
|
|
|
charbuf buf{cs};
|
|
|
|
print_stack(std::back_inserter(buf), e.get_stack());
|
2021-04-06 00:54:46 +02:00
|
|
|
tback.set_string(buf.str());
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
rc = false;
|
|
|
|
}
|
2021-04-06 00:54:46 +02:00
|
|
|
ret.set_integer(rc);
|
2021-04-05 04:10:39 +02:00
|
|
|
auto &ts = state_p{cs}.ts();
|
2021-04-02 05:47:49 +02:00
|
|
|
ts.get_astack(cret).set_alias(cret, ts, result);
|
|
|
|
ts.get_astack(css).set_alias(css, ts, tback);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("?", "ttt", [](auto &, auto args, auto &res) {
|
|
|
|
if (args[0].get_bool()) {
|
|
|
|
res = args[1];
|
|
|
|
} else {
|
|
|
|
res = args[2];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("cond", "ee2V", [](auto &cs, auto args, auto &res) {
|
|
|
|
for (size_t i = 0; i < args.size(); i += 2) {
|
|
|
|
if ((i + 1) < args.size()) {
|
|
|
|
if (cs.run(args[i].get_code()).get_bool()) {
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[i + 1].get_code());
|
2021-03-23 02:43:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[i].get_code());
|
2021-03-23 02:43:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("case", "ite2V", [](auto &cs, auto args, auto &res) {
|
2021-04-06 00:54:46 +02:00
|
|
|
integer_type val = args[0].get_integer();
|
2021-03-23 02:43:00 +01:00
|
|
|
for (size_t i = 1; (i + 1) < args.size(); i += 2) {
|
|
|
|
if (
|
2021-03-23 23:29:32 +01:00
|
|
|
(args[i].get_type() == value_type::NONE) ||
|
2021-04-06 00:54:46 +02:00
|
|
|
(args[i].get_integer() == val)
|
2021-03-23 02:43:00 +01:00
|
|
|
) {
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[i + 1].get_code());
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("casef", "fte2V", [](auto &cs, auto args, auto &res) {
|
2021-03-23 23:29:32 +01:00
|
|
|
float_type val = args[0].get_float();
|
2021-03-23 02:43:00 +01:00
|
|
|
for (size_t i = 1; (i + 1) < args.size(); i += 2) {
|
|
|
|
if (
|
2021-03-23 23:29:32 +01:00
|
|
|
(args[i].get_type() == value_type::NONE) ||
|
2021-03-23 02:43:00 +01:00
|
|
|
(args[i].get_float() == val)
|
|
|
|
) {
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[i + 1].get_code());
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("cases", "ste2V", [](auto &cs, auto args, auto &res) {
|
2021-04-06 00:54:46 +02:00
|
|
|
string_ref val = args[0].get_string();
|
2021-03-23 02:43:00 +01:00
|
|
|
for (size_t i = 1; (i + 1) < args.size(); i += 2) {
|
|
|
|
if (
|
2021-03-23 23:29:32 +01:00
|
|
|
(args[i].get_type() == value_type::NONE) ||
|
2021-04-06 00:54:46 +02:00
|
|
|
(args[i].get_string() == val)
|
2021-03-23 02:43:00 +01:00
|
|
|
) {
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[i + 1].get_code());
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("pushif", "rte", [](auto &cs, auto args, auto &res) {
|
2021-04-02 04:43:57 +02:00
|
|
|
if (alias_local st{cs, args[0].get_ident()}; st) {
|
2021-04-03 05:39:19 +02:00
|
|
|
if (st.get_alias()->is_arg()) {
|
2021-03-30 03:57:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args[1].get_bool()) {
|
2021-03-30 20:37:50 +02:00
|
|
|
st.set(args[1]);
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[2].get_code());
|
2021-03-30 03:57:43 +02:00
|
|
|
}
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loop", "rie", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-10 03:37:59 +02:00
|
|
|
cs, *args[0].get_ident(), 0, args[1].get_integer(), 1, bcode_ref{},
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loop+", "riie", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), args[1].get_integer(),
|
2021-04-10 03:37:59 +02:00
|
|
|
args[2].get_integer(), 1, bcode_ref{}, args[3].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loop*", "riie", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), 0, args[1].get_integer(),
|
2021-04-10 03:37:59 +02:00
|
|
|
args[2].get_integer(), bcode_ref{}, args[3].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loop+*", "riiie", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[3].get_integer(), args[2].get_integer(),
|
2021-04-10 03:37:59 +02:00
|
|
|
bcode_ref{}, args[4].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopwhile", "riee", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), 0, args[1].get_integer(), 1,
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code(), args[3].get_code()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopwhile+", "riiee", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[2].get_integer(), 1, args[3].get_code(), args[4].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopwhile*", "riiee", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), 0, args[2].get_integer(),
|
|
|
|
args[1].get_integer(), args[3].get_code(), args[4].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopwhile+*", "riiiee", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[3].get_integer(), args[2].get_integer(), args[4].get_code(),
|
|
|
|
args[5].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("while", "ee", [](auto &cs, auto args, auto &) {
|
2021-04-01 05:05:43 +02:00
|
|
|
auto cond = args[0].get_code();
|
|
|
|
auto body = args[1].get_code();
|
2021-03-23 02:43:00 +01:00
|
|
|
while (cs.run(cond).get_bool()) {
|
|
|
|
switch (cs.run_loop(body)) {
|
2021-03-23 23:29:32 +01:00
|
|
|
case loop_state::BREAK:
|
2021-03-23 02:43:00 +01:00
|
|
|
goto end;
|
|
|
|
default: /* continue and normal */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcat", "rie", [](auto &cs, auto args, auto &res) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), 0, args[1].get_integer(), 1,
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code(), true
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcat+", "riie", [](auto &cs, auto args, auto &res) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[2].get_integer(), 1, args[3].get_code(), true
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcat*", "riie", [](auto &cs, auto args, auto &res) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), 0, args[2].get_integer(),
|
|
|
|
args[1].get_integer(), args[3].get_code(), true
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcat+*", "riiie", [](auto &cs, auto args, auto &res) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[3].get_integer(), args[2].get_integer(),
|
|
|
|
args[4].get_code(), true
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcatword", "rie", [](auto &cs, auto args, auto &res) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), 0, args[1].get_integer(), 1,
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code(), false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcatword+", "riie", [](
|
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[2].get_integer(), 1, args[3].get_code(), false
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcatword*", "riie", [](
|
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), 0, args[2].get_integer(),
|
|
|
|
args[1].get_integer(), args[3].get_code(), false
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("loopconcatword+*", "riiie", [](
|
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-06 00:54:46 +02:00
|
|
|
cs, res, *args[0].get_ident(), args[1].get_integer(),
|
|
|
|
args[3].get_integer(), args[2].get_integer(),
|
|
|
|
args[4].get_code(), false
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("push", "rte", [](auto &cs, auto args, auto &res) {
|
2021-04-02 04:43:57 +02:00
|
|
|
if (alias_local st{cs, args[0].get_ident()}; st) {
|
2021-04-03 05:39:19 +02:00
|
|
|
if (st.get_alias()->is_arg()) {
|
2021-03-30 03:57:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-30 20:37:50 +02:00
|
|
|
st.set(args[1]);
|
2021-04-05 18:32:45 +02:00
|
|
|
res = cs.run(args[2].get_code());
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-04 02:33:28 +02:00
|
|
|
gcs.new_command("resetvar", "s", [](auto &cs, auto args, auto &) {
|
2021-04-06 00:54:46 +02:00
|
|
|
cs.reset_var(args[0].get_string());
|
2021-04-04 02:33:28 +02:00
|
|
|
});
|
|
|
|
|
2021-03-23 02:43:00 +01:00
|
|
|
gcs.new_command("alias", "st", [](auto &cs, auto args, auto &) {
|
2021-04-06 00:54:46 +02:00
|
|
|
cs.set_alias(args[0].get_string(), args[1]);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("identexists", "s", [](auto &cs, auto args, auto &res) {
|
2021-04-06 00:54:46 +02:00
|
|
|
res.set_integer(cs.have_ident(args[0].get_string()));
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
gcs.new_command("getalias", "s", [](auto &cs, auto args, auto &res) {
|
2021-04-06 00:54:46 +02:00
|
|
|
auto *id = cs.get_alias(args[0].get_string());
|
2021-04-05 18:19:32 +02:00
|
|
|
if (id) {
|
2021-04-06 00:54:46 +02:00
|
|
|
res = id->get_value(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
} /* namespace cubescript */
|