X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=rmac;a=blobdiff_plain;f=expr.c;h=dc0e22bf90b88485ee2872d3dccdc7d35ac4313d;hp=2361817784d7c0eccab31b9060de867613c1ff7d;hb=HEAD;hpb=d21544da607af148b96a9d926d4564800892aa4e diff --git a/expr.c b/expr.c index 2361817..e597257 100644 --- a/expr.c +++ b/expr.c @@ -93,24 +93,97 @@ void InitExpression(void) symbolNum = 0; } +extern int correctMathRules; +int xor(void); +int and(void); +int rel(void); +int shift(void); +int sum(void); +int product(void); + // -// Binary operators (all the same precedence) +// Binary operators (all the same precedence, +// except if -4 is passed to the command line) // +#define precedence(HIERARCHY_HIGHER, HIERARCHY_CURRENT) \ +do \ +{ \ + if (HIERARCHY_HIGHER() != OK) \ + return ERROR; \ + while (tokenClass[*tok] == HIERARCHY_CURRENT) \ + { \ + TOKEN t = *tok++; \ + if (HIERARCHY_HIGHER() != OK) \ + return ERROR; \ + *evalTokenBuffer.u32++ = t; \ + } \ +}while (0) + int expr0(void) { - if (expr1() != OK) - return ERROR; - - while (tokenClass[*tok] >= MULT) + if ( correctMathRules == 0 ) { - TOKEN t = *tok++; - if (expr1() != OK) return ERROR; - *evalTokenBuffer.u32++ = t; + while (tokenClass[*tok] >= MULT) + { + TOKEN t = *tok++; + + if (expr1() != OK) + return ERROR; + + *evalTokenBuffer.u32++ = t; + } } + else + { + // The order of C precedence (lower to higher): + // bitwise XOR ^ + // bitwise OR | + // bitwise AND & + // relational = < <= >= > != + // shifts << >> + // sum + - + // product * / + precedence(xor, OR); + } + return OK; +} +int xor(void) +{ + precedence(and, XOR); + return OK; +} + +int and(void) +{ + precedence(rel, AND); + return OK; +} + +int rel(void) +{ + precedence(shift, REL); + return OK; +} + +int shift(void) +{ + precedence(sum, SHIFT); + return OK; +} + +int sum(void) +{ + precedence(product, ADD); + return OK; +} + +int product(void) +{ + precedence(expr1, MULT); return OK; } @@ -189,7 +262,7 @@ int expr1(void) goto allright; } - return error("cannot open: \"%s\"", string[tok[1]]); + return error("cannot open: \"%s\"", string[*tok]); } allright: @@ -512,7 +585,7 @@ be converted from a linked list into an array). // // Evaluate expression. // If the expression involves only ONE external symbol, the expression is -// UNDEFINED, but it's value includes everything but the symbol value, and +// UNDEFINED, but its value includes everything but the symbol value, and // 'a_esym' is set to the external symbol. // int evexpr(TOKEN * _tk, uint64_t * a_value, WORD * a_attr, SYM ** a_esym)