]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Implemented concept of enumeration attributes (can be further extended, but currently...
authorDale Weiler <killfieldengine@gmail.com>
Fri, 8 Mar 2013 08:01:45 +0000 (08:01 +0000)
committerDale Weiler <killfieldengine@gmail.com>
Fri, 8 Mar 2013 08:01:45 +0000 (08:01 +0000)
parser.c
tests/enum.qc
tests/enum.tmpl

index 98c0c9f01172b05f3b4dcec18b927418e7ce8a6d..bd4f1c14b75936394f238fc9d91547c57955a30b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -3861,6 +3861,7 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression *
 
 static bool parse_enum(parser_t *parser)
 {
+    bool        flag = false;
     qcfloat     num = 0;
     ast_value **values = NULL;
     ast_value  *var = NULL;
@@ -3868,11 +3869,28 @@ static bool parse_enum(parser_t *parser)
 
     ast_expression *old;
 
-    if (!parser_next(parser) || parser->tok != '{') {
-        parseerror(parser, "expected `{` after `enum` keyword");
+    if (!parser_next(parser) || (parser->tok != '{' && parser->tok != ':')) {
+        parseerror(parser, "expected `{` or `:` after `enum` keyword");
         return false;
     }
 
+    /* enumeration attributes (can add more later) */
+    if (parser->tok == ':') {
+        if (!parser_next(parser) || parser->tok != TOKEN_IDENT || strcmp(parser_tokval(parser), "flag")) {
+            parseerror(parser, "expected `flag` after enumeration attribute ':'");
+            return false;
+        }
+
+        if (!parser_next(parser) || parser->tok != '{') {
+            parseerror(parser, "expected `{` after enum attribute `flag`");
+            return false;
+        }
+
+        /* flagged enumeration start from 1 */
+        num  = 1;
+        flag = true;
+    }
+
     while (true) {
         if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
             if (parser->tok == '}') {
@@ -3896,7 +3914,9 @@ static bool parse_enum(parser_t *parser)
         vec_push(values, var);
         var->cvq             = CV_CONST;
         var->hasvalue        = true;
-        var->constval.vfloat = num++;
+
+        /* for flagged enumerations increment in POTs of TWO */
+        var->constval.vfloat = (flag) ? (num *= 2) : (num ++);
 
         parser_addglobal(parser, var->name, (ast_expression*)var);
 
index da08ceee128aa2a0da962a5f9c1f852ed89b51a5..1691661e62d8d627f14e704524f5888668a98fae 100644 (file)
@@ -27,6 +27,12 @@ enum {
     N
 };
 
+enum : flag {
+    F1, /* = 1 << 1 */
+    F2, /* = 1 << 2 */
+    F3  /* = 1 << 3 */
+};
+
 void main() {
     print(ftos(A), "\n");
     print(ftos(B), "\n");
@@ -42,4 +48,8 @@ void main() {
     print(ftos(L), "\n");
     print(ftos(M), "\n");
     print(ftos(N), "\n");
+
+    print(ftos(F1), "\n");
+    print(ftos(F2), "\n");
+    print(ftos(F3), "\n");
 };
index 28cbd57495046a31567a165854a0afba808eb9ae..c39537a10d5e433d99df2644d9e17f72cc90d5d2 100644 (file)
@@ -16,3 +16,6 @@ M: 10
 M: 11
 M: 12
 M: 13
+M: 2
+M: 4
+M: 8