Root/
Source at commit 0fb83ed27446be7bc114945ca6e74895ac4e65b6 created 13 years 5 months ago. By Xiangfu Liu, use usual name for orig tarball top-level directory | |
---|---|
1 | /* |
2 | * expr.c - Expressions and values |
3 | * |
4 | * Written 2009, 2010 by Werner Almesberger |
5 | * Copyright 2009, 2010 by Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <math.h> |
16 | |
17 | #include "util.h" |
18 | #include "error.h" |
19 | #include "obj.h" |
20 | #include "unparse.h" |
21 | #include "fpd.h" |
22 | #include "expr.h" |
23 | |
24 | |
25 | struct num undef = { .type = nt_none }; |
26 | |
27 | |
28 | /* ----- error reporting --------------------------------------------------- */ |
29 | |
30 | |
31 | void fail_expr(const struct expr *expr) |
32 | { |
33 | char *s; |
34 | |
35 | s = unparse(expr); |
36 | fail("in \"%s\" at line %d", s, expr->lineno); |
37 | free(s); |
38 | } |
39 | |
40 | |
41 | /* ----- unit conversion --------------------------------------------------- */ |
42 | |
43 | |
44 | /* |
45 | * If an expression contains a typo, we may get large exponents. Thus, we just |
46 | * "sprintf" in order to be able to handle any integer. Since the number of |
47 | * different exponents in a session will still be small, we use "unique" to |
48 | * give us a constant string, so that we don't have to worry about memory |
49 | * allocation. |
50 | */ |
51 | |
52 | const char *str_unit(struct num n) |
53 | { |
54 | const char *unit; |
55 | char buf[20]; /* @@@ plenty */ |
56 | |
57 | if (n.exponent == 0) |
58 | return ""; |
59 | switch (n.type) { |
60 | case nt_mm: |
61 | unit = "mm"; |
62 | break; |
63 | case nt_mil: |
64 | unit = "mil"; |
65 | break; |
66 | default: |
67 | abort(); |
68 | } |
69 | if (n.exponent == 1) |
70 | return unit; |
71 | sprintf(buf, "%s^%d", unit, n.exponent); |
72 | return unique(buf); |
73 | } |
74 | |
75 | |
76 | int to_unit(struct num *n) |
77 | { |
78 | if (!is_distance(*n)) { |
79 | fail("%s^%d is not a distance", |
80 | n->type == nt_mm ? "mm" : n->type == nt_mil ? "mil" : "?", |
81 | n->exponent); |
82 | return 0; |
83 | } |
84 | switch (n->type) { |
85 | case nt_mil: |
86 | n->n = mil_to_units(n->n); |
87 | break; |
88 | case nt_mm: |
89 | n->n = mm_to_units(n->n); |
90 | break; |
91 | default: |
92 | abort(); |
93 | } |
94 | return 1; |
95 | } |
96 | |
97 | |
98 | /* ----- primary expressions ----------------------------------------------- */ |
99 | |
100 | |
101 | struct num op_string(const struct expr *self, const struct frame *frame) |
102 | { |
103 | fail("cannot evaluate string"); |
104 | return undef; |
105 | } |
106 | |
107 | |
108 | struct num op_num(const struct expr *self, const struct frame *frame) |
109 | { |
110 | return self->u.num; |
111 | } |
112 | |
113 | |
114 | /* |
115 | * We have two modes of operation: during instantiation and editing, after |
116 | * instantiation. During instantiation, we follow curr_row and curr_parent. |
117 | * These pointers are NULL when instantiation finishes, and we use this as a |
118 | * signal that we're now in editing mode. In editing mode, the "active" values |
119 | * are used instead of the "current" ones. |
120 | */ |
121 | |
122 | struct num eval_var(const struct frame *frame, const char *name) |
123 | { |
124 | const struct table *table; |
125 | const struct loop *loop; |
126 | const struct value *value; |
127 | struct var *var; |
128 | struct num res; |
129 | |
130 | for (table = frame->tables; table; table = table->next) { |
131 | value = table->curr_row ? table->curr_row->values : |
132 | table->active_row->values; |
133 | for (var = table->vars; var; var = var->next) { |
134 | if (var->name == name) { |
135 | if (var->visited) { |
136 | fail("recursive evaluation through " |
137 | "\"%s\"", name); |
138 | return undef; |
139 | } |
140 | var->visited = 1; |
141 | res = eval_num(value->expr, frame); |
142 | var->visited = 0; |
143 | return res; |
144 | |
145 | } |
146 | value = value->next; |
147 | } |
148 | } |
149 | for (loop = frame->loops; loop; loop = loop->next) |
150 | if (loop->var.name == name) { |
151 | if (loop->curr_value == UNDEF) |
152 | return make_num(loop->n+loop->active); |
153 | if (!loop->initialized) { |
154 | fail("uninitialized loop \"%s\"", name); |
155 | return undef; |
156 | } |
157 | return make_num(loop->curr_value); |
158 | } |
159 | if (frame->curr_parent) |
160 | return eval_var(frame->curr_parent, name); |
161 | if (frame->active_ref) |
162 | return eval_var(frame->active_ref->frame, name); |
163 | return undef; |
164 | } |
165 | |
166 | |
167 | static const char *eval_string_var(const struct frame *frame, const char *name) |
168 | { |
169 | const struct table *table; |
170 | const struct loop *loop; |
171 | const struct value *value; |
172 | struct var *var; |
173 | const char *res; |
174 | |
175 | for (table = frame->tables; table; table = table->next) { |
176 | value = table->curr_row ? table->curr_row->values : |
177 | table->active_row->values; |
178 | for (var = table->vars; var; var = var->next) { |
179 | if (var->name == name) { |
180 | if (var->visited) |
181 | return NULL; |
182 | var->visited = 1; |
183 | res = eval_str(value->expr, frame); |
184 | var->visited = 0; |
185 | return res; |
186 | |
187 | } |
188 | value = value->next; |
189 | } |
190 | } |
191 | for (loop = frame->loops; loop; loop = loop->next) |
192 | if (loop->var.name == name) |
193 | return NULL; |
194 | if (frame->curr_parent) |
195 | return eval_string_var(frame->curr_parent, name); |
196 | if (frame->active_ref) |
197 | return eval_string_var(frame->active_ref->frame, name); |
198 | return NULL; |
199 | } |
200 | |
201 | |
202 | struct num op_var(const struct expr *self, const struct frame *frame) |
203 | { |
204 | struct num res; |
205 | |
206 | res = eval_var(frame, self->u.var); |
207 | if (is_undef(res)) |
208 | fail("undefined variable \"%s\"", self->u.var); |
209 | return res; |
210 | } |
211 | |
212 | |
213 | /* ----- arithmetic -------------------------------------------------------- */ |
214 | |
215 | |
216 | static struct num compatible_sum(struct num *a, struct num *b) |
217 | { |
218 | struct num res; |
219 | |
220 | if (a->type != b->type) { |
221 | if (a->type == nt_mil) { |
222 | a->type = nt_mm; |
223 | a->n = mil_to_mm(a->n, a->exponent); |
224 | } |
225 | if (b->type == nt_mil) { |
226 | b->type = nt_mm; |
227 | b->n = mil_to_mm(b->n, a->exponent); |
228 | } |
229 | } |
230 | if (a->exponent != b->exponent) { |
231 | fail("incompatible exponents (%d, %d)", |
232 | a->exponent, b->exponent); |
233 | return undef; |
234 | } |
235 | res.type = a->type; |
236 | res.exponent = a->exponent; |
237 | res.n = 0; /* keep gcc happy */ |
238 | return res; |
239 | } |
240 | |
241 | |
242 | static struct num compatible_mult(struct num *a, struct num *b, |
243 | int exponent) |
244 | { |
245 | struct num res; |
246 | |
247 | if (a->type != b->type) { |
248 | if (a->type == nt_mil) { |
249 | a->type = nt_mm; |
250 | a->n = mil_to_mm(a->n, a->exponent); |
251 | } |
252 | if (b->type == nt_mil) { |
253 | b->type = nt_mm; |
254 | b->n = mil_to_mm(b->n, b->exponent); |
255 | } |
256 | } |
257 | res.type = a->type; |
258 | res.exponent = exponent; |
259 | res.n = 0; /* keep gcc happy */ |
260 | return res; |
261 | } |
262 | |
263 | |
264 | static struct num sin_cos(const struct expr *self, |
265 | const struct frame *frame, double (*fn)(double arg)) |
266 | { |
267 | struct num res; |
268 | |
269 | res = eval_num(self->u.op.a, frame); |
270 | if (is_undef(res)) |
271 | return undef; |
272 | if (!is_dimensionless(res)) { |
273 | fail("angle must be dimensionless"); |
274 | return undef; |
275 | } |
276 | res.n = fn(res.n/180.0*M_PI); |
277 | return res; |
278 | } |
279 | |
280 | |
281 | struct num op_sin(const struct expr *self, const struct frame *frame) |
282 | { |
283 | return sin_cos(self, frame, sin); |
284 | } |
285 | |
286 | |
287 | struct num op_cos(const struct expr *self, const struct frame *frame) |
288 | { |
289 | return sin_cos(self, frame, cos); |
290 | } |
291 | |
292 | |
293 | struct num op_sqrt(const struct expr *self, const struct frame *frame) |
294 | { |
295 | struct num res; |
296 | |
297 | res = eval_num(self->u.op.a, frame); |
298 | if (is_undef(res)) |
299 | return undef; |
300 | if (res.exponent & 1) { |
301 | fail("exponent of sqrt argument must be a multiple of two"); |
302 | return undef; |
303 | } |
304 | if (res.n < 0) { |
305 | fail("argument of sqrt must be positive"); |
306 | return undef; |
307 | } |
308 | res.n = sqrt(res.n); |
309 | res.exponent >>= 1; |
310 | return res; |
311 | } |
312 | |
313 | |
314 | struct num op_minus(const struct expr *self, const struct frame *frame) |
315 | { |
316 | struct num res; |
317 | |
318 | res = eval_num(self->u.op.a, frame); |
319 | if (!is_undef(res)) |
320 | res.n = -res.n; |
321 | return res; |
322 | } |
323 | |
324 | |
325 | #define BINARY \ |
326 | struct num a, b, res; \ |
327 | \ |
328 | a = eval_num(self->u.op.a, frame); \ |
329 | if (is_undef(a)) \ |
330 | return undef; \ |
331 | b = eval_num(self->u.op.b, frame); \ |
332 | if (is_undef(b)) \ |
333 | return undef; |
334 | |
335 | |
336 | struct num op_add(const struct expr *self, const struct frame *frame) |
337 | { |
338 | BINARY; |
339 | res = compatible_sum(&a, &b); |
340 | if (is_undef(res)) |
341 | return undef; |
342 | res.n = a.n+b.n; |
343 | return res; |
344 | } |
345 | |
346 | |
347 | struct num op_sub(const struct expr *self, const struct frame *frame) |
348 | { |
349 | BINARY; |
350 | res = compatible_sum(&a, &b); |
351 | if (is_undef(res)) |
352 | return undef; |
353 | res.n = a.n-b.n; |
354 | return res; |
355 | } |
356 | |
357 | |
358 | struct num op_mult(const struct expr *self, const struct frame *frame) |
359 | { |
360 | BINARY; |
361 | res = compatible_mult(&a, &b, a.exponent+b.exponent); |
362 | res.n = a.n*b.n; |
363 | return res; |
364 | } |
365 | |
366 | |
367 | struct num op_div(const struct expr *self, const struct frame *frame) |
368 | { |
369 | BINARY; |
370 | if (!b.n) { |
371 | fail("division by zero"); |
372 | return undef; |
373 | } |
374 | res = compatible_mult(&a, &b, a.exponent-b.exponent); |
375 | res.n = a.n/b.n; |
376 | return res; |
377 | } |
378 | |
379 | |
380 | /* ----- expression construction ------------------------------------------- */ |
381 | |
382 | |
383 | struct expr *new_op(op_type op) |
384 | { |
385 | struct expr *expr; |
386 | |
387 | expr = alloc_type(struct expr); |
388 | expr->op = op; |
389 | expr->lineno = lineno; |
390 | return expr; |
391 | } |
392 | |
393 | |
394 | struct expr *binary_op(op_type op, struct expr *a, struct expr *b) |
395 | { |
396 | struct expr *expr; |
397 | |
398 | expr = new_op(op); |
399 | expr->u.op.a = a; |
400 | expr->u.op.b = b; |
401 | return expr; |
402 | } |
403 | |
404 | |
405 | const char *eval_str(const struct expr *expr, const struct frame *frame) |
406 | { |
407 | if (expr->op == op_string) |
408 | return expr->u.str; |
409 | if (expr->op == op_var) |
410 | return eval_string_var(frame, expr->u.var); |
411 | return NULL; |
412 | } |
413 | |
414 | |
415 | struct num eval_num(const struct expr *expr, const struct frame *frame) |
416 | { |
417 | return expr->op(expr, frame); |
418 | } |
419 | |
420 | |
421 | /* ----- string expansion -------------------------------------------------- */ |
422 | |
423 | |
424 | char *expand(const char *name, const struct frame *frame) |
425 | { |
426 | int len = strlen(name); |
427 | char *buf = alloc_size(len+1); |
428 | char num_buf[100]; /* enough :-) */ |
429 | const char *s, *s0; |
430 | char *var; |
431 | const char *var_unique, *value_string; |
432 | struct num value; |
433 | int i, value_len; |
434 | |
435 | i = 0; |
436 | for (s = name; *s; s++) { |
437 | if (*s != '$') { |
438 | buf[i++] = *s; |
439 | continue; |
440 | } |
441 | s0 = ++s; |
442 | if (*s != '{') { |
443 | while (is_id_char(*s, s == s0)) |
444 | s++; |
445 | if (s == s0) { |
446 | if (*s) |
447 | goto invalid; |
448 | else { |
449 | fail("incomplete variable name"); |
450 | goto fail; |
451 | } |
452 | } |
453 | var = strnalloc(s0, s-s0); |
454 | len -= s-s0+1; |
455 | s--; |
456 | } else { |
457 | s++; |
458 | while (*s != '}') { |
459 | if (!*s) { |
460 | fail("unfinished \"${...}\""); |
461 | goto fail; |
462 | } |
463 | if (!is_id_char(*s, s == s0+1)) |
464 | goto invalid; |
465 | s++; |
466 | } |
467 | var = strnalloc(s0+1, s-s0-1); |
468 | len -= s-s0+2; |
469 | } |
470 | if (!frame) |
471 | continue; |
472 | var_unique = unique(var); |
473 | free(var); |
474 | value_string = eval_string_var(frame, var_unique); |
475 | if (value_string) |
476 | value_len = strlen(value_string); |
477 | else { |
478 | value = eval_var(frame, var_unique); |
479 | if (is_undef(value)) { |
480 | fail("undefined variable \"%s\"", var_unique); |
481 | goto fail; |
482 | } |
483 | value_len = snprintf(num_buf, sizeof(num_buf), "%lg%s", |
484 | value.n, str_unit(value)); |
485 | value_string = num_buf; |
486 | } |
487 | len += value_len; |
488 | buf = realloc(buf, len+1); |
489 | if (!buf) |
490 | abort(); |
491 | strcpy(buf+i, value_string); |
492 | i += value_len; |
493 | } |
494 | buf[i] = 0; |
495 | return buf; |
496 | |
497 | invalid: |
498 | fail("invalid character in variable name"); |
499 | fail: |
500 | free(buf); |
501 | return NULL; |
502 | } |
503 | |
504 | |
505 | /* ----- make a number -----------------------------------------------------*/ |
506 | |
507 | |
508 | struct expr *new_num(struct num num) |
509 | { |
510 | struct expr *expr; |
511 | |
512 | expr = new_op(op_num); |
513 | expr->u.num = num; |
514 | return expr; |
515 | } |
516 | |
517 | |
518 | /* ----- expression-only parser -------------------------------------------- */ |
519 | |
520 | |
521 | struct expr *parse_expr(const char *s) |
522 | { |
523 | scan_expr(s); |
524 | return yyparse() ? NULL : expr_result; |
525 | } |
526 | |
527 | |
528 | static void vacate_op(struct expr *expr) |
529 | { |
530 | if (expr->op == op_num || expr->op == op_var) |
531 | return; |
532 | if (expr->op == op_string) { |
533 | free(expr->u.str); |
534 | return; |
535 | } |
536 | if (expr->op == op_minus || |
537 | expr->op == op_sin || expr->op == op_cos || expr->op == op_sqrt) { |
538 | free_expr(expr->u.op.a); |
539 | return; |
540 | } |
541 | if (expr->op == op_add || expr->op == op_sub || |
542 | expr->op == op_mult || expr->op == op_div) { |
543 | free_expr(expr->u.op.a); |
544 | free_expr(expr->u.op.b); |
545 | return; |
546 | } |
547 | abort(); |
548 | } |
549 | |
550 | |
551 | void free_expr(struct expr *expr) |
552 | { |
553 | vacate_op(expr); |
554 | free(expr); |
555 | } |
556 | |
557 | |
558 | /* ----- [var =] value, ... shortcuts -------------------------------------- */ |
559 | |
560 | |
561 | int parse_var(const char *s, const char **id, struct value **values, |
562 | int max_values) |
563 | { |
564 | const struct value *value; |
565 | int n; |
566 | |
567 | scan_var(s); |
568 | if (yyparse()) |
569 | return -1; |
570 | if (id) |
571 | *id = var_id; |
572 | *values = var_value_list; |
573 | n = 0; |
574 | for (value = var_value_list; value; value = value->next) |
575 | n++; |
576 | if (max_values == -1 || n <= max_values) |
577 | return n; |
578 | free_values(var_value_list, 0); |
579 | return -1; |
580 | } |
581 | |
582 | |
583 | int parse_values(const char *s, struct value **values) |
584 | { |
585 | const struct value *value; |
586 | int n; |
587 | |
588 | scan_values(s); |
589 | if (yyparse()) |
590 | return -1; |
591 | *values = var_value_list; |
592 | n = 0; |
593 | for (value = var_value_list; value; value = value->next) |
594 | n++; |
595 | return n; |
596 | } |
597 | |
598 | |
599 | void free_values(struct value *values, int keep_expr) |
600 | { |
601 | struct value *next; |
602 | |
603 | while (values) { |
604 | next = values->next; |
605 | if (!keep_expr) |
606 | free_expr(values->expr); |
607 | free(values); |
608 | values = next; |
609 | } |
610 | } |
611 |
Branches:
master