]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
parser_sy_pop, has to pop operators and operands and push the result
authorWolfgang Bumiller <wolfgang.linux@bumiller.com>
Fri, 20 Jul 2012 13:20:07 +0000 (15:20 +0200)
committerWolfgang Bumiller <wolfgang.linux@bumiller.com>
Fri, 20 Jul 2012 13:20:07 +0000 (15:20 +0200)
lexer.h
parser.c

diff --git a/lexer.h b/lexer.h
index 8715b652e83ea18edc6aacef560ea6877328efa4..925610115bfce06bc21614f5366e5a3d8fa795f6 100644 (file)
--- a/lexer.h
+++ b/lexer.h
@@ -117,6 +117,7 @@ enum {
 
 typedef struct {
     const char   *op;
+    unsigned int operands;
     unsigned int id;
     unsigned int assoc;
     unsigned int prec;
@@ -128,60 +129,60 @@ typedef struct {
 #define opid3(a,b,c) ((a<<16)|(b<<8)|c)
 
 static const oper_info operators[] = {
-    { "++",  opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
-    { "--",  opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
+    { "++",  1, opid3('S','+','+'), ASSOC_LEFT,  16, OP_SUFFIX},
+    { "--",  1, opid3('S','-','-'), ASSOC_LEFT,  16, OP_SUFFIX},
 
-    { ".",   opid1('.'),         ASSOC_LEFT,  15, 0 },
+    { ".",   2, opid1('.'),         ASSOC_LEFT,  15, 0 },
 
-    { "!",   opid2('!', 'P'),    ASSOC_RIGHT, 14, 0 },
-    { "~",   opid2('~', 'P'),    ASSOC_RIGHT, 14, 0 },
-    { "+",   opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
-    { "-",   opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
-    { "++",  opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
-    { "--",  opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
-/*  { "&",   opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
+    { "!",   1, opid2('!', 'P'),    ASSOC_RIGHT, 14, 0 },
+    { "~",   1, opid2('~', 'P'),    ASSOC_RIGHT, 14, 0 },
+    { "+",   1, opid2('+','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
+    { "-",   1, opid2('-','P'),     ASSOC_RIGHT, 14, OP_PREFIX },
+    { "++",  1, opid3('+','+','P'), ASSOC_RIGHT, 14, OP_PREFIX },
+    { "--",  1, opid3('-','-','P'), ASSOC_RIGHT, 14, OP_PREFIX },
+/*  { "&",   1, opid2('&','P'),     ASSOC_RIGHT, 14, OP_PREFIX }, */
 
-    { "*",   opid1('*'),         ASSOC_LEFT,  13, 0 },
-    { "/",   opid1('/'),         ASSOC_LEFT,  13, 0 },
-    { "%",   opid1('%'),         ASSOC_LEFT,  13, 0 },
+    { "*",   2, opid1('*'),         ASSOC_LEFT,  13, 0 },
+    { "/",   2, opid1('/'),         ASSOC_LEFT,  13, 0 },
+    { "%",   2, opid1('%'),         ASSOC_LEFT,  13, 0 },
 
-    { "+",   opid1('+'),         ASSOC_LEFT,  12, 0 },
-    { "-",   opid1('-'),         ASSOC_LEFT,  12, 0 },
+    { "+",   2, opid1('+'),         ASSOC_LEFT,  12, 0 },
+    { "-",   2, opid1('-'),         ASSOC_LEFT,  12, 0 },
 
-    { "<<",  opid2('<','<'),     ASSOC_LEFT,  11, 0 },
-    { ">>",  opid2('>','>'),     ASSOC_LEFT,  11, 0 },
+    { "<<",  2, opid2('<','<'),     ASSOC_LEFT,  11, 0 },
+    { ">>",  2, opid2('>','>'),     ASSOC_LEFT,  11, 0 },
 
-    { "<",   opid1('<'),         ASSOC_LEFT,  10, 0 },
-    { ">",   opid1('>'),         ASSOC_LEFT,  10, 0 },
-    { "<=",  opid2('<','='),     ASSOC_LEFT,  10, 0 },
-    { ">=",  opid2('>','='),     ASSOC_LEFT,  10, 0 },
+    { "<",   2, opid1('<'),         ASSOC_LEFT,  10, 0 },
+    { ">",   2, opid1('>'),         ASSOC_LEFT,  10, 0 },
+    { "<=",  2, opid2('<','='),     ASSOC_LEFT,  10, 0 },
+    { ">=",  2, opid2('>','='),     ASSOC_LEFT,  10, 0 },
 
-    { "==",  opid2('=','='),     ASSOC_LEFT,  9,  0 },
-    { "!=",  opid2('!','='),     ASSOC_LEFT,  9,  0 },
+    { "==",  2, opid2('=','='),     ASSOC_LEFT,  9,  0 },
+    { "!=",  2, opid2('!','='),     ASSOC_LEFT,  9,  0 },
 
-    { "&",   opid1('&'),         ASSOC_LEFT,  8,  0 },
+    { "&",   2, opid1('&'),         ASSOC_LEFT,  8,  0 },
 
-    { "^",   opid1('^'),         ASSOC_LEFT,  7,  0 },
+    { "^",   2, opid1('^'),         ASSOC_LEFT,  7,  0 },
 
-    { "|",   opid1('|'),         ASSOC_LEFT,  6,  0 },
+    { "|",   2, opid1('|'),         ASSOC_LEFT,  6,  0 },
 
-    { "&&",  opid2('&','&'),     ASSOC_LEFT,  5,  0 },
+    { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
 
-    { "||",  opid2('|','|'),     ASSOC_LEFT,  4,  0 },
+    { "||",  2, opid2('|','|'),     ASSOC_LEFT,  4,  0 },
 
-    { "?",   opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
+    { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 3,  0 },
 
-    { "=",   opid1('='),         ASSOC_RIGHT, 2,  0 },
-    { "+=",  opid2('+','='),     ASSOC_RIGHT, 2,  0 },
-    { "-=",  opid2('-','='),     ASSOC_RIGHT, 2,  0 },
-    { "*=",  opid2('*','='),     ASSOC_RIGHT, 2,  0 },
-    { "/=",  opid2('/','='),     ASSOC_RIGHT, 2,  0 },
-    { "%=",  opid2('%','='),     ASSOC_RIGHT, 2,  0 },
-    { ">>=", opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
-    { "<<=", opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
-    { "&=",  opid2('&','='),     ASSOC_RIGHT, 2,  0 },
-    { "^=",  opid2('^','='),     ASSOC_RIGHT, 2,  0 },
-    { "|=",  opid2('|','='),     ASSOC_RIGHT, 2,  0 },
+    { "=",   2, opid1('='),         ASSOC_RIGHT, 2,  0 },
+    { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 2,  0 },
+    { "-=",  2, opid2('-','='),     ASSOC_RIGHT, 2,  0 },
+    { "*=",  2, opid2('*','='),     ASSOC_RIGHT, 2,  0 },
+    { "/=",  2, opid2('/','='),     ASSOC_RIGHT, 2,  0 },
+    { "%=",  2, opid2('%','='),     ASSOC_RIGHT, 2,  0 },
+    { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2,  0 },
+    { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2,  0 },
+    { "&=",  2, opid2('&','='),     ASSOC_RIGHT, 2,  0 },
+    { "^=",  2, opid2('^','='),     ASSOC_RIGHT, 2,  0 },
+    { "|=",  2, opid2('|','='),     ASSOC_RIGHT, 2,  0 },
 };
 const size_t operator_count = (sizeof(operators) / sizeof(operators[0]));
 
index 170e33247989324c023fae009c0f70c1ddfa34a6..e03fc8fa741fe3d706e2d4c23c765912fe8ff2ba 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -209,10 +209,35 @@ static sy_elem syop(const oper_info *op) {
 
 static bool parser_sy_pop(parser_t *parser, shynt *sy)
 {
+    const oper_info *op;
+    ast_expression *vals[3];
+    size_t i;
+
     if (!sy->ops_count) {
         parseerror(parser, "internal error: missing operator");
         return false;
     }
+
+    op = &operators[sy->ops[sy->ops_count-1].etype - 1];
+
+    if (sy->out_count < op->operands) {
+        parseerror(parser, "internal error: not enough operands");
+        return false;
+    }
+
+    sy->ops_count--;
+
+    sy->out_count -= op->operands;
+    for (i = 0; i < op->operands; ++i)
+        vals[i] = sy->out[sy->out_count+i].out;
+
+    switch (op->id)
+    {
+        default:
+            parseerror(parser, "internal error: unhandled operand");
+            return false;
+    }
+
     return true;
 }