diff options
Diffstat (limited to 'src/shared/command.h')
| -rw-r--r-- | src/shared/command.h | 128 |
1 files changed, 45 insertions, 83 deletions
diff --git a/src/shared/command.h b/src/shared/command.h index ffb2115..1c6a95c 100644 --- a/src/shared/command.h +++ b/src/shared/command.h @@ -2,8 +2,7 @@ enum { VAL_NULL = 0, VAL_INT, VAL_FLOAT, VAL_STR, VAL_ANY, VAL_CODE, VAL_MACRO, VAL_IDENT }; -enum -{ +enum { CODE_START = 0, CODE_OFFSET, CODE_POP, @@ -26,11 +25,9 @@ enum CODE_LOOKUP, CODE_LOOKUPU, CODE_LOOKUPARG, CODE_ALIAS, CODE_ALIASU, CODE_ALIASARG, CODE_CALL, CODE_CALLU, CODE_CALLARG, CODE_PRINT, CODE_LOCAL, - CODE_OP_MASK = 0x3F, CODE_RET = 6, CODE_RET_MASK = 0xC0, - /* return type flags */ RET_NULL = VAL_NULL<<CODE_RET, RET_STR = VAL_STR<<CODE_RET, @@ -44,10 +41,8 @@ enum { IDF_PERSIST = 1<<0, IDF_OVERRIDE = 1<<1, IDF_HEX = 1<<2, IDF_READONLY = 1 struct ident; -struct identval -{ - union - { +struct identval { + union { int i; // ID_VAR, VAL_INT float f; // ID_FVAR, VAL_FLOAT char *s; // ID_SVAR, VAL_STR @@ -56,10 +51,8 @@ struct identval }; }; -struct tagval : identval -{ +struct tagval : identval { int type; - void setint(int val) { type = VAL_INT; i = val; } void setfloat(float val) { type = VAL_FLOAT; f = val; } void setstr(char *val) { type = VAL_STR; s = val; } @@ -67,24 +60,20 @@ struct tagval : identval void setcode(const uint *val) { type = VAL_CODE; code = val; } void setmacro(const uint *val) { type = VAL_MACRO; code = val; } void setident(ident *val) { type = VAL_IDENT; id = val; } - const char *getstr() const; int getint() const; float getfloat() const; bool getbool() const; - void cleanup(); }; -struct identstack -{ +struct identstack { identval val; int valtype; identstack *next; }; -union identvalptr -{ +union identvalptr { int *i; // ID_VAR float *f; // ID_FVAR char **s; // ID_SVAR @@ -92,97 +81,81 @@ union identvalptr typedef void (__cdecl *identfun)(); -struct ident -{ +struct ident { uchar type; // one of ID_* above - union - { + union { uchar valtype; // ID_ALIAS uchar numargs; // ID_COMMAND }; ushort flags; int index; const char *name; - union - { - struct // ID_VAR, ID_FVAR, ID_SVAR - { - union - { + union { + struct { // ID_VAR, ID_FVAR, ID_SVAR { + union { struct { int minval, maxval; }; // ID_VAR struct { float minvalf, maxvalf; }; // ID_FVAR }; identvalptr storage; identval overrideval; }; - struct // ID_ALIAS - { + struct { // ID_ALIAS { uint *code; identval val; identstack *stack; }; - struct // ID_COMMAND - { + struct { // ID_COMMAND { const char *args; uint argmask; }; }; identfun fun; // ID_VAR, ID_FVAR, ID_SVAR, ID_COMMAND - ident() {} // ID_VAR ident(int t, const char *n, int m, int x, int *s, void *f = NULL, int flags = 0) - : type(t), flags(flags | (m > x ? IDF_READONLY : 0)), name(n), minval(m), maxval(x), fun((identfun)f) - { storage.i = s; } + : type(t), flags(flags | (m > x ? IDF_READONLY : 0)), name(n), minval(m), maxval(x), fun((identfun)f) { + storage.i = s; } // ID_FVAR ident(int t, const char *n, float m, float x, float *s, void *f = NULL, int flags = 0) - : type(t), flags(flags | (m > x ? IDF_READONLY : 0)), name(n), minvalf(m), maxvalf(x), fun((identfun)f) - { storage.f = s; } + : type(t), flags(flags | (m > x ? IDF_READONLY : 0)), name(n), minvalf(m), maxvalf(x), fun((identfun)f) { + storage.f = s; } // ID_SVAR ident(int t, const char *n, char **s, void *f = NULL, int flags = 0) - : type(t), flags(flags), name(n), fun((identfun)f) - { storage.s = s; } + : type(t), flags(flags), name(n), fun((identfun)f) { + storage.s = s; } // ID_ALIAS ident(int t, const char *n, char *a, int flags) - : type(t), valtype(VAL_STR), flags(flags), name(n), code(NULL), stack(NULL) - { val.s = a; } + : type(t), valtype(VAL_STR), flags(flags), name(n), code(NULL), stack(NULL) { + val.s = a; } ident(int t, const char *n, int a, int flags) - : type(t), valtype(VAL_INT), flags(flags), name(n), code(NULL), stack(NULL) - { val.i = a; } + : type(t), valtype(VAL_INT), flags(flags), name(n), code(NULL), stack(NULL) { + val.i = a; } ident(int t, const char *n, float a, int flags) - : type(t), valtype(VAL_FLOAT), flags(flags), name(n), code(NULL), stack(NULL) - { val.f = a; } + : type(t), valtype(VAL_FLOAT), flags(flags), name(n), code(NULL), stack(NULL) { + val.f = a; } ident(int t, const char *n, int flags) - : type(t), valtype(VAL_NULL), flags(flags), name(n), code(NULL), stack(NULL) - {} + : type(t), valtype(VAL_NULL), flags(flags), name(n), code(NULL), stack(NULL) { + } ident(int t, const char *n, const tagval &v, int flags) - : type(t), valtype(v.type), flags(flags), name(n), code(NULL), stack(NULL) - { val = v; } + : type(t), valtype(v.type), flags(flags), name(n), code(NULL), stack(NULL) { + val = v; } // ID_COMMAND ident(int t, const char *n, const char *args, uint argmask, int numargs, void *f = NULL, int flags = 0) - : type(t), numargs(numargs), flags(flags), name(n), args(args), argmask(argmask), fun((identfun)f) - {} - + : type(t), numargs(numargs), flags(flags), name(n), args(args), argmask(argmask), fun((identfun)f) { + } void changed() { if(fun) fun(); } - - void setval(const tagval &v) - { + void setval(const tagval &v) { valtype = v.type; val = v; } - - void setval(const identstack &v) - { + void setval(const identstack &v) { valtype = v.valtype; val = v.val; } - - void forcenull() - { + void forcenull() { if(valtype==VAL_STR) delete[] val.s; valtype = VAL_NULL; } - float getfloat() const; int getint() const; const char *getstr() const; @@ -200,13 +173,11 @@ extern void stringret(char *s); extern void result(tagval &v); extern void result(const char *s); -static inline int parseint(const char *s) -{ +static inline int parseint(const char *s) { return int(strtoul(s, NULL, 0)); } -static inline float parsefloat(const char *s) -{ +static inline float parsefloat(const char *s) { // not all platforms (windows) can parse hexadecimal integers via strtod char *end; double val = strtod(s, &end); @@ -216,10 +187,8 @@ static inline float parsefloat(const char *s) static inline void intformat(char *buf, int v, int len = 20) { nformatstring(buf, len, "%d", v); } static inline void floatformat(char *buf, float v, int len = 20) { nformatstring(buf, len, v==int(v) ? "%.1f" : "%.6g", v); } -static inline const char *getstr(const identval &v, int type) -{ - switch(type) - { +static inline const char *getstr(const identval &v, int type) { + switch(type) { case VAL_STR: case VAL_MACRO: return v.s; case VAL_INT: return intstr(v.i); case VAL_FLOAT: return floatstr(v.f); @@ -229,10 +198,8 @@ static inline const char *getstr(const identval &v, int type) inline const char *tagval::getstr() const { return ::getstr(*this, type); } inline const char *ident::getstr() const { return ::getstr(val, valtype); } -static inline int getint(const identval &v, int type) -{ - switch(type) - { +static inline int getint(const identval &v, int type) { + switch(type) { case VAL_INT: return v.i; case VAL_FLOAT: return int(v.f); case VAL_STR: case VAL_MACRO: return parseint(v.s); @@ -242,10 +209,8 @@ static inline int getint(const identval &v, int type) inline int tagval::getint() const { return ::getint(*this, type); } inline int ident::getint() const { return ::getint(val, valtype); } -static inline float getfloat(const identval &v, int type) -{ - switch(type) - { +static inline float getfloat(const identval &v, int type) { + switch(type) { case VAL_FLOAT: return v.f; case VAL_INT: return float(v.i); case VAL_STR: case VAL_MACRO: return parsefloat(v.s); @@ -255,10 +220,8 @@ static inline float getfloat(const identval &v, int type) inline float tagval::getfloat() const { return ::getfloat(*this, type); } inline float ident::getfloat() const { return ::getfloat(val, valtype); } -inline void ident::getval(tagval &v) const -{ - switch(valtype) - { +inline void ident::getval(tagval &v) const { + switch(valtype) { case VAL_STR: case VAL_MACRO: v.setstr(newstring(val.s)); break; case VAL_INT: v.setint(val.i); break; case VAL_FLOAT: v.setfloat(val.f); break; @@ -325,8 +288,7 @@ inline void ident::getval(tagval &v) const #define SVARFR(name, cur, body) _SVARF(name, name, cur, body, IDF_OVERRIDE) // anonymous inline commands, uses nasty template trick with line numbers to keep names unique -#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 = addcommand(name, (identfun)cmdname<__LINE__>::run, nargs); void cmdname<__LINE__>::run proto \ - { b; } +#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 = addcommand(name, (identfun)cmdname<__LINE__>::run, nargs); void cmdname<__LINE__>::run proto { b; } #define ICOMMANDN(name, cmdname, nargs, proto, b) ICOMMANDNS(#name, cmdname, nargs, proto, b) #define ICOMMANDNAME(name) _icmd_##name #define ICOMMAND(name, nargs, proto, b) ICOMMANDN(name, ICOMMANDNAME(name), nargs, proto, b) |
