]> git.sur5r.net Git - cc65/blob - src/ca65/enum.c
Move all attributes and other information that is attached to a token into a
[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 "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     long      Offs     = 0;
62     ExprNode* BaseExpr = GenLiteral0 ();
63
64     /* Check for a name */
65     int Anon = (CurTok.Tok != TOK_IDENT);
66     if (!Anon) {
67         /* Enter a new scope, then skip the name */
68         SymEnterLevel (&CurTok.SVal, ST_ENUM, ADDR_SIZE_ABS);
69         NextTok ();
70     }
71
72     /* Test for end of line */
73     ConsumeSep ();
74
75     /* Read until end of struct */
76     while (CurTok.Tok != TOK_ENDENUM && CurTok.Tok != TOK_EOF) {
77
78         SymEntry* Sym;
79         ExprNode* EnumExpr;
80
81         /* Skip empty lines */
82         if (CurTok.Tok == TOK_SEP) {
83             NextTok ();
84             continue;
85         }
86
87         /* The format is "identifier [ = value ]" */
88         if (CurTok.Tok != TOK_IDENT) {
89             /* Maybe it's a conditional? */
90             if (!CheckConditionals ()) {
91                 ErrorSkip ("Identifier expected");
92             }
93             continue;
94         }
95
96         /* We have an identifier, generate a symbol */
97         Sym = SymFind (CurrentScope, &CurTok.SVal, SYM_ALLOC_NEW);
98
99         /* Skip the member name */
100         NextTok ();
101
102         /* Check for an assignment */
103         if (CurTok.Tok == TOK_EQ) {
104
105             /* Skip the equal sign */
106             NextTok ();
107
108             /* Read the new expression */
109             EnumExpr = Expression ();
110
111             /* Reset the base expression and the offset */
112             FreeExpr (BaseExpr);
113             BaseExpr = CloneExpr (EnumExpr);
114             Offs     = 0;
115
116         } else {
117
118             /* No assignment, use last value + 1 */
119             EnumExpr = GenAddExpr (CloneExpr (BaseExpr), GenLiteralExpr (Offs));
120
121         }
122
123         /* Assign the value to the enum member */
124         SymDef (Sym, EnumExpr, ADDR_SIZE_DEFAULT, SF_NONE);
125
126         /* Increment the offset for the next member */
127         ++Offs;
128
129         /* Expect end of line */
130         ConsumeSep ();
131     }
132
133     /* If this is not an anon enum, leave its scope */
134     if (!Anon) {
135         /* Close the enum scope */
136         SymLeaveLevel ();
137     }
138
139     /* End of enum definition */
140     Consume (TOK_ENDENUM, "`.ENDENUM' expected");
141
142     /* Free the base expression */
143     FreeExpr (BaseExpr);
144 }
145
146
147
148