]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
do-while loops
[xonotic/gmqcc.git] / parser.c
index eb1f8a459b8ee705c61dacaae6a74ea741174e6c..ee5c76da4df170b18cab98fd9b2cf40c44a1ee1f 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -41,24 +41,47 @@ MEM_VEC_FUNCTIONS(parser_t, ast_value*, imm_vector)
 MEM_VEC_FUNCTIONS(parser_t, varentry_t, locals)
 MEM_VEC_FUNCTIONS(parser_t, ast_function*, functions)
 
+static void parser_pop_local(parser_t *parser);
+static bool parser_variable(parser_t *parser, ast_block *localblock);
+static ast_block* parser_parse_block(parser_t *parser);
+static ast_expression* parser_parse_statement_or_block(parser_t *parser);
+static ast_expression* parser_expression_leave(parser_t *parser);
+static ast_expression* parser_expression(parser_t *parser);
+
 void parseerror(parser_t *parser, const char *fmt, ...)
 {
        va_list ap;
 
        parser->errors++;
 
-    if (parser)
-           printf("error %s:%lu: ", parser->lex->tok->ctx.file, (unsigned long)parser->lex->tok->ctx.line);
-       else
-           printf("error: ");
-
        va_start(ap, fmt);
-       vprintf(fmt, ap);
+    vprintmsg(LVL_ERROR, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "parse error", fmt, ap);
        va_end(ap);
 
        printf("\n");
 }
 
+/* returns true if it counts as an error */
+bool GMQCC_WARN parsewarning(parser_t *parser, int warntype, const char *fmt, ...)
+{
+       va_list ap;
+       int lvl = LVL_WARNING;
+
+    if (!OPTS_WARN(warntype))
+        return false;
+
+    if (OPTS_WARN(WARN_ERROR)) {
+           parser->errors++;
+           lvl = LVL_ERROR;
+       }
+
+       va_start(ap, fmt);
+    vprintmsg(lvl, parser->lex->tok->ctx.file, parser->lex->tok->ctx.line, "warning", fmt, ap);
+       va_end(ap);
+
+       return OPTS_WARN(WARN_ERROR);
+}
+
 bool parser_next(parser_t *parser)
 {
     /* lex_do kills the previous token */
@@ -588,6 +611,33 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
                 assignop = type_store_instr[exprs[0]->expression.vtype];
             out = (ast_expression*)ast_store_new(ctx, assignop, exprs[0], exprs[1]);
             break;
+        case opid2('+','='):
+            if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
+                parseerror(parser, "Cannot add type %s and %s",
+                           type_name[exprs[0]->expression.vtype],
+                           type_name[exprs[1]->expression.vtype]);
+                return false;
+            }
+            if (exprs[0]->expression.vtype != TYPE_VECTOR && exprs[0]->expression.vtype != TYPE_FLOAT) {
+                parseerror(parser, "type error: %s - %s not defined",
+                           type_name[exprs[0]->expression.vtype],
+                           type_name[exprs[1]->expression.vtype]);
+                return false;
+            }
+            switch (exprs[0]->expression.vtype) {
+                case TYPE_FLOAT:
+                    out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_F, exprs[0], exprs[1]);
+                    break;
+                case TYPE_VECTOR:
+                    out = (ast_expression*)ast_binary_new(ctx, INSTR_ADD_V, exprs[0], exprs[1]);
+                    break;
+                default:
+                    parseerror(parser, "type error: cannot add type %s to %s",
+                               type_name[exprs[0]->expression.vtype],
+                               type_name[exprs[1]->expression.vtype]);
+                    return false;
+            };
+            break;
     }
 #undef NotSameType
 
@@ -705,7 +755,7 @@ static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
     return true;
 }
 
-static ast_expression* parser_expression(parser_t *parser)
+static ast_expression* parser_expression_leave(parser_t *parser)
 {
     ast_expression *expr = NULL;
     shunt sy;
@@ -924,10 +974,6 @@ static ast_expression* parser_expression(parser_t *parser)
             break;
         }
     }
-    if (parens >= 0 && !parser_next(parser)) {
-        parseerror(parser, "Unexpected end of file");
-        goto onerr;
-    }
 
     while (sy.ops_count) {
         if (!parser_sy_pop(parser, &sy))
@@ -952,9 +998,17 @@ onerr:
     return NULL;
 }
 
-static bool parser_variable(parser_t *parser, ast_block *localblock);
-static ast_block* parser_parse_block(parser_t *parser);
-static ast_expression* parser_parse_statement_or_block(parser_t *parser);
+static ast_expression* parser_expression(parser_t *parser)
+{
+    ast_expression *e = parser_expression_leave(parser);
+    if (!e)
+        return NULL;
+    if (!parser_next(parser)) {
+        ast_delete(e);
+        return NULL;
+    }
+    return e;
+}
 
 static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression **out)
 {
@@ -974,7 +1028,7 @@ static bool parser_parse_if(parser_t *parser, ast_block *block, ast_expression *
         return false;
     }
     /* parse the condition */
-    cond = parser_expression(parser);
+    cond = parser_expression_leave(parser);
     if (!cond)
         return false;
     /* closing paren */
@@ -1025,27 +1079,27 @@ static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expressio
 
     /* skip the 'while' and check for opening paren */
     if (!parser_next(parser) || parser->tok != '(') {
-        parseerror(parser, "expected 'if' condition in parenthesis");
+        parseerror(parser, "expected 'while' condition in parenthesis");
         return false;
     }
     /* parse into the expression */
     if (!parser_next(parser)) {
-        parseerror(parser, "expected 'if' condition after opening paren");
+        parseerror(parser, "expected 'while' condition after opening paren");
         return false;
     }
     /* parse the condition */
-    cond = parser_expression(parser);
+    cond = parser_expression_leave(parser);
     if (!cond)
         return false;
     /* closing paren */
     if (parser->tok != ')') {
-        parseerror(parser, "expected closing paren after 'if' condition");
+        parseerror(parser, "expected closing paren after 'while' condition");
         ast_delete(cond);
         return false;
     }
     /* parse into the 'then' branch */
     if (!parser_next(parser)) {
-        parseerror(parser, "expected statement for on-true branch of 'if'");
+        parseerror(parser, "expected while-loop body");
         ast_delete(cond);
         return false;
     }
@@ -1060,6 +1114,179 @@ static bool parser_parse_while(parser_t *parser, ast_block *block, ast_expressio
     return true;
 }
 
+static bool parser_parse_dowhile(parser_t *parser, ast_block *block, ast_expression **out)
+{
+    ast_loop *aloop;
+    ast_expression *cond, *ontrue;
+
+    lex_ctx ctx = parser_ctx(parser);
+
+    /* skip the 'do' and get the body */
+    if (!parser_next(parser) || parser->tok != '(') {
+        parseerror(parser, "expected loop body");
+        return false;
+    }
+    ontrue = parser_parse_statement_or_block(parser);
+    if (!ontrue)
+        return false;
+
+    /* expect the "while" */
+    if (parser->tok != TOKEN_KEYWORD ||
+        !strcmp(parser_tokval(parser), "while"))
+    {
+        parseerror(parser, "expected 'while' and condition");
+        ast_delete(ontrue);
+        return false;
+    }
+
+    /* skip the 'while' and check for opening paren */
+    if (!parser_next(parser) || parser->tok != '(') {
+        parseerror(parser, "expected 'while' condition in parenthesis");
+        ast_delete(ontrue);
+        return false;
+    }
+    /* parse into the expression */
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected 'while' condition after opening paren");
+        ast_delete(ontrue);
+        return false;
+    }
+    /* parse the condition */
+    cond = parser_expression_leave(parser);
+    if (!cond)
+        return false;
+    /* closing paren */
+    if (parser->tok != ')') {
+        parseerror(parser, "expected closing paren after 'while' condition");
+        ast_delete(ontrue);
+        ast_delete(cond);
+        return false;
+    }
+    /* parse on */
+    if (!parser_next(parser)) {
+        parseerror(parser, "parse error");
+        ast_delete(ontrue);
+        ast_delete(cond);
+        return false;
+    }
+
+    aloop = ast_loop_new(ctx, NULL, NULL, cond, NULL, ontrue);
+    *out = (ast_expression*)aloop;
+    return true;
+}
+
+static bool parser_parse_for(parser_t *parser, ast_block *block, ast_expression **out)
+{
+    ast_loop *aloop;
+    ast_expression *initexpr, *cond, *increment, *ontrue;
+    size_t oldblocklocal;
+
+    lex_ctx ctx = parser_ctx(parser);
+
+    oldblocklocal = parser->blocklocal;
+    parser->blocklocal = parser->locals_count;
+
+    initexpr  = NULL;
+    cond      = NULL;
+    increment = NULL;
+    ontrue    = NULL;
+
+    /* skip the 'while' and check for opening paren */
+    if (!parser_next(parser) || parser->tok != '(') {
+        parseerror(parser, "expected 'for' expressions in parenthesis");
+        goto onerr;
+    }
+    /* parse into the expression */
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected 'for' initializer after opening paren");
+        goto onerr;
+    }
+
+    if (parser->tok == TOKEN_TYPENAME) {
+        if (opts_standard != COMPILER_GMQCC) {
+            if (parsewarning(parser, WARN_EXTENSIONS,
+                             "current standard does not allow variable declarations in for-loop initializers"))
+                goto onerr;
+        }
+
+        parseerror(parser, "TODO: assignment of new variables to be non-const");
+        goto onerr;
+        if (!parser_variable(parser, block))
+            goto onerr;
+    }
+    else if (parser->tok != ';')
+    {
+        initexpr = parser_expression_leave(parser);
+        if (!initexpr)
+            goto onerr;
+    }
+
+    /* move on to condition */
+    if (parser->tok != ';') {
+        parseerror(parser, "expected semicolon after for-loop initializer");
+        goto onerr;
+    }
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected for-loop condition");
+        goto onerr;
+    }
+
+    /* parse the condition */
+    if (parser->tok != ';') {
+        cond = parser_expression_leave(parser);
+        if (!cond)
+            goto onerr;
+    }
+
+    /* move on to incrementor */
+    if (parser->tok != ';') {
+        parseerror(parser, "expected semicolon after for-loop initializer");
+        goto onerr;
+    }
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected for-loop condition");
+        goto onerr;
+    }
+
+    /* parse the incrementor */
+    if (parser->tok != ')') {
+        increment = parser_expression_leave(parser);
+        if (!increment)
+            goto onerr;
+    }
+
+    /* closing paren */
+    if (parser->tok != ')') {
+        parseerror(parser, "expected closing paren after 'for-loop' incrementor");
+        goto onerr;
+    }
+    /* parse into the 'then' branch */
+    if (!parser_next(parser)) {
+        parseerror(parser, "expected for-loop body");
+        goto onerr;
+    }
+    ontrue = parser_parse_statement_or_block(parser);
+    if (!ontrue) {
+        goto onerr;
+    }
+
+    aloop = ast_loop_new(ctx, initexpr, cond, NULL, increment, ontrue);
+    *out = (ast_expression*)aloop;
+
+    while (parser->locals_count > parser->blocklocal)
+        parser_pop_local(parser);
+    parser->blocklocal = oldblocklocal;
+    return true;
+onerr:
+    if (initexpr)  ast_delete(initexpr);
+    if (cond)      ast_delete(cond);
+    if (increment) ast_delete(increment);
+    while (parser->locals_count > parser->blocklocal)
+        parser_pop_local(parser);
+    parser->blocklocal = oldblocklocal;
+    return false;
+}
+
 static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expression **out)
 {
     if (parser->tok == TOKEN_TYPENAME)
@@ -1069,6 +1296,10 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
             parseerror(parser, "cannot declare a variable from here");
             return false;
         }
+        if (opts_standard == COMPILER_QCC) {
+            if (parsewarning(parser, WARN_EXTENSIONS, "missing 'local' keyword when declaring a local variable"))
+                return false;
+        }
         if (!parser_variable(parser, block))
             return false;
         *out = NULL;
@@ -1076,7 +1307,22 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
     }
     else if (parser->tok == TOKEN_KEYWORD)
     {
-        if (!strcmp(parser_tokval(parser), "return"))
+        if (!strcmp(parser_tokval(parser), "local"))
+        {
+            if (!block) {
+                parseerror(parser, "cannot declare a local variable here");
+                return false;
+            }
+            if (!parser_next(parser)) {
+                parseerror(parser, "expected variable declaration");
+                return false;
+            }
+            if (!parser_variable(parser, block))
+                return false;
+            *out = NULL;
+            return true;
+        }
+        else if (!strcmp(parser_tokval(parser), "return"))
         {
             ast_expression *exp = NULL;
             ast_return     *ret = NULL;
@@ -1119,6 +1365,14 @@ static bool parser_parse_statement(parser_t *parser, ast_block *block, ast_expre
         {
             return parser_parse_while(parser, block, out);
         }
+        else if (!strcmp(parser_tokval(parser), "do"))
+        {
+            return parser_parse_dowhile(parser, block, out);
+        }
+        else if (!strcmp(parser_tokval(parser), "for"))
+        {
+            return parser_parse_for(parser, block, out);
+        }
         parseerror(parser, "Unexpected keyword");
         return false;
     }
@@ -1215,6 +1469,7 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
     lex_ctx       ctx;
     ast_value    *var;
     varentry_t    varent;
+    ast_expression *olddecl;
 
     int basetype = parser_token(parser)->constval.t;
 
@@ -1225,8 +1480,9 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
             return false;
         }
 
-        isfunc = false;
-        func = NULL;
+        olddecl = NULL;
+        isfunc  = false;
+        func    = NULL;
         ctx = parser_ctx(parser);
         var = parser_parse_type(parser, basetype, &isfunc);
 
@@ -1238,16 +1494,20 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
             return false;
         }
 
-        if (!localblock && parser_find_global(parser, parser_tokval(parser))) {
-            ast_value_delete(var);
-            parseerror(parser, "global already exists: %s\n", parser_tokval(parser));
-            return false;
-        }
+        if (!isfunc) {
+            if (!localblock && (olddecl = parser_find_global(parser, parser_tokval(parser)))) {
+                ast_value_delete(var);
+                parseerror(parser, "global %s already declared here: %s:%i\n",
+                           parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
+                return false;
+            }
 
-        if (localblock && parser_find_local(parser, parser_tokval(parser), parser->blocklocal)) {
-            ast_value_delete(var);
-            parseerror(parser, "local variable already exists: %s\n", parser_tokval(parser));
-            return false;
+            if (localblock && parser_find_local(parser, parser_tokval(parser), parser->blocklocal)) {
+                ast_value_delete(var);
+                parseerror(parser, "local %s already declared here: %s:%i\n",
+                           parser_tokval(parser), ast_ctx(olddecl).file, (int)ast_ctx(olddecl).line);
+                return false;
+            }
         }
 
         if (!ast_value_set_name(var, parser_tokval(parser))) {
@@ -1259,6 +1519,30 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
         if (isfunc) {
             /* a function was defined */
             ast_value *fval;
+            ast_value *proto = NULL;
+
+            if (!localblock)
+                olddecl = parser_find_global(parser, parser_tokval(parser));
+            else
+                olddecl = parser_find_local(parser, parser_tokval(parser), parser->blocklocal);
+
+            if (olddecl) {
+                /* we had a prototype */
+                if (!ast_istype(olddecl, ast_value)) {
+                    /* theoretically not possible you think?
+                     * well:
+                     * vector v;
+                     * void() v_x = {}
+                     * got it?
+                     */
+                    parseerror(parser, "cannot declare a function with the same name as a vector's member: %s",
+                               parser_tokval(parser));
+                    ast_value_delete(var);
+                    return false;
+                }
+
+                proto = (ast_value*)olddecl;
+            }
 
             /* turn var into a value of TYPE_FUNCTION, with the old var
              * as return type
@@ -1275,11 +1559,29 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
             fval->expression.next = (ast_expression*)var;
             MEM_VECTOR_MOVE(&var->expression, params, &fval->expression, params);
 
-            if (!parser_t_functions_add(parser, func)) {
-                ast_value_delete(var);
-                if (fval) ast_value_delete(fval);
-                if (func) ast_function_delete(func);
-                return false;
+            /* we compare the type late here, but it's easier than
+             * messing with the parameter-vector etc. earlier
+             */
+            if (proto) {
+                if (!ast_compare_type((ast_expression*)proto, (ast_expression*)fval)) {
+                    parseerror(parser, "prototype declared at %s:%i had a different type",
+                               ast_ctx(proto).file, ast_ctx(proto).line);
+                    ast_function_delete(func);
+                    ast_value_delete(fval);
+                    return false;
+                }
+                ast_function_delete(func);
+                ast_value_delete(fval);
+                var = proto;
+                func = var->constval.vfunc;
+            }
+            else
+            {
+                if (!parser_t_functions_add(parser, func)) {
+                    ast_function_delete(func);
+                    ast_value_delete(fval);
+                    return false;
+                }
             }
 
             var = fval;
@@ -1402,6 +1704,11 @@ static bool parser_variable(parser_t *parser, ast_block *localblock)
                 ast_block_delete(block);
                 return false;
             }
+
+            if (parser->tok == ';')
+                return parser_next(parser) || parser->tok == TOKEN_EOF;
+            else if (opts_standard == COMPILER_QCC)
+                parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
             return true;
         } else {
             parseerror(parser, "TODO, const assignment");
@@ -1578,7 +1885,7 @@ bool parser_compile(const char *filename)
             if (!parser_do(parser)) {
                 if (parser->tok == TOKEN_EOF)
                     parseerror(parser, "unexpected eof");
-                else
+                else if (!parser->errors)
                     parseerror(parser, "parse error\n");
                 lex_close(parser->lex);
                 mem_d(parser);
@@ -1698,7 +2005,8 @@ bool parser_finish(const char *output)
             }
         }
 
-        ir_builder_dump(ir, printf);
+        if (opts_dump)
+            ir_builder_dump(ir, printf);
 
         if (!ir_builder_generate(ir, output)) {
             printf("*** failed to generate output file\n");