]> git.sur5r.net Git - cc65/blobdiff - src/ca65/enum.c
Move all attributes and other information that is attached to a token into a
[cc65] / src / ca65 / enum.c
index f226e4433f125652ecaa2b648bd0461a0755bd42..8b123e180b2baa4a9ac5f07c9d2563a11278c8be 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2003      Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2003-2011, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -58,13 +58,14 @@ void DoEnum (void)
 /* Handle the .ENUM command */
 {
     /* Start at zero */
-    ExprNode* NextExpr = GenLiteralExpr (0);
+    long      Offs     = 0;
+    ExprNode* BaseExpr = GenLiteral0 ();
 
     /* Check for a name */
-    int Anon = (Tok != TOK_IDENT);
+    int Anon = (CurTok.Tok != TOK_IDENT);
     if (!Anon) {
         /* Enter a new scope, then skip the name */
-        SymEnterLevel (SVal, ST_ENUM, ADDR_SIZE_ABS);
+        SymEnterLevel (&CurTok.SVal, ST_ENUM, ADDR_SIZE_ABS);
         NextTok ();
     }
 
@@ -72,19 +73,19 @@ void DoEnum (void)
     ConsumeSep ();
 
     /* Read until end of struct */
-    while (Tok != TOK_ENDENUM && Tok != TOK_EOF) {
+    while (CurTok.Tok != TOK_ENDENUM && CurTok.Tok != TOK_EOF) {
 
         SymEntry* Sym;
         ExprNode* EnumExpr;
 
         /* Skip empty lines */
-        if (Tok == TOK_SEP) {
+        if (CurTok.Tok == TOK_SEP) {
             NextTok ();
             continue;
         }
 
         /* The format is "identifier [ = value ]" */
-        if (Tok != TOK_IDENT) {
+        if (CurTok.Tok != TOK_IDENT) {
             /* Maybe it's a conditional? */
             if (!CheckConditionals ()) {
                 ErrorSkip ("Identifier expected");
@@ -93,36 +94,38 @@ void DoEnum (void)
         }
 
         /* We have an identifier, generate a symbol */
-        Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
+        Sym = SymFind (CurrentScope, &CurTok.SVal, SYM_ALLOC_NEW);
 
         /* Skip the member name */
         NextTok ();
 
         /* Check for an assignment */
-        if (Tok == TOK_EQ) {
+        if (CurTok.Tok == TOK_EQ) {
 
             /* Skip the equal sign */
             NextTok ();
 
-            /* Delete the old next expression */
-            FreeExpr (NextExpr);
-
-            /* Read the new one */
+            /* Read the new expression */
             EnumExpr = Expression ();
 
+            /* Reset the base expression and the offset */
+            FreeExpr (BaseExpr);
+            BaseExpr = CloneExpr (EnumExpr);
+            Offs     = 0;
+
         } else {
 
-            EnumExpr = NextExpr;
+            /* No assignment, use last value + 1 */
+            EnumExpr = GenAddExpr (CloneExpr (BaseExpr), GenLiteralExpr (Offs));
 
         }
 
-        /* Generate the next expression from the current one */
-        NextExpr = GenAddExpr (CloneExpr (EnumExpr), GenLiteralExpr (1));
-        NextExpr = SimplifyExpr (NextExpr);
-
         /* Assign the value to the enum member */
         SymDef (Sym, EnumExpr, ADDR_SIZE_DEFAULT, SF_NONE);
 
+        /* Increment the offset for the next member */
+        ++Offs;
+
         /* Expect end of line */
         ConsumeSep ();
     }
@@ -136,9 +139,10 @@ void DoEnum (void)
     /* End of enum definition */
     Consume (TOK_ENDENUM, "`.ENDENUM' expected");
 
-    /* Free the last (unused) enum expression */
-    FreeExpr (NextExpr);
+    /* Free the base expression */
+    FreeExpr (BaseExpr);
 }
 
 
 
+