1 /*****************************************************************************/
5 /* Simple expressions for use with in configuration file */
9 /* (C) 2005-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 static ExprNode* Factor (void)
57 /* Read and return a factor */
59 ExprNode* N = 0; /* Initialize to avoid compiler warnings */
67 /* Get the name as an id */
68 Name = GetStrBufId (&CfgSVal);
70 /* Check if we know the symbol already */
71 E = FindExport (Name);
72 if (E != 0 && IsConstExport (E)) {
73 N = LiteralExpr (GetExportVal (E), 0);
75 N = NewExprNode (0, EXPR_SYMBOL);
76 N->V.Imp = InsertImport (GenImport (Name, ADDR_SIZE_ABS));
79 /* Skip the symbol name */
84 /* An integer constant */
85 N = LiteralExpr (CfgIVal, 0);
98 N = NewExprNode (0, EXPR_UNARY_MINUS);
103 /* Left parenthesis */
106 CfgConsume (CFGTOK_RPAR, "')' expected");
110 CfgError (&CfgErrorPos, "Invalid expression: %d", CfgTok);
114 /* Return the new expression node */
120 static ExprNode* Term (void)
121 /* Multiplicative operators: * and / */
123 /* Read left hand side */
124 ExprNode* Root = Factor ();
126 /* Handle multiplicative operators */
127 while (CfgTok == CFGTOK_MUL || CfgTok == CFGTOK_DIV) {
133 /* Remember the token, then skip it */
134 cfgtok_t Tok = CfgTok;
137 /* Move root to left side, then read right side */
141 /* Handle the operation */
143 case CFGTOK_MUL: Op = EXPR_MUL; break;
144 case CFGTOK_DIV: Op = EXPR_DIV; break;
145 default: Internal ("Unhandled token in Term: %d", Tok);
147 Root = NewExprNode (0, Op);
152 /* Return the expression tree we've created */
158 static ExprNode* SimpleExpr (void)
159 /* Additive operators: + and - */
161 /* Read left hand side */
162 ExprNode* Root = Term ();
164 /* Handle additive operators */
165 while (CfgTok == CFGTOK_PLUS || CfgTok == CFGTOK_MINUS) {
171 /* Remember the token, then skip it */
172 cfgtok_t Tok = CfgTok;
175 /* Move root to left side, then read right side */
179 /* Handle the operation */
181 case CFGTOK_PLUS: Op = EXPR_PLUS; break;
182 case CFGTOK_MINUS: Op = EXPR_MINUS; break;
183 default: Internal ("Unhandled token in SimpleExpr: %d", Tok);
185 Root = NewExprNode (0, Op);
190 /* Return the expression tree we've created */
196 ExprNode* CfgExpr (void)
197 /* Full expression */
199 return SimpleExpr ();
204 long CfgConstExpr (void)
205 /* Read an integer expression, make sure its constant and return its value */
209 /* Parse the expression */
210 ExprNode* Expr = CfgExpr ();
212 /* Check that it's const */
213 if (!IsConstExpr (Expr)) {
214 CfgError (&CfgErrorPos, "Constant expression expected");
218 Val = GetExprVal (Expr);
223 /* Return the value */
229 long CfgCheckedConstExpr (long Min, long Max)
230 /* Read an expression, make sure it's an int and in range, then return its
235 long Val = CfgConstExpr ();
237 /* Check the range */
238 if (Val < Min || Val > Max) {
239 CfgError (&CfgErrorPos, "Range error");
242 /* Return the value */