]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
Removing unused _tokennames from lexer.h
[xonotic/gmqcc.git] / parser.c
index c21bbc7c0c65a9f1a424a2413922c73e09b1128b..dec6a77a412348b0d84b5a35725ea657ff453987 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -51,6 +51,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
 static ast_block* parse_block(parser_t *parser, bool warnreturn);
 static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn);
 static ast_expression* parse_statement_or_block(parser_t *parser);
+static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases);
 static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma);
 static ast_expression* parse_expression(parser_t *parser, bool stopatcomma);
 
@@ -422,13 +423,13 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
         return false;
     }
 
-    if (sy->ops[vec_size(sy->ops)-1].paren) {
+    if (vec_last(sy->ops).paren) {
         parseerror(parser, "unmatched parenthesis");
         return false;
     }
 
-    op = &operators[sy->ops[vec_size(sy->ops)-1].etype - 1];
-    ctx = sy->ops[vec_size(sy->ops)-1].ctx;
+    op = &operators[vec_last(sy->ops).etype - 1];
+    ctx = vec_last(sy->ops).ctx;
 
     DEBUGSHUNTDO(con_out("apply %s\n", op->op));
 
@@ -459,6 +460,8 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
              (ast_istype((A), ast_value) && ((ast_value*)(A))->isconst)
 #define CanConstFold(A, B) \
              (CanConstFold1(A) && CanConstFold1(B))
+#define CanConstFold3(A, B, C) \
+             (CanConstFold1(A) && CanConstFold1(B) && CanConstFold1(C))
 #define ConstV(i) (asvalue[(i)]->constval.vvec)
 #define ConstF(i) (asvalue[(i)]->constval.vfloat)
 #define ConstS(i) (asvalue[(i)]->constval.vstring)
@@ -764,6 +767,19 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
                 out = (ast_expression*)ast_binary_new(ctx, generated_op, exprs[0], exprs[1]);
             break;
 
+        case opid2('?',':'):
+            if (exprs[1]->expression.vtype != exprs[2]->expression.vtype) {
+                ast_type_to_string(exprs[1], ty1, sizeof(ty1));
+                ast_type_to_string(exprs[2], ty2, sizeof(ty2));
+                parseerror(parser, "iperands of ternary expression must have the same type, got %s and %s", ty1, ty2);
+                return false;
+            }
+            if (CanConstFold1(exprs[0]))
+                out = (ConstF(0) ? exprs[1] : exprs[2]);
+            else
+                out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
+            break;
+
         case opid1('>'):
             generated_op += 1; /* INSTR_GT */
         case opid1('<'):
@@ -1079,6 +1095,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
      * end of a condition is an unmatched closing paren
      */
     int parens = 0;
+    int ternaries = 0;
 
     sy.out = NULL;
     sy.ops = NULL;
@@ -1266,6 +1283,12 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                 break;
             }
 
+            /* a colon without a pervious question mark cannot be a ternary */
+            if (!ternaries && op->id == opid2(':','?')) {
+                parser->tok = ':';
+                break;
+            }
+
             if (vec_size(sy.ops) && !vec_last(sy.ops).paren)
                 olast = &operators[vec_last(sy.ops).etype-1];
 
@@ -1322,6 +1345,15 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                 vec_push(sy.ops, syop(parser_ctx(parser), op));
                 vec_push(sy.ops, syparen(parser_ctx(parser), SY_PAREN_INDEX, 0));
                 wantop = false;
+            } else if (op->id == opid2('?',':')) {
+                wantop = false;
+                vec_push(sy.ops, syop(parser_ctx(parser), op));
+                wantop = false;
+                --ternaries;
+            } else if (op->id == opid2(':','?')) {
+                /* we don't push this operator */
+                wantop = false;
+                ++ternaries;
             } else {
                 DEBUGSHUNTDO(con_out("push operator %s\n", op->op));
                 vec_push(sy.ops, syop(parser_ctx(parser), op));
@@ -1331,7 +1363,9 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
         if (!parser_next(parser)) {
             goto onerr;
         }
-        if (parser->tok == ';' || (!parens && parser->tok == ']')) {
+        if (parser->tok == ';' ||
+            (!parens && parser->tok == ']'))
+        {
             break;
         }
     }
@@ -1718,7 +1752,168 @@ static bool parse_return(parser_t *parser, ast_block *block, ast_expression **ou
     return true;
 }
 
-static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
+static bool parse_break_continue(parser_t *parser, ast_block *block, ast_expression **out, bool is_continue)
+{
+    lex_ctx ctx = parser_ctx(parser);
+
+    if (!parser_next(parser) || parser->tok != ';') {
+        parseerror(parser, "expected semicolon");
+        return false;
+    }
+
+    if (!parser_next(parser))
+        parseerror(parser, "parse error");
+
+    *out = (ast_expression*)ast_breakcont_new(ctx, is_continue);
+    return true;
+}
+
+static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
+{
+    ast_expression *operand;
+    ast_value      *opval;
+    ast_switch     *switchnode;
+    ast_switch_case swcase;
+
+    lex_ctx ctx = parser_ctx(parser);
+
+    /* parse over the opening paren */
+    if (!parser_next(parser) || parser->tok != '(') {
+        parseerror(parser, "expected switch operand in parenthesis");
+        return false;
+    }
+
+    /* parse into the expression */
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected switch operand");
+        return false;
+    }
+    /* parse the operand */
+    operand = parse_expression_leave(parser, false);
+    if (!operand)
+        return false;
+
+    if (!OPTS_FLAG(RELAXED_SWITCH)) {
+        opval = (ast_value*)operand;
+        if (!ast_istype(operand, ast_value) || !opval->isconst) {
+            parseerror(parser, "case on non-constant values need to be explicitly enabled via -frelaxed-switch");
+            ast_unref(operand);
+            return false;
+        }
+    }
+
+    switchnode = ast_switch_new(ctx, operand);
+
+    /* closing paren */
+    if (parser->tok != ')') {
+        ast_delete(switchnode);
+        parseerror(parser, "expected closing paren after 'switch' operand");
+        return false;
+    }
+
+    /* parse over the opening paren */
+    if (!parser_next(parser) || parser->tok != '{') {
+        ast_delete(switchnode);
+        parseerror(parser, "expected list of cases");
+        return false;
+    }
+
+    if (!parser_next(parser)) {
+        ast_delete(switchnode);
+        parseerror(parser, "expected 'case' or 'default'");
+        return false;
+    }
+
+    /* case list! */
+    while (parser->tok != '}') {
+        ast_block *block;
+
+        if (parser->tok != TOKEN_KEYWORD) {
+            ast_delete(switchnode);
+            parseerror(parser, "expected 'case' or 'default'");
+            return false;
+        }
+        if (!strcmp(parser_tokval(parser), "case")) {
+            if (!parser_next(parser)) {
+                ast_delete(switchnode);
+                parseerror(parser, "expected expression for case");
+                return false;
+            }
+            swcase.value = parse_expression_leave(parser, false);
+            if (!swcase.value) {
+                ast_delete(switchnode);
+                parseerror(parser, "expected expression for case");
+                return false;
+            }
+        }
+        else if (!strcmp(parser_tokval(parser), "default")) {
+            swcase.value = NULL;
+            if (!parser_next(parser)) {
+                ast_delete(switchnode);
+                parseerror(parser, "expected colon");
+                return false;
+            }
+        }
+
+        /* Now the colon and body */
+        if (parser->tok != ':') {
+            if (swcase.value) ast_unref(swcase.value);
+            ast_delete(switchnode);
+            parseerror(parser, "expected colon");
+            return false;
+        }
+
+        if (!parser_next(parser)) {
+            if (swcase.value) ast_unref(swcase.value);
+            ast_delete(switchnode);
+            parseerror(parser, "expected statements or case");
+            return false;
+        }
+        block = ast_block_new(parser_ctx(parser));
+        if (!block) {
+            if (swcase.value) ast_unref(swcase.value);
+            ast_delete(switchnode);
+            return false;
+        }
+        swcase.code = (ast_expression*)block;
+        vec_push(switchnode->cases, swcase);
+        while (true) {
+            ast_expression *expr;
+            if (parser->tok == '}')
+                break;
+            if (parser->tok == TOKEN_KEYWORD) {
+                if (!strcmp(parser_tokval(parser), "case") ||
+                    !strcmp(parser_tokval(parser), "default"))
+                {
+                    break;
+                }
+            }
+            if (!parse_statement(parser, block, &expr, true)) {
+                ast_delete(switchnode);
+                return false;
+            }
+            if (!expr)
+                continue;
+            vec_push(block->exprs, expr);
+        }
+    }
+
+    /* closing paren */
+    if (parser->tok != '}') {
+        ast_delete(switchnode);
+        parseerror(parser, "expected closing paren of case list");
+        return false;
+    }
+    if (!parser_next(parser)) {
+        ast_delete(switchnode);
+        parseerror(parser, "parse error after switch");
+        return false;
+    }
+    *out = (ast_expression*)switchnode;
+    return true;
+}
+
+static bool parse_statement(parser_t *parser, ast_block *block, ast_expression **out, bool allow_cases)
 {
     if (parser->tok == TOKEN_TYPENAME || parser->tok == '.')
     {
@@ -1777,6 +1972,27 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression *
             }
             return parse_for(parser, block, out);
         }
+        else if (!strcmp(parser_tokval(parser), "break"))
+        {
+            return parse_break_continue(parser, block, out, false);
+        }
+        else if (!strcmp(parser_tokval(parser), "continue"))
+        {
+            return parse_break_continue(parser, block, out, true);
+        }
+        else if (!strcmp(parser_tokval(parser), "switch"))
+        {
+            return parse_switch(parser, block, out);
+        }
+        else if (!strcmp(parser_tokval(parser), "case") ||
+                 !strcmp(parser_tokval(parser), "default"))
+        {
+            if (!allow_cases) {
+                parseerror(parser, "unexpected 'case' label");
+                return false;
+            }
+            return true;
+        }
         parseerror(parser, "Unexpected keyword");
         return false;
     }
@@ -1812,9 +2028,11 @@ static bool GMQCC_WARN parser_pop_local(parser_t *parser)
     varentry_t *ve;
 
     ve = &vec_last(parser->locals);
-    if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
-        if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
-            rv = false;
+    if (!parser->errors) {
+        if (ast_istype(ve->var, ast_value) && !(((ast_value*)(ve->var))->uses)) {
+            if (parsewarning(parser, WARN_UNUSED_VARIABLE, "unused variable: `%s`", ve->name))
+                rv = false;
+        }
     }
     mem_d(ve->name);
     vec_pop(parser->locals);
@@ -1840,7 +2058,7 @@ static bool parse_block_into(parser_t *parser, ast_block *block, bool warnreturn
         if (parser->tok == '}')
             break;
 
-        if (!parse_statement(parser, block, &expr)) {
+        if (!parse_statement(parser, block, &expr, false)) {
             /* parseerror(parser, "parse error"); */
             block = NULL;
             goto cleanup;
@@ -1892,7 +2110,7 @@ static ast_expression* parse_statement_or_block(parser_t *parser)
     ast_expression *expr = NULL;
     if (parser->tok == '{')
         return (ast_expression*)parse_block(parser, false);
-    if (!parse_statement(parser, NULL, &expr))
+    if (!parse_statement(parser, NULL, &expr, false))
         return NULL;
     return expr;
 }
@@ -3703,6 +3921,10 @@ bool parser_finish(const char *output)
                 ir_builder_delete(ir);
                 return false;
             }
+        }
+        if (opts_dump)
+            ir_builder_dump(ir, con_out);
+        for (i = 0; i < vec_size(parser->functions); ++i) {
             if (!ir_function_finalize(parser->functions[i]->ir_func)) {
                 con_out("failed to finalize function %s\n", parser->functions[i]->name);
                 ir_builder_delete(ir);
@@ -3711,7 +3933,7 @@ bool parser_finish(const char *output)
         }
 
         if (retval) {
-            if (opts_dump)
+            if (opts_dumpfin)
                 ir_builder_dump(ir, con_out);
 
             generate_checksum(parser);