]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
More ternary fixes
authorWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 30 Nov 2012 23:50:04 +0000 (00:50 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Fri, 30 Nov 2012 23:50:04 +0000 (00:50 +0100)
lexer.h
parser.c
tests/ternary.qc
tests/ternary.tmpl

diff --git a/lexer.h b/lexer.h
index 49c7762081454933216aac4e3d902dfa956f6e59..2d61f8bf2bc6364608a31362673d782fb002b9d4 100644 (file)
--- a/lexer.h
+++ b/lexer.h
@@ -253,7 +253,6 @@ static const oper_info fte_operators[] = {
     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
 
     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0 },
     { "!=",  2, opid2('!','='),     ASSOC_LEFT,  10,  0 },
 
     { "?",   3, opid2('?',':'),     ASSOC_RIGHT, 9,  0 },
-    { ":",   3, opid2(':','?'),     ASSOC_RIGHT, 9,  0 },
 
     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
 
     { "=",   2, opid1('='),         ASSOC_RIGHT, 8,  0 },
     { "+=",  2, opid2('+','='),     ASSOC_RIGHT, 8,  0 },
@@ -268,6 +267,8 @@ static const oper_info fte_operators[] = {
     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
 
     { "&&",  2, opid2('&','&'),     ASSOC_LEFT,  5,  0 },
     { "||",  2, opid2('|','|'),     ASSOC_LEFT,  5,  0 },
 
+    { ":",   0, opid2(':','?'),     ASSOC_RIGHT, 3,  0 },
+
     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0 }
 };
 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
     { ",",   2, opid1(','),         ASSOC_LEFT,  2,  0 }
 };
 static const size_t fte_operator_count = (sizeof(fte_operators) / sizeof(fte_operators[0]));
index 1a5c42790455aef96c556a31c5c3d0cf3b2b7140..d404839a870fdc979b569ce54dafdc8bdf296943 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -513,6 +513,10 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
 
     vec_shrinkby(sy->ops, 1);
 
 
     vec_shrinkby(sy->ops, 1);
 
+    /* op(:?) has no input and no output */
+    if (!op->operands)
+        return true;
+
     vec_shrinkby(sy->out, op->operands);
     for (i = 0; i < op->operands; ++i) {
         exprs[i]  = sy->out[vec_size(sy->out)+i].out;
     vec_shrinkby(sy->out, op->operands);
     for (i = 0; i < op->operands; ++i) {
         exprs[i]  = sy->out[vec_size(sy->out)+i].out;
@@ -1633,9 +1637,9 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                 wantop = false;
                 --ternaries;
             } else if (op->id == opid2(':','?')) {
                 wantop = false;
                 --ternaries;
             } else if (op->id == opid2(':','?')) {
-                /* we don't push this operator */
                 if (!parser_close_paren(parser, &sy, false))
                     goto onerr;
                 if (!parser_close_paren(parser, &sy, false))
                     goto onerr;
+                vec_push(sy.ops, syop(parser_ctx(parser), op));
                 wantop = false;
                 ++ternaries;
             } else {
                 wantop = false;
                 ++ternaries;
             } else {
index 0f9233a494e5412e5a345bd6622c052678009a92..a8e0bd73b5d037adabc8664a4405a9fd180b42a2 100644 (file)
@@ -12,6 +12,7 @@ void test(float cond, float v1, float v2, float a) {
 }
 
 void main() {
 }
 
 void main() {
+    float a, b;
        test(0, -99, 1, 1);
        test(0, -99, 1, 2);
        test(0, -99, 1, 3);
        test(0, -99, 1, 1);
        test(0, -99, 1, 2);
        test(0, -99, 1, 3);
@@ -24,4 +25,12 @@ void main() {
        test(1, 0, -99, 1);
        test(1, 0, -99, 2);
        test(1, 0, -99, 3);
        test(1, 0, -99, 1);
        test(1, 0, -99, 2);
        test(1, 0, -99, 3);
+
+       b = 5;
+       a = b ? 5 : 6;
+       print(ftos(a), "\n");
+       b ? a = 9 : a = 10;
+       print(ftos(a), "\n");
+       !b ? a = 9 : a = 10;
+       print(ftos(a), "\n");
 }
 }
index ebc6a41e4ccdb9269ee439e4e98a23e4da286f6e..35d015b440cd441c0c2587d6234f651b79ca92b6 100644 (file)
@@ -14,3 +14,6 @@ M: 1 a=other
 M: 0 not met
 M: 0 not met
 M: 0 not met
 M: 0 not met
 M: 0 not met
 M: 0 not met
+M: 5
+M: 9
+M: 10