]> git.sur5r.net Git - cc65/blob - src/ca65/enum.c
Check for macros within .STRUCT/.ENUM.
[cc65] / src / ca65 / enum.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  enum.c                                   */
4 /*                                                                           */
5 /*                               .ENUM command                               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 "macro.h"
45 #include "nexttok.h"
46 #include "scanner.h"
47 #include "symbol.h"
48 #include "symtab.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Code                                    */
54 /*****************************************************************************/
55
56
57
58 void DoEnum (void)
59 /* Handle the .ENUM command */
60 {
61     /* Start at zero */
62     long      Offs     = 0;
63     ExprNode* BaseExpr = GenLiteral0 ();
64
65     /* Check for a name */
66     int Anon = (CurTok.Tok != TOK_IDENT);
67     if (!Anon) {
68         /* Enter a new scope, then skip the name */
69         SymEnterLevel (&CurTok.SVal, ST_ENUM, ADDR_SIZE_ABS, 0);
70         NextTok ();
71     }
72
73     /* Test for end of line */
74     ConsumeSep ();
75
76     /* Read until end of struct */
77     while (CurTok.Tok != TOK_ENDENUM && CurTok.Tok != TOK_EOF) {
78
79         Macro*    M;
80         SymEntry* Sym;
81         ExprNode* EnumExpr;
82
83         /* Skip empty lines */
84         if (CurTok.Tok == TOK_SEP) {
85             NextTok ();
86             continue;
87         }
88
89         /* The format is "identifier [ = value ]" */
90         if (CurTok.Tok != TOK_IDENT) {
91             /* Maybe it's a conditional? */
92             if (!CheckConditionals ()) {
93                 ErrorSkip ("Identifier expected");
94             }
95             continue;
96         }
97
98         /* We have an identifier. Is it a macro? */
99         if ((M = FindMacro (&CurTok.SVal)) != 0) {
100             MacExpandStart (M);
101             continue;
102         }
103
104         /* We have an identifier, generate a symbol */
105         Sym = SymFind (CurrentScope, &CurTok.SVal, SYM_ALLOC_NEW);
106
107         /* Skip the member name */
108         NextTok ();
109
110         /* Check for an assignment */
111         if (CurTok.Tok == TOK_EQ) {
112
113             /* Skip the equal sign */
114             NextTok ();
115
116             /* Read the new expression */
117             EnumExpr = Expression ();
118
119             /* Reset the base expression and the offset */
120             FreeExpr (BaseExpr);
121             BaseExpr = CloneExpr (EnumExpr);
122             Offs     = 0;
123
124         } else {
125
126             /* No assignment, use last value + 1 */
127             EnumExpr = GenAddExpr (CloneExpr (BaseExpr), GenLiteralExpr (Offs));
128
129         }
130
131         /* Assign the value to the enum member */
132         SymDef (Sym, EnumExpr, ADDR_SIZE_DEFAULT, SF_NONE);
133
134         /* Increment the offset for the next member */
135         ++Offs;
136
137         /* Expect end of line */
138         ConsumeSep ();
139     }
140
141     /* If this is not an anon enum, leave its scope */
142     if (!Anon) {
143         /* Close the enum scope */
144         SymLeaveLevel ();
145     }
146
147     /* End of enum definition */
148     Consume (TOK_ENDENUM, "`.ENDENUM' expected");
149
150     /* Free the base expression */
151     FreeExpr (BaseExpr);
152 }
153
154
155
156