libcubescript/src/cs_vm.cc

1503 lines
54 KiB
C++
Raw Normal View History

2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
2016-08-12 18:38:43 +02:00
#include "cs_vm.hh"
2021-03-23 01:45:35 +01:00
#include "cs_std.hh"
#include "cs_parser.hh"
2016-08-12 18:38:43 +02:00
2021-03-20 06:52:10 +01:00
#include <cstdio>
2017-02-09 20:59:14 +01:00
#include <limits>
2016-08-13 01:26:16 +02:00
2021-03-23 23:32:25 +01:00
namespace cubescript {
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
static inline bool ident_has_cb(ident *id) {
2021-03-21 03:07:01 +01:00
if (!id->is_command() && !id->is_special()) {
return false;
2016-08-17 23:04:43 +02:00
}
2021-03-23 23:29:32 +01:00
return !!static_cast<command_impl *>(id)->p_cb_cftv;
2021-03-21 03:07:01 +01:00
}
2016-08-17 23:04:43 +02:00
2021-03-23 23:29:32 +01:00
static inline void push_alias(state &cs, ident *id, ident_stack &st) {
2021-03-24 02:21:27 +01:00
if (id->is_alias() && (id->get_index() >= MAX_ARGUMENTS)) {
2021-03-23 23:29:32 +01:00
any_value nv{cs};
static_cast<alias_impl *>(id)->push_arg(nv, st);
2016-08-18 00:06:39 +02:00
}
}
2021-03-23 23:29:32 +01:00
static inline void pop_alias(ident *id) {
2021-03-24 02:21:27 +01:00
if (id->is_alias() && (id->get_index() >= MAX_ARGUMENTS)) {
2021-03-23 23:29:32 +01:00
static_cast<alias_impl *>(id)->pop_arg();
2016-08-18 00:06:39 +02:00
}
}
2021-03-23 23:29:32 +01:00
stack_state::stack_state(state &cs, stack_state_node *nd, bool gap):
2016-09-14 21:46:47 +02:00
p_state(cs), p_node(nd), p_gap(gap)
2016-09-09 19:19:50 +02:00
{}
2021-03-23 23:29:32 +01:00
stack_state::stack_state(stack_state &&st):
2016-09-14 21:46:47 +02:00
p_state(st.p_state), p_node(st.p_node), p_gap(st.p_gap)
2016-09-09 19:19:50 +02:00
{
st.p_node = nullptr;
st.p_gap = false;
}
2021-03-23 23:29:32 +01:00
stack_state::~stack_state() {
2017-01-25 02:09:50 +01:00
size_t len = 0;
2021-03-23 23:29:32 +01:00
for (stack_state_node const *nd = p_node; nd; nd = nd->next) {
2016-09-14 21:46:47 +02:00
++len;
}
p_state.p_state->destroy_array(p_node, len);
2016-09-09 19:19:50 +02:00
}
2021-03-23 23:29:32 +01:00
stack_state &stack_state::operator=(stack_state &&st) {
2016-09-09 19:19:50 +02:00
p_node = st.p_node;
p_gap = st.p_gap;
st.p_node = nullptr;
st.p_gap = false;
return *this;
}
2021-03-23 23:29:32 +01:00
stack_state_node const *stack_state::get() const {
2016-09-09 19:19:50 +02:00
return p_node;
}
2021-03-23 23:29:32 +01:00
bool stack_state::gap() const {
2016-09-09 19:19:50 +02:00
return p_gap;
}
2021-03-23 23:29:32 +01:00
static inline void force_arg(any_value &v, int type) {
2016-08-12 18:38:43 +02:00
switch (type) {
2021-03-23 23:29:32 +01:00
case BC_RET_STRING:
if (v.get_type() != value_type::STRING) {
2016-08-17 04:57:53 +02:00
v.force_str();
}
break;
2021-03-23 23:29:32 +01:00
case BC_RET_INT:
if (v.get_type() != value_type::INT) {
2016-08-17 04:57:53 +02:00
v.force_int();
}
break;
2021-03-23 23:29:32 +01:00
case BC_RET_FLOAT:
if (v.get_type() != value_type::FLOAT) {
2016-08-17 04:57:53 +02:00
v.force_float();
}
break;
2016-08-12 18:38:43 +02:00
}
}
2016-08-17 04:57:53 +02:00
static inline void callcommand(
2021-03-23 23:29:32 +01:00
state &cs, command_impl *id, any_value *args, any_value &res,
2021-03-21 02:41:04 +01:00
int numargs, bool lookup = false
2016-08-17 04:57:53 +02:00
) {
2016-08-12 18:38:43 +02:00
int i = -1, fakeargs = 0;
bool rep = false;
auto fmt = id->get_args();
for (auto it = fmt.begin(); it != fmt.end(); ++it) {
switch (*it) {
2016-08-17 04:57:53 +02:00
case 'i':
if (++i >= numargs) {
if (rep) {
break;
}
args[i].set_int(0);
fakeargs++;
} else {
args[i].force_int();
}
break;
case 'b':
if (++i >= numargs) {
if (rep) {
break;
}
2021-03-23 23:29:32 +01:00
args[i].set_int(std::numeric_limits<integer_type>::min());
2016-08-17 04:57:53 +02:00
fakeargs++;
} else {
args[i].force_int();
}
break;
case 'f':
if (++i >= numargs) {
if (rep) {
break;
}
args[i].set_float(0.0f);
fakeargs++;
} else {
args[i].force_float();
}
break;
case 'F':
if (++i >= numargs) {
if (rep) {
break;
}
args[i].set_float(args[i - 1].get_float());
fakeargs++;
} else {
args[i].force_float();
}
break;
case 's':
if (++i >= numargs) {
if (rep) {
break;
}
args[i].set_str("");
2016-08-17 04:57:53 +02:00
fakeargs++;
} else {
args[i].force_str();
}
break;
case 'T':
case 't':
if (++i >= numargs) {
if (rep) {
break;
}
2021-03-18 20:55:14 +01:00
args[i].set_none();
2016-08-17 04:57:53 +02:00
fakeargs++;
}
break;
case 'E':
if (++i >= numargs) {
if (rep) {
break;
}
2021-03-18 20:55:14 +01:00
args[i].set_none();
2016-08-17 04:57:53 +02:00
fakeargs++;
} else if (args[i].get_type() == value_type::STRING) {
auto str = std::string_view{args[i].get_str()};
if (str.empty()) {
args[i].set_int(0);
} else {
args[i].force_code(cs);
}
2016-08-17 04:57:53 +02:00
}
break;
case 'e':
if (++i >= numargs) {
if (rep) {
break;
}
args[i].set_code(
2021-03-23 23:29:32 +01:00
bcode_get_empty(state_get_internal(cs)->empty, VAL_NULL)
2016-08-17 04:57:53 +02:00
);
fakeargs++;
} else {
args[i].force_code(cs);
2016-08-17 04:57:53 +02:00
}
break;
case 'r':
if (++i >= numargs) {
if (rep) {
break;
}
2021-03-24 02:21:27 +01:00
args[i].set_ident(cs.p_state->identmap[ID_IDX_DUMMY]);
2016-08-17 04:57:53 +02:00
fakeargs++;
} else {
args[i].force_ident(cs);
2016-08-17 04:57:53 +02:00
}
break;
case '$':
2016-09-06 20:48:24 +02:00
i += 1;
2016-08-17 04:57:53 +02:00
args[i].set_ident(id);
break;
case 'N':
2016-09-06 20:48:24 +02:00
i += 1;
2021-03-23 23:29:32 +01:00
args[i].set_int(integer_type(lookup ? -1 : i - fakeargs));
2016-08-17 04:57:53 +02:00
break;
case 'C': {
2017-02-18 17:49:01 +01:00
i = std::max(i + 1, numargs);
2021-03-23 23:29:32 +01:00
any_value tv{cs};
tv.set_str(concat_values(
2021-03-20 05:41:25 +01:00
cs, std::span{args, std::size_t(i)}, " "
));
2021-03-23 23:29:32 +01:00
static_cast<command_impl *>(id)->call(
cs, std::span<any_value>(&tv, &tv + 1), res
2021-03-20 05:41:25 +01:00
);
return;
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
case 'V':
2017-02-18 17:49:01 +01:00
i = std::max(i + 1, numargs);
2021-03-23 23:29:32 +01:00
static_cast<command_impl *>(id)->call(
2021-03-21 03:07:01 +01:00
cs, std::span{args, std::size_t(i)}, res
2021-03-20 05:41:25 +01:00
);
return;
2016-08-17 04:57:53 +02:00
case '1':
case '2':
case '3':
case '4':
if (i + 1 < numargs) {
it -= *it - '0' + 1;
2016-08-17 04:57:53 +02:00
rep = true;
}
break;
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
}
2016-08-12 18:38:43 +02:00
++i;
2021-03-23 23:29:32 +01:00
static_cast<command_impl *>(id)->call(
cs, std::span<any_value>{args, std::size_t(i)}, res
2021-03-20 05:41:25 +01:00
);
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
static uint32_t *runcode(state &cs, uint32_t *code, any_value &result);
2016-08-17 04:57:53 +02:00
2021-03-23 23:29:32 +01:00
static inline void call_alias(
state &cs, alias *a, any_value *args, any_value &result,
2017-01-25 02:10:17 +01:00
int callargs, int &nargs, int offset, int skip, uint32_t op
2016-08-17 04:57:53 +02:00
) {
2021-03-24 02:21:27 +01:00
integer_var *anargs = static_cast<integer_var *>(cs.p_state->identmap[ID_IDX_NUMARGS]);
valarray<ident_stack, MAX_ARGUMENTS> argstack{cs};
2016-08-17 04:57:53 +02:00
for(int i = 0; i < callargs; i++) {
2021-03-23 23:29:32 +01:00
static_cast<alias_impl *>(cs.p_state->identmap[i])->push_arg(
2016-08-18 00:06:39 +02:00
args[offset + i], argstack[i], false
);
2016-08-17 04:57:53 +02:00
}
int oldargs = anargs->get_value();
anargs->set_value(callargs);
2016-08-17 04:57:53 +02:00
int oldflags = cs.identflags;
2021-03-23 23:29:32 +01:00
cs.identflags |= a->get_flags()&IDENT_FLAG_OVERRIDDEN;
ident_link aliaslink = {
a, cs.p_tstate->callstack, (1<<callargs)-1, &argstack[0]
2016-08-17 04:57:53 +02:00
};
cs.p_tstate->callstack = &aliaslink;
2021-03-23 23:29:32 +01:00
uint32_t *codep = static_cast<alias_impl *>(a)->compile_code(cs)->get_raw();
2016-08-17 04:57:53 +02:00
bcode_incr(codep);
2021-03-23 01:11:21 +01:00
call_with_cleanup([&]() {
runcode(cs, codep+1, result);
}, [&]() {
bcode_decr(codep);
cs.p_tstate->callstack = aliaslink.next;
cs.identflags = oldflags;
for (int i = 0; i < callargs; i++) {
2021-03-23 23:29:32 +01:00
static_cast<alias_impl *>(cs.p_state->identmap[i])->pop_arg();
2016-08-17 04:57:53 +02:00
}
2017-11-06 01:07:53 +01:00
int argmask = aliaslink.usedargs & int(~0U << callargs);
for (; argmask; ++callargs) {
if (argmask & (1 << callargs)) {
2021-03-23 23:29:32 +01:00
static_cast<alias_impl *>(
cs.p_state->identmap[callargs]
)->pop_arg();
argmask &= ~(1 << callargs);
}
}
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
anargs->set_value(oldargs);
nargs = offset - skip;
});
2016-08-12 18:38:43 +02:00
}
static constexpr int MaxRunDepth = 255;
static thread_local int rundepth = 0;
2016-09-23 21:04:52 +02:00
struct RunDepthRef {
RunDepthRef() = delete;
2021-03-23 23:29:32 +01:00
RunDepthRef(state &cs) {
2016-09-23 21:04:52 +02:00
if (rundepth >= MaxRunDepth) {
2021-03-23 23:29:32 +01:00
throw error(cs, "exceeded recursion limit");
2016-09-23 21:04:52 +02:00
}
++rundepth;
}
RunDepthRef(RunDepthRef const &) = delete;
RunDepthRef(RunDepthRef &&) = delete;
~RunDepthRef() { --rundepth; }
};
2021-03-23 23:29:32 +01:00
static inline alias *get_lookup_id(state &cs, uint32_t op) {
ident *id = cs.p_state->identmap[op >> 8];
if (id->get_flags() & IDENT_FLAG_UNKNOWN) {
throw error(cs, "unknown alias lookup: %s", id->get_name().data());
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
return static_cast<alias *>(id);
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
static inline alias *get_lookuparg_id(state &cs, uint32_t op) {
ident *id = cs.p_state->identmap[op >> 8];
if (!ident_is_used_arg(id, cs)) {
2016-08-17 04:57:53 +02:00
return nullptr;
}
2021-03-23 23:29:32 +01:00
return static_cast<alias *>(id);
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
static inline int get_lookupu_type(
state &cs, any_value &arg, ident *&id, uint32_t op
2016-08-17 04:57:53 +02:00
) {
2021-03-23 23:29:32 +01:00
if (arg.get_type() != value_type::STRING) {
2016-08-17 04:57:53 +02:00
return -2; /* default case */
}
2021-03-17 21:57:47 +01:00
id = cs.get_ident(arg.get_str());
2016-08-17 04:57:53 +02:00
if (id) {
2016-08-18 03:53:51 +02:00
switch(id->get_type()) {
2021-03-23 23:29:32 +01:00
case ident_type::ALIAS:
if (id->get_flags() & IDENT_FLAG_UNKNOWN) {
2016-08-17 04:57:53 +02:00
break;
}
2021-03-24 02:21:27 +01:00
if ((id->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(id, cs)) {
return ID_UNKNOWN;
2016-08-17 04:57:53 +02:00
}
return ID_ALIAS;
2021-03-23 23:29:32 +01:00
case ident_type::SVAR:
return ID_SVAR;
2021-03-23 23:29:32 +01:00
case ident_type::IVAR:
return ID_IVAR;
2021-03-23 23:29:32 +01:00
case ident_type::FVAR:
return ID_FVAR;
2021-03-23 23:29:32 +01:00
case ident_type::COMMAND: {
2021-03-18 20:55:14 +01:00
arg.set_none();
2021-03-24 02:21:27 +01:00
valarray<any_value, MAX_ARGUMENTS> buf{cs};
2021-03-21 02:41:04 +01:00
callcommand(
2021-03-23 23:29:32 +01:00
cs, static_cast<command_impl *>(id),
2021-03-21 02:41:04 +01:00
&buf[0], arg, 0, true
);
2021-03-23 23:29:32 +01:00
force_arg(arg, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
return -2; /* ignore */
}
default:
return ID_UNKNOWN;
2016-08-17 04:57:53 +02:00
}
}
2021-03-23 23:29:32 +01:00
throw error(cs, "unknown alias lookup: %s", arg.get_str().data());
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
static uint32_t *runcode(state &cs, uint32_t *code, any_value &result) {
2021-03-18 20:55:14 +01:00
result.set_none();
2016-09-23 21:04:52 +02:00
RunDepthRef level{cs}; /* incr and decr on scope exit */
2016-08-12 18:38:43 +02:00
int numargs = 0;
2021-03-24 02:21:27 +01:00
valarray<any_value, MAX_ARGUMENTS + MAX_RESULTS> args{cs};
auto &chook = cs.get_call_hook();
if (chook) {
chook(cs);
}
2016-08-12 18:38:43 +02:00
for (;;) {
2017-01-25 02:10:17 +01:00
uint32_t op = *code++;
2016-08-12 18:38:43 +02:00
switch (op & 0xFF) {
2021-03-23 23:29:32 +01:00
case BC_INST_START:
case BC_INST_OFFSET:
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_NULL | BC_RET_NULL:
2021-03-18 20:55:14 +01:00
result.set_none();
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_NULL | BC_RET_STRING:
2016-08-17 04:57:53 +02:00
result.set_str("");
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_NULL | BC_RET_INT:
2016-08-17 04:57:53 +02:00
result.set_int(0);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_NULL | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
result.set_float(0.0f);
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_FALSE | BC_RET_STRING:
2016-08-17 04:57:53 +02:00
result.set_str("0");
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FALSE | BC_RET_NULL:
case BC_INST_FALSE | BC_RET_INT:
2016-08-17 04:57:53 +02:00
result.set_int(0);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FALSE | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
result.set_float(0.0f);
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_TRUE | BC_RET_STRING:
2016-08-17 04:57:53 +02:00
result.set_str("1");
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_TRUE | BC_RET_NULL:
case BC_INST_TRUE | BC_RET_INT:
2016-08-17 04:57:53 +02:00
result.set_int(1);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_TRUE | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
result.set_float(1.0f);
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_NOT | BC_RET_STRING:
2016-08-17 04:57:53 +02:00
--numargs;
result.set_str(args[numargs].get_bool() ? "0" : "1");
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_NOT | BC_RET_NULL:
case BC_INST_NOT | BC_RET_INT:
2016-08-17 04:57:53 +02:00
--numargs;
result.set_int(!args[numargs].get_bool());
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_NOT | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
--numargs;
2021-03-23 23:29:32 +01:00
result.set_float(float_type(!args[numargs].get_bool()));
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_POP:
numargs -= 1;
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_ENTER:
2016-08-17 04:57:53 +02:00
code = runcode(cs, code, args[numargs++]);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_ENTER_RESULT:
2016-08-17 04:57:53 +02:00
code = runcode(cs, code, result);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_EXIT | BC_RET_STRING:
case BC_INST_EXIT | BC_RET_INT:
case BC_INST_EXIT | BC_RET_FLOAT:
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
/* fallthrough */
2021-03-23 23:29:32 +01:00
case BC_INST_EXIT | BC_RET_NULL:
2016-09-23 21:06:44 +02:00
return code;
2021-03-23 23:29:32 +01:00
case BC_INST_RESULT_ARG | BC_RET_STRING:
case BC_INST_RESULT_ARG | BC_RET_INT:
case BC_INST_RESULT_ARG | BC_RET_FLOAT:
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
/* fallthrough */
2021-03-23 23:29:32 +01:00
case BC_INST_RESULT_ARG | BC_RET_NULL:
2017-01-25 01:57:33 +01:00
args[numargs++] = std::move(result);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_PRINT:
cs.print_var(*static_cast<global_var *>(cs.p_state->identmap[op >> 8]));
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_LOCAL: {
2016-08-17 04:57:53 +02:00
int numlocals = op >> 8, offset = numargs - numlocals;
2021-03-24 02:21:27 +01:00
valarray<ident_stack, MAX_ARGUMENTS> locals{cs};
2016-08-17 04:57:53 +02:00
for (int i = 0; i < numlocals; ++i) {
2021-03-23 23:29:32 +01:00
push_alias(cs, args[offset + i].get_ident(), locals[i]);
2016-08-17 04:57:53 +02:00
}
2021-03-23 01:11:21 +01:00
call_with_cleanup([&]() {
code = runcode(cs, code, result);
}, [&]() {
for (int i = offset; i < numargs; i++) {
2021-03-23 23:29:32 +01:00
pop_alias(args[i].get_ident());
}
});
2016-09-23 21:06:44 +02:00
return code;
2016-08-17 04:57:53 +02:00
}
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_DO_ARGS | BC_RET_NULL:
case BC_INST_DO_ARGS | BC_RET_STRING:
case BC_INST_DO_ARGS | BC_RET_INT:
case BC_INST_DO_ARGS | BC_RET_FLOAT:
call_with_args(cs, [&]() {
2016-09-10 19:54:55 +02:00
cs.run(args[--numargs].get_code(), result);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-09-10 19:54:55 +02:00
});
continue;
2016-08-17 04:57:53 +02:00
/* fallthrough */
2021-03-23 23:29:32 +01:00
case BC_INST_DO | BC_RET_NULL:
case BC_INST_DO | BC_RET_STRING:
case BC_INST_DO | BC_RET_INT:
case BC_INST_DO | BC_RET_FLOAT:
cs.run(args[--numargs].get_code(), result);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_JUMP: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
2016-08-17 04:57:53 +02:00
code += len;
2016-08-12 18:38:43 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_JUMP_B | BC_INST_FLAG_TRUE: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
2016-08-17 04:57:53 +02:00
if (args[--numargs].get_bool()) {
code += len;
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_JUMP_B | BC_INST_FLAG_FALSE: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
2016-08-17 04:57:53 +02:00
if (!args[--numargs].get_bool()) {
code += len;
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_JUMP_RESULT | BC_INST_FLAG_TRUE: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
2016-08-17 04:57:53 +02:00
--numargs;
2021-03-23 23:29:32 +01:00
if (args[numargs].get_type() == value_type::CODE) {
cs.run(args[numargs].get_code(), result);
2016-08-17 04:57:53 +02:00
} else {
2017-01-25 01:57:33 +01:00
result = std::move(args[numargs]);
2016-08-17 04:57:53 +02:00
}
if (result.get_bool()) {
code += len;
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_JUMP_RESULT | BC_INST_FLAG_FALSE: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
2016-08-17 04:57:53 +02:00
--numargs;
2021-03-23 23:29:32 +01:00
if (args[numargs].get_type() == value_type::CODE) {
cs.run(args[numargs].get_code(), result);
2016-08-17 04:57:53 +02:00
} else {
2017-01-25 01:57:33 +01:00
result = std::move(args[numargs]);
2016-08-17 04:57:53 +02:00
}
if (!result.get_bool()) {
code += len;
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_BREAK | BC_INST_FLAG_FALSE:
2016-09-15 02:12:22 +02:00
if (cs.is_in_loop()) {
2021-03-24 02:21:27 +01:00
throw break_exception();
2016-09-15 02:12:22 +02:00
} else {
2021-03-23 23:29:32 +01:00
throw error(cs, "no loop to break");
2016-09-15 02:12:22 +02:00
}
break;
2021-03-23 23:29:32 +01:00
case BC_INST_BREAK | BC_INST_FLAG_TRUE:
2016-09-15 02:12:22 +02:00
if (cs.is_in_loop()) {
2021-03-24 02:21:27 +01:00
throw continue_exception();
2016-09-15 02:12:22 +02:00
} else {
2021-03-23 23:29:32 +01:00
throw error(cs, "no loop to continue");
2016-09-15 02:12:22 +02:00
}
break;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_VAL | BC_RET_STRING: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
args[numargs++].set_str(std::string_view{
reinterpret_cast<char const *>(code), len
2017-01-30 01:18:55 +01:00
});
2017-01-25 02:10:17 +01:00
code += len / sizeof(uint32_t) + 1;
2016-08-17 04:57:53 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_VAL_INT | BC_RET_STRING: {
2016-08-17 04:57:53 +02:00
char s[4] = {
char((op >> 8) & 0xFF),
char((op >> 16) & 0xFF),
char((op >> 24) & 0xFF), '\0'
};
/* gotta cast or r.size() == potentially 3 */
args[numargs++].set_str(static_cast<char const *>(s));
2016-08-17 04:57:53 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_VAL | BC_RET_NULL:
case BC_INST_VAL_INT | BC_RET_NULL:
2021-03-18 20:55:14 +01:00
args[numargs++].set_none();
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_VAL | BC_RET_INT:
args[numargs++].set_int(
2021-03-23 23:29:32 +01:00
*reinterpret_cast<integer_type const *>(code)
);
2021-03-24 02:21:27 +01:00
code += bc_store_size<integer_type>;
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_VAL_INT | BC_RET_INT:
args[numargs++].set_int(integer_type(op) >> 8);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_VAL | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
args[numargs++].set_float(
2021-03-23 23:29:32 +01:00
*reinterpret_cast<float_type const *>(code)
2016-08-17 04:57:53 +02:00
);
2021-03-24 02:21:27 +01:00
code += bc_store_size<float_type>;
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_VAL_INT | BC_RET_FLOAT:
args[numargs++].set_float(float_type(integer_type(op) >> 8));
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_DUP | BC_RET_NULL:
2016-08-17 04:57:53 +02:00
args[numargs - 1].get_val(args[numargs]);
numargs++;
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_DUP | BC_RET_INT:
2016-08-17 04:57:53 +02:00
args[numargs].set_int(args[numargs - 1].get_int());
numargs++;
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_DUP | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
args[numargs].set_float(args[numargs - 1].get_float());
numargs++;
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_DUP | BC_RET_STRING:
args[numargs] = args[numargs - 1];
args[numargs].force_str();
2016-08-17 04:57:53 +02:00
numargs++;
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FORCE | BC_RET_STRING:
2016-08-17 04:57:53 +02:00
args[numargs - 1].force_str();
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FORCE | BC_RET_INT:
2016-08-17 04:57:53 +02:00
args[numargs - 1].force_int();
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FORCE | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
args[numargs - 1].force_float();
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_RESULT | BC_RET_NULL:
2017-01-25 01:57:33 +01:00
result = std::move(args[--numargs]);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_RESULT | BC_RET_STRING:
case BC_INST_RESULT | BC_RET_INT:
case BC_INST_RESULT | BC_RET_FLOAT:
2017-01-25 01:57:33 +01:00
result = std::move(args[--numargs]);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_EMPTY | BC_RET_NULL:
2016-08-17 04:57:53 +02:00
args[numargs++].set_code(
2021-03-23 23:29:32 +01:00
bcode_get_empty(state_get_internal(cs)->empty, VAL_NULL)
2016-08-17 04:57:53 +02:00
);
2016-08-12 18:38:43 +02:00
break;
2021-03-23 23:29:32 +01:00
case BC_INST_EMPTY | BC_RET_STRING:
2016-08-17 04:57:53 +02:00
args[numargs++].set_code(
2021-03-23 23:29:32 +01:00
bcode_get_empty(state_get_internal(cs)->empty, VAL_STRING)
2016-08-17 04:57:53 +02:00
);
2016-08-12 18:38:43 +02:00
break;
2021-03-23 23:29:32 +01:00
case BC_INST_EMPTY | BC_RET_INT:
2016-08-17 04:57:53 +02:00
args[numargs++].set_code(
2021-03-23 23:29:32 +01:00
bcode_get_empty(state_get_internal(cs)->empty, VAL_INT)
2016-08-17 04:57:53 +02:00
);
2016-08-12 18:38:43 +02:00
break;
2021-03-23 23:29:32 +01:00
case BC_INST_EMPTY | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
args[numargs++].set_code(
2021-03-23 23:29:32 +01:00
bcode_get_empty(state_get_internal(cs)->empty, VAL_FLOAT)
2016-08-17 04:57:53 +02:00
);
2016-08-12 18:38:43 +02:00
break;
2021-03-23 23:29:32 +01:00
case BC_INST_BLOCK: {
2017-01-25 02:10:17 +01:00
uint32_t len = op >> 8;
2016-08-17 04:57:53 +02:00
args[numargs++].set_code(
2021-03-23 23:29:32 +01:00
reinterpret_cast<bcode *>(code + 1)
2016-08-17 04:57:53 +02:00
);
code += len;
continue;
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_COMPILE: {
any_value &arg = args[numargs - 1];
codegen_state gs(cs);
2016-08-17 04:57:53 +02:00
switch (arg.get_type()) {
2021-03-23 23:29:32 +01:00
case value_type::INT:
2016-08-17 04:57:53 +02:00
gs.code.reserve(8);
2021-03-23 23:29:32 +01:00
gs.code.push_back(BC_INST_START);
2016-08-30 22:55:35 +02:00
gs.gen_int(arg.get_int());
2021-03-23 23:29:32 +01:00
gs.code.push_back(BC_INST_RESULT);
gs.code.push_back(BC_INST_EXIT);
2016-08-17 04:57:53 +02:00
break;
2021-03-23 23:29:32 +01:00
case value_type::FLOAT:
2016-08-17 04:57:53 +02:00
gs.code.reserve(8);
2021-03-23 23:29:32 +01:00
gs.code.push_back(BC_INST_START);
2016-08-30 22:55:35 +02:00
gs.gen_float(arg.get_float());
2021-03-23 23:29:32 +01:00
gs.code.push_back(BC_INST_RESULT);
gs.code.push_back(BC_INST_EXIT);
2016-08-17 04:57:53 +02:00
break;
2021-03-23 23:29:32 +01:00
case value_type::STRING:
2016-08-17 04:57:53 +02:00
gs.code.reserve(64);
2021-03-17 21:57:47 +01:00
gs.gen_main(arg.get_str());
2016-08-17 04:57:53 +02:00
break;
default:
gs.code.reserve(8);
2021-03-23 23:29:32 +01:00
gs.code.push_back(BC_INST_START);
2016-08-17 04:57:53 +02:00
gs.gen_null();
2021-03-23 23:29:32 +01:00
gs.code.push_back(BC_INST_RESULT);
gs.code.push_back(BC_INST_EXIT);
2016-08-17 04:57:53 +02:00
break;
}
gs.done();
uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size());
2017-01-25 01:18:29 +01:00
memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t));
2016-08-17 04:57:53 +02:00
arg.set_code(
2021-03-23 23:29:32 +01:00
reinterpret_cast<bcode *>(cbuf + 1)
2016-08-17 04:57:53 +02:00
);
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_COND: {
any_value &arg = args[numargs - 1];
2016-08-17 04:57:53 +02:00
switch (arg.get_type()) {
2021-03-23 23:29:32 +01:00
case value_type::STRING: {
std::string_view s = arg.get_str();
2016-08-30 22:55:35 +02:00
if (!s.empty()) {
2021-03-23 23:29:32 +01:00
codegen_state gs(cs);
2016-08-17 04:57:53 +02:00
gs.code.reserve(64);
2016-08-30 22:55:35 +02:00
gs.gen_main(s);
gs.done();
uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size());
2017-01-25 01:18:29 +01:00
memcpy(
cbuf, gs.code.data(),
gs.code.size() * sizeof(uint32_t)
);
2021-03-23 23:29:32 +01:00
arg.set_code(reinterpret_cast<bcode *>(cbuf + 1));
2016-08-17 04:57:53 +02:00
} else {
2021-03-18 20:55:14 +01:00
arg.force_none();
2016-08-17 04:57:53 +02:00
}
break;
2016-08-30 22:55:35 +02:00
}
default:
break;
2016-08-17 04:57:53 +02:00
}
continue;
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_IDENT:
args[numargs++].set_ident(cs.p_state->identmap[op >> 8]);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_IDENT_ARG: {
alias *a = static_cast<alias *>(
cs.p_state->identmap[op >> 8]
);
if (!ident_is_used_arg(a, cs)) {
2021-03-23 23:29:32 +01:00
any_value nv{cs};
static_cast<alias_impl *>(a)->push_arg(
nv, cs.p_tstate->callstack->argstack[a->get_index()],
false
2016-08-18 03:53:51 +02:00
);
cs.p_tstate->callstack->usedargs |= 1 << a->get_index();
2016-08-17 04:57:53 +02:00
}
2016-08-18 00:06:39 +02:00
args[numargs++].set_ident(a);
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_IDENT_U: {
any_value &arg = args[numargs - 1];
2021-03-24 02:21:27 +01:00
ident *id = cs.p_state->identmap[ID_IDX_DUMMY];
2021-03-23 23:29:32 +01:00
if (arg.get_type() == value_type::STRING) {
2021-03-17 21:57:47 +01:00
id = cs.new_ident(arg.get_str());
2016-08-17 04:57:53 +02:00
}
2021-03-24 02:21:27 +01:00
if ((id->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(id, cs)) {
2021-03-23 23:29:32 +01:00
any_value nv{cs};
static_cast<alias_impl *>(id)->push_arg(
nv, cs.p_tstate->callstack->argstack[id->get_index()],
false
2016-08-18 00:06:39 +02:00
);
cs.p_tstate->callstack->usedargs |= 1 << id->get_index();
2016-08-17 04:57:53 +02:00
}
arg.set_ident(id);
continue;
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_U | BC_RET_STRING: {
ident *id = nullptr;
any_value &arg = args[numargs - 1];
switch (get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
2021-03-23 23:29:32 +01:00
arg = static_cast<alias *>(id)->get_value();
arg.force_str();
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2021-03-23 23:29:32 +01:00
arg.set_str(static_cast<string_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_IVAR:
2021-03-23 23:29:32 +01:00
arg.set_int(static_cast<integer_var *>(id)->get_value());
arg.force_str();
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2021-03-23 23:29:32 +01:00
arg.set_float(static_cast<float_var *>(id)->get_value());
arg.force_str();
2016-08-17 04:57:53 +02:00
continue;
case ID_UNKNOWN:
2016-08-17 04:57:53 +02:00
arg.set_str("");
continue;
default:
continue;
}
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP | BC_RET_STRING:
args[numargs] = get_lookup_id(cs, op)->get_value();
args[numargs++].force_str();
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_ARG | BC_RET_STRING: {
alias *a = get_lookuparg_id(cs, op);
2016-08-18 00:18:36 +02:00
if (!a) {
2016-08-17 04:57:53 +02:00
args[numargs++].set_str("");
} else {
args[numargs] = a->get_value();
args[numargs++].force_str();
2016-08-17 04:57:53 +02:00
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_U | BC_RET_INT: {
ident *id = nullptr;
any_value &arg = args[numargs - 1];
switch (get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
2016-08-31 20:18:53 +02:00
arg.set_int(
2021-03-23 23:29:32 +01:00
static_cast<alias *>(id)->get_value().get_int()
2016-08-31 20:18:53 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2021-03-23 01:45:35 +01:00
arg.set_int(parse_int(
2021-03-23 23:29:32 +01:00
static_cast<string_var *>(id)->get_value()
));
2016-08-17 04:57:53 +02:00
continue;
case ID_IVAR:
2021-03-23 23:29:32 +01:00
arg.set_int(static_cast<integer_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2016-08-18 04:14:55 +02:00
arg.set_int(
2021-03-23 23:29:32 +01:00
integer_type(static_cast<float_var *>(id)->get_value())
2016-08-18 04:14:55 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
case ID_UNKNOWN:
2016-08-17 04:57:53 +02:00
arg.set_int(0);
continue;
default:
continue;
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP | BC_RET_INT:
2016-08-17 04:57:53 +02:00
args[numargs++].set_int(
2021-03-23 23:29:32 +01:00
get_lookup_id(cs, op)->get_value().get_int()
2016-08-17 04:57:53 +02:00
);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_ARG | BC_RET_INT: {
alias *a = get_lookuparg_id(cs, op);
2016-08-18 00:18:36 +02:00
if (!a) {
2016-08-17 04:57:53 +02:00
args[numargs++].set_int(0);
} else {
2016-08-31 20:18:53 +02:00
args[numargs++].set_int(a->get_value().get_int());
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_U | BC_RET_FLOAT: {
ident *id = nullptr;
any_value &arg = args[numargs - 1];
switch (get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
arg.set_float(
2021-03-23 23:29:32 +01:00
static_cast<alias *>(id)->get_value().get_float()
);
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2021-03-23 01:45:35 +01:00
arg.set_float(parse_float(
2021-03-23 23:29:32 +01:00
static_cast<string_var *>(id)->get_value()
));
2016-08-17 04:57:53 +02:00
continue;
case ID_IVAR:
2021-03-23 23:29:32 +01:00
arg.set_float(float_type(
static_cast<integer_var *>(id)->get_value()
));
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2016-08-18 04:14:55 +02:00
arg.set_float(
2021-03-23 23:29:32 +01:00
static_cast<float_var *>(id)->get_value()
2016-08-18 04:14:55 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
case ID_UNKNOWN:
2021-03-23 23:29:32 +01:00
arg.set_float(float_type(0));
2016-08-17 04:57:53 +02:00
continue;
default:
continue;
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP | BC_RET_FLOAT:
2016-08-17 04:57:53 +02:00
args[numargs++].set_float(
2021-03-23 23:29:32 +01:00
get_lookup_id(cs, op)->get_value().get_float()
2016-08-17 04:57:53 +02:00
);
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_ARG | BC_RET_FLOAT: {
alias *a = get_lookuparg_id(cs, op);
2016-08-18 00:18:36 +02:00
if (!a) {
2021-03-23 23:29:32 +01:00
args[numargs++].set_float(float_type(0));
2016-08-17 04:57:53 +02:00
} else {
2016-08-31 20:18:53 +02:00
args[numargs++].set_float(a->get_value().get_float());
2016-08-17 04:57:53 +02:00
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_U | BC_RET_NULL: {
ident *id = nullptr;
any_value &arg = args[numargs - 1];
switch (get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
2021-03-23 23:29:32 +01:00
static_cast<alias *>(id)->get_value().get_val(arg);
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2021-03-23 23:29:32 +01:00
arg.set_str(static_cast<string_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_IVAR:
2021-03-23 23:29:32 +01:00
arg.set_int(static_cast<integer_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2016-08-18 04:14:55 +02:00
arg.set_float(
2021-03-23 23:29:32 +01:00
static_cast<float_var *>(id)->get_value()
2016-08-18 04:14:55 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
case ID_UNKNOWN:
2021-03-18 20:55:14 +01:00
arg.set_none();
2016-08-17 04:57:53 +02:00
continue;
default:
continue;
}
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP | BC_RET_NULL:
get_lookup_id(cs, op)->get_value().get_val(args[numargs++]);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_ARG | BC_RET_NULL: {
alias *a = get_lookuparg_id(cs, op);
2016-08-18 00:18:36 +02:00
if (!a) {
2021-03-18 20:55:14 +01:00
args[numargs++].set_none();
2016-08-17 04:57:53 +02:00
} else {
2016-08-31 20:18:53 +02:00
a->get_value().get_val(args[numargs++]);
2016-08-17 04:57:53 +02:00
}
continue;
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_MU | BC_RET_STRING: {
ident *id = nullptr;
any_value &arg = args[numargs - 1];
switch (get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
2021-03-23 23:29:32 +01:00
arg = static_cast<alias *>(id)->get_value();
arg.force_str();
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2021-03-23 23:29:32 +01:00
arg.set_str(static_cast<string_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_IVAR:
2021-03-23 23:29:32 +01:00
arg.set_int(static_cast<integer_var *>(id)->get_value());
arg.force_str();
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2021-03-23 23:29:32 +01:00
arg.set_float(static_cast<float_var *>(id)->get_value());
arg.force_str();
2016-08-17 04:57:53 +02:00
continue;
case ID_UNKNOWN:
arg.set_str("");
2016-08-17 04:57:53 +02:00
continue;
default:
continue;
}
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_M | BC_RET_STRING:
args[numargs] = get_lookup_id(cs, op)->get_value();
args[numargs++].force_str();
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_MARG | BC_RET_STRING: {
alias *a = get_lookuparg_id(cs, op);
2016-08-18 00:18:36 +02:00
if (!a) {
args[numargs++].set_str("");
2016-08-17 04:57:53 +02:00
} else {
args[numargs] = a->get_value();
args[numargs++].force_str();
2016-08-17 04:57:53 +02:00
}
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_MU | BC_RET_NULL: {
ident *id = nullptr;
any_value &arg = args[numargs - 1];
switch (get_lookupu_type(cs, arg, id, op)) {
case ID_ALIAS:
2021-03-23 23:29:32 +01:00
static_cast<alias *>(id)->get_cval(arg);
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2021-03-23 23:29:32 +01:00
arg.set_str(static_cast<string_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_IVAR:
2021-03-23 23:29:32 +01:00
arg.set_int(static_cast<integer_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2021-03-23 23:29:32 +01:00
arg.set_float(static_cast<float_var *>(id)->get_value());
2016-08-17 04:57:53 +02:00
continue;
case ID_UNKNOWN:
2021-03-18 20:55:14 +01:00
arg.set_none();
2016-08-17 04:57:53 +02:00
continue;
default:
continue;
}
}
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_M | BC_RET_NULL:
get_lookup_id(cs, op)->get_cval(args[numargs++]);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_LOOKUP_MARG | BC_RET_NULL: {
alias *a = get_lookuparg_id(cs, op);
2016-08-18 00:18:36 +02:00
if (!a) {
2021-03-18 20:55:14 +01:00
args[numargs++].set_none();
2016-08-17 04:57:53 +02:00
} else {
2016-08-18 00:18:36 +02:00
a->get_cval(args[numargs++]);
2016-08-17 04:57:53 +02:00
}
continue;
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
case BC_INST_SVAR | BC_RET_STRING:
case BC_INST_SVAR | BC_RET_NULL:
args[numargs++].set_str(static_cast<string_var *>(
cs.p_state->identmap[op >> 8]
)->get_value());
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_SVAR | BC_RET_INT:
args[numargs++].set_int(parse_int(static_cast<string_var *>(
cs.p_state->identmap[op >> 8]
)->get_value()));
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_SVAR | BC_RET_FLOAT:
args[numargs++].set_float(parse_float(static_cast<string_var *>(
cs.p_state->identmap[op >> 8]
)->get_value()));
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_SVAR1:
2016-08-17 23:29:31 +02:00
cs.set_var_str_checked(
2021-03-23 23:29:32 +01:00
static_cast<string_var *>(cs.p_state->identmap[op >> 8]),
2021-03-17 21:57:47 +01:00
args[--numargs].get_str()
2016-08-17 23:29:31 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_IVAR | BC_RET_INT:
case BC_INST_IVAR | BC_RET_NULL:
args[numargs++].set_int(static_cast<integer_var *>(
cs.p_state->identmap[op >> 8]
)->get_value());
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_IVAR | BC_RET_STRING:
args[numargs].set_int(static_cast<integer_var *>(
cs.p_state->identmap[op >> 8]
)->get_value());
args[numargs++].force_str();
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_IVAR | BC_RET_FLOAT:
args[numargs++].set_float(float_type(static_cast<integer_var *>(
cs.p_state->identmap[op >> 8]
)->get_value()));
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_IVAR1:
2016-08-17 23:29:31 +02:00
cs.set_var_int_checked(
2021-03-23 23:29:32 +01:00
static_cast<integer_var *>(cs.p_state->identmap[op >> 8]),
2016-08-30 22:55:35 +02:00
args[--numargs].get_int()
2016-08-17 23:29:31 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_IVAR2:
2016-08-17 04:57:53 +02:00
numargs -= 2;
cs.set_var_int_checked(
2021-03-23 23:29:32 +01:00
static_cast<integer_var *>(cs.p_state->identmap[op >> 8]),
2016-08-30 22:55:35 +02:00
(args[numargs].get_int() << 16)
| (args[numargs + 1].get_int() << 8)
);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_IVAR3:
2016-08-17 04:57:53 +02:00
numargs -= 3;
cs.set_var_int_checked(
2021-03-23 23:29:32 +01:00
static_cast<integer_var *>(cs.p_state->identmap[op >> 8]),
2016-08-30 22:55:35 +02:00
(args[numargs].get_int() << 16)
| (args[numargs + 1].get_int() << 8)
| (args[numargs + 2].get_int()));
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_FVAR | BC_RET_FLOAT:
case BC_INST_FVAR | BC_RET_NULL:
args[numargs++].set_float(static_cast<float_var *>(
cs.p_state->identmap[op >> 8]
)->get_value());
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FVAR | BC_RET_STRING:
args[numargs].set_int(static_cast<float_var *>(
cs.p_state->identmap[op >> 8]
)->get_value());
args[numargs++].force_str();
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FVAR | BC_RET_INT:
args[numargs++].set_int(int(static_cast<float_var *>(
cs.p_state->identmap[op >> 8]
)->get_value()));
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_FVAR1:
2016-08-17 23:29:31 +02:00
cs.set_var_float_checked(
2021-03-23 23:29:32 +01:00
static_cast<float_var *>(cs.p_state->identmap[op >> 8]),
2016-08-30 22:55:35 +02:00
args[--numargs].get_float()
2016-08-17 23:29:31 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_COM | BC_RET_NULL:
case BC_INST_COM | BC_RET_STRING:
case BC_INST_COM | BC_RET_FLOAT:
case BC_INST_COM | BC_RET_INT: {
command_impl *id = static_cast<command_impl *>(
cs.p_state->identmap[op >> 8]
);
int offset = numargs - id->get_num_args();
2021-03-18 20:55:14 +01:00
result.force_none();
2021-03-23 23:29:32 +01:00
id->call(cs, std::span<any_value>{
2021-03-20 05:41:25 +01:00
&args[0] + offset, std::size_t(id->get_num_args())
}, result);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
numargs = offset;
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_COM_V | BC_RET_NULL:
case BC_INST_COM_V | BC_RET_STRING:
case BC_INST_COM_V | BC_RET_FLOAT:
case BC_INST_COM_V | BC_RET_INT: {
command_impl *id = static_cast<command_impl *>(
cs.p_state->identmap[op >> 13]
);
2021-03-20 05:41:25 +01:00
std::size_t callargs = (op >> 8) & 0x1F,
offset = numargs - callargs;
2021-03-18 20:55:14 +01:00
result.force_none();
2021-03-21 03:07:01 +01:00
id->call(cs, std::span{&args[offset], callargs}, result);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
numargs = offset;
2016-08-12 18:38:43 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_COM_C | BC_RET_NULL:
case BC_INST_COM_C | BC_RET_STRING:
case BC_INST_COM_C | BC_RET_FLOAT:
case BC_INST_COM_C | BC_RET_INT: {
command_impl *id = static_cast<command_impl *>(
cs.p_state->identmap[op >> 13]
);
2021-03-20 05:41:25 +01:00
std::size_t callargs = (op >> 8) & 0x1F,
offset = numargs - callargs;
2021-03-18 20:55:14 +01:00
result.force_none();
2016-08-17 04:57:53 +02:00
{
2021-03-23 23:29:32 +01:00
any_value tv{cs};
tv.set_str(concat_values(cs, std::span{
2021-03-20 05:41:25 +01:00
&args[offset], callargs
}, " "));
2021-03-23 23:29:32 +01:00
id->call(cs, std::span<any_value>{&tv, 1}, result);
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
numargs = offset;
2016-08-17 04:57:53 +02:00
continue;
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_CONC | BC_RET_NULL:
case BC_INST_CONC | BC_RET_STRING:
case BC_INST_CONC | BC_RET_FLOAT:
case BC_INST_CONC | BC_RET_INT:
case BC_INST_CONC_W | BC_RET_NULL:
case BC_INST_CONC_W | BC_RET_STRING:
case BC_INST_CONC_W | BC_RET_FLOAT:
case BC_INST_CONC_W | BC_RET_INT: {
2021-03-20 05:41:25 +01:00
std::size_t numconc = op >> 8;
2021-03-23 23:29:32 +01:00
auto buf = concat_values(
2021-03-20 05:41:25 +01:00
cs, std::span{&args[numargs - numconc], numconc},
2021-03-23 23:29:32 +01:00
((op & BC_INST_OP_MASK) == BC_INST_CONC) ? " " : ""
2016-08-17 04:57:53 +02:00
);
numargs = numargs - numconc;
args[numargs].set_str(buf);
2021-03-23 23:29:32 +01:00
force_arg(args[numargs], op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
numargs++;
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_CONC_M | BC_RET_NULL:
case BC_INST_CONC_M | BC_RET_STRING:
case BC_INST_CONC_M | BC_RET_FLOAT:
case BC_INST_CONC_M | BC_RET_INT: {
2021-03-20 05:41:25 +01:00
std::size_t numconc = op >> 8;
2021-03-23 23:29:32 +01:00
auto buf = concat_values(
2021-03-20 05:41:25 +01:00
cs, std::span{&args[numargs - numconc], numconc}
2016-08-17 04:57:53 +02:00
);
numargs = numargs - numconc;
result.set_str(buf);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-12 18:38:43 +02:00
continue;
}
2016-08-17 04:57:53 +02:00
2021-03-23 23:29:32 +01:00
case BC_INST_ALIAS:
static_cast<alias_impl *>(
cs.p_state->identmap[op >> 8]
)->set_alias(cs, args[--numargs]);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_ALIAS_ARG:
static_cast<alias_impl *>(
cs.p_state->identmap[op >> 8]
)->set_arg(cs, args[--numargs]);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_ALIAS_U:
2016-08-17 04:57:53 +02:00
numargs -= 2;
2016-09-06 22:57:10 +02:00
cs.set_alias(
2017-01-25 01:57:33 +01:00
args[numargs].get_str(), std::move(args[numargs + 1])
2016-09-06 22:57:10 +02:00
);
2016-08-17 04:57:53 +02:00
continue;
2021-03-23 23:29:32 +01:00
case BC_INST_CALL | BC_RET_NULL:
case BC_INST_CALL | BC_RET_STRING:
case BC_INST_CALL | BC_RET_FLOAT:
case BC_INST_CALL | BC_RET_INT: {
2021-03-18 20:55:14 +01:00
result.force_none();
2021-03-23 23:29:32 +01:00
ident *id = cs.p_state->identmap[op >> 13];
2016-08-17 04:57:53 +02:00
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
2021-03-23 23:29:32 +01:00
if (id->get_flags() & IDENT_FLAG_UNKNOWN) {
force_arg(result, op & BC_INST_RET_MASK);
throw error(
2021-03-20 08:22:15 +01:00
cs, "unknown command: %s", id->get_name().data()
2016-09-15 20:55:58 +02:00
);
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
call_alias(
cs, static_cast<alias *>(id), &args[0], result, callargs,
2016-08-18 00:06:39 +02:00
numargs, offset, 0, op
2016-08-17 04:57:53 +02:00
);
2016-08-12 18:38:43 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_CALL_ARG | BC_RET_NULL:
case BC_INST_CALL_ARG | BC_RET_STRING:
case BC_INST_CALL_ARG | BC_RET_FLOAT:
case BC_INST_CALL_ARG | BC_RET_INT: {
2021-03-18 20:55:14 +01:00
result.force_none();
2021-03-23 23:29:32 +01:00
ident *id = cs.p_state->identmap[op >> 13];
2016-08-17 04:57:53 +02:00
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
if (!ident_is_used_arg(id, cs)) {
numargs = offset;
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
call_alias(
cs, static_cast<alias *>(id), &args[0], result, callargs,
2016-08-18 00:06:39 +02:00
numargs, offset, 0, op
2016-08-17 04:57:53 +02:00
);
continue;
}
2021-03-23 23:29:32 +01:00
case BC_INST_CALL_U | BC_RET_NULL:
case BC_INST_CALL_U | BC_RET_STRING:
case BC_INST_CALL_U | BC_RET_FLOAT:
case BC_INST_CALL_U | BC_RET_INT: {
2016-08-17 04:57:53 +02:00
int callargs = op >> 8, offset = numargs - callargs;
2021-03-23 23:29:32 +01:00
any_value &idarg = args[offset - 1];
if (idarg.get_type() != value_type::STRING) {
2016-08-17 04:57:53 +02:00
litval:
2017-01-25 01:57:33 +01:00
result = std::move(idarg);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
numargs = offset - 1;
2016-08-17 04:57:53 +02:00
continue;
}
2021-03-17 21:57:47 +01:00
auto idn = idarg.get_str();
2021-03-23 23:29:32 +01:00
ident *id = cs.get_ident(idn);
2016-08-17 04:57:53 +02:00
if (!id) {
noid:
if (!is_valid_name(idn)) {
2016-08-17 04:57:53 +02:00
goto litval;
}
2021-03-18 20:55:14 +01:00
result.force_none();
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2021-03-20 08:22:15 +01:00
std::string_view ids{idn};
2021-03-23 23:29:32 +01:00
throw error(
2021-03-20 08:22:15 +01:00
cs, "unknown command: %s", ids.data()
2016-09-15 20:55:58 +02:00
);
2016-08-17 04:57:53 +02:00
}
2021-03-18 20:55:14 +01:00
result.force_none();
2021-03-21 02:41:04 +01:00
switch (id->get_raw_type()) {
2016-08-17 04:57:53 +02:00
default:
2021-03-23 23:29:32 +01:00
if (!ident_has_cb(id)) {
numargs = offset - 1;
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
}
/* fallthrough */
case ID_COMMAND:
2016-08-17 23:04:43 +02:00
callcommand(
2021-03-23 23:29:32 +01:00
cs, static_cast<command_impl *>(id),
2021-03-21 02:41:04 +01:00
&args[offset], result, callargs
2016-08-17 23:04:43 +02:00
);
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
numargs = offset - 1;
continue;
case ID_LOCAL: {
2021-03-24 02:21:27 +01:00
valarray<ident_stack, MAX_ARGUMENTS> locals{cs};
2017-01-25 02:09:50 +01:00
for (size_t j = 0; j < size_t(callargs); ++j) {
push_alias(
cs, args[offset + j].force_ident(cs), locals[j]
);
2016-08-17 04:57:53 +02:00
}
2021-03-23 01:11:21 +01:00
call_with_cleanup([&]() {
code = runcode(cs, code, result);
}, [&]() {
2017-01-25 02:09:50 +01:00
for (size_t j = 0; j < size_t(callargs); ++j) {
2021-03-23 23:29:32 +01:00
pop_alias(args[offset + j].get_ident());
}
});
2016-09-23 21:06:44 +02:00
return code;
2016-08-17 04:57:53 +02:00
}
case ID_IVAR:
2016-08-17 04:57:53 +02:00
if (callargs <= 0) {
2021-03-23 23:29:32 +01:00
cs.print_var(*static_cast<global_var *>(id));
2016-08-17 04:57:53 +02:00
} else {
cs.set_var_int_checked(
2021-03-23 23:29:32 +01:00
static_cast<integer_var *>(id),
2021-03-20 05:41:25 +01:00
std::span{&args[offset], std::size_t(callargs)}
2016-08-17 04:57:53 +02:00
);
}
numargs = offset - 1;
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
case ID_FVAR:
2016-08-17 04:57:53 +02:00
if (callargs <= 0) {
2021-03-23 23:29:32 +01:00
cs.print_var(*static_cast<global_var *>(id));
2016-08-17 04:57:53 +02:00
} else {
cs.set_var_float_checked(
2021-03-23 23:29:32 +01:00
static_cast<float_var *>(id),
2016-08-17 23:29:31 +02:00
args[offset].force_float()
2016-08-17 04:57:53 +02:00
);
}
numargs = offset - 1;
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
case ID_SVAR:
2016-08-17 04:57:53 +02:00
if (callargs <= 0) {
2021-03-23 23:29:32 +01:00
cs.print_var(*static_cast<global_var *>(id));
2016-08-17 04:57:53 +02:00
} else {
2016-08-17 23:29:31 +02:00
cs.set_var_str_checked(
2021-03-23 23:29:32 +01:00
static_cast<string_var *>(id),
2016-08-17 23:29:31 +02:00
args[offset].force_str()
);
2016-08-17 04:57:53 +02:00
}
numargs = offset - 1;
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
case ID_ALIAS: {
2021-03-23 23:29:32 +01:00
alias *a = static_cast<alias *>(id);
2016-08-17 04:57:53 +02:00
if (
2021-03-24 02:21:27 +01:00
(a->get_index() < MAX_ARGUMENTS) &&
!ident_is_used_arg(a, cs)
2016-08-17 04:57:53 +02:00
) {
numargs = offset - 1;
2021-03-23 23:29:32 +01:00
force_arg(result, op & BC_INST_RET_MASK);
2016-08-17 04:57:53 +02:00
continue;
}
2021-03-23 23:29:32 +01:00
if (a->get_value().get_type() == value_type::NONE) {
2016-08-17 04:57:53 +02:00
goto noid;
}
2021-03-23 23:29:32 +01:00
call_alias(
cs, a, &args[0], result, callargs, numargs,
2016-08-17 04:57:53 +02:00
offset, 1, op
);
continue;
2016-08-18 00:06:39 +02:00
}
2016-08-17 04:57:53 +02:00
}
}
2016-08-12 18:38:43 +02:00
}
}
return code;
}
2021-03-23 23:29:32 +01:00
void state::run(bcode *code, any_value &ret) {
2017-01-25 02:10:17 +01:00
runcode(*this, reinterpret_cast<uint32_t *>(code), ret);
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
static void do_run(
state &cs, std::string_view file, std::string_view code,
any_value &ret
) {
2021-03-23 23:29:32 +01:00
codegen_state gs(cs);
2016-09-22 01:44:35 +02:00
gs.src_name = file;
2016-08-12 18:38:43 +02:00
gs.code.reserve(64);
2021-03-23 23:29:32 +01:00
gs.gen_main(code, VAL_ANY);
gs.done();
uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size());
2017-01-25 01:18:29 +01:00
memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t));
bcode_incr(cbuf);
2017-01-25 01:18:29 +01:00
runcode(cs, cbuf + 1, ret);
bcode_decr(cbuf);
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
void state::run(std::string_view code, any_value &ret) {
do_run(*this, std::string_view{}, code, ret);
}
2021-03-23 23:29:32 +01:00
void state::run(
std::string_view code, any_value &ret, std::string_view source
) {
2021-03-23 23:29:32 +01:00
do_run(*this, source, code, ret);
}
2021-03-23 23:29:32 +01:00
void state::run(ident *id, std::span<any_value> args, any_value &ret) {
2016-08-12 18:38:43 +02:00
int nargs = int(args.size());
2021-03-18 20:55:14 +01:00
ret.set_none();
2016-09-23 21:04:52 +02:00
RunDepthRef level{*this}; /* incr and decr on scope exit */
if (id) {
2016-08-18 03:53:51 +02:00
switch (id->get_type()) {
2016-08-17 04:57:53 +02:00
default:
2021-03-23 23:29:32 +01:00
if (!ident_has_cb(id)) {
2016-08-17 04:57:53 +02:00
break;
}
/* fallthrough */
2021-03-23 23:29:32 +01:00
case ident_type::COMMAND:
if (nargs < static_cast<command_impl *>(id)->get_num_args()) {
2021-03-24 02:21:27 +01:00
valarray<any_value, MAX_ARGUMENTS> buf{*this};
for (std::size_t i = 0; i < args.size(); ++i) {
buf[i] = args[i];
}
2016-08-17 23:04:43 +02:00
callcommand(
2021-03-23 23:29:32 +01:00
*this, static_cast<command_impl *>(id), &buf[0], ret,
2016-08-17 23:04:43 +02:00
nargs, false
);
2016-08-17 04:57:53 +02:00
} else {
2016-08-17 23:04:43 +02:00
callcommand(
2021-03-23 23:29:32 +01:00
*this, static_cast<command_impl *>(id), &args[0],
2016-08-17 23:04:43 +02:00
ret, nargs, false
);
2016-08-17 04:57:53 +02:00
}
nargs = 0;
break;
2021-03-23 23:29:32 +01:00
case ident_type::IVAR:
2016-08-17 04:57:53 +02:00
if (args.empty()) {
2021-03-23 23:29:32 +01:00
print_var(*static_cast<global_var *>(id));
2016-08-17 04:57:53 +02:00
} else {
2021-03-23 23:29:32 +01:00
set_var_int_checked(static_cast<integer_var *>(id), args);
2016-08-17 04:57:53 +02:00
}
break;
2021-03-23 23:29:32 +01:00
case ident_type::FVAR:
2016-08-17 04:57:53 +02:00
if (args.empty()) {
2021-03-23 23:29:32 +01:00
print_var(*static_cast<global_var *>(id));
2016-08-17 04:57:53 +02:00
} else {
2016-08-17 23:29:31 +02:00
set_var_float_checked(
2021-03-23 23:29:32 +01:00
static_cast<float_var *>(id), args[0].force_float()
2016-08-17 23:29:31 +02:00
);
2016-08-17 04:57:53 +02:00
}
break;
2021-03-23 23:29:32 +01:00
case ident_type::SVAR:
2016-08-17 04:57:53 +02:00
if (args.empty()) {
2021-03-23 23:29:32 +01:00
print_var(*static_cast<global_var *>(id));
2016-08-17 04:57:53 +02:00
} else {
2016-08-17 23:29:31 +02:00
set_var_str_checked(
2021-03-23 23:29:32 +01:00
static_cast<string_var *>(id), args[0].force_str()
2016-08-17 23:29:31 +02:00
);
2016-08-17 04:57:53 +02:00
}
break;
2021-03-23 23:29:32 +01:00
case ident_type::ALIAS: {
alias *a = static_cast<alias *>(id);
2016-09-10 19:54:55 +02:00
if (
2021-03-24 02:21:27 +01:00
(a->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(a, *this)
2016-09-10 19:54:55 +02:00
) {
break;
2016-08-17 04:57:53 +02:00
}
2021-03-23 23:29:32 +01:00
if (a->get_value().get_type() == value_type::NONE) {
2016-08-17 04:57:53 +02:00
break;
}
2021-03-23 23:29:32 +01:00
call_alias(
*this, a, &args[0], ret, nargs, nargs, 0, 0, BC_RET_NULL
2016-08-17 04:57:53 +02:00
);
break;
2016-08-18 00:06:39 +02:00
}
2016-08-12 18:38:43 +02:00
}
2016-08-17 04:57:53 +02:00
}
2016-08-12 18:38:43 +02:00
}
2021-03-23 23:29:32 +01:00
any_value state::run(bcode *code) {
any_value ret{*this};
run(code, ret);
return ret;
2016-08-18 20:34:24 +02:00
}
2021-03-23 23:29:32 +01:00
any_value state::run(std::string_view code) {
any_value ret{*this};
run(code, ret);
return ret;
}
2021-03-23 23:29:32 +01:00
any_value state::run(std::string_view code, std::string_view source) {
any_value ret{*this};
run(code, ret, source);
return ret;
2016-08-18 20:34:24 +02:00
}
2021-03-23 23:29:32 +01:00
any_value state::run(ident *id, std::span<any_value> args) {
any_value ret{*this};
run(id, args, ret);
return ret;
2016-08-18 20:34:24 +02:00
}
2021-03-23 23:29:32 +01:00
loop_state state::run_loop(bcode *code, any_value &ret) {
++p_tstate->loop_level;
2016-09-14 23:24:13 +02:00
try {
run(code, ret);
2021-03-24 02:21:27 +01:00
} catch (break_exception) {
--p_tstate->loop_level;
2021-03-23 23:29:32 +01:00
return loop_state::BREAK;
2021-03-24 02:21:27 +01:00
} catch (continue_exception) {
--p_tstate->loop_level;
2021-03-23 23:29:32 +01:00
return loop_state::CONTINUE;
2016-09-14 23:24:13 +02:00
} catch (...) {
--p_tstate->loop_level;
2016-09-14 23:24:13 +02:00
throw;
}
2021-03-23 23:29:32 +01:00
return loop_state::NORMAL;
2016-09-14 23:24:13 +02:00
}
2021-03-23 23:29:32 +01:00
loop_state state::run_loop(bcode *code) {
any_value ret{*this};
2016-09-14 23:24:13 +02:00
return run_loop(code, ret);
}
bool state::is_in_loop() const {
return !!p_tstate->loop_level;
}
2021-03-23 23:32:25 +01:00
} /* namespace cubescript */