]> git.sur5r.net Git - cc65/blob - src/ca65/enum.c
Allow conditional directives within .STRUCT7:UNION and .ENUM
[cc65] / src / ca65 / enum.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  enum.c                                   */
4 /*                                                                           */
5 /*                               .ENUM command                               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003      Ullrich von Bassewitz                                       */
10 /*               Römerstraße 52                                              */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "addrsize.h"
38
39 /* ca65 */
40 #include "condasm.h"
41 #include "enum.h"
42 #include "error.h"
43 #include "expr.h"
44 #include "nexttok.h"
45 #include "scanner.h"
46 #include "symbol.h"
47 #include "symtab.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Code                                    */
53 /*****************************************************************************/
54
55
56
57 void DoEnum (void)
58 /* Handle the .ENUM command */
59 {
60     /* Start at zero */
61     ExprNode* NextExpr = GenLiteralExpr (0);
62
63     /* Check for a name */
64     int Anon = (Tok != TOK_IDENT);
65     if (!Anon) {
66         /* Enter a new scope, then skip the name */
67         SymEnterLevel (SVal, ST_ENUM, ADDR_SIZE_ABS);
68         NextTok ();
69     }
70
71     /* Test for end of line */
72     ConsumeSep ();
73
74     /* Read until end of struct */
75     while (Tok != TOK_ENDENUM && Tok != TOK_EOF) {
76
77         SymEntry* Sym;
78         ExprNode* EnumExpr;
79
80         /* Skip empty lines */
81         if (Tok == TOK_SEP) {
82             NextTok ();
83             continue;
84         }
85
86         /* The format is "identifier [ = value ]" */
87         if (Tok != TOK_IDENT) {
88             /* Maybe it's a conditional? */
89             if (!CheckConditionals ()) {
90                 ErrorSkip ("Identifier expected");
91             }
92             continue;
93         }
94
95         /* We have an identifier, generate a symbol */
96         Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
97
98         /* Skip the member name */
99         NextTok ();
100
101         /* Check for an assignment */
102         if (Tok == TOK_EQ) {
103
104             /* Skip the equal sign */
105             NextTok ();
106
107             /* Delete the old next expression */
108             FreeExpr (NextExpr);
109
110             /* Read the new one */
111             EnumExpr = Expression ();
112
113         } else {
114
115             EnumExpr = NextExpr;
116
117         }
118
119         /* Generate the next expression from the current one */
120         NextExpr = GenAddExpr (CloneExpr (EnumExpr), GenLiteralExpr (1));
121         NextExpr = SimplifyExpr (NextExpr);
122
123         /* Assign the value to the enum member */
124         SymDef (Sym, EnumExpr, ADDR_SIZE_DEFAULT, SF_NONE);
125
126         /* Expect end of line */
127         ConsumeSep ();
128     }
129
130     /* If this is not an anon enum, leave its scope */
131     if (!Anon) {
132         /* Close the enum scope */
133         SymLeaveLevel ();
134     }
135
136     /* End of enum definition */
137     Consume (TOK_ENDENUM, "`.ENDENUM' expected");
138
139     /* Free the last (unused) enum expression */
140     FreeExpr (NextExpr);
141 }
142
143
144