expr.c revision 1.13 1 1.13 kre /* $NetBSD: expr.c,v 1.13 2022/07/03 06:30:31 kre Exp $ */
2 1.2 tls
3 1.1 jtc /*
4 1.1 jtc * Korn expression evaluation
5 1.1 jtc */
6 1.1 jtc /*
7 1.1 jtc * todo: better error handling: if in builtin, should be builtin error, etc.
8 1.1 jtc */
9 1.5 agc #include <sys/cdefs.h>
10 1.5 agc
11 1.5 agc #ifndef lint
12 1.13 kre __RCSID("$NetBSD: expr.c,v 1.13 2022/07/03 06:30:31 kre Exp $");
13 1.5 agc #endif
14 1.5 agc
15 1.1 jtc
16 1.1 jtc #include "sh.h"
17 1.1 jtc #include <ctype.h>
18 1.10 kamil #include <stdbool.h>
19 1.1 jtc
20 1.1 jtc /* The order of these enums is constrained by the order of opinfo[] */
21 1.1 jtc enum token {
22 1.1 jtc /* some (long) unary operators */
23 1.1 jtc O_PLUSPLUS = 0, O_MINUSMINUS,
24 1.1 jtc /* binary operators */
25 1.1 jtc O_EQ, O_NE,
26 1.1 jtc /* assignments are assumed to be in range O_ASN .. O_BORASN */
27 1.1 jtc O_ASN, O_TIMESASN, O_DIVASN, O_MODASN, O_PLUSASN, O_MINUSASN,
28 1.1 jtc O_LSHIFTASN, O_RSHIFTASN, O_BANDASN, O_BXORASN, O_BORASN,
29 1.1 jtc O_LSHIFT, O_RSHIFT,
30 1.1 jtc O_LE, O_GE, O_LT, O_GT,
31 1.1 jtc O_LAND,
32 1.1 jtc O_LOR,
33 1.1 jtc O_TIMES, O_DIV, O_MOD,
34 1.1 jtc O_PLUS, O_MINUS,
35 1.1 jtc O_BAND,
36 1.1 jtc O_BXOR,
37 1.1 jtc O_BOR,
38 1.1 jtc O_TERN,
39 1.1 jtc O_COMMA,
40 1.1 jtc /* things after this aren't used as binary operators */
41 1.1 jtc /* unary that are not also binaries */
42 1.1 jtc O_BNOT, O_LNOT,
43 1.1 jtc /* misc */
44 1.1 jtc OPEN_PAREN, CLOSE_PAREN, CTERN,
45 1.1 jtc /* things that don't appear in the opinfo[] table */
46 1.1 jtc VAR, LIT, END, BAD
47 1.1 jtc };
48 1.3 hubertf #define IS_BINOP(op) (((int)op) >= (int)O_EQ && ((int)op) <= (int)O_COMMA)
49 1.1 jtc #define IS_ASSIGNOP(op) ((int)(op) >= (int)O_ASN && (int)(op) <= (int)O_BORASN)
50 1.1 jtc
51 1.1 jtc enum prec {
52 1.1 jtc P_PRIMARY = 0, /* VAR, LIT, (), ~ ! - + */
53 1.1 jtc P_MULT, /* * / % */
54 1.1 jtc P_ADD, /* + - */
55 1.1 jtc P_SHIFT, /* << >> */
56 1.1 jtc P_RELATION, /* < <= > >= */
57 1.1 jtc P_EQUALITY, /* == != */
58 1.1 jtc P_BAND, /* & */
59 1.1 jtc P_BXOR, /* ^ */
60 1.1 jtc P_BOR, /* | */
61 1.1 jtc P_LAND, /* && */
62 1.1 jtc P_LOR, /* || */
63 1.1 jtc P_TERN, /* ?: */
64 1.1 jtc P_ASSIGN, /* = *= /= %= += -= <<= >>= &= ^= |= */
65 1.1 jtc P_COMMA /* , */
66 1.1 jtc };
67 1.1 jtc #define MAX_PREC P_COMMA
68 1.1 jtc
69 1.1 jtc struct opinfo {
70 1.1 jtc char name[4];
71 1.1 jtc int len; /* name length */
72 1.6 mycroft enum prec prec; /* precedence: lower is higher */
73 1.1 jtc };
74 1.1 jtc
75 1.1 jtc /* Tokens in this table must be ordered so the longest are first
76 1.1 jtc * (eg, += before +). If you change something, change the order
77 1.1 jtc * of enum token too.
78 1.1 jtc */
79 1.1 jtc static const struct opinfo opinfo[] = {
80 1.1 jtc { "++", 2, P_PRIMARY }, /* before + */
81 1.1 jtc { "--", 2, P_PRIMARY }, /* before - */
82 1.1 jtc { "==", 2, P_EQUALITY }, /* before = */
83 1.1 jtc { "!=", 2, P_EQUALITY }, /* before ! */
84 1.1 jtc { "=", 1, P_ASSIGN }, /* keep assigns in a block */
85 1.1 jtc { "*=", 2, P_ASSIGN },
86 1.1 jtc { "/=", 2, P_ASSIGN },
87 1.1 jtc { "%=", 2, P_ASSIGN },
88 1.1 jtc { "+=", 2, P_ASSIGN },
89 1.1 jtc { "-=", 2, P_ASSIGN },
90 1.1 jtc { "<<=", 3, P_ASSIGN },
91 1.1 jtc { ">>=", 3, P_ASSIGN },
92 1.1 jtc { "&=", 2, P_ASSIGN },
93 1.1 jtc { "^=", 2, P_ASSIGN },
94 1.1 jtc { "|=", 2, P_ASSIGN },
95 1.1 jtc { "<<", 2, P_SHIFT },
96 1.1 jtc { ">>", 2, P_SHIFT },
97 1.1 jtc { "<=", 2, P_RELATION },
98 1.1 jtc { ">=", 2, P_RELATION },
99 1.1 jtc { "<", 1, P_RELATION },
100 1.1 jtc { ">", 1, P_RELATION },
101 1.1 jtc { "&&", 2, P_LAND },
102 1.1 jtc { "||", 2, P_LOR },
103 1.1 jtc { "*", 1, P_MULT },
104 1.1 jtc { "/", 1, P_MULT },
105 1.1 jtc { "%", 1, P_MULT },
106 1.1 jtc { "+", 1, P_ADD },
107 1.1 jtc { "-", 1, P_ADD },
108 1.1 jtc { "&", 1, P_BAND },
109 1.1 jtc { "^", 1, P_BXOR },
110 1.1 jtc { "|", 1, P_BOR },
111 1.1 jtc { "?", 1, P_TERN },
112 1.1 jtc { ",", 1, P_COMMA },
113 1.1 jtc { "~", 1, P_PRIMARY },
114 1.1 jtc { "!", 1, P_PRIMARY },
115 1.1 jtc { "(", 1, P_PRIMARY },
116 1.1 jtc { ")", 1, P_PRIMARY },
117 1.1 jtc { ":", 1, P_PRIMARY },
118 1.1 jtc { "", 0, P_PRIMARY } /* end of table */
119 1.1 jtc };
120 1.1 jtc
121 1.1 jtc
122 1.1 jtc typedef struct expr_state Expr_state;
123 1.1 jtc struct expr_state {
124 1.1 jtc const char *expression; /* expression being evaluated */
125 1.1 jtc const char *tokp; /* lexical position */
126 1.1 jtc enum token tok; /* token from token() */
127 1.1 jtc int noassign; /* don't do assigns (for ?:,&&,||) */
128 1.1 jtc struct tbl *val; /* value from token() */
129 1.1 jtc struct tbl *evaling; /* variable that is being recursively
130 1.1 jtc * expanded (EXPRINEVAL flag set)
131 1.1 jtc */
132 1.1 jtc };
133 1.1 jtc
134 1.1 jtc enum error_type { ET_UNEXPECTED, ET_BADLIT, ET_RECURSIVE,
135 1.1 jtc ET_LVALUE, ET_RDONLY, ET_STR };
136 1.1 jtc
137 1.3 hubertf static void evalerr ARGS((Expr_state *es, enum error_type type,
138 1.3 hubertf const char *str)) GCC_FUNC_ATTR(noreturn);
139 1.3 hubertf static struct tbl *evalexpr ARGS((Expr_state *es, enum prec prec));
140 1.3 hubertf static void token ARGS((Expr_state *es));
141 1.11 joerg static struct tbl *do_ppmm(Expr_state *, enum token, struct tbl *, bool);
142 1.3 hubertf static void assign_check ARGS((Expr_state *es, enum token op,
143 1.3 hubertf struct tbl *vasn));
144 1.1 jtc static struct tbl *tempvar ARGS((void));
145 1.3 hubertf static struct tbl *intvar ARGS((Expr_state *es, struct tbl *vp));
146 1.1 jtc
147 1.1 jtc /*
148 1.6 mycroft * parse and evaluate expression
149 1.1 jtc */
150 1.1 jtc int
151 1.1 jtc evaluate(expr, rval, error_ok)
152 1.1 jtc const char *expr;
153 1.1 jtc long *rval;
154 1.1 jtc int error_ok;
155 1.1 jtc {
156 1.1 jtc struct tbl v;
157 1.1 jtc int ret;
158 1.1 jtc
159 1.1 jtc v.flag = DEFINED|INTEGER;
160 1.1 jtc v.type = 0;
161 1.1 jtc ret = v_evaluate(&v, expr, error_ok);
162 1.1 jtc *rval = v.val.i;
163 1.1 jtc return ret;
164 1.1 jtc }
165 1.1 jtc
166 1.1 jtc /*
167 1.6 mycroft * parse and evaluate expression, storing result in vp.
168 1.1 jtc */
169 1.1 jtc int
170 1.1 jtc v_evaluate(vp, expr, error_ok)
171 1.1 jtc struct tbl *vp;
172 1.1 jtc const char *expr;
173 1.1 jtc volatile int error_ok;
174 1.1 jtc {
175 1.1 jtc struct tbl *v;
176 1.1 jtc Expr_state curstate;
177 1.3 hubertf Expr_state * const es = &curstate;
178 1.1 jtc int i;
179 1.1 jtc
180 1.1 jtc /* save state to allow recursive calls */
181 1.1 jtc curstate.expression = curstate.tokp = expr;
182 1.1 jtc curstate.noassign = 0;
183 1.1 jtc curstate.evaling = (struct tbl *) 0;
184 1.1 jtc
185 1.1 jtc newenv(E_ERRH);
186 1.1 jtc i = ksh_sigsetjmp(e->jbuf, 0);
187 1.1 jtc if (i) {
188 1.1 jtc /* Clear EXPRINEVAL in of any variables we were playing with */
189 1.1 jtc if (curstate.evaling)
190 1.1 jtc curstate.evaling->flag &= ~EXPRINEVAL;
191 1.1 jtc quitenv();
192 1.1 jtc if (i == LAEXPR) {
193 1.3 hubertf if (error_ok == KSH_RETURN_ERROR)
194 1.1 jtc return 0;
195 1.9 joerg errorf("%s", null);
196 1.1 jtc }
197 1.1 jtc unwind(i);
198 1.1 jtc /*NOTREACHED*/
199 1.1 jtc }
200 1.1 jtc
201 1.3 hubertf token(es);
202 1.1 jtc #if 1 /* ifdef-out to disallow empty expressions to be treated as 0 */
203 1.1 jtc if (es->tok == END) {
204 1.1 jtc es->tok = LIT;
205 1.1 jtc es->val = tempvar();
206 1.1 jtc }
207 1.1 jtc #endif /* 0 */
208 1.3 hubertf v = intvar(es, evalexpr(es, MAX_PREC));
209 1.1 jtc
210 1.1 jtc if (es->tok != END)
211 1.3 hubertf evalerr(es, ET_UNEXPECTED, (char *) 0);
212 1.1 jtc
213 1.1 jtc if (vp->flag & INTEGER)
214 1.1 jtc setint_v(vp, v);
215 1.1 jtc else
216 1.6 mycroft /* can fail if readonly */
217 1.3 hubertf setstr(vp, str_val(v), error_ok);
218 1.1 jtc
219 1.1 jtc quitenv();
220 1.1 jtc
221 1.1 jtc return 1;
222 1.1 jtc }
223 1.1 jtc
224 1.1 jtc static void
225 1.3 hubertf evalerr(es, type, str)
226 1.3 hubertf Expr_state *es;
227 1.1 jtc enum error_type type;
228 1.1 jtc const char *str;
229 1.1 jtc {
230 1.1 jtc char tbuf[2];
231 1.1 jtc const char *s;
232 1.1 jtc
233 1.1 jtc switch (type) {
234 1.1 jtc case ET_UNEXPECTED:
235 1.1 jtc switch (es->tok) {
236 1.1 jtc case VAR:
237 1.1 jtc s = es->val->name;
238 1.1 jtc break;
239 1.1 jtc case LIT:
240 1.1 jtc s = str_val(es->val);
241 1.1 jtc break;
242 1.1 jtc case END:
243 1.1 jtc s = "end of expression";
244 1.1 jtc break;
245 1.1 jtc case BAD:
246 1.1 jtc tbuf[0] = *es->tokp;
247 1.1 jtc tbuf[1] = '\0';
248 1.1 jtc s = tbuf;
249 1.1 jtc break;
250 1.1 jtc default:
251 1.1 jtc s = opinfo[(int)es->tok].name;
252 1.1 jtc }
253 1.10 kamil warningf(true, "%s: unexpected `%s'", es->expression, s);
254 1.1 jtc break;
255 1.1 jtc
256 1.1 jtc case ET_BADLIT:
257 1.10 kamil warningf(true, "%s: bad number `%s'", es->expression, str);
258 1.1 jtc break;
259 1.1 jtc
260 1.1 jtc case ET_RECURSIVE:
261 1.10 kamil warningf(true, "%s: expression recurses on parameter `%s'",
262 1.1 jtc es->expression, str);
263 1.1 jtc break;
264 1.1 jtc
265 1.1 jtc case ET_LVALUE:
266 1.10 kamil warningf(true, "%s: %s requires lvalue",
267 1.1 jtc es->expression, str);
268 1.1 jtc break;
269 1.1 jtc
270 1.1 jtc case ET_RDONLY:
271 1.10 kamil warningf(true, "%s: %s applied to read only variable",
272 1.1 jtc es->expression, str);
273 1.1 jtc break;
274 1.1 jtc
275 1.1 jtc default: /* keep gcc happy */
276 1.1 jtc case ET_STR:
277 1.10 kamil warningf(true, "%s: %s", es->expression, str);
278 1.1 jtc break;
279 1.1 jtc }
280 1.1 jtc unwind(LAEXPR);
281 1.1 jtc }
282 1.1 jtc
283 1.1 jtc static struct tbl *
284 1.3 hubertf evalexpr(es, prec)
285 1.3 hubertf Expr_state *es;
286 1.1 jtc enum prec prec;
287 1.1 jtc {
288 1.3 hubertf struct tbl *vl, UNINITIALIZED(*vr), *vasn;
289 1.3 hubertf enum token op;
290 1.1 jtc long UNINITIALIZED(res);
291 1.1 jtc
292 1.1 jtc if (prec == P_PRIMARY) {
293 1.1 jtc op = es->tok;
294 1.1 jtc if (op == O_BNOT || op == O_LNOT || op == O_MINUS
295 1.1 jtc || op == O_PLUS)
296 1.1 jtc {
297 1.3 hubertf token(es);
298 1.3 hubertf vl = intvar(es, evalexpr(es, P_PRIMARY));
299 1.1 jtc if (op == O_BNOT)
300 1.1 jtc vl->val.i = ~vl->val.i;
301 1.1 jtc else if (op == O_LNOT)
302 1.1 jtc vl->val.i = !vl->val.i;
303 1.1 jtc else if (op == O_MINUS)
304 1.1 jtc vl->val.i = -vl->val.i;
305 1.1 jtc /* op == O_PLUS is a no-op */
306 1.1 jtc } else if (op == OPEN_PAREN) {
307 1.3 hubertf token(es);
308 1.3 hubertf vl = evalexpr(es, MAX_PREC);
309 1.1 jtc if (es->tok != CLOSE_PAREN)
310 1.3 hubertf evalerr(es, ET_STR, "missing )");
311 1.3 hubertf token(es);
312 1.1 jtc } else if (op == O_PLUSPLUS || op == O_MINUSMINUS) {
313 1.3 hubertf token(es);
314 1.13 kre if (es->tok != VAR)
315 1.13 kre evalerr(es, ET_LVALUE, opinfo[(int) op].name);
316 1.10 kamil vl = do_ppmm(es, op, es->val, true);
317 1.3 hubertf token(es);
318 1.1 jtc } else if (op == VAR || op == LIT) {
319 1.1 jtc vl = es->val;
320 1.3 hubertf token(es);
321 1.1 jtc } else {
322 1.3 hubertf evalerr(es, ET_UNEXPECTED, (char *) 0);
323 1.1 jtc /*NOTREACHED*/
324 1.1 jtc }
325 1.1 jtc if (es->tok == O_PLUSPLUS || es->tok == O_MINUSMINUS) {
326 1.10 kamil vl = do_ppmm(es, es->tok, vl, false);
327 1.3 hubertf token(es);
328 1.1 jtc }
329 1.1 jtc return vl;
330 1.1 jtc }
331 1.3 hubertf vl = evalexpr(es, ((int) prec) - 1);
332 1.1 jtc for (op = es->tok; IS_BINOP(op) && opinfo[(int) op].prec == prec;
333 1.1 jtc op = es->tok)
334 1.1 jtc {
335 1.3 hubertf token(es);
336 1.1 jtc vasn = vl;
337 1.1 jtc if (op != O_ASN) /* vl may not have a value yet */
338 1.3 hubertf vl = intvar(es, vl);
339 1.1 jtc if (IS_ASSIGNOP(op)) {
340 1.3 hubertf assign_check(es, op, vasn);
341 1.3 hubertf vr = intvar(es, evalexpr(es, P_ASSIGN));
342 1.1 jtc } else if (op != O_TERN && op != O_LAND && op != O_LOR)
343 1.3 hubertf vr = intvar(es, evalexpr(es, ((int) prec) - 1));
344 1.1 jtc if ((op == O_DIV || op == O_MOD || op == O_DIVASN
345 1.1 jtc || op == O_MODASN) && vr->val.i == 0)
346 1.1 jtc {
347 1.1 jtc if (es->noassign)
348 1.1 jtc vr->val.i = 1;
349 1.1 jtc else
350 1.3 hubertf evalerr(es, ET_STR, "zero divisor");
351 1.1 jtc }
352 1.1 jtc switch ((int) op) {
353 1.1 jtc case O_TIMES:
354 1.1 jtc case O_TIMESASN:
355 1.1 jtc res = vl->val.i * vr->val.i;
356 1.1 jtc break;
357 1.1 jtc case O_DIV:
358 1.1 jtc case O_DIVASN:
359 1.1 jtc res = vl->val.i / vr->val.i;
360 1.1 jtc break;
361 1.1 jtc case O_MOD:
362 1.1 jtc case O_MODASN:
363 1.1 jtc res = vl->val.i % vr->val.i;
364 1.1 jtc break;
365 1.1 jtc case O_PLUS:
366 1.1 jtc case O_PLUSASN:
367 1.1 jtc res = vl->val.i + vr->val.i;
368 1.1 jtc break;
369 1.1 jtc case O_MINUS:
370 1.1 jtc case O_MINUSASN:
371 1.1 jtc res = vl->val.i - vr->val.i;
372 1.1 jtc break;
373 1.1 jtc case O_LSHIFT:
374 1.1 jtc case O_LSHIFTASN:
375 1.1 jtc res = vl->val.i << vr->val.i;
376 1.1 jtc break;
377 1.1 jtc case O_RSHIFT:
378 1.1 jtc case O_RSHIFTASN:
379 1.1 jtc res = vl->val.i >> vr->val.i;
380 1.1 jtc break;
381 1.1 jtc case O_LT:
382 1.1 jtc res = vl->val.i < vr->val.i;
383 1.1 jtc break;
384 1.1 jtc case O_LE:
385 1.1 jtc res = vl->val.i <= vr->val.i;
386 1.1 jtc break;
387 1.1 jtc case O_GT:
388 1.1 jtc res = vl->val.i > vr->val.i;
389 1.1 jtc break;
390 1.1 jtc case O_GE:
391 1.1 jtc res = vl->val.i >= vr->val.i;
392 1.1 jtc break;
393 1.1 jtc case O_EQ:
394 1.1 jtc res = vl->val.i == vr->val.i;
395 1.1 jtc break;
396 1.1 jtc case O_NE:
397 1.1 jtc res = vl->val.i != vr->val.i;
398 1.1 jtc break;
399 1.1 jtc case O_BAND:
400 1.1 jtc case O_BANDASN:
401 1.1 jtc res = vl->val.i & vr->val.i;
402 1.1 jtc break;
403 1.1 jtc case O_BXOR:
404 1.1 jtc case O_BXORASN:
405 1.1 jtc res = vl->val.i ^ vr->val.i;
406 1.1 jtc break;
407 1.1 jtc case O_BOR:
408 1.1 jtc case O_BORASN:
409 1.1 jtc res = vl->val.i | vr->val.i;
410 1.1 jtc break;
411 1.1 jtc case O_LAND:
412 1.1 jtc if (!vl->val.i)
413 1.1 jtc es->noassign++;
414 1.3 hubertf vr = intvar(es, evalexpr(es, ((int) prec) - 1));
415 1.1 jtc res = vl->val.i && vr->val.i;
416 1.1 jtc if (!vl->val.i)
417 1.1 jtc es->noassign--;
418 1.1 jtc break;
419 1.1 jtc case O_LOR:
420 1.1 jtc if (vl->val.i)
421 1.1 jtc es->noassign++;
422 1.3 hubertf vr = intvar(es, evalexpr(es, ((int) prec) - 1));
423 1.1 jtc res = vl->val.i || vr->val.i;
424 1.1 jtc if (vl->val.i)
425 1.1 jtc es->noassign--;
426 1.1 jtc break;
427 1.1 jtc case O_TERN:
428 1.1 jtc {
429 1.8 christos int ex = vl->val.i != 0;
430 1.8 christos if (!ex)
431 1.1 jtc es->noassign++;
432 1.3 hubertf vl = evalexpr(es, MAX_PREC);
433 1.8 christos if (!ex)
434 1.1 jtc es->noassign--;
435 1.1 jtc if (es->tok != CTERN)
436 1.3 hubertf evalerr(es, ET_STR, "missing :");
437 1.3 hubertf token(es);
438 1.8 christos if (ex)
439 1.1 jtc es->noassign++;
440 1.3 hubertf vr = evalexpr(es, P_TERN);
441 1.8 christos if (ex)
442 1.1 jtc es->noassign--;
443 1.8 christos vl = ex ? vl : vr;
444 1.1 jtc }
445 1.1 jtc break;
446 1.1 jtc case O_ASN:
447 1.1 jtc res = vr->val.i;
448 1.1 jtc break;
449 1.1 jtc case O_COMMA:
450 1.1 jtc res = vr->val.i;
451 1.1 jtc break;
452 1.1 jtc }
453 1.1 jtc if (IS_ASSIGNOP(op)) {
454 1.1 jtc vr->val.i = res;
455 1.1 jtc if (vasn->flag & INTEGER)
456 1.1 jtc setint_v(vasn, vr);
457 1.1 jtc else
458 1.1 jtc setint(vasn, res);
459 1.1 jtc vl = vr;
460 1.1 jtc } else if (op != O_TERN)
461 1.1 jtc vl->val.i = res;
462 1.1 jtc }
463 1.1 jtc return vl;
464 1.1 jtc }
465 1.1 jtc
466 1.1 jtc static void
467 1.3 hubertf token(es)
468 1.3 hubertf Expr_state *es;
469 1.1 jtc {
470 1.3 hubertf const char *cp;
471 1.3 hubertf int c;
472 1.1 jtc char *tvar;
473 1.1 jtc
474 1.1 jtc /* skip white space */
475 1.7 rillig for (cp = es->tokp; (c = *cp), isspace((unsigned char)c); cp++)
476 1.1 jtc ;
477 1.1 jtc es->tokp = cp;
478 1.1 jtc
479 1.1 jtc if (c == '\0')
480 1.1 jtc es->tok = END;
481 1.1 jtc else if (letter(c)) {
482 1.1 jtc for (; letnum(c); c = *cp)
483 1.1 jtc cp++;
484 1.1 jtc if (c == '[') {
485 1.1 jtc int len;
486 1.1 jtc
487 1.1 jtc len = array_ref_len(cp);
488 1.1 jtc if (len == 0)
489 1.3 hubertf evalerr(es, ET_STR, "missing ]");
490 1.1 jtc cp += len;
491 1.1 jtc }
492 1.3 hubertf #ifdef KSH
493 1.3 hubertf else if (c == '(' /*)*/ ) {
494 1.3 hubertf /* todo: add math functions (all take single argument):
495 1.3 hubertf * abs acos asin atan cos cosh exp int log sin sinh sqrt
496 1.3 hubertf * tan tanh
497 1.3 hubertf */
498 1.3 hubertf ;
499 1.3 hubertf }
500 1.3 hubertf #endif /* KSH */
501 1.1 jtc if (es->noassign) {
502 1.1 jtc es->val = tempvar();
503 1.1 jtc es->val->flag |= EXPRLVALUE;
504 1.1 jtc } else {
505 1.1 jtc tvar = str_nsave(es->tokp, cp - es->tokp, ATEMP);
506 1.1 jtc es->val = global(tvar);
507 1.1 jtc afree(tvar, ATEMP);
508 1.1 jtc }
509 1.1 jtc es->tok = VAR;
510 1.1 jtc } else if (digit(c)) {
511 1.1 jtc for (; c != '_' && (letnum(c) || c == '#'); c = *cp++)
512 1.1 jtc ;
513 1.1 jtc tvar = str_nsave(es->tokp, --cp - es->tokp, ATEMP);
514 1.1 jtc es->val = tempvar();
515 1.1 jtc es->val->flag &= ~INTEGER;
516 1.1 jtc es->val->type = 0;
517 1.1 jtc es->val->val.s = tvar;
518 1.1 jtc if (setint_v(es->val, es->val) == NULL)
519 1.3 hubertf evalerr(es, ET_BADLIT, tvar);
520 1.1 jtc afree(tvar, ATEMP);
521 1.1 jtc es->tok = LIT;
522 1.1 jtc } else {
523 1.1 jtc int i, n0;
524 1.1 jtc
525 1.1 jtc for (i = 0; (n0 = opinfo[i].name[0]); i++)
526 1.1 jtc if (c == n0
527 1.1 jtc && strncmp(cp, opinfo[i].name, opinfo[i].len) == 0)
528 1.1 jtc {
529 1.1 jtc es->tok = (enum token) i;
530 1.1 jtc cp += opinfo[i].len;
531 1.1 jtc break;
532 1.1 jtc }
533 1.1 jtc if (!n0)
534 1.1 jtc es->tok = BAD;
535 1.1 jtc }
536 1.1 jtc es->tokp = cp;
537 1.1 jtc }
538 1.1 jtc
539 1.1 jtc /* Do a ++ or -- operation */
540 1.1 jtc static struct tbl *
541 1.11 joerg do_ppmm(Expr_state *es, enum token op, struct tbl *vasn, bool is_prefix)
542 1.1 jtc {
543 1.1 jtc struct tbl *vl;
544 1.1 jtc int oval;
545 1.1 jtc
546 1.3 hubertf assign_check(es, op, vasn);
547 1.1 jtc
548 1.3 hubertf vl = intvar(es, vasn);
549 1.1 jtc oval = op == O_PLUSPLUS ? vl->val.i++ : vl->val.i--;
550 1.1 jtc if (vasn->flag & INTEGER)
551 1.1 jtc setint_v(vasn, vl);
552 1.1 jtc else
553 1.1 jtc setint(vasn, vl->val.i);
554 1.1 jtc if (!is_prefix) /* undo the inc/dec */
555 1.1 jtc vl->val.i = oval;
556 1.1 jtc
557 1.1 jtc return vl;
558 1.1 jtc }
559 1.1 jtc
560 1.1 jtc static void
561 1.3 hubertf assign_check(es, op, vasn)
562 1.3 hubertf Expr_state *es;
563 1.1 jtc enum token op;
564 1.1 jtc struct tbl *vasn;
565 1.1 jtc {
566 1.1 jtc if (vasn->name[0] == '\0' && !(vasn->flag & EXPRLVALUE))
567 1.3 hubertf evalerr(es, ET_LVALUE, opinfo[(int) op].name);
568 1.1 jtc else if (vasn->flag & RDONLY)
569 1.3 hubertf evalerr(es, ET_RDONLY, opinfo[(int) op].name);
570 1.1 jtc }
571 1.1 jtc
572 1.1 jtc static struct tbl *
573 1.1 jtc tempvar()
574 1.1 jtc {
575 1.12 kamil struct tbl *vp;
576 1.1 jtc
577 1.1 jtc vp = (struct tbl*) alloc(sizeof(struct tbl), ATEMP);
578 1.1 jtc vp->flag = ISSET|INTEGER;
579 1.1 jtc vp->type = 0;
580 1.1 jtc vp->areap = ATEMP;
581 1.1 jtc vp->val.i = 0;
582 1.1 jtc vp->name[0] = '\0';
583 1.1 jtc return vp;
584 1.1 jtc }
585 1.1 jtc
586 1.1 jtc /* cast (string) variable to temporary integer variable */
587 1.1 jtc static struct tbl *
588 1.3 hubertf intvar(es, vp)
589 1.3 hubertf Expr_state *es;
590 1.3 hubertf struct tbl *vp;
591 1.1 jtc {
592 1.3 hubertf struct tbl *vq;
593 1.1 jtc
594 1.1 jtc /* try to avoid replacing a temp var with another temp var */
595 1.1 jtc if (vp->name[0] == '\0'
596 1.1 jtc && (vp->flag & (ISSET|INTEGER|EXPRLVALUE)) == (ISSET|INTEGER))
597 1.1 jtc return vp;
598 1.1 jtc
599 1.1 jtc vq = tempvar();
600 1.1 jtc if (setint_v(vq, vp) == NULL) {
601 1.1 jtc if (vp->flag & EXPRINEVAL)
602 1.3 hubertf evalerr(es, ET_RECURSIVE, vp->name);
603 1.1 jtc es->evaling = vp;
604 1.1 jtc vp->flag |= EXPRINEVAL;
605 1.3 hubertf v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR);
606 1.1 jtc vp->flag &= ~EXPRINEVAL;
607 1.1 jtc es->evaling = (struct tbl *) 0;
608 1.1 jtc }
609 1.1 jtc return vq;
610 1.1 jtc }
611