]> git.sur5r.net Git - cc65/commitdiff
Added enums
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Fri, 14 Nov 2003 09:03:32 +0000 (09:03 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Fri, 14 Nov 2003 09:03:32 +0000 (09:03 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@2665 b7a2c559-68d2-44c3-8de9-860c34a00d81

12 files changed:
src/ca65/enum.c [new file with mode: 0644]
src/ca65/enum.h [new file with mode: 0644]
src/ca65/expr.c
src/ca65/expr.h
src/ca65/make/gcc.mak
src/ca65/make/watcom.mak
src/ca65/pseudo.c
src/ca65/scanner.c
src/ca65/scanner.h
src/ca65/struct.c
src/ca65/struct.h
src/ca65/symtab.h

diff --git a/src/ca65/enum.c b/src/ca65/enum.c
new file mode 100644 (file)
index 0000000..8d9da86
--- /dev/null
@@ -0,0 +1,135 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                  enum.c                                   */
+/*                                                                           */
+/*                               .ENUM command                               */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2003      Ullrich von Bassewitz                                       */
+/*               Römerstraße 52                                              */
+/*               D-70794 Filderstadt                                         */
+/* EMail:        uz@cc65.org                                                 */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+/* common */
+#include "addrsize.h"
+
+/* ca65 */
+#include "enum.h"
+#include "error.h"
+#include "expr.h"
+#include "nexttok.h"
+#include "scanner.h"
+#include "symbol.h"
+#include "symtab.h"
+
+
+
+/*****************************************************************************/
+/*                                          Code                                    */
+/*****************************************************************************/
+
+
+
+void DoEnum (void)
+/* Handle the .ENUM command */
+{
+    /* Start at zero */
+    ExprNode* NextExpr = GenLiteralExpr (0);
+
+    /* Check for a name */
+    int Anon = (Tok != TOK_IDENT);
+    if (!Anon) {
+        /* Enter a new scope, then skip the name */
+        SymEnterLevel (SVal, ST_ENUM, ADDR_SIZE_ABS);
+        NextTok ();
+    }
+
+    /* Test for end of line */
+    ConsumeSep ();
+
+    /* Read until end of struct */
+    while (Tok != TOK_ENDENUM && Tok != TOK_EOF) {
+
+        SymEntry* Sym;
+        ExprNode* EnumExpr;
+        
+
+        /* The format is "identifier [ = value ]" */
+        if (Tok != TOK_IDENT) {
+            ErrorSkip ("Identifier expected");
+            continue;
+        }
+
+        /* We have an identifier, generate a symbol */
+        Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
+
+        /* Skip the member name */
+        NextTok ();
+
+        /* Check for an assignment */
+        if (Tok == TOK_EQ) {
+
+            /* Skip the equal sign */
+            NextTok ();
+
+            /* Delete the old next expression */
+            FreeExpr (NextExpr);
+
+            /* Read the new one */
+            EnumExpr = Expression ();
+
+        } else {
+
+            EnumExpr = NextExpr;
+
+        }
+
+        /* 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);
+
+        /* Expect end of line */
+        ConsumeSep ();
+    }
+
+    /* If this is not an anon enum, leave its scope */
+    if (!Anon) {
+        /* Close the enum scope */
+        SymLeaveLevel ();
+    }
+
+    /* End of enum definition */
+    Consume (TOK_ENDENUM, "`.ENDENUM' expected");
+
+    /* Free the last (unused) enum expression */
+    FreeExpr (NextExpr);
+}
+
+
+
diff --git a/src/ca65/enum.h b/src/ca65/enum.h
new file mode 100644 (file)
index 0000000..eecadf0
--- /dev/null
@@ -0,0 +1,57 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                  enum.h                                   */
+/*                                                                           */
+/*                               .ENUM command                               */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2003      Ullrich von Bassewitz                                       */
+/*               Römerstraße 52                                              */
+/*               D-70794 Filderstadt                                         */
+/* EMail:        uz@cc65.org                                                 */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#ifndef ENUM_H
+#define ENUM_H
+
+
+
+/*****************************************************************************/
+/*                                          Code                                    */
+/*****************************************************************************/
+
+
+
+void DoEnum (void);
+/* Handle the .ENUM command */
+
+
+
+/* End of enum.h */
+
+#endif
+
+
+
index b0eb0503c9ad351238be66bad8319d9312f72351..b7ccc9798262484c808801b3b91e88983dc87165 100644 (file)
@@ -1523,30 +1523,6 @@ static void StudyExpr (ExprNode* Expr, ExprDesc* D, int Sign)
 
 
 
-static ExprNode* SimplifyExpr (ExprNode* Expr)
-/* Try to simplify the given expression tree */
-{
-    if (Expr && Expr->Op != EXPR_LITERAL) {
-
-        /* Create an expression description and initialize it */
-        ExprDesc D;
-        InitExprDesc (&D);
-
-        /* Study the expression */
-        StudyExpr (Expr, &D, 1);
-
-        /* Now check if we can generate a literal value */
-        if (ExprDescIsConst (&D)) {
-            /* No external references */
-            FreeExpr (Expr);
-            Expr = GenLiteralExpr (D.Val);
-        }
-    }
-    return Expr;
-}
-
-
-
 ExprNode* Expression (void)
 /* Evaluate an expression, build the expression tree on the heap and return
  * a pointer to the root of the tree.
@@ -1610,6 +1586,30 @@ void FreeExpr (ExprNode* Root)
 
 
 
+ExprNode* SimplifyExpr (ExprNode* Expr)
+/* Try to simplify the given expression tree */
+{
+    if (Expr && Expr->Op != EXPR_LITERAL) {
+
+        /* Create an expression description and initialize it */
+        ExprDesc D;
+        InitExprDesc (&D);
+
+        /* Study the expression */
+        StudyExpr (Expr, &D, 1);
+
+        /* Now check if we can generate a literal value */
+        if (ExprDescIsConst (&D)) {
+            /* No external references */
+            FreeExpr (Expr);
+            Expr = GenLiteralExpr (D.Val);
+        }
+    }
+    return Expr;
+}
+
+
+
 ExprNode* GenLiteralExpr (long Val)
 /* Return an expression tree that encodes the given literal value */
 {
index 91ccbadb57edd8eaa6593be86707be307cbd7fa3..d7b046f62dcd62ed24ba9892a707f1d8f8ec58c9 100644 (file)
@@ -44,7 +44,7 @@
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
@@ -63,7 +63,10 @@ long ConstExpression (void);
 void FreeExpr (ExprNode* Root);
 /* Free the expression tree, Root is pointing to. */
 
-ExprNode* GenLiteralExpr (long Val);
+ExprNode* SimplifyExpr (ExprNode* Expr);
+/* Try to simplify the given expression tree */
+
+ExprNode* GenLiteralExpr (long Val);                   
 /* Return an expression tree that encodes the given literal value */
 
 ExprNode* GenSymExpr (struct SymEntry* Sym);
index 8fbfdbbba509bea196ad7b5181faac21894837ff..baaf3257c6cadccaefff0fd852f44fab2ab8c874 100644 (file)
@@ -15,6 +15,7 @@ OBJS =  anonname.o      \
         condasm.o      \
        dbginfo.o       \
        ea.o            \
+        enum.o          \
         error.o                \
         expr.o         \
        feature.o       \
index bcc4ab7743624441a0dba86dc3b9f74154a08bbd..dd04e44dfcf344cfcdba9edff8e627bb894b09f8 100644 (file)
@@ -64,6 +64,7 @@ OBJS =        anonname.obj    \
         condasm.obj    \
        dbginfo.obj     \
        ea.obj          \
+        enum.obj        \
        error.obj       \
        expr.obj        \
        feature.obj     \
index c3752168a1c2e0496b7d9ab986626abd1f966c46..419f226b812937cda2139ccc333f7574ac4dcec2 100644 (file)
@@ -53,6 +53,7 @@
 #include "asserts.h"
 #include "condasm.h"
 #include "dbginfo.h"
+#include "enum.h"
 #include "error.h"
 #include "expr.h"
 #include "feature.h"
@@ -1639,6 +1640,7 @@ static CtrlDesc CtrlCmdTab [] = {
     { ccKeepToken,     DoConditionals  },      /* .ELSE */
     { ccKeepToken,     DoConditionals  },      /* .ELSEIF */
     { ccKeepToken,             DoEnd           },
+    { ccNone,           DoUnexpected    },      /* .ENDENUM */
     { ccKeepToken,     DoConditionals  },      /* .ENDIF */
     { ccNone,          DoUnexpected    },      /* .ENDMACRO */
     { ccNone,          DoEndProc       },
@@ -1646,6 +1648,7 @@ static CtrlDesc CtrlCmdTab [] = {
     { ccNone,           DoEndScope      },
     { ccNone,           DoUnexpected    },      /* .ENDSTRUCT */
     { ccNone,           DoUnexpected    },      /* .ENDUNION */
+    { ccNone,           DoEnum          },
     { ccNone,          DoError         },
     { ccNone,          DoExitMacro     },
     { ccNone,          DoExport        },
index e75b73e02225af681e6d19ceb27dd1e5c3e30eb8..469d997a37a249f4772d065dc221362960c4a250 100644 (file)
@@ -154,6 +154,7 @@ struct DotKeyword {
     { ".ELSE",         TOK_ELSE        },
     { ".ELSEIF",       TOK_ELSEIF      },
     { ".END",          TOK_END         },
+    { ".ENDENUM",       TOK_ENDENUM     },
     { ".ENDIF",        TOK_ENDIF       },
     { ".ENDMAC",       TOK_ENDMACRO    },
     { ".ENDMACRO",     TOK_ENDMACRO    },
@@ -163,6 +164,7 @@ struct DotKeyword {
     { ".ENDSCOPE",      TOK_ENDSCOPE    },
     { ".ENDSTRUCT",    TOK_ENDSTRUCT   },
     { ".ENDUNION",             TOK_ENDUNION    },
+    { ".ENUM",          TOK_ENUM        },
     { ".ERROR",        TOK_ERROR       },
     { ".EXITMAC",      TOK_EXITMACRO   },
     { ".EXITMACRO",    TOK_EXITMACRO   },
index 373fdb57b1112ab7f97bcde37c51ee9ef778ad0c..005441721d3cf5515a9b6eb46ed0d94b6e3ba7b4 100644 (file)
@@ -144,7 +144,8 @@ enum Token {
     TOK_DWORD,
     TOK_ELSE,
     TOK_ELSEIF,
-    TOK_END,
+    TOK_END, 
+    TOK_ENDENUM,
     TOK_ENDIF,
     TOK_ENDMACRO,
     TOK_ENDPROC,
@@ -152,6 +153,7 @@ enum Token {
     TOK_ENDSCOPE,
     TOK_ENDSTRUCT,
     TOK_ENDUNION,
+    TOK_ENUM,
     TOK_ERROR,
     TOK_EXITMACRO,
     TOK_EXPORT,
index 69b8864e31d86e529218911a8ac86c3c34cb69b4..a737e36147eef87f96e21d13aca38580a3486e97 100644 (file)
@@ -2,7 +2,7 @@
 /*                                                                           */
 /*                                 struct.c                                  */
 /*                                                                           */
-/*                              .STRUCT command                              */
+/*                          .STRUCT/.UNION commands                          */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
index f90edee817a8341734326dce35b0a35a6044dc13..871fa6a42f749012998e9fc1f5964b682d4cf105 100644 (file)
@@ -2,7 +2,7 @@
 /*                                                                           */
 /*                                 struct.h                                  */
 /*                                                                           */
-/*                              .STRUCT command                              */
+/*                          .STRUCT/.UNION commands                          */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
index 5873842678d749235923b29fb6e1d090b451fa79..20aa2a354f2b4ccf22f18901829f05bd1721d5ce 100644 (file)
@@ -63,8 +63,8 @@
 #define ST_GLOBAL       0x00            /* Root level */
 #define ST_PROC         0x01            /* .PROC */
 #define ST_SCOPE        0x02            /* .SCOPE */
-#define ST_STRUCT       0x03            /* .STRUCT */
-#define ST_UNION        0x04            /* .UNION */
+#define ST_STRUCT       0x03            /* .STRUCT/.UNION */
+#define ST_ENUM         0x04            /* .ENUM */
 #define ST_UNDEF        0xFF
 
 /* A symbol table */