]> git.sur5r.net Git - cc65/blob - src/ld65/cfgexpr.c
2ed08c613409bb549580991d9ba1ce388d0fb4dc
[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,      Ullrich von Bassewitz                                      */
10 /*                Römerstrasse 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         DoneStrBuf (&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 */
199         cfgtok_t Tok = CfgTok;
200
201         /* Left side must be an int */
202         CE_AssureInt (E);
203
204         /* Get the right operand and make sure it's an int */
205         Factor (&RightSide);
206         CE_AssureInt (&RightSide);
207
208         /* Handle the operation */
209         switch (Tok) {
210
211             case CFGTOK_MUL:
212                 E->IVal *= RightSide.IVal;
213                 break;
214
215             case CFGTOK_DIV:
216                 if (RightSide.IVal == 0) {
217                     CfgError ("Division by zero");
218                 }
219                 E->IVal /= RightSide.IVal;
220                 break;
221
222             default:
223                 Internal ("Unhandled token in Term: %d", Tok);
224         }
225
226         /* Cleanup RightSide (this is not really needed since it may not
227          * contain strings at this point, but call it anyway for clarity.
228          */
229         CE_Done (&RightSide);
230     }
231 }
232
233
234
235 static void SimpleExpr (CfgExpr* E)
236 /* Additive operators: + and - */
237 {
238     /* Left operand */
239     Term (E);
240
241     /* Handle additive operators */
242     while (CfgTok == CFGTOK_PLUS || CfgTok == CFGTOK_MINUS) {
243
244         CfgExpr RightSide = CFGEXPR_INITIALIZER;
245
246         /* Remember the token, then skip it */
247         cfgtok_t Tok = CfgTok;
248         CfgNextTok ();
249
250         /* Get the right operand */
251         Term (&RightSide);
252
253         /* Make sure, left and right side are of the same type */
254         if (E->Type != RightSide.Type) {
255             CfgError ("Incompatible types in expression");
256         }
257
258         /* Handle the operation */
259         switch (Tok) {
260
261             case CFGTOK_PLUS:
262                 /* Plus is defined for strings and ints */
263                 if (E->Type == ceInt) {
264                     E->IVal += RightSide.IVal;
265                 } else if (E->Type == ceString) {
266                     SB_Append (&E->SVal, &RightSide.SVal);
267                 } else {
268                     Internal ("Unhandled type in '+' operator: %u", E->Type);
269                 }
270                 break;
271
272             case CFGTOK_MINUS:
273                 /* Operands must be ints */
274                 CE_AssureInt (E);
275                 E->IVal -= RightSide.IVal;
276                 break;
277
278             default:
279                 Internal ("Unhandled token in SimpleExpr: %d", Tok);
280         }
281
282         /* Cleanup RightSide */
283         CE_Done (&RightSide);
284     }
285 }
286
287
288
289 static void Expr (CfgExpr* E)
290 /* Full expression */
291 {
292     SimpleExpr (E);
293 }
294
295
296
297 long CfgIntExpr (void)
298 /* Read an expression, make sure it's an int, and return its value */
299 {
300     long Val;
301
302     CfgExpr E = CFGEXPR_INITIALIZER;
303
304     /* Parse the expression */
305     Expr (&E);
306
307     /* Make sure it's an integer */
308     CE_AssureInt (&E);
309
310     /* Get the value */
311     Val = E.IVal;
312
313     /* Cleaup E */
314     CE_Done (&E);
315
316     /* Return the value */
317     return Val;
318 }
319
320
321
322 long CfgCheckedIntExpr (long Min, long Max)
323 /* Read an expression, make sure it's an int and in range, then return its
324  * value.
325  */
326 {
327     /* Get the value */
328     long Val = CfgIntExpr ();
329
330     /* Check the range */
331     if (Val < Min || Val > Max) {
332         CfgError ("Range error");
333     }
334
335     /* Return the value */
336     return Val;
337 }
338
339
340