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