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-05-08 17:20:56 +02:00
|
|
|
#include "cs_error.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-24 23:33:19 +02:00
|
|
|
alias_local st{cs, id};
|
|
|
|
any_value idv{};
|
|
|
|
for (integer_type i = 0; i < n; ++i) {
|
|
|
|
idv.set_integer(offset + i * step);
|
|
|
|
st.set(idv);
|
2021-05-02 22:44:38 +02:00
|
|
|
if (cond && !cond.call(cs).get_bool()) {
|
2021-04-24 23:33:19 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-05-02 22:44:38 +02:00
|
|
|
switch (body.call_loop(cs)) {
|
2021-04-24 23:33:19 +02:00
|
|
|
case loop_state::BREAK:
|
|
|
|
return;
|
|
|
|
default: /* continue and normal */
|
2021-03-23 02:43:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-24 23:33:19 +02:00
|
|
|
alias_local st{cs, id};
|
|
|
|
charbuf s{cs};
|
|
|
|
any_value idv{};
|
|
|
|
for (integer_type i = 0; i < n; ++i) {
|
|
|
|
idv.set_integer(offset + i * step);
|
|
|
|
st.set(idv);
|
|
|
|
any_value v{};
|
2021-05-02 22:44:38 +02:00
|
|
|
switch (body.call_loop(cs, v)) {
|
2021-04-24 23:33:19 +02:00
|
|
|
case loop_state::BREAK:
|
|
|
|
goto end;
|
|
|
|
case loop_state::CONTINUE:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
2021-04-24 23:33:19 +02:00
|
|
|
if (space && i) {
|
|
|
|
s.push_back(' ');
|
|
|
|
}
|
|
|
|
s.append(v.get_string(cs));
|
2021-03-30 03:57:43 +02:00
|
|
|
}
|
2021-04-24 23:33:19 +02:00
|
|
|
end:
|
|
|
|
res.set_string(s.str(), cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 19:36:20 +02:00
|
|
|
LIBCUBESCRIPT_EXPORT void std_init_base(state &gcs) {
|
|
|
|
new_cmd_quiet(gcs, "error", "s", [](auto &cs, auto args, auto &) {
|
2021-04-11 03:32:14 +02:00
|
|
|
throw error{cs, args[0].get_string(cs)};
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "pcall", "bvv", [](auto &cs, auto args, auto &ret) {
|
2021-04-25 00:44:36 +02:00
|
|
|
auto &cret = args[1].get_ident(cs);
|
|
|
|
auto &css = args[2].get_ident(cs);
|
2021-05-05 03:24:41 +02:00
|
|
|
if (cret.type() != ident_type::ALIAS) {
|
2021-05-08 17:20:56 +02:00
|
|
|
throw error_p::make(cs, "'%s' is not an alias", cret.name().data());
|
2021-04-25 00:44:36 +02:00
|
|
|
}
|
2021-05-05 03:24:41 +02:00
|
|
|
if (css.type() != ident_type::ALIAS) {
|
2021-05-08 17:20:56 +02:00
|
|
|
throw error_p::make(cs, "'%s' is not an alias", css.name().data());
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
2021-04-11 03:32:14 +02:00
|
|
|
any_value result{}, tback{};
|
2021-03-23 02:43:00 +01:00
|
|
|
bool rc = true;
|
|
|
|
try {
|
2021-05-02 22:44:38 +02:00
|
|
|
result = args[0].get_code().call(cs);
|
2021-03-23 23:29:32 +01:00
|
|
|
} catch (error const &e) {
|
2021-04-11 03:32:14 +02:00
|
|
|
result.set_string(e.what(), cs);
|
2021-05-05 03:16:32 +02:00
|
|
|
if (e.stack().get()) {
|
2021-03-23 23:29:32 +01:00
|
|
|
charbuf buf{cs};
|
2021-05-05 03:16:32 +02:00
|
|
|
print_stack(std::back_inserter(buf), e.stack());
|
2021-04-11 03:32:14 +02:00
|
|
|
tback.set_string(buf.str(), cs);
|
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-25 00:44:36 +02:00
|
|
|
auto *reta = static_cast<alias *>(&cret);
|
|
|
|
auto *ssa = static_cast<alias *>(&css);
|
|
|
|
ts.get_astack(reta).set_alias(reta, ts, result);
|
|
|
|
ts.get_astack(ssa).set_alias(ssa, ts, tback);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
2021-05-08 17:20:56 +02:00
|
|
|
new_cmd_quiet(gcs, "assert", "ss#", [](auto &s, auto args, auto &ret) {
|
|
|
|
auto val = args[0];
|
|
|
|
val.force_code(s);
|
|
|
|
if (!val.get_code().call(s).get_bool()) {
|
|
|
|
if (args[2].get_integer() > 1) {
|
|
|
|
throw error_p::make(
|
|
|
|
s, "assertion failed: [%s] (%s)",
|
|
|
|
args[0].get_string(s).data(), args[1].get_string(s).data()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw error_p::make(
|
|
|
|
s, "assertion failed: [%s]",
|
|
|
|
args[0].get_string(s).data()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = std::move(args[0]);
|
|
|
|
});
|
|
|
|
|
2021-04-29 01:24:05 +02:00
|
|
|
new_cmd_quiet(gcs, "?", "aaa", [](auto &, auto args, auto &res) {
|
2021-03-23 02:43:00 +01:00
|
|
|
if (args[0].get_bool()) {
|
2021-04-11 19:36:20 +02:00
|
|
|
res = std::move(args[1]);
|
2021-03-23 02:43:00 +01:00
|
|
|
} else {
|
2021-04-11 19:36:20 +02:00
|
|
|
res = std::move(args[2]);
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 19:56:48 +02:00
|
|
|
new_cmd_quiet(gcs, "cond", "bb2...", [](auto &cs, auto args, auto &res) {
|
2021-03-23 02:43:00 +01:00
|
|
|
for (size_t i = 0; i < args.size(); i += 2) {
|
|
|
|
if ((i + 1) < args.size()) {
|
2021-05-02 22:44:38 +02:00
|
|
|
if (args[i].get_code().call(cs).get_bool()) {
|
|
|
|
res = args[i + 1].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-02 22:44:38 +02:00
|
|
|
res = args[i].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 19:56:48 +02:00
|
|
|
new_cmd_quiet(gcs, "case", "iab2...", [](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-05-05 03:16:32 +02:00
|
|
|
(args[i].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-05-02 22:44:38 +02:00
|
|
|
res = args[i + 1].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 19:56:48 +02:00
|
|
|
new_cmd_quiet(gcs, "casef", "fab2...", [](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-05-05 03:16:32 +02:00
|
|
|
(args[i].type() == value_type::NONE) ||
|
2021-03-23 02:43:00 +01:00
|
|
|
(args[i].get_float() == val)
|
|
|
|
) {
|
2021-05-02 22:44:38 +02:00
|
|
|
res = args[i + 1].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 19:56:48 +02:00
|
|
|
new_cmd_quiet(gcs, "cases", "sab2...", [](auto &cs, auto args, auto &res) {
|
2021-04-11 03:32:14 +02:00
|
|
|
string_ref val = args[0].get_string(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
for (size_t i = 1; (i + 1) < args.size(); i += 2) {
|
|
|
|
if (
|
2021-05-05 03:16:32 +02:00
|
|
|
(args[i].type() == value_type::NONE) ||
|
2021-04-11 03:32:14 +02:00
|
|
|
(args[i].get_string(cs) == val)
|
2021-03-23 02:43:00 +01:00
|
|
|
) {
|
2021-05-02 22:44:38 +02:00
|
|
|
res = args[i + 1].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "pushif", "vab", [](auto &cs, auto args, auto &res) {
|
2021-04-24 23:33:19 +02:00
|
|
|
alias_local st{cs, args[0]};
|
2021-04-24 23:50:06 +02:00
|
|
|
if (st.get_alias().is_arg()) {
|
2021-04-24 23:33:19 +02:00
|
|
|
throw error{cs, "cannot push an argument"};
|
|
|
|
}
|
|
|
|
if (args[1].get_bool()) {
|
|
|
|
st.set(args[1]);
|
2021-05-02 22:44:38 +02:00
|
|
|
res = args[2].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loop", "vab", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), 0, args[1].get_integer(), 1,
|
|
|
|
bcode_ref{}, args[2].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loop+", "viib", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), 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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loop*", "viib", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), 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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loop+*", "viiib", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopwhile", "vibb", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), 0, args[1].get_integer(), 1,
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code(), args[3].get_code()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopwhile+", "viibb", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[2].get_integer(), 1, args[3].get_code(), args[4].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopwhile*", "viibb", [](auto &cs, auto args, auto &) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), 0, args[2].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[1].get_integer(), args[3].get_code(), args[4].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopwhile+*", "viiibb", [](
|
2021-04-11 19:36:20 +02:00
|
|
|
auto &cs, auto args, auto &
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[3].get_integer(), args[2].get_integer(), args[4].get_code(),
|
|
|
|
args[5].get_code()
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 01:24:05 +02:00
|
|
|
new_cmd_quiet(gcs, "while", "bb", [](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-05-02 22:44:38 +02:00
|
|
|
while (cond.call(cs).get_bool()) {
|
|
|
|
switch (body.call_loop(cs)) {
|
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;
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcat", "vib", [](
|
2021-04-11 19:36:20 +02:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), 0, args[1].get_integer(), 1,
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code(), true
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcat+", "viib", [](
|
2021-04-11 19:36:20 +02:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[2].get_integer(), 1, args[3].get_code(), true
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcat*", "viib", [](
|
2021-04-11 19:36:20 +02:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), 0, args[2].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[1].get_integer(), args[3].get_code(), true
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcat+*", "viiib", [](
|
2021-04-11 19:36:20 +02:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[3].get_integer(), args[2].get_integer(),
|
|
|
|
args[4].get_code(), true
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcatword", "vib", [](
|
2021-04-11 19:36:20 +02:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), 0, args[1].get_integer(), 1,
|
2021-03-23 02:43:00 +01:00
|
|
|
args[2].get_code(), false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcatword+", "viib", [](
|
2021-03-23 02:43:00 +01:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[2].get_integer(), 1, args[3].get_code(), false
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcatword*", "viib", [](
|
2021-03-23 02:43:00 +01:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), 0, args[2].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[1].get_integer(), args[3].get_code(), false
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "loopconcatword+*", "viiib", [](
|
2021-03-23 02:43:00 +01:00
|
|
|
auto &cs, auto args, auto &res
|
|
|
|
) {
|
2021-03-23 23:29:32 +01:00
|
|
|
do_loop_conc(
|
2021-04-24 23:33:19 +02:00
|
|
|
cs, res, args[0].get_ident(cs), args[1].get_integer(),
|
2021-04-06 00:54:46 +02:00
|
|
|
args[3].get_integer(), args[2].get_integer(),
|
|
|
|
args[4].get_code(), false
|
2021-03-23 02:43:00 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-29 20:02:04 +02:00
|
|
|
new_cmd_quiet(gcs, "push", "vab", [](auto &cs, auto args, auto &res) {
|
2021-04-24 23:33:19 +02:00
|
|
|
alias_local st{cs, args[0]};
|
2021-04-24 23:50:06 +02:00
|
|
|
if (st.get_alias().is_arg()) {
|
2021-04-24 23:33:19 +02:00
|
|
|
throw error{cs, "cannot push an argument"};
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
2021-04-24 23:33:19 +02:00
|
|
|
st.set(args[1]);
|
2021-05-02 22:44:38 +02:00
|
|
|
res = args[2].get_code().call(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
2021-04-11 19:36:20 +02:00
|
|
|
new_cmd_quiet(gcs, "resetvar", "s", [](auto &cs, auto args, auto &) {
|
2021-04-28 03:20:36 +02:00
|
|
|
cs.reset_value(args[0].get_string(cs));
|
2021-04-04 02:33:28 +02:00
|
|
|
});
|
|
|
|
|
2021-04-29 01:24:05 +02:00
|
|
|
new_cmd_quiet(gcs, "alias", "sa", [](auto &cs, auto args, auto &) {
|
2021-04-28 03:20:36 +02:00
|
|
|
cs.assign_value(args[0].get_string(cs), args[1]);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
2021-04-11 19:36:20 +02:00
|
|
|
new_cmd_quiet(gcs, "identexists", "s", [](auto &cs, auto args, auto &res) {
|
2021-04-26 02:42:08 +02:00
|
|
|
res.set_integer(cs.get_ident(args[0].get_string(cs)) != std::nullopt);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
|
2021-04-11 19:36:20 +02:00
|
|
|
new_cmd_quiet(gcs, "getalias", "s", [](auto &cs, auto args, auto &res) {
|
2021-04-26 02:42:08 +02:00
|
|
|
auto &id = cs.new_ident(args[0].get_string(cs));
|
2021-05-05 03:16:32 +02:00
|
|
|
if (id.type() != ident_type::ALIAS) {
|
2021-05-08 17:20:56 +02:00
|
|
|
throw error_p::make(cs, "'%s' is not an alias", id.name().data());
|
2021-03-23 02:43:00 +01:00
|
|
|
}
|
2021-04-26 02:42:08 +02:00
|
|
|
if (ident_p{id}.impl().p_flags & IDENT_FLAG_UNKNOWN) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-05 03:16:32 +02:00
|
|
|
res = static_cast<alias &>(id).value(cs);
|
2021-03-23 02:43:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
} /* namespace cubescript */
|