forked from OctaForge/libcubescript
more cleanups
parent
505bf1064a
commit
caa156f37e
96
command.cc
96
command.cc
|
@ -3295,6 +3295,64 @@ bool validateblock(const char *s) {
|
|||
/* standard lib */
|
||||
|
||||
void init_lib_base(CsState &cs) {
|
||||
cs.add_command("do", "e", [](CsState &cs, ostd::uint *body) {
|
||||
cs.run_ret(body);
|
||||
}, ID_DO);
|
||||
|
||||
cs.add_command("doargs", "e", [](CsState &cs, ostd::uint *body) {
|
||||
if (cs.stack != &cs.noalias)
|
||||
cs_do_args(cs, [&]() { cs.run_ret(body); });
|
||||
else
|
||||
cs.run_ret(body);
|
||||
}, ID_DOARGS);
|
||||
|
||||
cs.add_command("if", "tee", [](CsState &cs, TaggedValue *cond,
|
||||
ostd::uint *t, ostd::uint *f) {
|
||||
cs.run_ret(getbool(*cond) ? t : f);
|
||||
}, ID_IF);
|
||||
|
||||
cs.add_command("result", "T", [](CsState &cs, TaggedValue *v) {
|
||||
*cs.result = *v;
|
||||
v->type = VAL_NULL;
|
||||
}, ID_RESULT);
|
||||
|
||||
cs.add_command("!", "t", [](CsState &cs, TaggedValue *a) {
|
||||
cs.result->set_int(!getbool(*a));
|
||||
}, ID_NOT);
|
||||
|
||||
cs.add_command("&&", "E1V", [](CsState &cs, TaggedValue *args,
|
||||
int numargs) {
|
||||
if (!numargs)
|
||||
cs.result->set_int(1);
|
||||
else for (int i = 0; i < numargs; ++i) {
|
||||
if (i) cs.result->cleanup();
|
||||
if (args[i].type == VAL_CODE)
|
||||
cs.run_ret(args[i].code);
|
||||
else
|
||||
*cs.result = args[i];
|
||||
if (!getbool(*cs.result)) break;
|
||||
}
|
||||
}, ID_AND);
|
||||
|
||||
cs.add_command("||", "E1V", [](CsState &cs, TaggedValue *args,
|
||||
int numargs) {
|
||||
if (!numargs)
|
||||
cs.result->set_int(0);
|
||||
else for (int i = 0; i < numargs; ++i) {
|
||||
if (i) cs.result->cleanup();
|
||||
if (args[i].type == VAL_CODE)
|
||||
cs.run_ret(args[i].code);
|
||||
else
|
||||
*cs.result = args[i];
|
||||
if (getbool(*cs.result)) break;
|
||||
}
|
||||
}, ID_OR);
|
||||
|
||||
cs.add_command("?", "tTT", [](CsState &cs, TaggedValue *cond,
|
||||
TaggedValue *t, TaggedValue *f) {
|
||||
result(cs, *(getbool(*cond) ? t : f));
|
||||
});
|
||||
|
||||
cs_init_lib_base_var(cs);
|
||||
}
|
||||
|
||||
|
@ -3318,18 +3376,6 @@ const char *floatstr(float v) {
|
|||
#undef ICOMMANDSNAME
|
||||
#define ICOMMANDSNAME _stdcmd
|
||||
|
||||
ICOMMANDK(do, ID_DO, "e", (CsState &cs, ostd::uint *body), cs.run_ret(body));
|
||||
|
||||
static void doargs(CsState &cs, ostd::uint *body) {
|
||||
if (cs.stack != &cs.noalias) {
|
||||
cs_do_args(cs, [&]() { cs.run_ret(body); });
|
||||
} else cs.run_ret(body);
|
||||
}
|
||||
COMMANDK(doargs, ID_DOARGS, "e");
|
||||
|
||||
ICOMMANDK(if, ID_IF, "tee", (CsState &cs, TaggedValue *cond, ostd::uint *t, ostd::uint *f), cs.run_ret(getbool(*cond) ? t : f));
|
||||
ICOMMAND(?, "tTT", (CsState &cs, TaggedValue *cond, TaggedValue *t, TaggedValue *f), result(cs, *(getbool(*cond) ? t : f)));
|
||||
|
||||
ICOMMAND(pushif, "rTe", (CsState &cs, Ident *id, TaggedValue *v, ostd::uint *code), {
|
||||
if (id->type != ID_ALIAS || id->index < MAX_ARGUMENTS) return;
|
||||
if (getbool(*v)) {
|
||||
|
@ -3439,11 +3485,6 @@ void result(CsState &cs, const char *s) {
|
|||
cs.result->set_str(dup_ostr(s));
|
||||
}
|
||||
|
||||
ICOMMANDK(result, ID_RESULT, "T", (CsState &cs, TaggedValue *v), {
|
||||
*cs.result = *v;
|
||||
v->type = VAL_NULL;
|
||||
});
|
||||
|
||||
void format(CsState &cs, TaggedValue *args, int numargs) {
|
||||
ostd::Vector<char> s;
|
||||
const char *f = args[0].get_str();
|
||||
|
@ -4047,27 +4088,6 @@ CMPFCMD(>);
|
|||
CMPFCMD(<=);
|
||||
CMPFCMD(>=);
|
||||
|
||||
ICOMMANDK(!, ID_NOT, "t", (CsState &cs, TaggedValue *a), cs.result->set_int(getbool(*a) ? 0 : 1));
|
||||
ICOMMANDK(&&, ID_AND, "E1V", (CsState &cs, TaggedValue *args, int numargs), {
|
||||
if (!numargs) cs.result->set_int(1);
|
||||
else for (int i = 0; i < numargs; ++i) {
|
||||
if (i) cs.result->cleanup();
|
||||
if (args[i].type == VAL_CODE) cs.run_ret(args[i].code);
|
||||
else *cs.result = args[i];
|
||||
if (!getbool(*cs.result)) break;
|
||||
}
|
||||
});
|
||||
ICOMMANDK( ||, ID_OR, "E1V", (CsState &cs, TaggedValue *args, int numargs), {
|
||||
if (!numargs) cs.result->set_int(0);
|
||||
else for (int i = 0; i < numargs; ++i) {
|
||||
if (i) cs.result->cleanup();
|
||||
if (args[i].type == VAL_CODE) cs.run_ret(args[i].code);
|
||||
else *cs.result = args[i];
|
||||
if (getbool(*cs.result)) break;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
#define DIVCMD(name, fmt, type, op) MATHCMD(#name, fmt, type, { if(val2) op; else val = 0; }, 0, )
|
||||
|
||||
DIVCMD(div, i, int, val /= val2);
|
||||
|
|
10
command.hh
10
command.hh
|
@ -595,18 +595,12 @@ extern void printvar(Ident *id, int i);
|
|||
extern void printfvar(Ident *id, float f);
|
||||
extern void printsvar(Ident *id, const char *s);
|
||||
|
||||
#define COMMANDKN(name, type, fun, nargs) static bool __dummy_##fun = cstate.add_command(#name, nargs, (IdentFunc)fun, type)
|
||||
#define COMMANDK(name, type, nargs) COMMANDKN(name, type, name, nargs)
|
||||
#define COMMAND(name, nargs) COMMANDK(name, ID_COMMAND, nargs)
|
||||
#define COMMAND(name, nargs) static bool __dummy_##name = cstate.add_command(#name, nargs, (IdentFunc)name, ID_COMMAND)
|
||||
|
||||
#define ICOMMANDNAME(name) _icmd_##name
|
||||
#define ICOMMANDSNAME _icmds_
|
||||
#define ICOMMANDKNS(name, type, cmdname, nargs, proto, b) template<int N> struct cmdname; template<> struct cmdname<__LINE__> { static bool init; static void run proto; }; bool cmdname<__LINE__>::init = cstate.add_command(name, nargs, (IdentFunc)cmdname<__LINE__>::run, type); void cmdname<__LINE__>::run proto \
|
||||
#define ICOMMANDNS(name, cmdname, nargs, proto, b) template<int N> struct cmdname; template<> struct cmdname<__LINE__> { static bool init; static void run proto; }; bool cmdname<__LINE__>::init = cstate.add_command(name, nargs, (IdentFunc)cmdname<__LINE__>::run, ID_COMMAND); void cmdname<__LINE__>::run proto \
|
||||
{ b; }
|
||||
#define ICOMMANDKN(name, type, cmdname, nargs, proto, b) ICOMMANDKNS(#name, type, cmdname, nargs, proto, b)
|
||||
#define ICOMMANDK(name, type, nargs, proto, b) ICOMMANDKN(name, type, ICOMMANDNAME(name), nargs, proto, b)
|
||||
#define ICOMMANDKS(name, type, nargs, proto, b) ICOMMANDKNS(name, type, ICOMMANDSNAME, nargs, proto, b)
|
||||
#define ICOMMANDNS(name, cmdname, nargs, proto, b) ICOMMANDKNS(name, ID_COMMAND, cmdname, nargs, proto, b)
|
||||
#define ICOMMANDN(name, cmdname, nargs, proto, b) ICOMMANDNS(#name, cmdname, nargs, proto, b)
|
||||
#define ICOMMAND(name, nargs, proto, b) ICOMMANDN(name, ICOMMANDNAME(name), nargs, proto, b)
|
||||
#define ICOMMANDS(name, nargs, proto, b) ICOMMANDNS(name, ICOMMANDSNAME, nargs, proto, b)
|
||||
|
|
Loading…
Reference in New Issue