From: Wolfgang (Blub) Bumiller Date: Fri, 30 Nov 2012 23:50:04 +0000 (+0100) Subject: More ternary fixes X-Git-Tag: 0.1.9~147 X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=commitdiff_plain;h=b966cd4f4d10f1d9550401571e24a23e774ad6ce More ternary fixes --- diff --git a/lexer.h b/lexer.h index 49c7762..2d61f8b 100644 --- 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 }, - { ":", 3, opid2(':','?'), ASSOC_RIGHT, 9, 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 }, + { ":", 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])); diff --git a/parser.c b/parser.c index 1a5c427..d404839 100644 --- 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); + /* 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; @@ -1633,9 +1637,9 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma wantop = false; --ternaries; } else if (op->id == opid2(':','?')) { - /* we don't push this operator */ if (!parser_close_paren(parser, &sy, false)) goto onerr; + vec_push(sy.ops, syop(parser_ctx(parser), op)); wantop = false; ++ternaries; } else { diff --git a/tests/ternary.qc b/tests/ternary.qc index 0f9233a..a8e0bd7 100644 --- a/tests/ternary.qc +++ b/tests/ternary.qc @@ -12,6 +12,7 @@ void test(float cond, float v1, float v2, float a) { } void main() { + float a, b; 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); + + 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"); } diff --git a/tests/ternary.tmpl b/tests/ternary.tmpl index ebc6a41..35d015b 100644 --- a/tests/ternary.tmpl +++ b/tests/ternary.tmpl @@ -14,3 +14,6 @@ M: 1 a=other M: 0 not met M: 0 not met M: 0 not met +M: 5 +M: 9 +M: 10