]> git.sur5r.net Git - cc65/blob - src/ld65/cfgexpr.c
Reworked and improved the SYMBOLS section. The old syntax (using symbol =
[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-2010, 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 #include "strbuf.h"
39
40 /* ld65 */
41 #include "cfgexpr.h"
42 #include "error.h"
43 #include "exports.h"
44 #include "expr.h"
45 #include "scanner.h"
46 #include "spool.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Code                                    */
52 /*****************************************************************************/
53
54
55
56 static ExprNode* Factor (void)
57 /* Read and return a factor */
58 {
59     ExprNode* N = 0;            /* Initialize to avoid compiler warnings */
60     Export*   E;
61     unsigned  Name;
62
63
64     switch (CfgTok) {
65
66         case CFGTOK_IDENT:
67             /* Get the name as an id */
68             Name = GetStrBufId (&CfgSVal);
69
70             /* Check if we know the symbol already */
71             E = FindExport (Name);
72             if (E != 0 && IsConstExport (E)) {
73                 N = LiteralExpr (GetExportVal (E), 0);
74             } else {
75                 N = NewExprNode (0, EXPR_SYMBOL);
76                 N->V.Imp = InsertImport (GenImport (Name, ADDR_SIZE_ABS));
77                 N->V.Imp->Pos = CfgErrorPos;
78             }
79
80             /* Skip the symbol name */
81             CfgNextTok ();
82             break;
83
84         case CFGTOK_INTCON:
85             /* An integer constant */
86             N = LiteralExpr (CfgIVal, 0);
87             CfgNextTok ();
88             break;
89
90         case CFGTOK_PLUS:
91             /* Unary plus */
92             CfgNextTok ();
93             N = Factor ();
94             break;
95
96         case CFGTOK_MINUS:
97             /* Unary minus */
98             CfgNextTok ();
99             N = NewExprNode (0, EXPR_UNARY_MINUS);
100             N->Left = Factor ();
101             break;
102
103         case CFGTOK_LPAR:
104             /* Left parenthesis */
105             CfgNextTok ();
106             N = CfgExpr ();
107             CfgConsume (CFGTOK_RPAR, "')' expected");
108             break;
109
110         default:
111             CfgError (&CfgErrorPos, "Invalid expression: %d", CfgTok);
112             break;
113     }
114
115     /* Return the new expression node */
116     return N;
117 }
118
119
120
121 static ExprNode* Term (void)
122 /* Multiplicative operators: * and / */
123 {
124     /* Read left hand side */
125     ExprNode* Root = Factor ();
126
127     /* Handle multiplicative operators */
128     while (CfgTok == CFGTOK_MUL || CfgTok == CFGTOK_DIV) {
129
130         ExprNode* Left;
131         ExprNode* Right;
132         unsigned char Op;
133
134         /* Remember the token, then skip it */
135         cfgtok_t Tok = CfgTok;
136         CfgNextTok ();
137
138         /* Move root to left side, then read right side */
139         Left = Root;
140         Right = Factor ();
141
142         /* Handle the operation */
143         switch (Tok) {
144             case CFGTOK_MUL:    Op = EXPR_MUL;  break;
145             case CFGTOK_DIV:    Op = EXPR_DIV;  break;
146             default:            Internal ("Unhandled token in Term: %d", Tok);
147         }
148         Root = NewExprNode (0, Op);
149         Root->Left = Left;
150         Root->Right = Right;
151     }
152
153     /* Return the expression tree we've created */
154     return Root;
155 }
156
157
158
159 static ExprNode* SimpleExpr (void)
160 /* Additive operators: + and - */
161 {
162     /* Read left hand side */
163     ExprNode* Root = Term ();
164
165     /* Handle additive operators */
166     while (CfgTok == CFGTOK_PLUS || CfgTok == CFGTOK_MINUS) {
167
168         ExprNode* Left;
169         ExprNode* Right;
170         unsigned char Op;
171
172         /* Remember the token, then skip it */
173         cfgtok_t Tok = CfgTok;
174         CfgNextTok ();
175
176         /* Move root to left side, then read right side */
177         Left = Root;
178         Right = Term ();
179
180         /* Handle the operation */
181         switch (Tok) {
182             case CFGTOK_PLUS:   Op = EXPR_PLUS;         break;
183             case CFGTOK_MINUS:  Op = EXPR_MINUS;        break;
184             default:            Internal ("Unhandled token in SimpleExpr: %d", Tok);
185         }
186         Root = NewExprNode (0, Op);
187         Root->Left = Left;
188         Root->Right = Right;
189     }
190
191     /* Return the expression tree we've created */
192     return Root;
193 }
194
195
196
197 ExprNode* CfgExpr (void)
198 /* Full expression */
199 {
200     return SimpleExpr ();
201 }
202
203
204
205 long CfgConstExpr (void)
206 /* Read an integer expression, make sure its constant and return its value */
207 {
208     long Val;
209
210     /* Parse the expression */
211     ExprNode* Expr = CfgExpr ();
212
213     /* Check that it's const */
214     if (!IsConstExpr (Expr)) {
215         CfgError (&CfgErrorPos, "Constant expression expected");
216     }
217
218     /* Get the value */
219     Val = GetExprVal (Expr);
220
221     /* Cleanup E */
222     FreeExpr (Expr);
223
224     /* Return the value */
225     return Val;
226 }
227
228
229
230 long CfgCheckedConstExpr (long Min, long Max)
231 /* Read an expression, make sure it's an int and in range, then return its
232  * value.
233  */
234 {
235     /* Get the value */
236     long Val = CfgConstExpr ();
237
238     /* Check the range */
239     if (Val < Min || Val > Max) {
240         CfgError (&CfgErrorPos, "Range error");
241     }
242
243     /* Return the value */
244     return Val;
245 }
246
247
248