]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
parsing array initializers
authorWolfgang Bumiller <wry.git@bumiller.com>
Wed, 12 Jun 2013 12:17:33 +0000 (14:17 +0200)
committerWolfgang Bumiller <wry.git@bumiller.com>
Wed, 12 Jun 2013 12:32:12 +0000 (14:32 +0200)
ast.h
parser.c

diff --git a/ast.h b/ast.h
index a0fa14defca70bfadeeee0224bf5936d6f6249b4..7f56bee618e52ae805117a1847be45861668f3da 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -180,11 +180,6 @@ struct ast_value_s
 
     const char *argcounter;
 
-    /*
-    int         vtype;
-    ast_value  *next;
-    */
-
     int  cvq;     /* const/var qualifier */
     bool isfield; /* this declares a field */
     bool isimm;   /* an immediate, not just const */
index e92cb6e6326c82faf2822e0c421ca3fbfaa453af..eac983ee3b643259f3ffb92b482febae4ffea749 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -5192,6 +5192,43 @@ static bool parser_check_qualifiers(parser_t *parser, const ast_value *var, cons
     return true;
 }
 
+static bool parse_array(parser_t *parser, ast_value *array)
+{
+    if (!parser_next(parser)) {
+        parseerror(parser, "parse error in array initializer");
+        return false;
+    }
+    while (parser->tok != '}') {
+        ast_value *v = (ast_value*)parse_expression_leave(parser, true, false, false);
+        if (!v)
+            return false;
+        if (!ast_istype(v, ast_value) || !v->hasvalue || v->cvq != CV_CONST) {
+            ast_unref(v);
+            parseerror(parser, "initializing element must be a compile time constant");
+            return false;
+        }
+        vec_push(array->initlist, v->constval);
+        ast_unref(v);
+        if (parser->tok == '}')
+            break;
+        if (parser->tok != ',' || !parser_next(parser)) {
+            parseerror(parser, "expected comma or '}' in element list");
+            return false;
+        }
+    }
+    if (!parser_next(parser) || parser->tok != ';') {
+        parseerror(parser, "expected semicolon after initializer, got %s");
+        return false;
+    }
+    /*
+    if (!parser_next(parser)) {
+        parseerror(parser, "parse error after initializer");
+        return false;
+    }
+    */
+    return true;
+}
+
 static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofields, int qualifier, ast_value *cached_typedef, bool noref, bool is_static, uint32_t qflags, char *vstring)
 {
     ast_value *var;
@@ -5803,11 +5840,9 @@ skipvar:
                 parseerror(parser, "TODO: initializers for local arrays");
                 break;
             }
-            /*
-static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma, bool truthvalue, bool with_labels);
-*/
-            parseerror(parser, "TODO: initializing global arrays is not supported yet!");
-            break;
+
+            if (!parse_array(parser, var))
+                break;
         }
         else if (var->expression.vtype == TYPE_FUNCTION && (parser->tok == '{' || parser->tok == '['))
         {