simplify exec_alias more
parent
72c3ecedb1
commit
b5127c52bf
|
@ -329,17 +329,16 @@ LIBCUBESCRIPT_EXPORT bool alias::is_arg() const {
|
||||||
LIBCUBESCRIPT_EXPORT any_value alias::call(
|
LIBCUBESCRIPT_EXPORT any_value alias::call(
|
||||||
span_type<any_value> args, state &cs
|
span_type<any_value> args, state &cs
|
||||||
) {
|
) {
|
||||||
any_value ret{};
|
|
||||||
auto &ts = state_p{cs}.ts();
|
auto &ts = state_p{cs}.ts();
|
||||||
if (is_arg() && !ident_is_used_arg(this, ts)) {
|
if (is_arg() && !ident_is_used_arg(this, ts)) {
|
||||||
return ret;
|
return any_value{};
|
||||||
}
|
}
|
||||||
auto nargs = args.size();
|
auto nargs = args.size();
|
||||||
auto &ast = ts.get_astack(this);
|
auto &ast = ts.get_astack(this);
|
||||||
if (ast.node->val_s.type() != value_type::NONE) {
|
if (ast.node->val_s.type() != value_type::NONE) {
|
||||||
exec_alias(ts, this, &args[0], ret, nargs, nargs, 0, 0, ast);
|
return exec_alias(ts, this, &args[0], nargs, ast);
|
||||||
}
|
}
|
||||||
return ret;
|
return any_value{};
|
||||||
}
|
}
|
||||||
|
|
||||||
LIBCUBESCRIPT_EXPORT std::string_view command::args() const {
|
LIBCUBESCRIPT_EXPORT std::string_view command::args() const {
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace cubescript {
|
||||||
|
|
||||||
struct ident_level {
|
struct ident_level {
|
||||||
ident &id;
|
ident &id;
|
||||||
std::bitset<MAX_ARGUMENTS> usedargs{};
|
argset usedargs{};
|
||||||
|
|
||||||
ident_level(ident &i): id{i} {};
|
ident_level(ident &i): id{i} {};
|
||||||
};
|
};
|
||||||
|
|
37
src/cs_vm.cc
37
src/cs_vm.cc
|
@ -121,12 +121,12 @@ void exec_command(
|
||||||
res.force_plain();
|
res.force_plain();
|
||||||
}
|
}
|
||||||
|
|
||||||
void exec_alias(
|
any_value exec_alias(
|
||||||
thread_state &ts, alias *a, any_value *args, any_value &result,
|
thread_state &ts, alias *a, any_value *args,
|
||||||
std::size_t callargs, std::size_t &nargs,
|
std::size_t callargs, alias_stack &astack
|
||||||
std::size_t offset, std::size_t skip, alias_stack &astack
|
|
||||||
) {
|
) {
|
||||||
/* excess arguments get ignored (make error maybe?) */
|
/* excess arguments get ignored (make error maybe?) */
|
||||||
|
any_value ret;
|
||||||
callargs = std::min(callargs, MAX_ARGUMENTS);
|
callargs = std::min(callargs, MAX_ARGUMENTS);
|
||||||
builtin_var *anargs = ts.istate->ivar_numargs;
|
builtin_var *anargs = ts.istate->ivar_numargs;
|
||||||
argset uargs{};
|
argset uargs{};
|
||||||
|
@ -137,7 +137,7 @@ void exec_alias(
|
||||||
);
|
);
|
||||||
auto &st = ts.idstack.emplace_back();
|
auto &st = ts.idstack.emplace_back();
|
||||||
ast.push(st);
|
ast.push(st);
|
||||||
st.val_s = std::move(args[offset + i]);
|
st.val_s = std::move(args[i]);
|
||||||
uargs[i] = true;
|
uargs[i] = true;
|
||||||
}
|
}
|
||||||
auto oldargs = anargs->value();
|
auto oldargs = anargs->value();
|
||||||
|
@ -146,7 +146,8 @@ void exec_alias(
|
||||||
any_value cv;
|
any_value cv;
|
||||||
cv.set_integer(integer_type(callargs));
|
cv.set_integer(integer_type(callargs));
|
||||||
anargs->set_raw_value(*ts.pstate, std::move(cv));
|
anargs->set_raw_value(*ts.pstate, std::move(cv));
|
||||||
ts.callstack.emplace_back(*a);
|
auto &lev = ts.callstack.emplace_back(*a);
|
||||||
|
lev.usedargs = std::move(uargs);
|
||||||
if (!astack.node->code) {
|
if (!astack.node->code) {
|
||||||
try {
|
try {
|
||||||
gen_state gs{ts};
|
gen_state gs{ts};
|
||||||
|
@ -181,16 +182,15 @@ void exec_alias(
|
||||||
tss.idstack.resize(nids);
|
tss.idstack.resize(nids);
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
vm_exec(ts, bcode_p{coderef}.get()->raw(), result);
|
vm_exec(ts, bcode_p{coderef}.get()->raw(), ret);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
cleanup(ts, callargs, noff, oldflags);
|
cleanup(ts, callargs, noff, oldflags);
|
||||||
anargs->set_raw_value(*ts.pstate, std::move(oldargs));
|
anargs->set_raw_value(*ts.pstate, std::move(oldargs));
|
||||||
nargs = offset - skip;
|
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
cleanup(ts, callargs, noff, oldflags);
|
cleanup(ts, callargs, noff, oldflags);
|
||||||
anargs->set_raw_value(*ts.pstate, std::move(oldargs));
|
anargs->set_raw_value(*ts.pstate, std::move(oldargs));
|
||||||
nargs = offset - skip;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
any_value exec_code_with_args(thread_state &ts, bcode_ref const &body) {
|
any_value exec_code_with_args(thread_state &ts, bcode_ref const &body) {
|
||||||
|
@ -647,8 +647,7 @@ std::uint32_t *vm_exec(
|
||||||
result.force_none();
|
result.force_none();
|
||||||
ident *id = ts.istate->identmap[op >> 8];
|
ident *id = ts.istate->identmap[op >> 8];
|
||||||
std::size_t callargs = *code++;
|
std::size_t callargs = *code++;
|
||||||
std::size_t nnargs = args.size();
|
std::size_t offset = args.size() - callargs;
|
||||||
std::size_t offset = nnargs - callargs;
|
|
||||||
auto *imp = static_cast<alias_impl *>(id);
|
auto *imp = static_cast<alias_impl *>(id);
|
||||||
if (imp->is_arg()) {
|
if (imp->is_arg()) {
|
||||||
if (!ident_is_used_arg(id, ts)) {
|
if (!ident_is_used_arg(id, ts)) {
|
||||||
|
@ -662,17 +661,14 @@ std::uint32_t *vm_exec(
|
||||||
cs, "unknown command: %s", id->name().data()
|
cs, "unknown command: %s", id->name().data()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
exec_alias(
|
result = exec_alias(ts, imp, &args[offset], callargs, ast);
|
||||||
ts, imp, &args[0], result, callargs, nnargs, offset, 0, ast
|
args.resize(offset);
|
||||||
);
|
|
||||||
args.resize(nnargs);
|
|
||||||
goto use_result;
|
goto use_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
case BC_INST_CALL_U: {
|
case BC_INST_CALL_U: {
|
||||||
std::size_t callargs = op >> 8;
|
std::size_t callargs = op >> 8;
|
||||||
std::size_t nnargs = args.size();
|
std::size_t offset = args.size() - callargs;
|
||||||
std::size_t offset = nnargs - callargs;
|
|
||||||
any_value &idarg = args[offset - 1];
|
any_value &idarg = args[offset - 1];
|
||||||
if (idarg.type() != value_type::STRING) {
|
if (idarg.type() != value_type::STRING) {
|
||||||
litval:
|
litval:
|
||||||
|
@ -761,11 +757,10 @@ noid:
|
||||||
if (ast.node->val_s.type() == value_type::NONE) {
|
if (ast.node->val_s.type() == value_type::NONE) {
|
||||||
goto noid;
|
goto noid;
|
||||||
}
|
}
|
||||||
exec_alias(
|
result = exec_alias(
|
||||||
ts, a, &args[0], result, callargs, nnargs,
|
ts, a, &args[offset], callargs, ast
|
||||||
offset, 1, ast
|
|
||||||
);
|
);
|
||||||
args.resize(nnargs);
|
args.resize(offset - 1);
|
||||||
goto use_result;
|
goto use_result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,9 @@ void exec_command(
|
||||||
any_value &res, std::size_t nargs, bool lookup = false
|
any_value &res, std::size_t nargs, bool lookup = false
|
||||||
);
|
);
|
||||||
|
|
||||||
void exec_alias(
|
any_value exec_alias(
|
||||||
thread_state &ts, alias *a, any_value *args, any_value &result,
|
thread_state &ts, alias *a, any_value *args,
|
||||||
std::size_t callargs, std::size_t &nargs, std::size_t offset,
|
std::size_t callargs, alias_stack &astack
|
||||||
std::size_t skip, alias_stack &astack
|
|
||||||
);
|
);
|
||||||
|
|
||||||
any_value exec_code_with_args(thread_state &ts, bcode_ref const &body);
|
any_value exec_code_with_args(thread_state &ts, bcode_ref const &body);
|
||||||
|
|
Loading…
Reference in New Issue