summaryrefslogtreecommitdiff
path: root/src/engine/command.cpp
diff options
context:
space:
mode:
authorxolatile2025-08-06 22:54:55 +0200
committerxolatile2025-08-06 22:54:55 +0200
commit0a1172b75f571685c264a8b9d8ee224bbf11381f (patch)
treed041fdc68a60f0ebb48a3852bbcce6d9432f83d5 /src/engine/command.cpp
parentaffde05dc07a94643f1fd2751b2b441f57f73d7d (diff)
downloadxolatile-badassbug-0a1172b75f571685c264a8b9d8ee224bbf11381f.tar.xz
xolatile-badassbug-0a1172b75f571685c264a8b9d8ee224bbf11381f.tar.zst
Please do not hate me, it makes sense...
Diffstat (limited to 'src/engine/command.cpp')
-rw-r--r--src/engine/command.cpp1301
1 files changed, 430 insertions, 871 deletions
diff --git a/src/engine/command.cpp b/src/engine/command.cpp
index 012b8c1..5248899 100644
--- a/src/engine/command.cpp
+++ b/src/engine/command.cpp
@@ -9,38 +9,31 @@ ident *dummyident = NULL;
int identflags = 0;
-enum
-{
+enum {
MAXARGS = 25,
MAXCOMARGS = 12
};
VARN(numargs, _numargs, MAXARGS, 0, 0);
-static inline void freearg(tagval &v)
-{
- switch(v.type)
- {
+static inline void freearg(tagval &v) {
+ switch(v.type) {
case VAL_STR: delete[] v.s; break;
case VAL_CODE: if(v.code[-1] == CODE_START) delete[] (uchar *)&v.code[-1]; break;
}
}
-static inline void forcenull(tagval &v)
-{
- switch(v.type)
- {
+static inline void forcenull(tagval &v) {
+ switch(v.type) {
case VAL_NULL: return;
}
freearg(v);
v.setnull();
}
-static inline float forcefloat(tagval &v)
-{
+static inline float forcefloat(tagval &v) {
float f = 0.0f;
- switch(v.type)
- {
+ switch(v.type) {
case VAL_INT: f = v.i; break;
case VAL_STR: f = parsefloat(v.s); break;
case VAL_MACRO: f = parsefloat(v.s); break;
@@ -51,11 +44,9 @@ static inline float forcefloat(tagval &v)
return f;
}
-static inline int forceint(tagval &v)
-{
+static inline int forceint(tagval &v) {
int i = 0;
- switch(v.type)
- {
+ switch(v.type) {
case VAL_FLOAT: i = v.f; break;
case VAL_STR: i = parseint(v.s); break;
case VAL_MACRO: i = parseint(v.s); break;
@@ -66,11 +57,9 @@ static inline int forceint(tagval &v)
return i;
}
-static inline const char *forcestr(tagval &v)
-{
+static inline const char *forcestr(tagval &v) {
const char *s = "";
- switch(v.type)
- {
+ switch(v.type) {
case VAL_FLOAT: s = floatstr(v.f); break;
case VAL_INT: s = intstr(v.i); break;
case VAL_STR:
@@ -82,29 +71,23 @@ static inline const char *forcestr(tagval &v)
return s;
}
-static inline void forcearg(tagval &v, int type)
-{
- switch(type)
- {
+static inline void forcearg(tagval &v, int type) {
+ switch(type) {
case RET_STR: if(v.type != VAL_STR) forcestr(v); break;
case RET_INT: if(v.type != VAL_INT) forceint(v); break;
case RET_FLOAT: if(v.type != VAL_FLOAT) forcefloat(v); break;
}
}
-static inline ident *forceident(tagval &v)
-{
- switch(v.type)
- {
+static inline ident *forceident(tagval &v) {
+ switch(v.type) {
case VAL_IDENT: return v.id;
- case VAL_MACRO:
- {
+ case VAL_MACRO: {
ident *id = newident(v.s, IDF_UNKNOWN);
v.setident(id);
return id;
}
- case VAL_STR:
- {
+ case VAL_STR: {
ident *id = newident(v.s, IDF_UNKNOWN);
delete[] v.s;
v.setident(id);
@@ -116,39 +99,31 @@ static inline ident *forceident(tagval &v)
return dummyident;
}
-void tagval::cleanup()
-{
+void tagval::cleanup() {
freearg(*this);
}
-static inline void freeargs(tagval *args, int &oldnum, int newnum)
-{
+static inline void freeargs(tagval *args, int &oldnum, int newnum) {
for(int i = newnum; i < oldnum; i++) freearg(args[i]);
oldnum = newnum;
}
-static inline void cleancode(ident &id)
-{
- if(id.code)
- {
+static inline void cleancode(ident &id) {
+ if(id.code) {
id.code[0] -= 0x100;
if(int(id.code[0]) < 0x100) delete[] id.code;
id.code = NULL;
}
}
-struct nullval : tagval
-{
+struct nullval : tagval {
nullval() { setnull(); }
} nullval;
tagval noret = nullval, *commandret = &noret;
-void clear_command()
-{
- enumerate(idents, ident, i,
- {
- if(i.type==ID_ALIAS)
- {
+void clear_command() {
+ enumerate(idents, ident, i, {
+ if(i.type==ID_ALIAS) {
DELETEA(i.name);
i.forcenull();
DELETEA(i.code);
@@ -156,14 +131,11 @@ void clear_command()
});
}
-void clearoverride(ident &i)
-{
+void clearoverride(ident &i) {
if(!(i.flags&IDF_OVERRIDDEN)) return;
- switch(i.type)
- {
+ switch(i.type) {
case ID_ALIAS:
- if(i.valtype==VAL_STR)
- {
+ if(i.valtype==VAL_STR) {
if(!i.val.s[0]) break;
delete[] i.val.s;
}
@@ -188,18 +160,15 @@ void clearoverride(ident &i)
i.flags &= ~IDF_OVERRIDDEN;
}
-void clearoverrides()
-{
+void clearoverrides() {
enumerate(idents, ident, i, clearoverride(i));
}
static bool initedidents = false;
static vector<ident> *identinits = NULL;
-static inline ident *addident(const ident &id)
-{
- if(!initedidents)
- {
+static inline ident *addident(const ident &id) {
+ if(!initedidents) {
if(!identinits) identinits = new vector<ident>;
identinits->add(id);
return NULL;
@@ -209,17 +178,14 @@ static inline ident *addident(const ident &id)
return identmap.add(&def);
}
-static bool initidents()
-{
+static bool initidents() {
initedidents = true;
- for(int i = 0; i < MAXARGS; i++)
- {
+ for(int i = 0; i < MAXARGS; i++) {
defformatstring(argname, "arg%d", i+1);
newident(argname, IDF_ARG);
}
dummyident = newident("//dummy", IDF_UNKNOWN);
- if(identinits)
- {
+ if(identinits) {
loopv(*identinits) addident((*identinits)[i]);
DELETEP(identinits);
}
@@ -229,17 +195,14 @@ UNUSED static bool forceinitidents = initidents();
static const char *sourcefile = NULL, *sourcestr = NULL;
-static const char *debugline(const char *p, const char *fmt)
-{
+static const char *debugline(const char *p, const char *fmt) {
if(!sourcestr) return fmt;
int num = 1;
const char *line = sourcestr;
- for(;;)
- {
+ for(;;) {
const char *end = strchr(line, '\n');
if(!end) end = line + strlen(line);
- if(p >= line && p <= end)
- {
+ if(p >= line && p <= end) {
static string buf;
if(sourcefile) formatstring(buf, "%s:%d: %s", sourcefile, num, fmt);
else formatstring(buf, "%d: %s", num, fmt);
@@ -252,8 +215,7 @@ static const char *debugline(const char *p, const char *fmt)
return fmt;
}
-static struct identlink
-{
+static struct identlink {
ident *id;
identlink *next;
int usedargs;
@@ -262,13 +224,11 @@ static struct identlink
VAR(dbgalias, 0, 4, 1000);
-static void debugalias()
-{
+static void debugalias() {
if(!dbgalias) return;
int total = 0, depth = 0;
for(identlink *l = aliasstack; l != &noalias; l = l->next) total++;
- for(identlink *l = aliasstack; l != &noalias; l = l->next)
- {
+ for(identlink *l = aliasstack; l != &noalias; l = l->next) {
ident *id = l->id;
++depth;
if(depth < dbgalias) conoutf(CON_ERROR, " %d) %s", total-depth+1, id->name);
@@ -280,41 +240,33 @@ static int nodebug = 0;
static void debugcode(const char *fmt, ...) PRINTFARGS(1, 2);
-static void debugcode(const char *fmt, ...)
-{
+static void debugcode(const char *fmt, ...) {
if(nodebug) return;
-
va_list args;
va_start(args, fmt);
conoutfv(CON_ERROR, fmt, args);
va_end(args);
-
debugalias();
}
static void debugcodeline(const char *p, const char *fmt, ...) PRINTFARGS(2, 3);
-static void debugcodeline(const char *p, const char *fmt, ...)
-{
+static void debugcodeline(const char *p, const char *fmt, ...) {
if(nodebug) return;
-
va_list args;
va_start(args, fmt);
conoutfv(CON_ERROR, debugline(p, fmt), args);
va_end(args);
-
debugalias();
}
ICOMMAND(nodebug, "e", (uint *body), { nodebug++; executeret(body, *commandret); nodebug--; });
-void addident(ident *id)
-{
+void addident(ident *id) {
addident(*id);
}
-static inline void pusharg(ident &id, const tagval &v, identstack &stack)
-{
+static inline void pusharg(ident &id, const tagval &v, identstack &stack) {
stack.val = id.val;
stack.valtype = id.valtype;
stack.next = id.stack;
@@ -323,8 +275,7 @@ static inline void pusharg(ident &id, const tagval &v, identstack &stack)
cleancode(id);
}
-static inline void poparg(ident &id)
-{
+static inline void poparg(ident &id) {
if(!id.stack) return;
identstack *stack = id.stack;
if(id.valtype == VAL_STR) delete[] id.val.s;
@@ -333,8 +284,7 @@ static inline void poparg(ident &id)
id.stack = stack->next;
}
-ICOMMAND(push, "rte", (ident *id, tagval *v, uint *code),
-{
+ICOMMAND(push, "rte", (ident *id, tagval *v, uint *code), {
if(id->type != ID_ALIAS || id->index < MAXARGS) return;
identstack stack;
pusharg(*id, *v, stack);
@@ -344,40 +294,32 @@ ICOMMAND(push, "rte", (ident *id, tagval *v, uint *code),
poparg(*id);
});
-static inline void pushalias(ident &id, identstack &stack)
-{
- if(id.type == ID_ALIAS && id.index >= MAXARGS)
- {
+static inline void pushalias(ident &id, identstack &stack) {
+ if(id.type == ID_ALIAS && id.index >= MAXARGS) {
pusharg(id, nullval, stack);
id.flags &= ~IDF_UNKNOWN;
}
}
-static inline void popalias(ident &id)
-{
+static inline void popalias(ident &id) {
if(id.type == ID_ALIAS && id.index >= MAXARGS) poparg(id);
}
KEYWORD(local, ID_LOCAL);
-static inline bool checknumber(const char *s)
-{
+static inline bool checknumber(const char *s) {
if(isdigit(s[0])) return true;
- else switch(s[0])
- {
+ else switch(s[0]) {
case '+': case '-': return isdigit(s[1]) || (s[1] == '.' && isdigit(s[2]));
case '.': return isdigit(s[1]) != 0;
default: return false;
}
}
-ident *newident(const char *name, int flags)
-{
+ident *newident(const char *name, int flags) {
ident *id = idents.access(name);
- if(!id)
- {
- if(checknumber(name))
- {
+ if(!id) {
+ if(checknumber(name)) {
debugcode("number %s is not a valid identifier name", name);
return dummyident;
}
@@ -386,27 +328,23 @@ ident *newident(const char *name, int flags)
return id;
}
-ident *writeident(const char *name, int flags)
-{
+ident *writeident(const char *name, int flags) {
ident *id = newident(name, flags);
- if(id->index < MAXARGS && !(aliasstack->usedargs&(1<<id->index)))
- {
+ if(id->index < MAXARGS && !(aliasstack->usedargs&(1<<id->index))) {
pusharg(*id, nullval, aliasstack->argstack[id->index]);
aliasstack->usedargs |= 1<<id->index;
}
return id;
}
-ident *readident(const char *name)
-{
+ident *readident(const char *name) {
ident *id = idents.access(name);
if(id && id->index < MAXARGS && !(aliasstack->usedargs&(1<<id->index)))
return NULL;
return id;
}
-void resetvar(char *name)
-{
+void resetvar(char *name) {
ident *id = idents.access(name);
if(!id) return;
if(id->flags&IDF_READONLY) debugcode("variable %s is read-only", id->name);
@@ -415,89 +353,73 @@ void resetvar(char *name)
COMMAND(resetvar, "s");
-static inline void setarg(ident &id, tagval &v)
-{
- if(aliasstack->usedargs&(1<<id.index))
- {
+static inline void setarg(ident &id, tagval &v) {
+ if(aliasstack->usedargs&(1<<id.index)) {
if(id.valtype == VAL_STR) delete[] id.val.s;
id.setval(v);
cleancode(id);
}
- else
- {
+ else {
pusharg(id, v, aliasstack->argstack[id.index]);
aliasstack->usedargs |= 1<<id.index;
}
}
-static inline void setalias(ident &id, tagval &v)
-{
+static inline void setalias(ident &id, tagval &v) {
if(id.valtype == VAL_STR) delete[] id.val.s;
id.setval(v);
cleancode(id);
id.flags = (id.flags & identflags) | identflags;
}
-static void setalias(const char *name, tagval &v)
-{
+static void setalias(const char *name, tagval &v) {
ident *id = idents.access(name);
- if(id)
- {
- if(id->type == ID_ALIAS)
- {
+ if(id) {
+ if(id->type == ID_ALIAS) {
if(id->index < MAXARGS) setarg(*id, v); else setalias(*id, v);
}
- else
- {
+ else {
debugcode("cannot redefine builtin %s with an alias", id->name);
freearg(v);
}
}
- else if(checknumber(name))
- {
+ else if(checknumber(name)) {
debugcode("cannot alias number %s", name);
freearg(v);
}
- else
- {
+ else {
addident(ident(ID_ALIAS, newstring(name), v, identflags));
}
}
-void alias(const char *name, const char *str)
-{
+void alias(const char *name, const char *str) {
tagval v;
v.setstr(newstring(str));
setalias(name, v);
}
-void alias(const char *name, tagval &v)
-{
+void alias(const char *name, tagval &v) {
setalias(name, v);
}
-ICOMMAND(alias, "st", (const char *name, tagval *v),
-{
+ICOMMAND(alias, "st", (const char *name, tagval *v), {
setalias(name, *v);
v->type = VAL_NULL;
});
// variable's and commands are registered through globals, see cube.h
-int variable(const char *name, int min, int cur, int max, int *storage, identfun fun, int flags)
-{
+int variable(const char *name, int min, int cur, int max, int *storage, identfun fun, int flags) {
addident(ident(ID_VAR, name, min, max, storage, (void *)fun, flags));
return cur;
}
-float fvariable(const char *name, float min, float cur, float max, float *storage, identfun fun, int flags)
-{
+float fvariable(const char *name, float min, float cur, float max, float *storage, identfun fun, int flags) {
addident(ident(ID_FVAR, name, min, max, storage, (void *)fun, flags));
return cur;
}
-char *svariable(const char *name, const char *cur, char **storage, identfun fun, int flags)
-{
+char *svariable(const char *name, const char *cur, char **storage, identfun fun, int flags) {
addident(ident(ID_SVAR, name, storage, (void *)fun, flags));
return newstring(cur);
}
@@ -507,67 +429,59 @@ char *svariable(const char *name, const char *cur, char **storage, identfun fun,
if(!id || id->type!=vartype) return retval;
#define GETVAR(id, name, retval) _GETVAR(id, ID_VAR, name, retval)
#define OVERRIDEVAR(errorval, saveval, resetval, clearval) \
- if(identflags&IDF_OVERRIDDEN || id->flags&IDF_OVERRIDE) \
- { \
- if(id->flags&IDF_PERSIST) \
- { \
+ if(identflags&IDF_OVERRIDDEN || id->flags&IDF_OVERRIDE) { \
+ \
+ if(id->flags&IDF_PERSIST) { \
+ \
debugcode("cannot override persistent variable %s", id->name); \
errorval; \
} \
if(!(id->flags&IDF_OVERRIDDEN)) { saveval; id->flags |= IDF_OVERRIDDEN; } \
else { clearval; } \
} \
- else \
- { \
+ else { \
+ \
if(id->flags&IDF_OVERRIDDEN) { resetval; id->flags &= ~IDF_OVERRIDDEN; } \
clearval; \
}
-void setvar(const char *name, int i, bool dofunc, bool doclamp)
-{
+void setvar(const char *name, int i, bool dofunc, bool doclamp) {
GETVAR(id, name, );
OVERRIDEVAR(return, id->overrideval.i = *id->storage.i, , )
if(doclamp) *id->storage.i = clamp(i, id->minval, id->maxval);
else *id->storage.i = i;
if(dofunc) id->changed();
}
-void setfvar(const char *name, float f, bool dofunc, bool doclamp)
-{
+void setfvar(const char *name, float f, bool dofunc, bool doclamp) {
_GETVAR(id, ID_FVAR, name, );
OVERRIDEVAR(return, id->overrideval.f = *id->storage.f, , );
if(doclamp) *id->storage.f = clamp(f, id->minvalf, id->maxvalf);
else *id->storage.f = f;
if(dofunc) id->changed();
}
-void setsvar(const char *name, const char *str, bool dofunc)
-{
+void setsvar(const char *name, const char *str, bool dofunc) {
_GETVAR(id, ID_SVAR, name, );
OVERRIDEVAR(return, id->overrideval.s = *id->storage.s, delete[] id->overrideval.s, delete[] *id->storage.s);
*id->storage.s = newstring(str);
if(dofunc) id->changed();
}
-int getvar(const char *name)
-{
+int getvar(const char *name) {
GETVAR(id, name, 0);
return *id->storage.i;
}
-int getvarmin(const char *name)
-{
+int getvarmin(const char *name) {
GETVAR(id, name, 0);
return id->minval;
}
-int getvarmax(const char *name)
-{
+int getvarmax(const char *name) {
GETVAR(id, name, 0);
return id->maxval;
}
-float getfvarmin(const char *name)
-{
+float getfvarmin(const char *name) {
_GETVAR(id, ID_FVAR, name, 0);
return id->minvalf;
}
-float getfvarmax(const char *name)
-{
+float getfvarmax(const char *name) {
_GETVAR(id, ID_FVAR, name, 0);
return id->maxvalf;
}
@@ -580,11 +494,9 @@ ICOMMAND(getfvarmax, "s", (char *s), floatret(getfvarmax(s)));
bool identexists(const char *name) { return idents.access(name)!=NULL; }
ident *getident(const char *name) { return idents.access(name); }
-void touchvar(const char *name)
-{
+void touchvar(const char *name) {
ident *id = idents.access(name);
- if(id) switch(id->type)
- {
+ if(id) switch(id->type) {
case ID_VAR:
case ID_FVAR:
case ID_SVAR:
@@ -593,16 +505,14 @@ void touchvar(const char *name)
}
}
-const char *getalias(const char *name)
-{
+const char *getalias(const char *name) {
ident *i = idents.access(name);
return i && i->type==ID_ALIAS && (i->index >= MAXARGS || aliasstack->usedargs&(1<<i->index)) ? i->getstr() : "";
}
ICOMMAND(getalias, "s", (char *s), result(getalias(s)));
-int clampvar(ident *id, int val, int minval, int maxval)
-{
+int clampvar(ident *id, int val, int minval, int maxval) {
if(val < minval) val = minval;
else if(val > maxval) val = maxval;
else return val;
@@ -613,15 +523,13 @@ int clampvar(ident *id, int val, int minval, int maxval)
return val;
}
-void setvarchecked(ident *id, int val)
-{
+void setvarchecked(ident *id, int val) {
if(id->flags&IDF_READONLY) debugcode("variable %s is read-only", id->name);
#ifndef STANDALONE
- else if(!(id->flags&IDF_OVERRIDE) || identflags&IDF_OVERRIDDEN || game::allowedittoggle())
+ else if(!(id->flags&IDF_OVERRIDE) || identflags&IDF_OVERRIDDEN || game::allowedittoggle()) {
#else
- else
+ else {
#endif
- {
OVERRIDEVAR(return, id->overrideval.i = *id->storage.i, , )
if(val < id->minval || val > id->maxval) val = clampvar(id, val, id->minval, id->maxval);
*id->storage.i = val;
@@ -632,19 +540,16 @@ void setvarchecked(ident *id, int val)
}
}
-static inline void setvarchecked(ident *id, tagval *args, int numargs)
-{
+static inline void setvarchecked(ident *id, tagval *args, int numargs) {
int val = forceint(args[0]);
- if(id->flags&IDF_HEX && numargs > 1)
- {
+ if(id->flags&IDF_HEX && numargs > 1) {
val = (val << 16) | (forceint(args[1])<<8);
if(numargs > 2) val |= forceint(args[2]);
}
setvarchecked(id, val);
}
-float clampfvar(ident *id, float val, float minval, float maxval)
-{
+float clampfvar(ident *id, float val, float minval, float maxval) {
if(val < minval) val = minval;
else if(val > maxval) val = maxval;
else return val;
@@ -652,15 +557,13 @@ float clampfvar(ident *id, float val, float minval, float maxval)
return val;
}
-void setfvarchecked(ident *id, float val)
-{
+void setfvarchecked(ident *id, float val) {
if(id->flags&IDF_READONLY) debugcode("variable %s is read-only", id->name);
#ifndef STANDALONE
- else if(!(id->flags&IDF_OVERRIDE) || identflags&IDF_OVERRIDDEN || game::allowedittoggle())
+ else if(!(id->flags&IDF_OVERRIDE) || identflags&IDF_OVERRIDDEN || game::allowedittoggle()) {
#else
- else
+ else {
#endif
- {
OVERRIDEVAR(return, id->overrideval.f = *id->storage.f, , );
if(val < id->minvalf || val > id->maxvalf) val = clampfvar(id, val, id->minvalf, id->maxvalf);
*id->storage.f = val;
@@ -671,15 +574,13 @@ void setfvarchecked(ident *id, float val)
}
}
-void setsvarchecked(ident *id, const char *val)
-{
+void setsvarchecked(ident *id, const char *val) {
if(id->flags&IDF_READONLY) debugcode("variable %s is read-only", id->name);
#ifndef STANDALONE
- else if(!(id->flags&IDF_OVERRIDE) || identflags&IDF_OVERRIDDEN || game::allowedittoggle())
+ else if(!(id->flags&IDF_OVERRIDE) || identflags&IDF_OVERRIDDEN || game::allowedittoggle()) {
#else
- else
+ else {
#endif
- {
OVERRIDEVAR(return, id->overrideval.s = *id->storage.s, delete[] id->overrideval.s, delete[] *id->storage.s);
*id->storage.s = newstring(val);
id->changed();
@@ -689,10 +590,8 @@ void setsvarchecked(ident *id, const char *val)
}
}
-ICOMMAND(set, "rt", (ident *id, tagval *v),
-{
- switch(id->type)
- {
+ICOMMAND(set, "rt", (ident *id, tagval *v), {
+ switch(id->type) {
case ID_ALIAS:
if(id->index < MAXARGS) setarg(*id, *v); else setalias(*id, *v);
v->type = VAL_NULL;
@@ -707,8 +606,7 @@ ICOMMAND(set, "rt", (ident *id, tagval *v),
setsvarchecked(id, forcestr(*v));
break;
case ID_COMMAND:
- if(id->flags&IDF_EMUVAR)
- {
+ if(id->flags&IDF_EMUVAR) {
execute(id, v, 1);
v->type = VAL_NULL;
break;
@@ -720,13 +618,11 @@ ICOMMAND(set, "rt", (ident *id, tagval *v),
}
});
-bool addcommand(const char *name, identfun fun, const char *args)
-{
+bool addcommand(const char *name, identfun fun, const char *args) {
uint argmask = 0;
int numargs = 0, flags = 0;
bool limit = true;
- for(const char *fmt = args; *fmt; fmt++) switch(*fmt)
- {
+ for(const char *fmt = args; *fmt; fmt++) switch(*fmt) {
case 'i': case 'b': case 'f': case 't': case 'N': case 'D': if(numargs < MAXARGS) numargs++; break;
case '$': flags |= IDF_EMUVAR; // fall through
case 's': case 'e': case 'r': if(numargs < MAXARGS) { argmask |= 1<<numargs; numargs++; } break;
@@ -739,16 +635,13 @@ bool addcommand(const char *name, identfun fun, const char *args)
return false;
}
-bool addkeyword(int type, const char *name)
-{
+bool addkeyword(int type, const char *name) {
addident(ident(type, name, "", 0, 0, NULL));
return true;
}
-const char *parsestring(const char *p)
-{
- for(; *p; p++) switch(*p)
- {
+const char *parsestring(const char *p) {
+ for(; *p; p++) switch(*p) {
case '\r':
case '\n':
case '\"':
@@ -760,18 +653,14 @@ const char *parsestring(const char *p)
return p;
}
-int unescapestring(char *dst, const char *src, const char *end)
-{
+int unescapestring(char *dst, const char *src, const char *end) {
char *start = dst;
- while(src < end)
- {
+ while(src < end) {
int c = *src++;
- if(c == '^')
- {
+ if(c == '^') {
if(src >= end) break;
int e = *src++;
- switch(e)
- {
+ switch(e) {
case 'n': *dst++ = '\n'; break;
case 't': *dst++ = '\t'; break;
case 'f': *dst++ = '\f'; break;
@@ -783,19 +672,15 @@ int unescapestring(char *dst, const char *src, const char *end)
return dst - start;
}
-static char *conc(vector<char> &buf, tagval *v, int n, bool space, const char *prefix = NULL, int prefixlen = 0)
-{
- if(prefix)
- {
+static char *conc(vector<char> &buf, tagval *v, int n, bool space, const char *prefix = NULL, int prefixlen = 0) {
+ if(prefix) {
buf.put(prefix, prefixlen);
if(space && n) buf.add(' ');
}
- loopi(n)
- {
+ loopi(n) {
const char *s = "";
int len = 0;
- switch(v[i].type)
- {
+ switch(v[i].type) {
case VAL_INT: s = intstr(v[i].i); break;
case VAL_FLOAT: s = floatstr(v[i].f); break;
case VAL_STR: s = v[i].s; break;
@@ -811,13 +696,11 @@ static char *conc(vector<char> &buf, tagval *v, int n, bool space, const char *p
return buf.getbuf();
}
-static char *conc(tagval *v, int n, bool space, const char *prefix, int prefixlen)
-{
+static char *conc(tagval *v, int n, bool space, const char *prefix, int prefixlen) {
static int vlen[MAXARGS];
static char numbuf[3*MAXSTRLEN];
int len = prefixlen, numlen = 0, i = 0;
- for(; i < n; i++) switch(v[i].type)
- {
+ for(; i < n; i++) switch(v[i].type) {
case VAL_MACRO: len += (vlen[i] = v[i].code[-1]>>8); break;
case VAL_STR: len += (vlen[i] = int(strlen(v[i].s))); break;
case VAL_INT:
@@ -836,16 +719,13 @@ overflow:
if(space) len += max(prefix ? i : i-1, 0);
char *buf = newstring(len + numlen);
int offset = 0, numoffset = 0;
- if(prefix)
- {
+ if(prefix) {
memcpy(buf, prefix, prefixlen);
offset += prefixlen;
if(space && i) buf[offset++] = ' ';
}
- loopj(i)
- {
- if(v[j].type == VAL_INT || v[j].type == VAL_FLOAT)
- {
+ loopj(i) {
+ if(v[j].type == VAL_INT || v[j].type == VAL_FLOAT) {
memcpy(&buf[offset], &numbuf[numoffset], vlen[j]);
numoffset += vlen[j];
}
@@ -855,8 +735,7 @@ overflow:
if(space) buf[offset++] = ' ';
}
buf[offset] = '\0';
- if(i < n)
- {
+ if(i < n) {
char *morebuf = conc(&v[i], n-i, space, buf, offset);
delete[] buf;
return morebuf;
@@ -864,28 +743,23 @@ overflow:
return buf;
}
-static inline char *conc(tagval *v, int n, bool space)
-{
+static inline char *conc(tagval *v, int n, bool space) {
return conc(v, n, space, NULL, 0);
}
-static inline char *conc(tagval *v, int n, bool space, const char *prefix)
-{
+static inline char *conc(tagval *v, int n, bool space, const char *prefix) {
return conc(v, n, space, prefix, strlen(prefix));
}
-static inline void skipcomments(const char *&p)
-{
- for(;;)
- {
+static inline void skipcomments(const char *&p) {
+ for(;;) {
p += strspn(p, " \t\r");
if(p[0]!='/' || p[1]!='/') break;
p += strcspn(p, "\n\0");
}
}
-static inline char *cutstring(const char *&p, int &len)
-{
+static inline char *cutstring(const char *&p, int &len) {
p++;
const char *end = parsestring(p);
char *s = newstring(end - p);
@@ -896,16 +770,13 @@ static inline char *cutstring(const char *&p, int &len)
return s;
}
-static inline const char *parseword(const char *p)
-{
+static inline const char *parseword(const char *p) {
const int maxbrak = 100;
static char brakstack[maxbrak];
int brakdepth = 0;
- for(;; p++)
- {
+ for(;; p++) {
p += strcspn(p, "\"/;()[] \t\r\n\0");
- switch(p[0])
- {
+ switch(p[0]) {
case '"': case ';': case ' ': case '\t': case '\r': case '\n': case '\0': return p;
case '/': if(p[1] == '/') return p; break;
case '[': case '(': if(brakdepth >= maxbrak) return p; brakstack[brakdepth++] = p[0]; break;
@@ -916,8 +787,7 @@ static inline const char *parseword(const char *p)
return p;
}
-static inline char *cutword(const char *&p, int &len)
-{
+static inline char *cutword(const char *&p, int &len) {
const char *word = p;
p = parseword(p);
len = p-word;
@@ -925,10 +795,8 @@ static inline char *cutword(const char *&p, int &len)
return newstring(word, len);
}
-static inline void compilestr(vector<uint> &code, const char *word, int len, bool macro = false)
-{
- if(len <= 3 && !macro)
- {
+static inline void compilestr(vector<uint> &code, const char *word, int len, bool macro = false) {
+ if(len <= 3 && !macro) {
uint op = CODE_VALI|RET_STR;
for(int i = 0; i < len; i++) op |= uint(uchar(word[i]))<<((i+1)*8);
code.add(op);
@@ -937,8 +805,7 @@ static inline void compilestr(vector<uint> &code, const char *word, int len, boo
code.add((macro ? CODE_MACRO : CODE_VAL|RET_STR)|(len<<8));
code.put((const uint *)word, len/sizeof(uint));
size_t endlen = len%sizeof(uint);
- union
- {
+ union {
char c[sizeof(uint)];
uint u;
} end;
@@ -947,30 +814,25 @@ static inline void compilestr(vector<uint> &code, const char *word, int len, boo
code.add(end.u);
}
-static inline void compilestr(vector<uint> &code, const char *word = NULL)
-{
+static inline void compilestr(vector<uint> &code, const char *word = NULL) {
if(!word) { code.add(CODE_VALI|RET_STR); return; }
compilestr(code, word, int(strlen(word)));
}
-static inline void compileint(vector<uint> &code, int i)
-{
+static inline void compileint(vector<uint> &code, int i) {
if(i >= -0x800000 && i <= 0x7FFFFF)
code.add(CODE_VALI|RET_INT|(i<<8));
- else
- {
+ else {
code.add(CODE_VAL|RET_INT);
code.add(i);
}
}
-static inline void compilenull(vector<uint> &code)
-{
+static inline void compilenull(vector<uint> &code) {
code.add(CODE_VALI|RET_NULL);
}
-static inline void compileblock(vector<uint> &code)
-{
+static inline void compileblock(vector<uint> &code) {
int start = code.length();
code.add(CODE_BLOCK);
code.add(CODE_OFFSET|((start+2)<<8));
@@ -978,27 +840,22 @@ static inline void compileblock(vector<uint> &code)
code[start] |= uint(code.length() - (start + 1))<<8;
}
-static inline void compileident(vector<uint> &code, ident *id)
-{
+static inline void compileident(vector<uint> &code, ident *id) {
code.add((id->index < MAXARGS ? CODE_IDENTARG : CODE_IDENT)|(id->index<<8));
}
-static inline void compileident(vector<uint> &code, const char *word = NULL)
-{
+static inline void compileident(vector<uint> &code, const char *word = NULL) {
compileident(code, word ? newident(word, IDF_UNKNOWN) : dummyident);
}
-static inline void compileint(vector<uint> &code, const char *word = NULL)
-{
+static inline void compileint(vector<uint> &code, const char *word = NULL) {
return compileint(code, word ? parseint(word) : 0);
}
-static inline void compilefloat(vector<uint> &code, float f)
-{
+static inline void compilefloat(vector<uint> &code, float f) {
if(int(f) == f && f >= -0x800000 && f <= 0x7FFFFF)
code.add(CODE_VALI|RET_FLOAT|(int(f)<<8));
- else
- {
+ else {
union { float f; uint u; } conv;
conv.f = f;
code.add(CODE_VAL|RET_FLOAT);
@@ -1006,24 +863,20 @@ static inline void compilefloat(vector<uint> &code, float f)
}
}
-static inline void compilefloat(vector<uint> &code, const char *word = NULL)
-{
+static inline void compilefloat(vector<uint> &code, const char *word = NULL) {
return compilefloat(code, word ? parsefloat(word) : 0.0f);
}
static bool compilearg(vector<uint> &code, const char *&p, int wordtype);
static void compilestatements(vector<uint> &code, const char *&p, int rettype, int brak = '\0');
-static inline void compileval(vector<uint> &code, int wordtype, char *word, int wordlen)
-{
- switch(wordtype)
- {
+static inline void compileval(vector<uint> &code, int wordtype, char *word, int wordlen) {
+ switch(wordtype) {
case VAL_STR: compilestr(code, word, wordlen, true); break;
case VAL_ANY: compilestr(code, word, wordlen); break;
case VAL_FLOAT: compilefloat(code, word); break;
case VAL_INT: compileint(code, word); break;
- case VAL_CODE:
- {
+ case VAL_CODE: {
int start = code.length();
code.add(CODE_BLOCK);
code.add(CODE_OFFSET|((start+2)<<8));
@@ -1041,12 +894,10 @@ static inline void compileval(vector<uint> &code, int wordtype, char *word, int
static bool compileword(vector<uint> &code, const char *&p, int wordtype, char *&word, int &wordlen);
-static void compilelookup(vector<uint> &code, const char *&p, int ltype)
-{
+static void compilelookup(vector<uint> &code, const char *&p, int ltype) {
char *lookup = NULL;
int lookuplen = 0;
- switch(*++p)
- {
+ switch(*++p) {
case '(':
case '[':
if(!compileword(code, p, VAL_STR, lookup, lookuplen)) goto invalid;
@@ -1057,24 +908,20 @@ static void compilelookup(vector<uint> &code, const char *&p, int ltype)
case '\"':
lookup = cutstring(p, lookuplen);
goto lookupid;
- default:
- {
+ default: {
lookup = cutword(p, lookuplen);
if(!lookup) goto invalid;
lookupid:
ident *id = newident(lookup, IDF_UNKNOWN);
- if(id) switch(id->type)
- {
+ if(id) switch(id->type) {
case ID_VAR: code.add(CODE_IVAR|((ltype >= VAL_ANY ? VAL_INT : ltype)<<CODE_RET)|(id->index<<8)); goto done;
case ID_FVAR: code.add(CODE_FVAR|((ltype >= VAL_ANY ? VAL_FLOAT : ltype)<<CODE_RET)|(id->index<<8)); goto done;
case ID_SVAR: code.add(CODE_SVAR|((ltype >= VAL_ANY ? VAL_STR : ltype)<<CODE_RET)|(id->index<<8)); goto done;
case ID_ALIAS: code.add((id->index < MAXARGS ? CODE_LOOKUPARG : CODE_LOOKUP)|((ltype >= VAL_ANY ? VAL_STR : ltype)<<CODE_RET)|(id->index<<8)); goto done;
- case ID_COMMAND:
- {
+ case ID_COMMAND: {
int comtype = CODE_COM, numargs = 0;
code.add(CODE_ENTER);
- for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt)
- {
+ for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt) {
case 's': compilestr(code, NULL, 0, true); numargs++; break;
case 'i': compileint(code); numargs++; break;
case 'b': compileint(code, INT_MIN); numargs++; break;
@@ -1105,37 +952,31 @@ static void compilelookup(vector<uint> &code, const char *&p, int ltype)
code.add(CODE_LOOKUPU|((ltype < VAL_ANY ? ltype<<CODE_RET : 0)));
done:
delete[] lookup;
- switch(ltype)
- {
+ switch(ltype) {
case VAL_CODE: code.add(CODE_COMPILE); break;
case VAL_IDENT: code.add(CODE_IDENTU); break;
}
return;
invalid:
- switch(ltype)
- {
+ switch(ltype) {
case VAL_NULL: case VAL_ANY: compilenull(code); break;
default: compileval(code, ltype, NULL, 0); break;
}
}
-static bool compileblockstr(vector<uint> &code, const char *str, const char *end, bool macro)
-{
+static bool compileblockstr(vector<uint> &code, const char *str, const char *end, bool macro) {
int start = code.length();
code.add(macro ? CODE_MACRO : CODE_VAL|RET_STR);
char *buf = (char *)code.reserve((end-str)/sizeof(uint)+1).buf;
int len = 0;
- while(str < end)
- {
+ while(str < end) {
int n = strcspn(str, "\r/\"@]\0");
memcpy(&buf[len], str, n);
len += n;
str += n;
- switch(*str)
- {
+ switch(*str) {
case '\r': str++; break;
- case '\"':
- {
+ case '\"': {
const char *start = str;
str = parsestring(str+1);
if(*str=='\"') str++;
@@ -1144,11 +985,9 @@ static bool compileblockstr(vector<uint> &code, const char *str, const char *end
break;
}
case '/':
- if(str[1] == '/')
- {
+ if(str[1] == '/') {
size_t comment = strcspn(str, "\n\0");
- if (iscubepunct(str[2]))
- {
+ if (iscubepunct(str[2])) {
memcpy(&buf[len], str, comment);
len += comment;
}
@@ -1171,12 +1010,10 @@ done:
return true;
}
-static bool compileblocksub(vector<uint> &code, const char *&p)
-{
+static bool compileblocksub(vector<uint> &code, const char *&p) {
char *lookup = NULL;
int lookuplen = 0;
- switch(*p)
- {
+ switch(*p) {
case '(':
if(!compilearg(code, p, VAL_STR)) return false;
break;
@@ -1187,9 +1024,8 @@ static bool compileblocksub(vector<uint> &code, const char *&p)
case '\"':
lookup = cutstring(p, lookuplen);
goto lookupid;
- default:
- {
- {
+ default: {
+ {
const char *start = p;
while(iscubealnum(*p) || *p=='_') p++;
lookuplen = p-start;
@@ -1198,8 +1034,7 @@ static bool compileblocksub(vector<uint> &code, const char *&p)
}
lookupid:
ident *id = newident(lookup, IDF_UNKNOWN);
- if(id) switch(id->type)
- {
+ if(id) switch(id->type) {
case ID_VAR: code.add(CODE_IVAR|RET_STR|(id->index<<8)); goto done;
case ID_FVAR: code.add(CODE_FVAR|RET_STR|(id->index<<8)); goto done;
case ID_SVAR: code.add(CODE_SVAR|RET_STR|(id->index<<8)); goto done;
@@ -1215,16 +1050,13 @@ static bool compileblocksub(vector<uint> &code, const char *&p)
return true;
}
-static void compileblock(vector<uint> &code, const char *&p, int wordtype)
-{
+static void compileblock(vector<uint> &code, const char *&p, int wordtype) {
const char *line = p, *start = p;
int concs = 0;
- for(int brak = 1; brak;)
- {
+ for(int brak = 1; brak;) {
p += strcspn(p, "@\"/[]\0");
int c = *p++;
- switch(c)
- {
+ switch(c) {
case '\0':
debugcodeline(line, "missing \"]\"");
p--;
@@ -1238,16 +1070,14 @@ static void compileblock(vector<uint> &code, const char *&p, int wordtype)
break;
case '[': brak++; break;
case ']': brak--; break;
- case '@':
- {
+ case '@': {
const char *esc = p;
while(*p == '@') p++;
int level = p - (esc - 1);
if(brak > level) continue;
else if(brak < level) debugcodeline(line, "too many @s");
if(!concs) code.add(CODE_ENTER);
- if(concs + 2 > MAXARGS)
- {
+ if(concs + 2 > MAXARGS) {
code.add(CODE_CONCW|RET_STR|(concs<<8));
concs = 1;
}
@@ -1260,12 +1090,9 @@ static void compileblock(vector<uint> &code, const char *&p, int wordtype)
}
}
done:
- if(p-1 > start)
- {
- if(!concs) switch(wordtype)
- {
- case VAL_CODE:
- {
+ if(p-1 > start) {
+ if(!concs) switch(wordtype) {
+ case VAL_CODE: {
p = start;
int inst = code.length();
code.add(CODE_BLOCK);
@@ -1275,8 +1102,7 @@ done:
code[inst] |= uint(code.length() - (inst + 1))<<8;
return;
}
- case VAL_IDENT:
- {
+ case VAL_IDENT: {
char *name = newstring(start, p-1-start);
compileident(code, name);
delete[] name;
@@ -1286,21 +1112,18 @@ done:
compileblockstr(code, start, p-1, concs > 0);
if(concs > 1) concs++;
}
- if(concs)
- {
+ if(concs) {
code.add(CODE_CONCM|(wordtype < VAL_ANY ? wordtype<<CODE_RET : RET_STR)|(concs<<8));
code.add(CODE_EXIT|(wordtype < VAL_ANY ? wordtype<<CODE_RET : RET_STR));
}
- switch(wordtype)
- {
+ switch(wordtype) {
case VAL_CODE: if(!concs && p-1 <= start) compileblock(code); else code.add(CODE_COMPILE); break;
case VAL_IDENT: if(!concs && p-1 <= start) compileident(code); else code.add(CODE_IDENTU); break;
case VAL_STR: case VAL_NULL: case VAL_ANY:
if(!concs && p-1 <= start) compilestr(code);
break;
default:
- if(!concs)
- {
+ if(!concs) {
if(p-1 <= start) compileval(code, wordtype, NULL, 0);
else code.add(CODE_FORCE|(wordtype<<CODE_RET));
}
@@ -1308,11 +1131,9 @@ done:
}
}
-static bool compileword(vector<uint> &code, const char *&p, int wordtype, char *&word, int &wordlen)
-{
+static bool compileword(vector<uint> &code, const char *&p, int wordtype, char *&word, int &wordlen) {
skipcomments(p);
- switch(*p)
- {
+ switch(*p) {
case '\"': word = cutstring(p, wordlen); break;
case '$': compilelookup(code, p, wordtype); return true;
case '(':
@@ -1320,8 +1141,7 @@ static bool compileword(vector<uint> &code, const char *&p, int wordtype, char *
code.add(CODE_ENTER);
compilestatements(code, p, VAL_ANY, ')');
code.add(CODE_EXIT|(wordtype < VAL_ANY ? wordtype<<CODE_RET : 0));
- switch(wordtype)
- {
+ switch(wordtype) {
case VAL_CODE: code.add(CODE_COMPILE); break;
case VAL_IDENT: code.add(CODE_IDENTU); break;
}
@@ -1335,36 +1155,31 @@ static bool compileword(vector<uint> &code, const char *&p, int wordtype, char *
return word!=NULL;
}
-static inline bool compilearg(vector<uint> &code, const char *&p, int wordtype)
-{
+static inline bool compilearg(vector<uint> &code, const char *&p, int wordtype) {
char *word = NULL;
int wordlen = 0;
bool more = compileword(code, p, wordtype, word, wordlen);
if(!more) return false;
- if(word)
- {
+ if(word) {
compileval(code, wordtype, word, wordlen);
delete[] word;
}
return true;
}
-static void compilestatements(vector<uint> &code, const char *&p, int rettype, int brak)
-{
+static void compilestatements(vector<uint> &code, const char *&p, int rettype, int brak) {
const char *line = p;
char *idname = NULL;
int idlen = 0;
ident *id = NULL;
int numargs = 0;
- for(;;)
- {
+ for(;;) {
skipcomments(p);
idname = NULL;
bool more = compileword(code, p, VAL_ANY, idname, idlen);
if(!more) goto endstatement;
skipcomments(p);
- if(p[0] == '=') switch(p[1])
- {
+ if(p[0] == '=') switch(p[1]) {
case '/':
if(p[2] != '/') break;
[[fallthrough]];
@@ -1380,11 +1195,9 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
[[fallthrough]];
case '\0':
p++;
- if(idname)
- {
+ if(idname) {
id = newident(idname, IDF_UNKNOWN);
- if(id) switch(id->type)
- {
+ if(id) switch(id->type) {
case ID_ALIAS:
if(!(more = compilearg(code, p, VAL_ANY))) compilestr(code);
code.add((id->index < MAXARGS ? CODE_ALIASARG : CODE_ALIAS)|(id->index<<8));
@@ -1414,17 +1227,14 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
}
compilecommand:
numargs = 0;
- if(!idname)
- {
+ if(!idname) {
noid:
while(numargs < MAXARGS && (more = compilearg(code, p, VAL_ANY))) numargs++;
code.add(CODE_CALLU);
}
- else
- {
+ else {
id = idents.access(idname);
- if(!id)
- {
+ if(!id) {
if(!checknumber(idname)) { compilestr(code, idname, idlen); delete[] idname; goto noid; }
char *end = idname;
int val = int(strtoul(idname, &end, 0));
@@ -1432,28 +1242,23 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
else compileint(code, val);
code.add(CODE_RESULT);
}
- else switch(id->type)
- {
+ else switch(id->type) {
case ID_ALIAS:
while(numargs < MAXARGS && (more = compilearg(code, p, VAL_ANY))) numargs++;
code.add((id->index < MAXARGS ? CODE_CALLARG : CODE_CALL)|(id->index<<8));
break;
- case ID_COMMAND:
- {
+ case ID_COMMAND: {
int comtype = CODE_COM, fakeargs = 0;
bool rep = false;
- for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt)
- {
+ for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt) {
case 's':
if(more) more = compilearg(code, p, VAL_STR);
- if(!more)
- {
+ if(!more) {
if(rep) break;
compilestr(code, NULL, 0, true);
fakeargs++;
}
- else if(!fmt[1])
- {
+ else if(!fmt[1]) {
int numconc = 0;
while(numargs + numconc < MAXARGS && (more = compilearg(code, p, VAL_STR))) numconc++;
if(numconc > 0) code.add(CODE_CONC|RET_STR|((numconc+1)<<8));
@@ -1474,8 +1279,7 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
case 'C': comtype = CODE_COMC; if(more) while(numargs < MAXARGS && (more = compilearg(code, p, VAL_ANY))) numargs++; numargs = 1; goto endfmt;
case 'V': comtype = CODE_COMV; if(more) while(numargs < MAXARGS && (more = compilearg(code, p, VAL_ANY))) numargs++; numargs = 2; goto endfmt;
case '1': case '2': case '3': case '4':
- if(more && numargs < MAXARGS)
- {
+ if(more && numargs < MAXARGS) {
int numrep = *fmt-'0'+1;
fmt -= numrep;
rep = true;
@@ -1504,8 +1308,7 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
break;
case ID_SVAR:
if(!(more = compilearg(code, p, VAL_STR))) code.add(CODE_PRINT|(id->index<<8));
- else
- {
+ else {
int numconc = 0;
while(numconc+1 < MAXARGS && (more = compilearg(code, p, VAL_ANY))) numconc++;
if(numconc > 0) code.add(CODE_CONC|RET_STR|((numconc+1)<<8));
@@ -1520,19 +1323,16 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
if(more) while(compilearg(code, p, VAL_ANY)) code.add(CODE_POP);
p += strcspn(p, ")];/\n\0");
int c = *p++;
- switch(c)
- {
+ switch(c) {
case '\0':
if(c != brak) debugcodeline(line, "missing \"%c\"", brak);
p--;
return;
-
case ')':
case ']':
if(c == brak) return;
debugcodeline(line, "unexpected \"%c\"", c);
break;
-
case '/':
if(*p == '/') p += strcspn(p, "\n\0");
goto endstatement;
@@ -1540,15 +1340,13 @@ static void compilestatements(vector<uint> &code, const char *&p, int rettype, i
}
}
-static void compilemain(vector<uint> &code, const char *p, int rettype = VAL_ANY)
-{
+static void compilemain(vector<uint> &code, const char *p, int rettype = VAL_ANY) {
code.add(CODE_START);
compilestatements(code, p, VAL_ANY);
code.add(CODE_EXIT|(rettype < VAL_ANY ? rettype<<CODE_RET : 0));
}
-uint *compilecode(const char *p)
-{
+uint *compilecode(const char *p) {
vector<uint> buf;
buf.reserve(64);
compilemain(buf, p);
@@ -1558,17 +1356,14 @@ uint *compilecode(const char *p)
return code;
}
-void keepcode(uint *code)
-{
+void keepcode(uint *code) {
if(!code) return;
- switch(*code&CODE_OP_MASK)
- {
+ switch(*code&CODE_OP_MASK) {
case CODE_START:
*code += 0x100;
return;
}
- switch(code[-1]&CODE_OP_MASK)
- {
+ switch(code[-1]&CODE_OP_MASK) {
case CODE_START:
code[-1] += 0x100;
break;
@@ -1579,18 +1374,15 @@ void keepcode(uint *code)
}
}
-void freecode(uint *code)
-{
+void freecode(uint *code) {
if(!code) return;
- switch(*code&CODE_OP_MASK)
- {
+ switch(*code&CODE_OP_MASK) {
case CODE_START:
*code -= 0x100;
if(int(*code) < 0x100) delete[] code;
return;
}
- switch(code[-1]&CODE_OP_MASK)
- {
+ switch(code[-1]&CODE_OP_MASK) {
case CODE_START:
code[-1] -= 0x100;
if(int(code[-1]) < 0x100) delete[] &code[-1];
@@ -1603,8 +1395,7 @@ void freecode(uint *code)
}
}
-void printvar(ident *id, int i)
-{
+void printvar(ident *id, int i) {
if(i < 0) conoutf(CON_INFO, id->index, "%s = %d", id->name, i);
else if(id->flags&IDF_HEX && id->maxval==0xFFFFFF)
conoutf(CON_INFO, id->index, "%s = 0x%.6X (%d, %d, %d)", id->name, i, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF);
@@ -1612,38 +1403,31 @@ void printvar(ident *id, int i)
conoutf(CON_INFO, id->index, id->flags&IDF_HEX ? "%s = 0x%X" : "%s = %d", id->name, i);
}
-void printfvar(ident *id, float f)
-{
+void printfvar(ident *id, float f) {
conoutf(CON_INFO, id->index, "%s = %s", id->name, floatstr(f));
}
-void printsvar(ident *id, const char *s)
-{
+void printsvar(ident *id, const char *s) {
conoutf(CON_INFO, id->index, strchr(s, '"') ? "%s = [%s]" : "%s = \"%s\"", id->name, s);
}
template <class V>
-static void printvar(ident *id, int type, V &val)
-{
- switch(type)
- {
+static void printvar(ident *id, int type, V &val) {
+ switch(type) {
case VAL_INT: printvar(id, val.getint()); break;
case VAL_FLOAT: printfvar(id, val.getfloat()); break;
default: printsvar(id, val.getstr()); break;
}
}
-void printvar(ident *id)
-{
- switch(id->type)
- {
+void printvar(ident *id) {
+ switch(id->type) {
case ID_VAR: printvar(id, *id->storage.i); break;
case ID_FVAR: printfvar(id, *id->storage.f); break;
case ID_SVAR: printsvar(id, *id->storage.s); break;
case ID_ALIAS: printvar(id, id->valtype, *id); break;
case ID_COMMAND:
- if(id->flags&IDF_EMUVAR)
- {
+ if(id->flags&IDF_EMUVAR) {
tagval result;
executeret(id, NULL, 0, true, result);
printvar(id, result.type, result);
@@ -1669,23 +1453,18 @@ typedef void (__cdecl *comfun11)(void *, void *, void *, void *, void *, void *,
typedef void (__cdecl *comfun12)(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *);
typedef void (__cdecl *comfunv)(tagval *, int);
-static const uint *skipcode(const uint *code, tagval &result)
-{
+static const uint *skipcode(const uint *code, tagval &result) {
int depth = 0;
- for(;;)
- {
+ for(;;) {
uint op = *code++;
- switch(op&0xFF)
- {
+ switch(op&0xFF) {
case CODE_MACRO:
- case CODE_VAL|RET_STR:
- {
+ case CODE_VAL|RET_STR: {
uint len = op>>8;
code += len/sizeof(uint) + 1;
continue;
}
- case CODE_BLOCK:
- {
+ case CODE_BLOCK: {
uint len = op>>8;
code += len;
continue;
@@ -1694,8 +1473,7 @@ static const uint *skipcode(const uint *code, tagval &result)
++depth;
continue;
case CODE_EXIT|RET_NULL: case CODE_EXIT|RET_STR: case CODE_EXIT|RET_INT: case CODE_EXIT|RET_FLOAT:
- if(depth <= 0)
- {
+ if(depth <= 0) {
forcearg(result, op&CODE_RET_MASK);
return code;
}
@@ -1705,27 +1483,23 @@ static const uint *skipcode(const uint *code, tagval &result)
}
}
-static inline void callcommand(ident *id, tagval *args, int numargs, bool lookup = false)
-{
+static inline void callcommand(ident *id, tagval *args, int numargs, bool lookup = false) {
int i = -1, fakeargs = 0;
bool rep = false;
- for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt)
- {
+ for(const char *fmt = id->args; *fmt; fmt++) switch(*fmt) {
case 'i': if(++i >= numargs) { if(rep) break; args[i].setint(0); fakeargs++; } else forceint(args[i]); break;
case 'b': if(++i >= numargs) { if(rep) break; args[i].setint(INT_MIN); fakeargs++; } else forceint(args[i]); break;
case 'f': if(++i >= numargs) { if(rep) break; args[i].setfloat(0.0f); fakeargs++; } else forcefloat(args[i]); break;
case 's': if(++i >= numargs) { if(rep) break; args[i].setstr(newstring("")); fakeargs++; } else forcestr(args[i]); break;
case 't': if(++i >= numargs) { if(rep) break; args[i].setnull(); fakeargs++; } break;
case 'e':
- if(++i >= numargs)
- {
+ if(++i >= numargs) {
if(rep) break;
static uint buf[2] = { CODE_START + 0x100, CODE_EXIT };
args[i].setcode(buf);
fakeargs++;
}
- else
- {
+ else {
vector<uint> buf;
buf.reserve(64);
compilemain(buf, numargs <= i ? "" : args[i].getstr());
@@ -1746,8 +1520,8 @@ static inline void callcommand(ident *id, tagval *args, int numargs, bool lookup
}
#define ARG(n) (id->argmask&(1<<n) ? (void *)args[n].s : (void *)&args[n].i)
#define CALLCOM(n) \
- switch(n) \
- { \
+ switch(n) { \
+ \
case 0: ((comfun)id->fun)(); break; \
case 1: ((comfun1)id->fun)(ARG(0)); break; \
case 2: ((comfun2)id->fun)(ARG(0), ARG(1)); break; \
@@ -1772,11 +1546,9 @@ cleanup:
#define MAXRUNDEPTH 255
static int rundepth = 0;
-static const uint *runcode(const uint *code, tagval &result)
-{
+static const uint *runcode(const uint *code, tagval &result) {
result.setnull();
- if(rundepth >= MAXRUNDEPTH)
- {
+ if(rundepth >= MAXRUNDEPTH) {
debugcode("exceeded recursion limit");
return skipcode(code, result);
}
@@ -1785,13 +1557,10 @@ static const uint *runcode(const uint *code, tagval &result)
int numargs = 0;
tagval args[MAXARGS+1], *prevret = commandret;
commandret = &result;
- for(;;)
- {
+ for(;;) {
uint op = *code++;
- switch(op&0xFF)
- {
+ switch(op&0xFF) {
case CODE_START: case CODE_OFFSET: continue;
-
case CODE_POP:
freearg(args[--numargs]);
continue;
@@ -1804,8 +1573,7 @@ static const uint *runcode(const uint *code, tagval &result)
case CODE_PRINT:
printvar(identmap[op>>8]);
continue;
- case CODE_LOCAL:
- {
+ case CODE_LOCAL: {
identstack locals[MAXARGS];
freearg(result);
loopi(numargs) pushalias(*args[i].id, locals[i]);
@@ -1813,24 +1581,19 @@ static const uint *runcode(const uint *code, tagval &result)
loopi(numargs) popalias(*args[i].id);
goto exit;
}
-
- case CODE_MACRO:
- {
+ case CODE_MACRO: {
uint len = op>>8;
args[numargs++].setmacro(code);
code += len/sizeof(uint) + 1;
continue;
}
-
- case CODE_VAL|RET_STR:
- {
+ case CODE_VAL|RET_STR: {
uint len = op>>8;
args[numargs++].setstr(newstring((const char *)code, len));
code += len/sizeof(uint) + 1;
continue;
}
- case CODE_VALI|RET_STR:
- {
+ case CODE_VALI|RET_STR: {
char s[4] = { char((op>>8)&0xFF), char((op>>16)&0xFF), char((op>>24)&0xFF), '\0' };
args[numargs++].setstr(newstring(s));
continue;
@@ -1841,11 +1604,9 @@ static const uint *runcode(const uint *code, tagval &result)
case CODE_VALI|RET_INT: args[numargs++].setint(int(op)>>8); continue;
case CODE_VAL|RET_FLOAT: args[numargs++].setfloat(*(const float *)code++); continue;
case CODE_VALI|RET_FLOAT: args[numargs++].setfloat(float(int(op)>>8)); continue;
-
case CODE_FORCE|RET_STR: forcestr(args[numargs-1]); continue;
case CODE_FORCE|RET_INT: forceint(args[numargs-1]); continue;
case CODE_FORCE|RET_FLOAT: forcefloat(args[numargs-1]); continue;
-
case CODE_RESULT|RET_NULL: case CODE_RESULT|RET_STR: case CODE_RESULT|RET_INT: case CODE_RESULT|RET_FLOAT:
litval:
freearg(result);
@@ -1854,20 +1615,16 @@ static const uint *runcode(const uint *code, tagval &result)
args[0].setnull();
freeargs(args, numargs, 0);
continue;
-
- case CODE_BLOCK:
- {
+ case CODE_BLOCK: {
uint len = op>>8;
args[numargs++].setcode(code+1);
code += len;
continue;
}
- case CODE_COMPILE:
- {
+ case CODE_COMPILE: {
tagval &arg = args[numargs-1];
vector<uint> buf;
- switch(arg.type)
- {
+ switch(arg.type) {
case VAL_INT: buf.reserve(8); buf.add(CODE_START); compileint(buf, arg.i); buf.add(CODE_RESULT); buf.add(CODE_EXIT); break;
case VAL_FLOAT: buf.reserve(8); buf.add(CODE_START); compilefloat(buf, arg.f); buf.add(CODE_RESULT); buf.add(CODE_EXIT); break;
case VAL_STR: case VAL_MACRO: buf.reserve(64); compilemain(buf, arg.s); freearg(arg); break;
@@ -1877,27 +1634,22 @@ static const uint *runcode(const uint *code, tagval &result)
buf.disown();
continue;
}
-
case CODE_IDENT:
args[numargs++].setident(identmap[op>>8]);
continue;
- case CODE_IDENTARG:
- {
+ case CODE_IDENTARG: {
ident *id = identmap[op>>8];
- if(!(aliasstack->usedargs&(1<<id->index)))
- {
+ if(!(aliasstack->usedargs&(1<<id->index))) {
pusharg(*id, nullval, aliasstack->argstack[id->index]);
aliasstack->usedargs |= 1<<id->index;
}
args[numargs++].setident(id);
continue;
}
- case CODE_IDENTU:
- {
+ case CODE_IDENTU: {
tagval &arg = args[numargs-1];
ident *id = arg.type == VAL_STR || arg.type == VAL_MACRO ? newident(arg.s, IDF_UNKNOWN) : dummyident;
- if(id->index < MAXARGS && !(aliasstack->usedargs&(1<<id->index)))
- {
+ if(id->index < MAXARGS && !(aliasstack->usedargs&(1<<id->index))) {
pusharg(*id, nullval, aliasstack->argstack[id->index]);
aliasstack->usedargs |= 1<<id->index;
}
@@ -1905,14 +1657,13 @@ static const uint *runcode(const uint *code, tagval &result)
arg.setident(id);
continue;
}
-
case CODE_LOOKUPU|RET_STR:
#define LOOKUPU(aval, sval, ival, fval, nval) { \
tagval &arg = args[numargs-1]; \
if(arg.type != VAL_STR && arg.type != VAL_MACRO) continue; \
id = idents.access(arg.s); \
- if(id) switch(id->type) \
- { \
+ if(id) switch(id->type) { \
+ \
case ID_ALIAS: \
if(id->flags&IDF_UNKNOWN) break; \
freearg(arg); \
@@ -1922,8 +1673,8 @@ static const uint *runcode(const uint *code, tagval &result)
case ID_SVAR: freearg(arg); sval; continue; \
case ID_VAR: freearg(arg); ival; continue; \
case ID_FVAR: freearg(arg); fval; continue; \
- case ID_COMMAND: \
- { \
+ case ID_COMMAND: { \
+ \
freearg(arg); \
arg.setnull(); \
commandret = &arg; \
@@ -1991,24 +1742,20 @@ static const uint *runcode(const uint *code, tagval &result)
LOOKUP(id->getval(args[numargs++]));
case CODE_LOOKUPARG|RET_NULL:
LOOKUPARG(id->getval(args[numargs++]), args[numargs++].setnull());
-
case CODE_SVAR|RET_STR: case CODE_SVAR|RET_NULL: args[numargs++].setstr(newstring(*identmap[op>>8]->storage.s)); continue;
case CODE_SVAR|RET_INT: args[numargs++].setint(parseint(*identmap[op>>8]->storage.s)); continue;
case CODE_SVAR|RET_FLOAT: args[numargs++].setfloat(parsefloat(*identmap[op>>8]->storage.s)); continue;
case CODE_SVAR1: setsvarchecked(identmap[op>>8], args[0].s); freeargs(args, numargs, 0); continue;
-
case CODE_IVAR|RET_INT: case CODE_IVAR|RET_NULL: args[numargs++].setint(*identmap[op>>8]->storage.i); continue;
case CODE_IVAR|RET_STR: args[numargs++].setstr(newstring(intstr(*identmap[op>>8]->storage.i))); continue;
case CODE_IVAR|RET_FLOAT: args[numargs++].setfloat(float(*identmap[op>>8]->storage.i)); continue;
case CODE_IVAR1: setvarchecked(identmap[op>>8], args[0].i); numargs = 0; continue;
case CODE_IVAR2: setvarchecked(identmap[op>>8], (args[0].i<<16)|(args[1].i<<8)); numargs = 0; continue;
case CODE_IVAR3: setvarchecked(identmap[op>>8], (args[0].i<<16)|(args[1].i<<8)|args[2].i); numargs = 0; continue;
-
case CODE_FVAR|RET_FLOAT: case CODE_FVAR|RET_NULL: args[numargs++].setfloat(*identmap[op>>8]->storage.f); continue;
case CODE_FVAR|RET_STR: args[numargs++].setstr(newstring(floatstr(*identmap[op>>8]->storage.f))); continue;
case CODE_FVAR|RET_INT: args[numargs++].setint(int(*identmap[op>>8]->storage.f)); continue;
case CODE_FVAR1: setfvarchecked(identmap[op>>8], args[0].f); numargs = 0; continue;
-
case CODE_COM|RET_NULL: case CODE_COM|RET_STR: case CODE_COM|RET_FLOAT: case CODE_COM|RET_INT:
id = identmap[op>>8];
#ifndef STANDALONE
@@ -2034,17 +1781,14 @@ static const uint *runcode(const uint *code, tagval &result)
goto forceresult;
case CODE_COMC|RET_NULL: case CODE_COMC|RET_STR: case CODE_COMC|RET_FLOAT: case CODE_COMC|RET_INT:
id = identmap[op>>8];
- forcenull(result);
- {
+ forcenull(result); {
vector<char> buf;
buf.reserve(MAXSTRLEN);
((comfun1)id->fun)(conc(buf, args, numargs, true));
}
goto forceresult;
-
case CODE_CONC|RET_NULL: case CODE_CONC|RET_STR: case CODE_CONC|RET_FLOAT: case CODE_CONC|RET_INT:
- case CODE_CONCW|RET_NULL: case CODE_CONCW|RET_STR: case CODE_CONCW|RET_FLOAT: case CODE_CONCW|RET_INT:
- {
+ case CODE_CONCW|RET_NULL: case CODE_CONCW|RET_STR: case CODE_CONCW|RET_FLOAT: case CODE_CONCW|RET_INT: {
int numconc = op>>8;
char *s = conc(&args[numargs-numconc], numconc, (op&CODE_OP_MASK)==CODE_CONC);
freeargs(args, numargs, numargs-numconc);
@@ -2052,9 +1796,7 @@ static const uint *runcode(const uint *code, tagval &result)
forcearg(args[numargs-1], op&CODE_RET_MASK);
continue;
}
-
- case CODE_CONCM|RET_NULL: case CODE_CONCM|RET_STR: case CODE_CONCM|RET_FLOAT: case CODE_CONCM|RET_INT:
- {
+ case CODE_CONCM|RET_NULL: case CODE_CONCM|RET_STR: case CODE_CONCM|RET_FLOAT: case CODE_CONCM|RET_INT: {
int numconc = op>>8;
char *s = conc(&args[numargs-numconc], numconc, false);
freeargs(args, numargs, numargs-numconc);
@@ -2062,7 +1804,6 @@ static const uint *runcode(const uint *code, tagval &result)
forcearg(result, op&CODE_RET_MASK);
continue;
}
-
case CODE_ALIAS:
setalias(*identmap[op>>8], args[--numargs]);
freeargs(args, numargs, 0);
@@ -2076,7 +1817,6 @@ static const uint *runcode(const uint *code, tagval &result)
setalias(args[0].s, args[--numargs]);
freeargs(args, numargs, 0);
continue;
-
case CODE_CALL|RET_NULL: case CODE_CALL|RET_STR: case CODE_CALL|RET_FLOAT: case CODE_CALL|RET_INT:
#define CALLALIAS(offset) { \
identstack argstack[MAXARGS]; \
@@ -2106,8 +1846,7 @@ static const uint *runcode(const uint *code, tagval &result)
}
forcenull(result);
id = identmap[op>>8];
- if(id->flags&IDF_UNKNOWN)
- {
+ if(id->flags&IDF_UNKNOWN) {
debugcode("unknown command: %s", id->name);
goto forceresult;
}
@@ -2119,12 +1858,10 @@ static const uint *runcode(const uint *code, tagval &result)
if(!(aliasstack->usedargs&(1<<id->index))) goto forceresult;
CALLALIAS(0);
continue;
-
case CODE_CALLU|RET_NULL: case CODE_CALLU|RET_STR: case CODE_CALLU|RET_FLOAT: case CODE_CALLU|RET_INT:
if(args[0].type != VAL_STR) goto litval;
id = idents.access(args[0].s);
- if(!id)
- {
+ if(!id) {
noid:
if(checknumber(args[0].s)) goto litval;
debugcode("unknown command: %s", args[0].s);
@@ -2132,16 +1869,14 @@ static const uint *runcode(const uint *code, tagval &result)
goto forceresult;
}
forcenull(result);
- switch(id->type)
- {
+ switch(id->type) {
case ID_COMMAND:
freearg(args[0]);
callcommand(id, args+1, numargs-1);
forcearg(result, op&CODE_RET_MASK);
numargs = 0;
continue;
- case ID_LOCAL:
- {
+ case ID_LOCAL: {
identstack locals[MAXARGS];
freearg(args[0]);
loopj(numargs-1) pushalias(*forceident(args[j+1]), locals[j]);
@@ -2175,13 +1910,11 @@ exit:
return code;
}
-void executeret(const uint *code, tagval &result)
-{
+void executeret(const uint *code, tagval &result) {
runcode(code, result);
}
-void executeret(const char *p, tagval &result)
-{
+void executeret(const char *p, tagval &result) {
vector<uint> code;
code.reserve(64);
compilemain(code, p, VAL_ANY);
@@ -2189,21 +1922,18 @@ void executeret(const char *p, tagval &result)
if(int(code[0]) >= 0x100) code.disown();
}
-void executeret(ident *id, tagval *args, int numargs, bool lookup, tagval &result)
-{
+void executeret(ident *id, tagval *args, int numargs, bool lookup, tagval &result) {
result.setnull();
++rundepth;
tagval *prevret = commandret;
commandret = &result;
if(rundepth > MAXRUNDEPTH) debugcode("exceeded recursion limit");
- else if(id) switch(id->type)
- {
+ else if(id) switch(id->type) {
default:
if(!id->fun) break;
// fall-through
case ID_COMMAND:
- if(numargs < id->numargs)
- {
+ if(numargs < id->numargs) {
tagval buf[MAXARGS];
memcpy(buf, args, numargs*sizeof(tagval));
callcommand(id, buf, numargs, lookup);
@@ -2233,8 +1963,7 @@ void executeret(ident *id, tagval *args, int numargs, bool lookup, tagval &resul
--rundepth;
}
-char *executestr(const uint *code)
-{
+char *executestr(const uint *code) {
tagval result;
runcode(code, result);
if(result.type == VAL_NULL) return NULL;
@@ -2242,8 +1971,7 @@ char *executestr(const uint *code)
return result.s;
}
-char *executestr(const char *p)
-{
+char *executestr(const char *p) {
tagval result;
executeret(p, result);
if(result.type == VAL_NULL) return NULL;
@@ -2251,8 +1979,7 @@ char *executestr(const char *p)
return result.s;
}
-char *executestr(ident *id, tagval *args, int numargs, bool lookup)
-{
+char *executestr(ident *id, tagval *args, int numargs, bool lookup) {
tagval result;
executeret(id, args, numargs, lookup, result);
if(result.type == VAL_NULL) return NULL;
@@ -2260,14 +1987,12 @@ char *executestr(ident *id, tagval *args, int numargs, bool lookup)
return result.s;
}
-char *execidentstr(const char *name, bool lookup)
-{
+char *execidentstr(const char *name, bool lookup) {
ident *id = idents.access(name);
return id ? executestr(id, NULL, 0, lookup) : NULL;
}
-int execute(const uint *code)
-{
+int execute(const uint *code) {
tagval result;
runcode(code, result);
int i = result.getint();
@@ -2275,8 +2000,7 @@ int execute(const uint *code)
return i;
}
-int execute(const char *p)
-{
+int execute(const char *p) {
vector<uint> code;
code.reserve(64);
compilemain(code, p, VAL_INT);
@@ -2288,8 +2012,7 @@ int execute(const char *p)
return i;
}
-int execute(ident *id, tagval *args, int numargs, bool lookup)
-{
+int execute(ident *id, tagval *args, int numargs, bool lookup) {
tagval result;
executeret(id, args, numargs, lookup, result);
int i = result.getint();
@@ -2297,31 +2020,25 @@ int execute(ident *id, tagval *args, int numargs, bool lookup)
return i;
}
-int execident(const char *name, int noid, bool lookup)
-{
+int execident(const char *name, int noid, bool lookup) {
ident *id = idents.access(name);
return id ? execute(id, NULL, 0, lookup) : noid;
}
-static inline bool getbool(const char *s)
-{
- switch(s[0])
- {
+static inline bool getbool(const char *s) {
+ switch(s[0]) {
case '+': case '-':
- switch(s[1])
- {
+ switch(s[1]) {
case '0': break;
case '.': return !isdigit(s[2]) || parsefloat(s) != 0;
default: return true;
}
// fall through
- case '0':
- {
+ case '0': {
char *end;
int val = int(strtoul((char *)s, &end, 0));
if(val) return true;
- switch(*end)
- {
+ switch(*end) {
case 'e': case '.': return parsefloat(s) != 0;
default: return false;
}
@@ -2332,10 +2049,8 @@ static inline bool getbool(const char *s)
}
}
-static inline bool getbool(const tagval &v)
-{
- switch(v.type)
- {
+static inline bool getbool(const tagval &v) {
+ switch(v.type) {
case VAL_FLOAT: return v.f!=0;
case VAL_INT: return v.i!=0;
case VAL_STR: case VAL_MACRO: return getbool(v.s);
@@ -2343,8 +2058,7 @@ static inline bool getbool(const tagval &v)
}
}
-bool executebool(const uint *code)
-{
+bool executebool(const uint *code) {
tagval result;
runcode(code, result);
bool b = getbool(result);
@@ -2352,8 +2066,7 @@ bool executebool(const uint *code)
return b;
}
-bool executebool(const char *p)
-{
+bool executebool(const char *p) {
tagval result;
executeret(p, result);
bool b = getbool(result);
@@ -2361,8 +2074,7 @@ bool executebool(const char *p)
return b;
}
-bool executebool(ident *id, tagval *args, int numargs, bool lookup)
-{
+bool executebool(ident *id, tagval *args, int numargs, bool lookup) {
tagval result;
executeret(id, args, numargs, lookup, result);
bool b = getbool(result);
@@ -2370,19 +2082,16 @@ bool executebool(ident *id, tagval *args, int numargs, bool lookup)
return b;
}
-bool execidentbool(const char *name, bool noid, bool lookup)
-{
+bool execidentbool(const char *name, bool noid, bool lookup) {
ident *id = idents.access(name);
return id ? executebool(id, NULL, 0, lookup) : noid;
}
-bool execfile(const char *cfgfile, bool msg)
-{
+bool execfile(const char *cfgfile, bool msg) {
string s;
copystring(s, cfgfile);
char *buf = loadfile(path(s), NULL);
- if(!buf)
- {
+ if(!buf) {
if(msg) conoutf(CON_ERROR, "could not read \"%s\"", cfgfile);
return false;
}
@@ -2397,16 +2106,14 @@ bool execfile(const char *cfgfile, bool msg)
}
ICOMMAND(exec, "sb", (char *file, int *msg), intret(execfile(file, *msg != 0) ? 1 : 0));
-const char *escapestring(const char *s)
-{
+const char *escapestring(const char *s) {
static vector<char> strbuf[3];
static int stridx = 0;
stridx = (stridx + 1)%3;
vector<char> &buf = strbuf[stridx];
buf.setsize(0);
buf.add('"');
- for(; *s; s++) switch(*s)
- {
+ for(; *s; s++) switch(*s) {
case '\n': buf.put("^n", 2); break;
case '\t': buf.put("^t", 2); break;
case '\f': buf.put("^f", 2); break;
@@ -2419,27 +2126,23 @@ const char *escapestring(const char *s)
}
ICOMMAND(escape, "s", (char *s), result(escapestring(s)));
-ICOMMAND(unescape, "s", (char *s),
-{
+ICOMMAND(unescape, "s", (char *s), {
int len = strlen(s);
char *d = newstring(len);
d[unescapestring(d, s, &s[len])] = '\0';
stringret(d);
});
-const char *escapeid(const char *s)
-{
+const char *escapeid(const char *s) {
const char *end = s + strcspn(s, "\"/;()[]@ \f\t\r\n\0");
return *end ? escapestring(s) : s;
}
-bool validateblock(const char *s)
-{
+bool validateblock(const char *s) {
const int maxbrak = 100;
static char brakstack[maxbrak];
int brakdepth = 0;
- for(; *s; s++) switch(*s)
- {
+ for(; *s; s++) switch(*s) {
case '[': case '(': if(brakdepth >= maxbrak) return false; brakstack[brakdepth++] = *s; break;
case ']': if(brakdepth <= 0 || brakstack[--brakdepth] != '[') return false; break;
case ')': if(brakdepth <= 0 || brakstack[--brakdepth] != '(') return false; break;
@@ -2451,8 +2154,7 @@ bool validateblock(const char *s)
}
#ifndef STANDALONE
-void writecfg(const char *name)
-{
+void writecfg(const char *name) {
stream *f = openutf8file(path(name && name[0] ? name : game::savedconfig(), true), "w");
if(!f) return;
f->printf("// automatically written on exit, DO NOT MODIFY\n// delete this file to have %s overwrite these settings\n// modify settings in game, or put settings in %s to override anything\n\n", game::defaultconfig(), game::autoexec());
@@ -2462,11 +2164,9 @@ void writecfg(const char *name)
vector<ident *> ids;
enumerate(idents, ident, id, ids.add(&id));
ids.sortname();
- loopv(ids)
- {
+ loopv(ids) {
ident &id = *ids[i];
- if(id.flags&IDF_PERSIST) switch(id.type)
- {
+ if(id.flags&IDF_PERSIST) switch(id.type) {
case ID_VAR: f->printf("%s %d\n", escapeid(id), *id.storage.i); break;
case ID_FVAR: f->printf("%s %s\n", escapeid(id), floatstr(*id.storage.f)); break;
case ID_SVAR: f->printf("%s %s\n", escapeid(id), escapestring(*id.storage.s)); break;
@@ -2475,11 +2175,9 @@ void writecfg(const char *name)
f->printf("\n");
writebinds(f);
f->printf("\n");
- loopv(ids)
- {
+ loopv(ids) {
ident &id = *ids[i];
- if(id.type==ID_ALIAS && id.flags&IDF_PERSIST && !(id.flags&IDF_OVERRIDDEN)) switch(id.valtype)
- {
+ if(id.type==ID_ALIAS && id.flags&IDF_PERSIST && !(id.flags&IDF_OVERRIDDEN)) switch(id.valtype) {
case VAL_STR:
if(!id.val.s[0]) break;
if(!validateblock(id.val.s)) { f->printf("%s = %s\n", escapeid(id), escapestring(id.val.s)); break; }
@@ -2498,8 +2196,7 @@ void writecfg(const char *name)
COMMAND(writecfg, "s");
#endif
-void changedvars()
-{
+void changedvars() {
vector<ident *> ids;
enumerate(idents, ident, id, if(id.flags&IDF_OVERRIDDEN) ids.add(&id));
ids.sortname();
@@ -2513,27 +2210,23 @@ COMMAND(changedvars, "");
static string retbuf[4];
static int retidx = 0;
-const char *intstr(int v)
-{
+const char *intstr(int v) {
retidx = (retidx + 1)%4;
intformat(retbuf[retidx], v);
return retbuf[retidx];
}
-void intret(int v)
-{
+void intret(int v) {
commandret->setint(v);
}
-const char *floatstr(float v)
-{
+const char *floatstr(float v) {
retidx = (retidx + 1)%4;
floatformat(retbuf[retidx], v);
return retbuf[retidx];
}
-void floatret(float v)
-{
+void floatret(float v) {
commandret->setfloat(v);
}
@@ -2544,11 +2237,9 @@ ICOMMAND(do, "e", (uint *body), executeret(body, *commandret));
ICOMMAND(if, "tee", (tagval *cond, uint *t, uint *f), executeret(getbool(*cond) ? t : f, *commandret));
ICOMMAND(?, "ttt", (tagval *cond, tagval *t, tagval *f), result(*(getbool(*cond) ? t : f)));
-ICOMMAND(pushif, "rte", (ident *id, tagval *v, uint *code),
-{
+ICOMMAND(pushif, "rte", (ident *id, tagval *v, uint *code), {
if(id->type != ID_ALIAS || id->index < MAXARGS) return;
- if(getbool(*v))
- {
+ if(getbool(*v)) {
identstack stack;
pusharg(*id, *v, stack);
v->type = VAL_NULL;
@@ -2558,63 +2249,51 @@ ICOMMAND(pushif, "rte", (ident *id, tagval *v, uint *code),
}
});
-void loopiter(ident *id, identstack &stack, const tagval &v)
-{
- if(id->stack != &stack)
- {
+void loopiter(ident *id, identstack &stack, const tagval &v) {
+ if(id->stack != &stack) {
pusharg(*id, v, stack);
id->flags &= ~IDF_UNKNOWN;
}
- else
- {
+ else {
if(id->valtype == VAL_STR) delete[] id->val.s;
cleancode(*id);
id->setval(v);
}
}
-void loopend(ident *id, identstack &stack)
-{
+void loopend(ident *id, identstack &stack) {
if(id->stack == &stack) poparg(*id);
}
-static inline void setiter(ident &id, int i, identstack &stack)
-{
- if(id.stack == &stack)
- {
- if(id.valtype != VAL_INT)
- {
+static inline void setiter(ident &id, int i, identstack &stack) {
+ if(id.stack == &stack) {
+ if(id.valtype != VAL_INT) {
if(id.valtype == VAL_STR) delete[] id.val.s;
cleancode(id);
id.valtype = VAL_INT;
}
id.val.i = i;
}
- else
- {
+ else {
tagval t;
t.setint(i);
pusharg(id, t, stack);
id.flags &= ~IDF_UNKNOWN;
}
}
-ICOMMAND(loop, "rie", (ident *id, int *n, uint *body),
-{
+ICOMMAND(loop, "rie", (ident *id, int *n, uint *body), {
if(*n <= 0 || id->type!=ID_ALIAS) return;
identstack stack;
- loopi(*n)
- {
+ loopi(*n) {
setiter(*id, i, stack);
execute(body);
}
poparg(*id);
});
-ICOMMAND(loopwhile, "riee", (ident *id, int *n, uint *cond, uint *body),
-{
+ICOMMAND(loopwhile, "riee", (ident *id, int *n, uint *cond, uint *body), {
if(*n <= 0 || id->type!=ID_ALIAS) return;
identstack stack;
- loopi(*n)
- {
+ loopi(*n) {
setiter(*id, i, stack);
if(!executebool(cond)) break;
execute(body);
@@ -2623,12 +2302,10 @@ ICOMMAND(loopwhile, "riee", (ident *id, int *n, uint *cond, uint *body),
});
ICOMMAND(while, "ee", (uint *cond, uint *body), while(executebool(cond)) execute(body));
-char *loopconc(ident *id, int n, uint *body, bool space)
-{
+char *loopconc(ident *id, int n, uint *body, bool space) {
identstack stack;
vector<char> s;
- loopi(n)
- {
+ loopi(n) {
setiter(*id, i, stack);
tagval v;
executeret(body, v);
@@ -2643,39 +2320,32 @@ char *loopconc(ident *id, int n, uint *body, bool space)
return newstring(s.getbuf(), s.length()-1);
}
-ICOMMAND(loopconcat, "rie", (ident *id, int *n, uint *body),
-{
+ICOMMAND(loopconcat, "rie", (ident *id, int *n, uint *body), {
if(*n > 0 && id->type==ID_ALIAS) commandret->setstr(loopconc(id, *n, body, true));
});
-ICOMMAND(loopconcatword, "rie", (ident *id, int *n, uint *body),
-{
+ICOMMAND(loopconcatword, "rie", (ident *id, int *n, uint *body), {
if(*n > 0 && id->type==ID_ALIAS) commandret->setstr(loopconc(id, *n, body, false));
});
-void concat(tagval *v, int n)
-{
+void concat(tagval *v, int n) {
commandret->setstr(conc(v, n, true));
}
COMMAND(concat, "V");
-void concatword(tagval *v, int n)
-{
+void concatword(tagval *v, int n) {
commandret->setstr(conc(v, n, false));
}
COMMAND(concatword, "V");
-void append(ident *id, tagval *v, bool space)
-{
+void append(ident *id, tagval *v, bool space) {
if(id->type != ID_ALIAS || v->type == VAL_NULL) return;
- if(id->valtype == VAL_NULL)
- {
+ if(id->valtype == VAL_NULL) {
noprefix:
if(id->index < MAXARGS) setarg(*id, *v); else setalias(*id, *v);
v->type = VAL_NULL;
}
- else
- {
+ else {
const char *prefix = id->getstr();
if(!prefix[0]) goto noprefix;
tagval r;
@@ -2686,40 +2356,32 @@ void append(ident *id, tagval *v, bool space)
ICOMMAND(append, "rt", (ident *id, tagval *v), append(id, v, true));
ICOMMAND(appendword, "rt", (ident *id, tagval *v), append(id, v, false));
-void result(tagval &v)
-{
+void result(tagval &v) {
*commandret = v;
v.type = VAL_NULL;
}
-void stringret(char *s)
-{
+void stringret(char *s) {
commandret->setstr(s);
}
-void result(const char *s)
-{
+void result(const char *s) {
commandret->setstr(newstring(s));
}
-ICOMMAND(result, "t", (tagval *v),
-{
+ICOMMAND(result, "t", (tagval *v), {
*commandret = *v;
v->type = VAL_NULL;
});
-void format(tagval *args, int numargs)
-{
+void format(tagval *args, int numargs) {
vector<char> s;
const char *f = args[0].getstr();
- while(*f)
- {
+ while(*f) {
int c = *f++;
- if(c == '%')
- {
+ if(c == '%') {
int i = *f++;
- if(i >= '1' && i <= '9')
- {
+ if(i >= '1' && i <= '9') {
i -= '0';
const char *sub = i < numargs ? args[i].getstr() : "";
while(*sub) s.add(*sub++);
@@ -2735,31 +2397,25 @@ COMMAND(format, "V");
static const char *liststart = NULL, *listend = NULL, *listquotestart = NULL, *listquoteend = NULL;
-static inline void skiplist(const char *&p)
-{
- for(;;)
- {
+static inline void skiplist(const char *&p) {
+ for(;;) {
p += strspn(p, " \t\r\n");
if(p[0]!='/' || p[1]!='/') break;
p += strcspn(p, "\n\0");
}
}
-static bool parselist(const char *&s, const char *&start = liststart, const char *&end = listend, const char *&quotestart = listquotestart, const char *&quoteend = listquoteend)
-{
+static bool parselist(const char *&s, const char *&start = liststart, const char *&end = listend, const char *&quotestart = listquotestart, const char *&quoteend = listquoteend) {
skiplist(s);
- switch(*s)
- {
+ switch(*s) {
case '"': quotestart = s++; start = s; s = parsestring(s); end = s; if(*s == '"') s++; quoteend = s; break;
case '(': case '[':
quotestart = s;
start = s+1;
- for(int braktype = *s++, brak = 1;;)
- {
+ for(int braktype = *s++, brak = 1;;) {
s += strcspn(s, "\"/;()[]\0");
int c = *s++;
- switch(c)
- {
+ switch(c) {
case '\0': s--; quoteend = end = s; return true;
case '"': s = parsestring(s); if(*s == '"') s++; break;
case '/': if(*s == '/') s += strcspn(s, "\n\0"); break;
@@ -2780,34 +2436,29 @@ static bool parselist(const char *&s, const char *&start = liststart, const char
return true;
}
-void explodelist(const char *s, vector<char *> &elems, int limit)
-{
+void explodelist(const char *s, vector<char *> &elems, int limit) {
const char *start, *end;
while((limit < 0 || elems.length() < limit) && parselist(s, start, end))
elems.add(newstring(start, end-start));
}
-char *indexlist(const char *s, int pos)
-{
+char *indexlist(const char *s, int pos) {
loopi(pos) if(!parselist(s)) return newstring("");
const char *start, *end;
return parselist(s, start, end) ? newstring(start, end-start) : newstring("");
}
-int listlen(const char *s)
-{
+int listlen(const char *s) {
int n = 0;
while(parselist(s)) n++;
return n;
}
ICOMMAND(listlen, "s", (char *s), intret(listlen(s)));
-void at(tagval *args, int numargs)
-{
+void at(tagval *args, int numargs) {
if(!numargs) return;
const char *start = args[0].getstr(), *end = start + strlen(start);
- for(int i = 1; i < numargs; i++)
- {
+ for(int i = 1; i < numargs; i++) {
const char *list = start;
int pos = args[i].getint();
for(; pos > 0; pos--) if(!parselist(list)) break;
@@ -2817,28 +2468,23 @@ void at(tagval *args, int numargs)
}
COMMAND(at, "si1V");
-void substr(char *s, int *start, int *count, int *numargs)
-{
+void substr(char *s, int *start, int *count, int *numargs) {
int len = strlen(s), offset = clamp(*start, 0, len);
commandret->setstr(newstring(&s[offset], *numargs >= 3 ? clamp(*count, 0, len - offset) : len - offset));
}
COMMAND(substr, "siiN");
-void chopstr(char *s, int *lim, char *ellipsis)
-{
+void chopstr(char *s, int *lim, char *ellipsis) {
int len = strlen(s), maxlen = abs(*lim);
- if(len > maxlen)
- {
+ if(len > maxlen) {
int elen = strlen(ellipsis);
maxlen = max(maxlen, elen);
char *chopped = newstring(maxlen);
- if(*lim < 0)
- {
+ if(*lim < 0) {
memcpy(chopped, ellipsis, elen);
memcpy(&chopped[elen], &s[len - (maxlen - elen)], maxlen - elen);
}
- else
- {
+ else {
memcpy(chopped, s, maxlen - elen);
memcpy(&chopped[maxlen - elen], ellipsis, elen);
}
@@ -2849,8 +2495,7 @@ void chopstr(char *s, int *lim, char *ellipsis)
}
COMMAND(chopstr, "sis");
-void sublist(const char *s, int *skip, int *count, int *numargs)
-{
+void sublist(const char *s, int *skip, int *count, int *numargs) {
int offset = max(*skip, 0), len = *numargs >= 3 ? max(*count, 0) : -1;
loopi(offset) if(!parselist(s)) break;
if(len < 0) { if(offset > 0) skiplist(s); commandret->setstr(newstring(s)); return; }
@@ -2860,25 +2505,21 @@ void sublist(const char *s, int *skip, int *count, int *numargs)
}
COMMAND(sublist, "siiN");
-ICOMMAND(stripcolors, "s", (char *s),
-{
+ICOMMAND(stripcolors, "s", (char *s), {
int len = strlen(s);
char *d = newstring(len);
filtertext(d, s, true, false, len);
stringret(d);
});
-static inline void setiter(ident &id, char *val, identstack &stack)
-{
- if(id.stack == &stack)
- {
+static inline void setiter(ident &id, char *val, identstack &stack) {
+ if(id.stack == &stack) {
if(id.valtype == VAL_STR) delete[] id.val.s;
else id.valtype = VAL_STR;
cleancode(id);
id.val.s = val;
}
- else
- {
+ else {
tagval t;
t.setstr(val);
pusharg(id, t, stack);
@@ -2886,13 +2527,11 @@ static inline void setiter(ident &id, char *val, identstack &stack)
}
}
-void listfind(ident *id, const char *list, const uint *body)
-{
+void listfind(ident *id, const char *list, const uint *body) {
if(id->type!=ID_ALIAS) { intret(-1); return; }
identstack stack;
int n = -1;
- for(const char *s = list, *start, *end; parselist(s, start, end);)
- {
+ for(const char *s = list, *start, *end; parselist(s, start, end);) {
++n;
char *val = newstring(start, end-start);
setiter(*id, val, stack);
@@ -2904,13 +2543,11 @@ found:
}
COMMAND(listfind, "rse");
-void looplist(ident *id, const char *list, const uint *body)
-{
+void looplist(ident *id, const char *list, const uint *body) {
if(id->type!=ID_ALIAS) return;
identstack stack;
int n = 0;
- for(const char *s = list, *start, *end; parselist(s, start, end); n++)
- {
+ for(const char *s = list, *start, *end; parselist(s, start, end); n++) {
char *val = newstring(start, end-start);
setiter(*id, val, stack);
execute(body);
@@ -2919,13 +2556,11 @@ void looplist(ident *id, const char *list, const uint *body)
}
COMMAND(looplist, "rse");
-void loopsublist(ident *id, const char *list, int *skip, int *count, const uint *body)
-{
+void loopsublist(ident *id, const char *list, int *skip, int *count, const uint *body) {
if(id->type!=ID_ALIAS) return;
identstack stack;
int n = 0, offset = max(*skip, 0), len = *count < 0 ? INT_MAX : offset + *count;
- for(const char *s = list, *start, *end; parselist(s, start, end) && n < len; n++) if(n >= offset)
- {
+ for(const char *s = list, *start, *end; parselist(s, start, end) && n < len; n++) if(n >= offset) {
char *val = newstring(start, end-start);
setiter(*id, val, stack);
execute(body);
@@ -2934,19 +2569,15 @@ void loopsublist(ident *id, const char *list, int *skip, int *count, const uint
}
COMMAND(loopsublist, "rsiie");
-void looplistconc(ident *id, const char *list, const uint *body, bool space)
-{
+void looplistconc(ident *id, const char *list, const uint *body, bool space) {
if(id->type!=ID_ALIAS) return;
identstack stack;
vector<char> r;
int n = 0;
- for(const char *s = list, *start, *end; parselist(s, start, end); n++)
- {
+ for(const char *s = list, *start, *end; parselist(s, start, end); n++) {
char *val = newstring(start, end-start);
setiter(*id, val, stack);
-
if(n && space) r.add(' ');
-
tagval v;
executeret(body, v);
const char *vstr = v.getstr();
@@ -2961,19 +2592,15 @@ void looplistconc(ident *id, const char *list, const uint *body, bool space)
ICOMMAND(looplistconcat, "rse", (ident *id, char *list, uint *body), looplistconc(id, list, body, true));
ICOMMAND(looplistconcatword, "rse", (ident *id, char *list, uint *body), looplistconc(id, list, body, false));
-void listfilter(ident *id, const char *list, const uint *body)
-{
+void listfilter(ident *id, const char *list, const uint *body) {
if(id->type!=ID_ALIAS) return;
identstack stack;
vector<char> r;
int n = 0;
- for(const char *s = list, *start, *end, *quotestart, *quoteend; parselist(s, start, end, quotestart, quoteend); n++)
- {
+ for(const char *s = list, *start, *end, *quotestart, *quoteend; parselist(s, start, end, quotestart, quoteend); n++) {
char *val = newstring(start, end-start);
setiter(*id, val, stack);
-
- if(executebool(body))
- {
+ if(executebool(body)) {
if(r.length()) r.add(' ');
r.put(quotestart, quoteend-quotestart);
}
@@ -2984,18 +2611,14 @@ void listfilter(ident *id, const char *list, const uint *body)
}
COMMAND(listfilter, "rse");
-void prettylist(const char *s, const char *conj)
-{
+void prettylist(const char *s, const char *conj) {
vector<char> p;
const char *start, *end;
- for(int len = listlen(s), n = 0; parselist(s, start, end); n++)
- {
+ for(int len = listlen(s), n = 0; parselist(s, start, end); n++) {
p.put(start, end - start);
- if(n+1 < len)
- {
+ if(n+1 < len) {
if(len > 2 || !conj[0]) p.add(',');
- if(n+2 == len && conj[0])
- {
+ if(n+2 == len && conj[0]) {
p.add(' ');
p.put(conj, strlen(conj));
}
@@ -3007,11 +2630,9 @@ void prettylist(const char *s, const char *conj)
}
COMMAND(prettylist, "ss");
-int listincludes(const char *list, const char *needle, int needlelen)
-{
+int listincludes(const char *list, const char *needle, int needlelen) {
int offset = 0;
- for(const char *s = list, *start, *end; parselist(s, start, end);)
- {
+ for(const char *s = list, *start, *end; parselist(s, start, end);) {
int len = end - start;
if(needlelen == len && !strncmp(needle, start, len)) return offset;
offset++;
@@ -3020,13 +2641,10 @@ int listincludes(const char *list, const char *needle, int needlelen)
}
ICOMMAND(indexof, "ss", (char *list, char *elem), intret(listincludes(list, elem, strlen(elem))));
-char *listdel(const char *s, const char *del)
-{
+char *listdel(const char *s, const char *del) {
vector<char> p;
- for(const char *start, *end, *qstart, *qend; parselist(s, start, end, qstart, qend);)
- {
- if(listincludes(del, start, end-start) < 0)
- {
+ for(const char *start, *end, *qstart, *qend; parselist(s, start, end, qstart, qend);) {
+ if(listincludes(del, start, end-start) < 0) {
if(!p.empty()) p.add(' ');
p.put(qstart, qend-qstart);
}
@@ -3036,22 +2654,19 @@ char *listdel(const char *s, const char *del)
}
ICOMMAND(listdel, "ss", (char *list, char *del), commandret->setstr(listdel(list, del)));
-void listsplice(const char *s, const char *vals, int *skip, int *count)
-{
+void listsplice(const char *s, const char *vals, int *skip, int *count) {
int offset = max(*skip, 0), len = max(*count, 0);
const char *list = s, *start, *end, *qstart, *qend = s;
loopi(offset) if(!parselist(s, start, end, qstart, qend)) break;
vector<char> p;
if(qend > list) p.put(list, qend-list);
- if(*vals)
- {
+ if(*vals) {
if(!p.empty()) p.add(' ');
p.put(vals, strlen(vals));
}
loopi(len) if(!parselist(s)) break;
skiplist(s);
- switch(*s)
- {
+ switch(*s) {
case '\0': case ')': case ']': break;
default:
if(!p.empty()) p.add(' ');
@@ -3063,30 +2678,25 @@ void listsplice(const char *s, const char *vals, int *skip, int *count)
}
COMMAND(listsplice, "ssii");
-ICOMMAND(loopfiles, "rsse", (ident *id, char *dir, char *ext, uint *body),
-{
+ICOMMAND(loopfiles, "rsse", (ident *id, char *dir, char *ext, uint *body), {
if(id->type!=ID_ALIAS) return;
identstack stack;
vector<char *> files;
listfiles(dir, ext[0] ? ext : NULL, files);
- loopvrev(files)
- {
+ loopvrev(files) {
char *file = files[i];
bool redundant = false;
loopj(i) if(!strcmp(files[j], file)) { redundant = true; break; }
if(redundant) delete[] files.removeunordered(i);
}
- loopv(files)
- {
+ loopv(files) {
char *file = files[i];
- if(i)
- {
+ if(i) {
if(id->valtype == VAL_STR) delete[] id->val.s;
else id->valtype = VAL_STR;
id->val.s = file;
}
- else
- {
+ else {
tagval t;
t.setstr(file);
pusharg(*id, t, stack);
@@ -3097,8 +2707,7 @@ ICOMMAND(loopfiles, "rsse", (ident *id, char *dir, char *ext, uint *body),
if(files.length()) poparg(*id);
});
-void findfile_(char *name)
-{
+void findfile_(char *name) {
string fname;
copystring(fname, name);
path(fname);
@@ -3111,18 +2720,14 @@ void findfile_(char *name)
}
COMMANDN(findfile, findfile_, "s");
-struct sortitem
-{
+struct sortitem {
const char *str, *quotestart, *quoteend;
};
-struct sortfun
-{
+struct sortfun {
ident *x, *y;
uint *body;
-
- bool operator()(const sortitem &xval, const sortitem &yval)
- {
+ bool operator()(const sortitem &xval, const sortitem &yval) {
if(x->valtype != VAL_MACRO) x->valtype = VAL_MACRO;
cleancode(*x);
x->val.code = (const uint *)xval.str;
@@ -3133,43 +2738,33 @@ struct sortfun
}
};
-void sortlist(char *list, ident *x, ident *y, uint *body)
-{
+void sortlist(char *list, ident *x, ident *y, uint *body) {
if(x == y || x->type != ID_ALIAS || y->type != ID_ALIAS) return;
-
vector<sortitem> items;
int macrolen = strlen(list), total = 0;
char *macros = newstring(list, macrolen);
const char *curlist = list, *start, *end, *quotestart, *quoteend;
- while(parselist(curlist, start, end, quotestart, quoteend))
- {
+ while(parselist(curlist, start, end, quotestart, quoteend)) {
macros[end - list] = '\0';
sortitem item = { &macros[start - list], quotestart, quoteend };
items.add(item);
total += int(quoteend - quotestart);
}
-
identstack xstack, ystack;
pusharg(*x, nullval, xstack); x->flags &= ~IDF_UNKNOWN;
pusharg(*y, nullval, ystack); y->flags &= ~IDF_UNKNOWN;
-
sortfun f = { x, y, body };
items.sort(f);
-
poparg(*x);
poparg(*y);
-
char *sorted = macros;
int sortedlen = total + max(items.length() - 1, 0);
- if(macrolen < sortedlen)
- {
+ if(macrolen < sortedlen) {
delete[] macros;
sorted = newstring(sortedlen);
}
-
int offset = 0;
- loopv(items)
- {
+ loopv(items) {
sortitem &item = items[i];
int len = int(item.quoteend - item.quotestart);
if(i) sorted[offset++] = ' ';
@@ -3177,7 +2772,6 @@ void sortlist(char *list, ident *x, ident *y, uint *body)
offset += len;
}
sorted[offset] = '\0';
-
commandret->setstr(sorted);
}
COMMAND(sortlist, "srre");
@@ -3210,21 +2804,17 @@ ICOMMAND(&~, "ii", (int *a, int *b), intret(*a & ~*b));
ICOMMAND(|~, "ii", (int *a, int *b), intret(*a | ~*b));
ICOMMAND(<<, "ii", (int *a, int *b), intret(*b < 32 ? *a << max(*b, 0) : 0));
ICOMMAND(>>, "ii", (int *a, int *b), intret(*a >> clamp(*b, 0, 31)));
-ICOMMAND(&&, "e1V", (tagval *args, int numargs),
-{
+ICOMMAND(&&, "e1V", (tagval *args, int numargs), {
if(!numargs) intret(1);
- else loopi(numargs)
- {
+ else loopi(numargs) {
if(i) freearg(*commandret);
executeret(args[i].code, *commandret);
if(!getbool(*commandret)) break;
}
});
-ICOMMAND(||, "e1V", (tagval *args, int numargs),
-{
+ICOMMAND(||, "e1V", (tagval *args, int numargs), {
if(!numargs) intret(0);
- else loopi(numargs)
- {
+ else loopi(numargs) {
if(i) freearg(*commandret);
executeret(args[i].code, *commandret);
if(getbool(*commandret)) break;
@@ -3248,26 +2838,22 @@ ICOMMAND(loge, "f", (float *a), floatret(log(*a)));
ICOMMAND(log2, "f", (float *a), floatret(log(*a)/M_LN2));
ICOMMAND(log10, "f", (float *a), floatret(log10(*a)));
ICOMMAND(exp, "f", (float *a), floatret(exp(*a)));
-ICOMMAND(min, "V", (tagval *args, int numargs),
-{
+ICOMMAND(min, "V", (tagval *args, int numargs), {
int val = numargs > 0 ? args[numargs - 1].getint() : 0;
loopi(numargs - 1) val = min(val, args[i].getint());
intret(val);
});
-ICOMMAND(max, "V", (tagval *args, int numargs),
-{
+ICOMMAND(max, "V", (tagval *args, int numargs), {
int val = numargs > 0 ? args[numargs - 1].getint() : 0;
loopi(numargs - 1) val = max(val, args[i].getint());
intret(val);
});
-ICOMMAND(minf, "V", (tagval *args, int numargs),
-{
+ICOMMAND(minf, "V", (tagval *args, int numargs), {
float val = numargs > 0 ? args[numargs - 1].getfloat() : 0.0f;
loopi(numargs - 1) val = min(val, args[i].getfloat());
floatret(val);
});
-ICOMMAND(maxf, "V", (tagval *args, int numargs),
-{
+ICOMMAND(maxf, "V", (tagval *args, int numargs), {
float val = numargs > 0 ? args[numargs - 1].getfloat() : 0.0f;
loopi(numargs - 1) val = max(val, args[i].getfloat());
floatret(val);
@@ -3277,12 +2863,10 @@ ICOMMAND(absf, "f", (float *n), floatret(fabs(*n)));
ICOMMAND(floor, "f", (float *n), floatret(floor(*n)));
ICOMMAND(ceil, "f", (float *n), floatret(ceil(*n)));
-ICOMMAND(round, "ff", (float *n, float *k),
-{
+ICOMMAND(round, "ff", (float *n, float *k), {
double step = *k;
double r = *n;
- if(step > 0)
- {
+ if(step > 0) {
r += step * (r < 0 ? -0.5 : 0.5);
r -= fmod(r, step);
}
@@ -3290,34 +2874,29 @@ ICOMMAND(round, "ff", (float *n, float *k),
floatret(float(r));
});
-ICOMMAND(cond, "ee2V", (tagval *args, int numargs),
-{
- for(int i = 0; i < numargs; i += 2)
- {
- if(i+1 < numargs)
- {
- if(executebool(args[i].code))
- {
+ICOMMAND(cond, "ee2V", (tagval *args, int numargs), {
+ for(int i = 0; i < numargs; i += 2) {
+ if(i+1 < numargs) {
+ if(executebool(args[i].code)) {
executeret(args[i+1].code, *commandret);
break;
}
}
- else
- {
+ else {
executeret(args[i].code, *commandret);
break;
}
}
});
#define CASECOMMAND(name, fmt, type, acc, compare) \
- ICOMMAND(name, fmt "te2V", (tagval *args, int numargs), \
- { \
+ ICOMMAND(name, fmt "te2V", (tagval *args, int numargs), { \
+ \
type val = acc; \
int i; \
- for(i = 1; i+1 < numargs; i += 2) \
- { \
- if(compare) \
- { \
+ for(i = 1; i+1 < numargs; i += 2) { \
+ \
+ if(compare) { \
+ \
executeret(args[i+1].code, *commandret); \
return; \
} \
@@ -3328,15 +2907,12 @@ CASECOMMAND(casef, "f", float, args[0].getfloat(), args[i].type == VAL_NULL || a
CASECOMMAND(cases, "s", const char *, args[0].getstr(), args[i].type == VAL_NULL || !strcmp(args[i].getstr(), val));
ICOMMAND(rnd, "ii", (int *a, int *b), intret(*a - *b > 0 ? rnd(*a - *b) + *b : *b));
-ICOMMAND(rndstr, "i", (int *len),
-{
+ICOMMAND(rndstr, "i", (int *len), {
int n = clamp(*len, 0, 10000);
char *s = newstring(n);
- for(int i = 0; i < n;)
- {
+ for(int i = 0; i < n;) {
uint r = randomMT();
- for(int j = min(i + 4, n); i < j; i++)
- {
+ for(int j = min(i + 4, n); i < j; i++) {
s[i] = (r%255) + 1;
r /= 255;
}
@@ -3361,15 +2937,12 @@ ICOMMAND(codestr, "i", (int *i), { char *s = newstring(1); s[0] = char(*i); s[1]
ICOMMAND(struni, "si", (char *s, int *i), intret(*i > 0 ? (memchr(s, 0, *i) ? 0 : cube2uni(s[*i])) : cube2uni(s[0])));
ICOMMAND(unistr, "i", (int *i), { char *s = newstring(1); s[0] = uni2cube(*i); s[1] = '\0'; stringret(s); });
-int naturalsort(const char *a, const char *b)
-{
- for(;;)
- {
+int naturalsort(const char *a, const char *b) {
+ for(;;) {
int ac = *a, bc = *b;
if(!ac) return bc ? -1 : 0;
else if(!bc) return 1;
- else if(isdigit(ac) && isdigit(bc))
- {
+ else if(isdigit(ac) && isdigit(bc)) {
while(*a == '0') a++;
while(*b == '0') b++;
const char *a0 = a, *b0 = b;
@@ -3388,8 +2961,8 @@ int naturalsort(const char *a, const char *b)
ICOMMAND(naturalsort, "ss", (char *a, char *b), intret(naturalsort(a,b)<=0));
#define STRMAPCOMMAND(name, map) \
- ICOMMAND(name, "s", (char *s), \
- { \
+ ICOMMAND(name, "s", (char *s), { \
+ \
int len = strlen(s); \
char *m = newstring(len); \
loopi(len) m[i] = map(s[i]); \
@@ -3400,23 +2973,18 @@ ICOMMAND(naturalsort, "ss", (char *a, char *b), intret(naturalsort(a,b)<=0));
STRMAPCOMMAND(strlower, cubelower);
STRMAPCOMMAND(strupper, cubeupper);
-char *strreplace(const char *s, const char *oldval, const char *newval)
-{
+char *strreplace(const char *s, const char *oldval, const char *newval) {
vector<char> buf;
-
int oldlen = strlen(oldval);
if(!oldlen) return newstring(s);
- for(;;)
- {
+ for(;;) {
const char *found = strstr(s, oldval);
- if(found)
- {
+ if(found) {
while(s < found) buf.add(*s++);
for(const char *n = newval; *n; n++) buf.add(*n);
s = found + oldlen;
}
- else
- {
+ else {
while(*s) buf.add(*s++);
buf.add('\0');
return newstring(buf.getbuf(), buf.length());
@@ -3426,8 +2994,7 @@ char *strreplace(const char *s, const char *oldval, const char *newval)
ICOMMAND(strreplace, "sss", (char *s, char *o, char *n), commandret->setstr(strreplace(s, o, n)));
-void strsplice(const char *s, const char *vals, int *skip, int *count)
-{
+void strsplice(const char *s, const char *vals, int *skip, int *count) {
int slen = strlen(s), vlen = strlen(vals),
offset = clamp(*skip, 0, slen),
len = clamp(*count, 0, slen - offset);
@@ -3443,15 +3010,13 @@ COMMAND(strsplice, "ssii");
#ifndef STANDALONE
ICOMMAND(getmillis, "i", (int *total), intret(*total ? totalmillis : lastmillis));
-struct sleepcmd
-{
+struct sleepcmd {
int delay, millis, flags;
char *command;
};
vector<sleepcmd> sleepcmds;
-void addsleep(int *msec, char *cmd)
-{
+void addsleep(int *msec, char *cmd) {
sleepcmd &s = sleepcmds.add();
s.delay = max(*msec, 1);
s.millis = lastmillis;
@@ -3461,13 +3026,10 @@ void addsleep(int *msec, char *cmd)
COMMANDN(sleep, addsleep, "is");
-void checksleep(int millis)
-{
- loopv(sleepcmds)
- {
+void checksleep(int millis) {
+ loopv(sleepcmds) {
sleepcmd &s = sleepcmds[i];
- if(millis - s.millis >= s.delay)
- {
+ if(millis - s.millis >= s.delay) {
char *cmd = s.command; // execute might create more sleep commands
s.command = NULL;
int oldflags = identflags;
@@ -3480,19 +3042,16 @@ void checksleep(int millis)
}
}
-void clearsleep(bool clearoverrides)
-{
+void clearsleep(bool clearoverrides) {
int len = 0;
- loopv(sleepcmds) if(sleepcmds[i].command)
- {
+ loopv(sleepcmds) if(sleepcmds[i].command) {
if(clearoverrides && !(sleepcmds[i].flags&IDF_OVERRIDDEN)) sleepcmds[len++] = sleepcmds[i];
else delete[] sleepcmds[i].command;
}
sleepcmds.shrink(len);
}
-void clearsleep_(int *clearoverrides)
-{
+void clearsleep_(int *clearoverrides) {
clearsleep(*clearoverrides!=0 || identflags&IDF_OVERRIDDEN);
}