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));
77 N->V.Imp->Pos = CfgErrorPos;
80 /* Skip the symbol name */
85 /* An integer constant */
86 N = LiteralExpr (CfgIVal, 0);
99 N = NewExprNode (0, EXPR_UNARY_MINUS);
104 /* Left parenthesis */
107 CfgConsume (CFGTOK_RPAR, "')' expected");
111 CfgError (&CfgErrorPos, "Invalid expression: %d", CfgTok);
115 /* Return the new expression node */
121 static ExprNode* Term (void)
122 /* Multiplicative operators: * and / */
124 /* Read left hand side */
125 ExprNode* Root = Factor ();
127 /* Handle multiplicative operators */
128 while (CfgTok == CFGTOK_MUL || CfgTok == CFGTOK_DIV) {
134 /* Remember the token, then skip it */
135 cfgtok_t Tok = CfgTok;
138 /* Move root to left side, then read right side */
142 /* Handle the operation */
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);
148 Root = NewExprNode (0, Op);
153 /* Return the expression tree we've created */
159 static ExprNode* SimpleExpr (void)
160 /* Additive operators: + and - */
162 /* Read left hand side */
163 ExprNode* Root = Term ();
165 /* Handle additive operators */
166 while (CfgTok == CFGTOK_PLUS || CfgTok == CFGTOK_MINUS) {
172 /* Remember the token, then skip it */
173 cfgtok_t Tok = CfgTok;
176 /* Move root to left side, then read right side */
180 /* Handle the operation */
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);
186 Root = NewExprNode (0, Op);
191 /* Return the expression tree we've created */
197 ExprNode* CfgExpr (void)
198 /* Full expression */
200 return SimpleExpr ();
205 long CfgConstExpr (void)
206 /* Read an integer expression, make sure its constant and return its value */
210 /* Parse the expression */
211 ExprNode* Expr = CfgExpr ();
213 /* Check that it's const */
214 if (!IsConstExpr (Expr)) {
215 CfgError (&CfgErrorPos, "Constant expression expected");
219 Val = GetExprVal (Expr);
224 /* Return the value */
230 long CfgCheckedConstExpr (long Min, long Max)
231 /* Read an expression, make sure it's an int and in range, then return its
236 long Val = CfgConstExpr ();
238 /* Check the range */
239 if (Val < Min || Val > Max) {
240 CfgError (&CfgErrorPos, "Range error");
243 /* Return the value */