]> git.sur5r.net Git - cc65/blob - src/ld65/cfgexpr.c
New linker config for the VIC-20 with 32K cartridge by Stefan Haubenthal.
[cc65] / src / ld65 / cfgexpr.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 cfgexpr.c                                 */
4 /*                                                                           */
5 /*          Simple expressions for use with in configuration file            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2005-2008, 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 "strbuf.h"
38
39 /* ld65 */
40 #include "cfgexpr.h"
41 #include "error.h"
42 #include "exports.h"
43 #include "scanner.h"
44 #include "spool.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Type of a CfgExpr */
55 enum {
56     ceEmpty,
57     ceInt,
58     ceString
59 };
60
61 typedef struct CfgExpr CfgExpr;
62 struct CfgExpr {
63     unsigned    Type;           /* Type of the expression */
64     long        IVal;           /* Integer value if it's a string */
65     StrBuf      SVal;           /* String value if it's a string */
66 };
67
68 #define CFGEXPR_INITIALIZER { ceEmpty, 0, STATIC_STRBUF_INITIALIZER }
69
70
71
72 /*****************************************************************************/
73 /*                                 Forwards                                  */
74 /*****************************************************************************/
75
76
77
78 static void Expr (CfgExpr* E);
79 /* Full expression */
80
81
82
83 /*****************************************************************************/
84 /*                              struct CfgExpr                               */
85 /*****************************************************************************/
86
87
88
89 static void CE_Done (CfgExpr* E)
90 /* Cleanup a CfgExpr struct */
91 {
92     /* If the type is a string, we must delete the string buffer */
93     if (E->Type == ceString) {
94         SB_Done (&E->SVal);
95     }
96 }
97
98
99
100 static void CE_AssureInt (const CfgExpr* E)
101 /* Make sure, E contains an integer */
102 {
103     if (E->Type != ceInt) {
104         CfgError ("Integer type expected");
105     }
106 }
107
108
109
110 /*****************************************************************************/
111 /*                                   Code                                    */
112 /*****************************************************************************/
113
114
115
116 static void Factor (CfgExpr* E)
117 /* Read and return a factor in E */
118 {
119     Export* Sym;
120
121
122     switch (CfgTok) {
123
124         case CFGTOK_IDENT:
125             /* An identifier - search an export with the given name */
126             Sym = FindExport (GetStringId (CfgSVal));
127             if (Sym == 0) {
128                 CfgError ("Unknown symbol in expression: `%s'", CfgSVal);
129             }
130             /* We can only handle constants */
131             if (!IsConstExport (Sym)) {
132                 CfgError ("Value for symbol `%s' is not constant", CfgSVal);
133             }
134
135             /* Use the symbol value */
136             E->IVal = GetExportVal (Sym);
137             E->Type = ceInt;
138
139             /* Skip the symbol name */
140             CfgNextTok ();
141             break;
142
143         case CFGTOK_INTCON:
144             /* An integer constant */
145             E->IVal = CfgIVal;
146             E->Type = ceInt;
147             CfgNextTok ();
148             break;
149
150         case CFGTOK_STRCON:
151             /* A string constant */
152             SB_CopyStr (&E->SVal, CfgSVal);
153             E->Type = ceString;
154             CfgNextTok ();
155             break;
156
157         case CFGTOK_PLUS:
158             /* Unary plus */
159             CfgNextTok ();
160             Factor (E);
161             CE_AssureInt (E);
162             break;
163
164         case CFGTOK_MINUS:
165             /* Unary minus */
166             CfgNextTok ();
167             Factor (E);
168             CE_AssureInt (E);
169             E->IVal = -E->IVal;
170             break;
171
172         case CFGTOK_LPAR:
173             /* Left parenthesis */
174             CfgNextTok ();
175             Expr (E);
176             CfgConsume (CFGTOK_RPAR, "')' expected");
177             break;
178
179         default:
180             CfgError ("Invalid expression: %d", CfgTok);
181             break;
182     }
183 }
184
185
186
187 static void Term (CfgExpr* E)
188 /* Multiplicative operators: * and / */
189 {
190     /* Left operand */
191     Factor (E);
192
193     /* Handle multiplicative operators */
194     while (CfgTok == CFGTOK_MUL || CfgTok == CFGTOK_DIV) {
195
196         CfgExpr RightSide = CFGEXPR_INITIALIZER;
197
198         /* Remember the token, then skip it */
199         cfgtok_t Tok = CfgTok;
200         CfgNextTok ();
201
202         /* Left side must be an int */
203         CE_AssureInt (E);
204
205         /* Get the right operand and make sure it's an int */
206         Factor (&RightSide);
207         CE_AssureInt (&RightSide);
208
209         /* Handle the operation */
210         switch (Tok) {
211
212             case CFGTOK_MUL:
213                 E->IVal *= RightSide.IVal;
214                 break;
215
216             case CFGTOK_DIV:
217                 if (RightSide.IVal == 0) {
218                     CfgError ("Division by zero");
219                 }
220                 E->IVal /= RightSide.IVal;
221                 break;
222
223             default:
224                 Internal ("Unhandled token in Term: %d", Tok);
225         }
226
227         /* Cleanup RightSide (this is not really needed since it may not
228          * contain strings at this point, but call it anyway for clarity.
229          */
230         CE_Done (&RightSide);
231     }
232 }
233
234
235
236 static void SimpleExpr (CfgExpr* E)
237 /* Additive operators: + and - */
238 {
239     /* Left operand */
240     Term (E);
241
242     /* Handle additive operators */
243     while (CfgTok == CFGTOK_PLUS || CfgTok == CFGTOK_MINUS) {
244
245         CfgExpr RightSide = CFGEXPR_INITIALIZER;
246
247         /* Remember the token, then skip it */
248         cfgtok_t Tok = CfgTok;
249         CfgNextTok ();
250
251         /* Get the right operand */
252         Term (&RightSide);
253
254         /* Make sure, left and right side are of the same type */
255         if (E->Type != RightSide.Type) {
256             CfgError ("Incompatible types in expression");
257         }
258
259         /* Handle the operation */
260         switch (Tok) {
261
262             case CFGTOK_PLUS:
263                 /* Plus is defined for strings and ints */
264                 if (E->Type == ceInt) {
265                     E->IVal += RightSide.IVal;
266                 } else if (E->Type == ceString) {
267                     SB_Append (&E->SVal, &RightSide.SVal);
268                 } else {
269                     Internal ("Unhandled type in '+' operator: %u", E->Type);
270                 }
271                 break;
272
273             case CFGTOK_MINUS:
274                 /* Operands must be ints */
275                 CE_AssureInt (E);
276                 E->IVal -= RightSide.IVal;
277                 break;
278
279             default:
280                 Internal ("Unhandled token in SimpleExpr: %d", Tok);
281         }
282
283         /* Cleanup RightSide */
284         CE_Done (&RightSide);
285     }
286 }
287
288
289
290 static void Expr (CfgExpr* E)
291 /* Full expression */
292 {
293     SimpleExpr (E);
294 }
295
296
297
298 long CfgIntExpr (void)
299 /* Read an expression, make sure it's an int, and return its value */
300 {
301     long Val;
302
303     CfgExpr E = CFGEXPR_INITIALIZER;
304
305     /* Parse the expression */
306     Expr (&E);
307
308     /* Make sure it's an integer */
309     CE_AssureInt (&E);
310
311     /* Get the value */
312     Val = E.IVal;
313
314     /* Cleaup E */
315     CE_Done (&E);
316
317     /* Return the value */
318     return Val;
319 }
320
321
322
323 long CfgCheckedIntExpr (long Min, long Max)
324 /* Read an expression, make sure it's an int and in range, then return its
325  * value.
326  */
327 {
328     /* Get the value */
329     long Val = CfgIntExpr ();
330
331     /* Check the range */
332     if (Val < Min || Val > Max) {
333         CfgError ("Range error");
334     }
335
336     /* Return the value */
337     return Val;
338 }
339
340
341