]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
parse_expression_leave: also end at a closing } - enum: check for } and , after an...
[xonotic/gmqcc.git] / parser.c
index 6c59d3ca7c73cf555aeae84e6cb1a13064de5439..f99fd790cc0ac1e4a643c9315c762aa615801b63 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -109,6 +109,7 @@ static const ast_expression *intrinsic_debug_typestring = (ast_expression*)0x10;
 static void parser_enterblock(parser_t *parser);
 static bool parser_leaveblock(parser_t *parser);
 static void parser_addlocal(parser_t *parser, const char *name, ast_expression *e);
+static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e);
 static bool parse_typedef(parser_t *parser);
 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);
 static ast_block* parse_block(parser_t *parser);
@@ -928,14 +929,21 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
                     exprs[0], exprs[1]);
             break;
         case opid1('^'):
-            parseerror(parser, "TODO: bitxor");
+            compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-xor via ^");
             return false;
 
         case opid2('<','<'):
         case opid2('>','>'):
+            if (CanConstFold(exprs[0], exprs[1]) && ! NotSameType(TYPE_FLOAT)) {
+                if (op->id == opid2('<','<'))
+                    out = (ast_expression*)parser_const_float(parser, (double)((int)(ConstF(0)) << (int)(ConstF(1))));
+                else
+                    out = (ast_expression*)parser_const_float(parser, (double)((int)(ConstF(0)) >> (int)(ConstF(1))));
+                break;
+            }
         case opid3('<','<','='):
         case opid3('>','>','='):
-            parseerror(parser, "TODO: shifts");
+            compile_error(ast_ctx(exprs[0]), "Not Yet Implemented: bit-shifts");
             return false;
 
         case opid2('|','|'):
@@ -1643,8 +1651,11 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                      * other things as well.
                      */
                     if (OPTS_FLAG(ENHANCED_DIAGNOSTICS)) {
+                        correction_t corr;
+                        correct_init(&corr);
+
                         for (i = 0; i < vec_size(parser->correct_variables); i++) {
-                            correct = correct_str(parser->correct_variables[i], parser_tokval(parser));
+                            correct = correct_str(&corr, parser->correct_variables[i], parser_tokval(parser));
                             if (strcmp(correct, parser_tokval(parser))) {
                                 break;
                             } else if (correct) {
@@ -1652,6 +1663,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                                 correct = NULL;
                             }
                         }
+                        correct_free(&corr);
 
                         if (correct) {
                             parseerror(parser, "unexpected ident: %s (did you mean %s?)", parser_tokval(parser), correct);
@@ -1945,7 +1957,7 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
             goto onerr;
         }
         if (parser->tok == ';' ||
-            (!parens && parser->tok == ']'))
+            (!parens && (parser->tok == ']' || parser->tok == ')' || parser->tok == '}')))
         {
             break;
         }
@@ -1984,8 +1996,13 @@ static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool
     ast_expression *e = parse_expression_leave(parser, stopatcomma, false, with_labels);
     if (!e)
         return NULL;
+    if (parser->tok != ';') {
+        parseerror(parser, "semicolon expected after expression");
+        ast_unref(e);
+        return NULL;
+    }
     if (!parser_next(parser)) {
-        ast_delete(e);
+        ast_unref(e);
         return NULL;
     }
     return e;
@@ -2063,6 +2080,19 @@ static void parser_addlocal(parser_t *parser, const char *name, ast_expression *
     );
 }
 
+static void parser_addglobal(parser_t *parser, const char *name, ast_expression *e)
+{
+    vec_push(parser->globals, e);
+    util_htset(parser->htglobals, name, e);
+
+    /* corrector */
+    correct_add (
+         parser->correct_variables[0],
+        &parser->correct_variables_score[0],
+        name
+    );
+}
+
 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
 {
     bool       ifnot = false;
@@ -3410,6 +3440,106 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression *
     }
 }
 
+static bool parse_enum(parser_t *parser)
+{
+    qcfloat     num = 0;
+    ast_value **values = NULL;
+    ast_value  *var = NULL;
+    ast_value  *asvalue;
+
+    ast_expression *old;
+
+    if (!parser_next(parser) || parser->tok != '{') {
+        parseerror(parser, "expected `{` after `enum` keyword");
+        return false;
+    }
+
+    while (true) {
+        if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
+            if (parser->tok == '}') {
+                /* allow an empty enum */
+                break;
+            }
+            parseerror(parser, "expected identifier or `}`");
+            goto onerror;
+        }
+
+        old = parser_find_field(parser, parser_tokval(parser));
+        if (!old)
+            old = parser_find_global(parser, parser_tokval(parser));
+        if (old) {
+            parseerror(parser, "value `%s` has already been declared here: %s:%i",
+                       parser_tokval(parser), ast_ctx(old).file, ast_ctx(old).line);
+            goto onerror;
+        }
+
+        var = ast_value_new(parser_ctx(parser), parser_tokval(parser), TYPE_FLOAT);
+        vec_push(values, var);
+        var->cvq             = CV_CONST;
+        var->hasvalue        = true;
+        var->constval.vfloat = num++;
+
+        parser_addglobal(parser, var->name, (ast_expression*)var);
+
+        if (!parser_next(parser)) {
+            parseerror(parser, "expected `=`, `}` or comma after identifier");
+            goto onerror;
+        }
+
+        if (parser->tok == ',')
+            continue;
+        if (parser->tok == '}')
+            break;
+        if (parser->tok != '=') {
+            parseerror(parser, "expected `=`, `}` or comma after identifier");
+            goto onerror;
+        }
+
+        if (!parser_next(parser)) {
+            parseerror(parser, "expected expression after `=`");
+            goto onerror;
+        }
+
+        /* We got a value! */
+        old = parse_expression_leave(parser, true, false, false);
+        asvalue = (ast_value*)old;
+        if (!ast_istype(old, ast_value) || asvalue->cvq != CV_CONST || !asvalue->hasvalue) {
+            parseerror(parser, "enumeration value for must be a constant");
+            goto onerror;
+        }
+        num = (var->constval.vfloat = asvalue->constval.vfloat) + 1;
+
+        if (parser->tok == '}')
+            break;
+        if (parser->tok != ',') {
+            parseerror(parser, "expected `}` or comma after expression");
+            goto onerror;
+        }
+    }
+
+    if (parser->tok != '}') {
+        parseerror(parser, "internal error: breaking without `}`");
+        goto onerror;
+    }
+
+    if (!parser_next(parser) || parser->tok != ';') {
+        parseerror(parser, "expected semicolon after enumeration");
+        goto onerror;
+    }
+
+    if (!parser_next(parser)) {
+        parseerror(parser, "parse error after enumeration");
+        goto onerror;
+    }
+
+    vec_free(values);
+    return true;
+
+onerror:
+    vec_free(values);
+    return false;
+}
+
 static bool parse_block_into(parser_t *parser, ast_block *block)
 {
     bool   retval = true;
@@ -3602,8 +3732,7 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
                 return false;
             }
 
-            vec_push(parser->globals, (ast_expression*)thinkfunc);
-            util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
+            parser_addglobal(parser, thinkfunc->name, (ast_expression*)thinkfunc);
 
             nextthink = (ast_expression*)thinkfunc;
 
@@ -4820,12 +4949,10 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                     }
                 }
                 else {
-                    vec_push(parser->globals, (ast_expression*)var);
-                    util_htset(parser->htglobals, var->name, var);
+                    parser_addglobal(parser, var->name, (ast_expression*)var);
                     if (isvector) {
                         for (i = 0; i < 3; ++i) {
-                            vec_push(parser->globals, (ast_expression*)me[i]);
-                            util_htset(parser->htglobals, me[i]->name, me[i]);
+                            parser_addglobal(parser, me[i]->name, (ast_expression*)me[i]);
                         }
                     }
                 }
@@ -5182,6 +5309,10 @@ static bool parser_global_statement(parser_t *parser)
             return false;
         return parse_variable(parser, NULL, true, cvq, NULL, noref, is_static, qflags, vstring);
     }
+    else if (parser->tok == TOKEN_IDENT && !strcmp(parser_tokval(parser), "enum"))
+    {
+        return parse_enum(parser);
+    }
     else if (parser->tok == TOKEN_KEYWORD)
     {
         if (!strcmp(parser_tokval(parser), "typedef")) {
@@ -5319,6 +5450,10 @@ bool parser_init()
     vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
     vec_push(parser->_blocktypedefs, 0);
 
+    /* corrector */
+    vec_push(parser->correct_variables, correct_trie_new());
+    vec_push(parser->correct_variables_score, NULL);
+
     empty_ctx.file = "<internal>";
     empty_ctx.line = 0;
     parser->nil = ast_value_new(empty_ctx, "nil", TYPE_NIL);
@@ -5424,9 +5559,6 @@ void parser_cleanup()
     for (i = 0; i < vec_size(parser->correct_variables); ++i) {
         correct_del(parser->correct_variables[i], parser->correct_variables_score[i]);
     }
-    for (i = 0; i < vec_size(parser->correct_variables_score); ++i) {
-        vec_free(parser->correct_variables_score[i]);
-    }
     vec_free(parser->correct_variables);
     vec_free(parser->correct_variables_score);