expr.c 38.6 KB
Newer Older
Miguel de Icaza's avatar
Miguel de Icaza committed
1 2 3 4 5 6
/*
 * expr.c: Expression evaluation in Gnumeriuc
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org).
 */
7
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
8
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
9
#include <math.h>
10
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "expr.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
14
#include "format.h"
15
#include "func.h"
16
#include "utils.h"
17

18
ExprTree *
19 20
expr_parse_string (const char *expr, Sheet *sheet, int col, int row,
		   const char **desired_format, char **error_msg)
21
{
22
	ExprTree *tree;
23
	g_return_val_if_fail (expr != NULL, NULL);
24 25

	switch (gnumeric_expr_parser (expr, sheet, col, row, desired_format, &tree)) {
26 27
	case PARSE_OK:
		*error_msg = NULL;
28 29
		tree->ref_count = 1;
		return tree;
30

Arturo Espinosa's avatar
Arturo Espinosa committed
31 32 33
	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;
34

35 36 37
	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
38
	default:
Arturo Espinosa's avatar
Arturo Espinosa committed
39 40 41
		g_assert_not_reached ();
		*error_msg = _("Impossible!");
		break;
42
	}
43 44 45
	return NULL;
}

46

47
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
48
expr_tree_new (void)
49
{
50 51 52 53 54 55
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (!ans)
		return NULL;
	
56 57 58 59 60 61
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = NULL;
	return ans;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
62 63 64 65
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
66 67 68 69 70 71
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Morten Welinder's avatar
Morten Welinder committed
72
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
73 74 75 76 77
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
78 79 80
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
81
	switch (tree->oper){
82
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
83
		break;
84

85
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
86
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
87
		break;
88

Morten Welinder's avatar
Morten Welinder committed
89 90 91 92 93 94 95
	case OPER_FUNCALL: {
		GList *l;

		for (l = tree->u.function.arg_list; l; l = l->next)
			do_expr_tree_unref (l->data);
		g_list_free (tree->u.function.arg_list);
		symbol_unref (tree->u.function.symbol);
Arturo Espinosa's avatar
Arturo Espinosa committed
96
		break;
Morten Welinder's avatar
Morten Welinder committed
97
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
98

99
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
100 101 102 103
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

104
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
105 106 107
		do_expr_tree_unref (tree->u.value);
		break;
	}
108

Morten Welinder's avatar
Morten Welinder committed
109
	g_free (tree);
110 111
}

Morten Welinder's avatar
Morten Welinder committed
112 113 114 115 116 117
/*
 * expr_tree_unref:
 * Decrements the ref_count for part of a tree.  (All trees are expected
 * to have been created with a ref-count of one, so when we hit zero, we
 * go down over the tree and unref the tree and its leaves stuff.)
 */
118 119 120 121 122 123
void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Arturo Espinosa's avatar
Arturo Espinosa committed
124
	do_expr_tree_unref (tree);
125 126
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
127 128 129 130
/*
 * simplistic value rendering
 */
char *
131
value_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
132 133 134 135 136 137
{
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
138
		return g_strdup_printf ("%d", value->v.v_int);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
139 140

	case VALUE_FLOAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
141
		return g_strdup_printf ("%g", value->v.v_float);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
142 143

	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
144
		return g_strdup ("ARRAY");
145

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
146
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
147
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
148
	}
149

Arturo Espinosa's avatar
Arturo Espinosa committed
150
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
151 152
}

153 154 155
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
156
	g_return_if_fail (value != NULL);
157

158 159 160 161 162 163 164
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
165
		break;
166

167 168 169 170
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

171 172
	case VALUE_ARRAY:{
		guint i;
173

174 175
		for (i = 0; i < value->v.array.x; i++)
			g_free (value->v.array.vals [i]);
176

177
		g_free (value->v.array.vals);
Morten Welinder's avatar
Morten Welinder committed
178
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
179
	}
180

Arturo Espinosa's avatar
Arturo Espinosa committed
181 182
	case VALUE_CELLRANGE:
		break;
183

184
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
185
	g_free (value);
186
}
187

Arturo Espinosa's avatar
Arturo Espinosa committed
188 189 190 191
/*
 * Copies a Value.
 */
void
192
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
193 194 195 196 197
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
198

Arturo Espinosa's avatar
Arturo Espinosa committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212
	switch (source->type){
	case VALUE_STRING:
		dest->v.str = source->v.str;
		string_ref (dest->v.str);
		break;

	case VALUE_INTEGER:
		dest->v.v_int = source->v.v_int;
		break;

	case VALUE_FLOAT:
		dest->v.v_float = source->v.v_float;
		break;

Arturo Espinosa's avatar
Arturo Espinosa committed
213
	case VALUE_ARRAY: {
214
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
215 216 217 218 219
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
220 221 222
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
223 224 225 226
/*
 * Makes a copy of a Value
 */
Value *
227
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
228 229 230 231 232 233
{
	Value *new_value;

	g_return_val_if_fail (value != NULL, NULL);
	new_value = g_new (Value, 1);
	value_copy_to (new_value, value);
234

Arturo Espinosa's avatar
Arturo Espinosa committed
235 236 237
	return new_value;
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
Value *
value_float (float_t f)
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = f;

	return v;
}

Value *
value_int (int i)
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_INTEGER;
	v->v.v_int = i;

	return v;
}

260
Value *
261
value_str (const char *str)
262 263 264 265 266 267 268 269 270
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_STRING;
	v->v.str = string_get (str);

	return v;
}

271
Value *
Morten Welinder's avatar
Morten Welinder committed
272
value_cellrange (const CellRef *a, const CellRef *b)
273 274
{
	Value *v = g_new (Value, 1);
Morten Welinder's avatar
Morten Welinder committed
275

276
	v->type = VALUE_CELLRANGE;
Morten Welinder's avatar
Morten Welinder committed
277 278 279
	v->v.cell_range.cell_a = *a;
	v->v.cell_range.cell_b = *b;

280 281 282
	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
283 284 285 286
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
287
Value *
288
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
289 290
{
	Value *newv;
291

Arturo Espinosa's avatar
Arturo Espinosa committed
292 293 294 295
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

	if (v->type == VALUE_FLOAT)
		return v;
296

Arturo Espinosa's avatar
Arturo Espinosa committed
297 298 299
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
300
	value_release (v);
301

Arturo Espinosa's avatar
Arturo Espinosa committed
302 303 304
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
305
int
306
value_get_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
{
	*err = 0;

	if (v->type == VALUE_STRING)
		return atoi (v->v.str->str);

	if (v->type == VALUE_CELLRANGE){
		*err = 1;
		return 0;
	}

	if (v->type == VALUE_INTEGER)
		return v->v.v_int != 0;

	if (v->type == VALUE_FLOAT)
		return v->v.v_float != 0.0;

	if (v->type == VALUE_ARRAY)
		return 0;
326

Arturo Espinosa's avatar
Arturo Espinosa committed
327 328 329 330 331
	g_warning ("Unhandled value in value_get_boolean");

	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
332
float_t
333
value_get_as_double (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
334 335 336 337 338 339 340 341 342 343 344 345 346
{
	if (v->type == VALUE_STRING){
		return atof (v->v.str->str);
	}

	if (v->type == VALUE_CELLRANGE){
		g_warning ("Getting range as a double: what to do?");
		return 0.0;
	}

	if (v->type == VALUE_INTEGER)
		return (float_t) v->v.v_int;

Arturo Espinosa's avatar
Arturo Espinosa committed
347 348
	if (v->type == VALUE_ARRAY)
		return 0.0;
349

Arturo Espinosa's avatar
Arturo Espinosa committed
350
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
351 352
}

353
int
354
value_get_as_int (const Value *v)
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
{
	if (v->type == VALUE_STRING){
		return atoi (v->v.str->str);
	}

	if (v->type == VALUE_CELLRANGE){
		g_warning ("Getting range as a double: what to do?");
		return 0.0;
	}

	if (v->type == VALUE_INTEGER)
		return v->v.v_int;

	if (v->type == VALUE_ARRAY)
		return 0.0;
370

371 372 373
	return (int) v->v.v_float;
}

374 375 376
Value *
value_array_new (guint width, guint height)
{
377
	int x, y;
378 379 380 381 382 383

	Value *v = g_new (Value, 1);
	v->type = VALUE_ARRAY;
	v->v.array.x = width;
	v->v.array.y = height;
	v->v.array.vals = g_new (Value *, width);
384 385 386 387 388 389 390

	for (x = 0; x < width; x++){
		v->v.array.vals [x] = g_new (Value, height);

		for (y = 0; y < height; y++){
			v->v.array.vals[x][y].type = VALUE_INTEGER;
			v->v.array.vals[x][y].v.v_int = 0;
391 392 393 394 395 396 397 398
		}
	}
	return v;
}

void
value_array_resize (Value *v, guint width, guint height)
{
399
	int x, xcpy, ycpy;
400 401 402 403 404 405 406 407 408 409
	Value *newval;

	g_return_if_fail (v);
	g_return_if_fail (v->type == VALUE_ARRAY);

	newval = value_array_new (width, height);

	if (width>v->v.array.x)
		xcpy = v->v.array.x;
	else
410
		xcpy = width;
411 412 413 414 415 416

	if (height>v->v.array.y)
		ycpy = v->v.array.y;
	else
		ycpy = height;

417 418 419 420
	for (x = 0; x < xcpy; x++){
		memcpy (newval->v.array.vals [x],
			v->v.array.vals [x],
			sizeof (Value) * ycpy);
421 422 423 424
	}
	v->v.array.vals = newval->v.array.vals;
	v->v.array.x = width;
	v->v.array.y = height;
425
	value_release (newval);
426 427 428 429 430
}

void
value_array_copy_to (Value *v, const Value *src)
{
431
	int x;
432 433 434 435 436 437

	g_return_if_fail (src->type == VALUE_ARRAY);
	v->type = VALUE_ARRAY;
	v->v.array.x = src->v.array.x;
	v->v.array.y = src->v.array.y;
	v->v.array.vals = g_new (Value *, v->v.array.x);
438 439 440

	for (x = 0; x < v->v.array.x; x++){
		v->v.array.vals [x] = g_new (Value,v->v.array.y);
441

442 443 444
		memcpy (v->v.array.vals   [x],
			src->v.array.vals [x],
			sizeof(Value)*v->v.array.y);
445 446 447 448 449 450 451 452 453
	}
}

guint
value_area_get_width (Value *v)
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
454

455 456 457 458 459 460 461 462 463 464 465 466 467
	if (v->type == VALUE_ARRAY)
		return v->v.array.x;
	else
		return v->v.cell_range.cell_b.col -
		       v->v.cell_range.cell_a.col + 1;
}

guint
value_area_get_height (Value *v)
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
468

469 470 471 472 473 474 475 476 477 478 479 480 481
	if (v->type == VALUE_ARRAY)
		return v->v.array.y;
	else
		return v->v.cell_range.cell_b.row -
		       v->v.cell_range.cell_a.row + 1;
}

const Value *
value_area_get_at_x_y (Value *v, guint x, guint y)
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE,
482 483 484
			      value_int (0));

	if (v->type == VALUE_ARRAY){
485
		g_return_val_if_fail (v->v.array.x < x &&
486 487 488
				      v->v.array.y < y,
				      value_int (0));
		return &v->v.array.vals [x][y];
489 490 491
	} else {
		CellRef *a, *b;
		Cell *cell;
492

493 494 495
		a = &v->v.cell_range.cell_a;
		b = &v->v.cell_range.cell_b;
		g_return_val_if_fail (!a->col_relative,
496
				      value_int (0));
497
		g_return_val_if_fail (!b->col_relative,
498
				      value_int (0));
499
		g_return_val_if_fail (!a->row_relative,
500
				      value_int (0));
501
		g_return_val_if_fail (!b->row_relative,
502
				      value_int (0));
503
		g_return_val_if_fail (a->col<=b->col,
504
				      value_int (0));
505
		g_return_val_if_fail (a->row<=b->row,
506
				      value_int (0));
507

508
		cell = sheet_cell_get (a->sheet, a->col+x, a->row+y);
509

510 511 512
		if (cell && cell->value)
			return cell->value;
		else
513
			return value_int (0);
514
	}
515
	return value_int (0);
516 517
}

518 519 520 521 522 523
static void
cell_ref_make_absolute (CellRef *cell_ref, int eval_col, int eval_row)
{
	g_return_if_fail (cell_ref != NULL);

	if (cell_ref->col_relative)
524
		cell_ref->col = eval_col + cell_ref->col;
525 526 527

	if (cell_ref->row_relative)
		cell_ref->row = eval_row + cell_ref->row;
528

Arturo Espinosa's avatar
Arturo Espinosa committed
529 530
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
531 532
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546
static void
cell_ref_restore_absolute (CellRef *cell_ref, const CellRef *orig, int eval_col, int eval_row)
{
	if (orig->col_relative) {
		cell_ref->col -= eval_col;
		cell_ref->col_relative = 1;
	}

	if (orig->row_relative) {
		cell_ref->row -= eval_row;
		cell_ref->row_relative = 1;
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
547 548
static Value *
eval_cell_value (Sheet *sheet, Value *value)
549
{
Arturo Espinosa's avatar
Arturo Espinosa committed
550
	Value *res;
551

Arturo Espinosa's avatar
Arturo Espinosa committed
552 553
	res = g_new (Value, 1);
	res->type = value->type;
554

Arturo Espinosa's avatar
Arturo Espinosa committed
555 556 557
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
558
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
559
		break;
560

Arturo Espinosa's avatar
Arturo Espinosa committed
561
	case VALUE_INTEGER:
562
		res->v.v_int = value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
563
		break;
564

Arturo Espinosa's avatar
Arturo Espinosa committed
565
	case VALUE_FLOAT:
566
		res->v.v_float = value->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
567
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
568 569 570 571 572 573

	case VALUE_ARRAY:
		g_warning ("VALUE_ARRAY not handled in eval_cell_value\n");
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
		break;
574

Arturo Espinosa's avatar
Arturo Espinosa committed
575 576 577 578 579 580 581
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

582 583 584 585
static void
free_values (Value **values, int top)
{
	int i;
586

587 588 589 590 591
	for (i = 0; i < top; i++)
		value_release (values [i]);
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
592 593 594 595 596 597 598
static Value *
eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
{
	FunctionDefinition *fd;
	GList *l;
	int argc, arg, i;
	Value *v;
599

Arturo Espinosa's avatar
Arturo Espinosa committed
600
	fd = (FunctionDefinition *) tree->u.function.symbol->data;
601

Arturo Espinosa's avatar
Arturo Espinosa committed
602 603 604 605 606 607 608 609 610 611 612 613
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

	if (fd->expr_fn)
	{
		/* Functions that deal with ExprNodes */
		v = fd->expr_fn (sheet, l, eval_col, eval_row, error_string);
	}
	else
	{
		/* Functions that take pre-computed Values */
		Value **values;
Arturo Espinosa's avatar
Arturo Espinosa committed
614 615 616
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
		char *arg_type = fd->args;
		char *argptr = fd->args;
617

618
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
619 620 621 622
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
623 624
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
625 626
				fn_argc_min++;
			fn_argc_max++;
627
		}
628

Arturo Espinosa's avatar
Arturo Espinosa committed
629
		if (argc > fn_argc_max || argc < fn_argc_min){
Arturo Espinosa's avatar
Arturo Espinosa committed
630 631 632 633
			*error_string = _("Invalid number of arguments");
			return NULL;
		}

634
		values = g_new (Value *, fn_argc_max);
635

636
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
637
			ExprTree *t = (ExprTree *) l->data;
638
			int type_mismatch = 0;
639

Arturo Espinosa's avatar
Arturo Espinosa committed
640 641
			if (*arg_type=='|')
				arg_type++;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

			if ((*arg_type != 'A' &&          /* This is so a cell reference */
			     *arg_type != 'r') ||         /* can be converted to a cell range */
			    !t || (t->oper != OPER_VAR)) { /* without being evaluated */
				if ((v = eval_expr (sheet, t, eval_col,
						    eval_row, error_string))==NULL)
					goto free_list;
			} else {
				g_assert (t->oper == OPER_VAR);
				v = value_cellrange (&t->u.ref,
						     &t->u.ref);
				if (!v->v.cell_range.cell_a.sheet)
					v->v.cell_range.cell_a.sheet = sheet;
				if (!v->v.cell_range.cell_b.sheet)
					v->v.cell_range.cell_b.sheet = sheet;
			}
658

Arturo Espinosa's avatar
Arturo Espinosa committed
659
			switch (*arg_type){
660
			case 'f':
661

662 663 664 665
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
666
			case 's':
667 668
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
669
				break;
670
			case 'r':
671 672 673 674 675
				if (v->type != VALUE_CELLRANGE)
					type_mismatch = 1;
				else {
					cell_ref_make_absolute (&v->v.cell_range.cell_a, eval_col, eval_row);
					cell_ref_make_absolute (&v->v.cell_range.cell_b, eval_col, eval_row);
676
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
677
				break;
678 679 680 681 682 683 684 685
			case 'a':
				if (v->type != VALUE_ARRAY)
					type_mismatch = 1;
				break;
			case 'A':
				if (v->type != VALUE_ARRAY &&
				    v->type != VALUE_CELLRANGE)
					type_mismatch = 1;
Michael Meeks's avatar
Michael Meeks committed
686 687 688 689 690

				if (v->type == VALUE_CELLRANGE) {
					cell_ref_make_absolute (&v->v.cell_range.cell_a, eval_col, eval_row);
					cell_ref_make_absolute (&v->v.cell_range.cell_b, eval_col, eval_row);
				}
691 692
				break;
			}
693
			if (type_mismatch){
694 695 696
				free_values (values, arg);
				*error_string = _("Type mismatch");
				return NULL;
697
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
698 699
			values [arg] = v;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
700 701
		while (arg < fn_argc_max)
			values [arg++] = NULL;
702
		v = fd->fn (fd, values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
703 704

	free_list:
705
		for (i = 0; i < arg; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
706 707
			if (values [i] != NULL)
				value_release (values [i]);
708
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
709 710 711 712 713 714
		g_free (values);
		return v;
	}
	return v;
}

715 716 717 718 719
Value *
function_def_call_with_values (Sheet *sheet, FunctionDefinition *fd, int argc,
			       Value *values [], char **error_string)
{
	Value *retval;
720

721 722 723 724 725 726 727 728 729 730 731
	if (fd->expr_fn){
		/*
		 * If function deals with ExprNodes, create some
		 * temporary ExprNodes with constants.
		 */
		ExprTree *tree = NULL;
		GList *l = NULL;
		int i;

		if (argc){
			tree = g_new (ExprTree, argc);
732

733
			for (i = 0; i < argc; i++){
734
				tree [i].oper = OPER_CONSTANT;
735 736
				tree [i].ref_count = 1;
				tree [i].u.constant = values [i];
737

738 739 740
				l = g_list_append (l, &(tree[i]));
			}
		}
741

742 743 744 745 746 747 748
		retval = fd->expr_fn (sheet, l, 0, 0, error_string);

		if (tree){
			g_free (tree);
			g_list_free (l);
		}

749
	} else
750 751 752 753 754 755 756 757 758 759
		retval = fd->fn (fd, values, error_string);

	return retval;
}

/*
 * Use this to invoke a register function: the only drawback is that
 * you have to compute/expand all of the values to use this
 */
Value *
760
function_call_with_values (Sheet *sheet, const char *name, int argc, Value *values[], char **error_string)
761 762 763 764 765 766 767 768 769
{
	FunctionDefinition *fd;
	Value *retval;
	Symbol *sym;

	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
	g_return_val_if_fail (name != NULL, NULL);

Miguel de Icaza's avatar
Miguel de Icaza committed
770
	sym = symbol_lookup (global_symbol_table, name);
771
	if (sym == NULL){
772
		*error_string = _("Function does not exist");
773 774
		return NULL;
	}
775
	if (sym->type != SYMBOL_FUNCTION){
776
		*error_string = _("Calling non-function");
777 778
		return NULL;
	}
779

780 781 782 783 784 785 786 787 788 789
	fd = sym->data;

	symbol_ref (sym);
	retval = function_def_call_with_values (sheet, fd, argc, values, error_string);

	symbol_unref (sym);

	return retval;
}

790
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
791 792
	IS_EQUAL,
	IS_LESS,
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	IS_GREATER,
	TYPE_ERROR
} compare_t;

static compare_t
compare_int_int (int a, int b)
{
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
compare_float_float (float_t a, float_t b)
{
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
818

819 820 821 822
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
Arturo Espinosa's avatar
Arturo Espinosa committed
823 824
compare (Value *a, Value *b)
{
825 826
	if (a->type == VALUE_INTEGER){
		int f;
827

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
		switch (b->type){
		case VALUE_INTEGER:
			return compare_int_int (a->v.v_int, b->v.v_int);

		case VALUE_FLOAT:
			return compare_float_float (a->v.v_int, b->v.v_float);

		case VALUE_STRING:
			f = value_get_as_double (b);
			return compare_float_float (a->v.v_int, f);

		default:
			return TYPE_ERROR;
		}
	}
843

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
	if (a->type == VALUE_FLOAT){
		float_t f;

		switch (b->type){
		case VALUE_INTEGER:
			return compare_float_float (a->v.v_float, b->v.v_int);

		case VALUE_FLOAT:
			return compare_float_float (a->v.v_float, b->v.v_float);

		case VALUE_STRING:
			f = value_get_as_double (b);
			return compare_float_float (a->v.v_float, f);

		default:
			return TYPE_ERROR;
		}
	}

	if (a->type == VALUE_STRING && b->type == VALUE_STRING){
		int t;

		t = strcasecmp (a->v.str->str, b->v.str->str);
		if (t == 0)
			return IS_EQUAL;
		else if (t > 0)
			return IS_GREATER;
		else
			return IS_LESS;
	}

	return TYPE_ERROR;
Arturo Espinosa's avatar
Arturo Espinosa committed
876 877
}

Arturo Espinosa's avatar
Arturo Espinosa committed
878
Value *
879
eval_expr (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
880 881
{
	Value *a, *b, *res;
Arturo Espinosa's avatar
Arturo Espinosa committed
882 883

	g_return_val_if_fail (tree != NULL, NULL);
884
	g_return_val_if_fail (sheet != NULL, NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
885
	g_return_val_if_fail (error_string != NULL, NULL);
886
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
887

888
	switch (tree->oper){
889 890 891 892 893 894
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
Arturo Espinosa's avatar
Arturo Espinosa committed
895
		int comp;
896

Arturo Espinosa's avatar
Arturo Espinosa committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
		if (!(a && b)){
			if (a)
				value_release (a);
			if (b)
				value_release (b);
			return NULL;
		}
		res = g_new (Value, 1);
		res->type = VALUE_INTEGER;

		comp = compare (a, b);

913 914 915 916 917 918
		if (comp == TYPE_ERROR){
			value_release (a);
			value_release (b);
			*error_string = _("Type error");
			return NULL;
		}
919

Arturo Espinosa's avatar
Arturo Espinosa committed
920
		switch (tree->oper){
921
		case OPER_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
922 923 924
			res->v.v_int = comp == IS_EQUAL;
			break;

925
		case OPER_GT:
926
			res->v.v_int = comp == IS_GREATER;
Arturo Espinosa's avatar
Arturo Espinosa committed
927 928
			break;

929
		case OPER_LT:
Arturo Espinosa's avatar
Arturo Espinosa committed
930 931 932
			res->v.v_int = comp == IS_LESS;
			break;

933
		case OPER_LTE:
Arturo Espinosa's avatar
Arturo Espinosa committed
934 935 936
			res->v.v_int = (comp == IS_EQUAL || comp == IS_LESS);
			break;

937
		case OPER_GTE:
938
			res->v.v_int = (comp == IS_EQUAL || comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
939 940
			break;

941
		case OPER_NOT_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
942 943
			res->v.v_int = comp != IS_EQUAL;
			break;
944

Arturo Espinosa's avatar
Arturo Espinosa committed
945 946 947 948 949 950 951
		default:
			g_warning ("This should never be reached: comparission ops\n");
		}
		value_release (a);
		value_release (b);
		return res;
	}
952

953 954 955 956 957
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
958 959
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
960 961 962

		if (!a)
			return NULL;
963

964 965
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
966

967 968
		if (!b){
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
969 970
			return NULL;
		}
971

Arturo Espinosa's avatar
Arturo Espinosa committed
972
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
973 974
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
975 976 977
			*error_string = _("Type mismatch");
			return NULL;
		}
978

Arturo Espinosa's avatar
Arturo Espinosa committed
979 980
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
981 982
			int ia = a->v.v_int;
			int ib = b->v.v_int;
983

Arturo Espinosa's avatar
Arturo Espinosa committed
984
			res->type = VALUE_INTEGER;
985

986
			switch (tree->oper){
987
			case OPER_SUB:
988 989 990 991
			case OPER_ADD: {
				int sum;

				if (tree->oper == OPER_SUB){
Arturo Espinosa's avatar
0.20  
Arturo Espinosa committed
992
					ib = -ib;
993 994 995
				}

				sum = ia + ib;
996

997
				if ((ia > 0) && (ib > 0)){
998

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
					if (sum < ia){
						res->type = VALUE_FLOAT;
						res->v.v_float = ((double)ia) + ib;
					} else
						res->v.v_int = sum;
				} else if ((ia < 0) && (ib < 0)){
					if (sum > ia){
						res->type = VALUE_FLOAT;
						res->v.v_float = ((double)ia) + ib;
					} else
						res->v.v_int = sum;
				} else
					res->v.v_int = sum;
1012

Arturo Espinosa's avatar
Arturo Espinosa committed
1013
				break;
1014
			}
1015

1016
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1017
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1018
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1019

1020
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
1021
				if (b->v.v_int == 0){
1022 1023 1024
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
1025 1026 1027
					*error_string = _("Division by zero");
					return NULL;
				}
1028 1029
				res->type = VALUE_FLOAT;
				res->v.v_float =  a->v.v_int / (float_t)b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1030
				break;
1031 1032

			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
1033
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
1034 1035
				break;
			default:
1036
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1037 1038 1039
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
1040
			res->v.v_float = 0.0;
1041 1042
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
1043

1044
			switch (tree->oper){
1045
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
1046
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1047
				break;
1048

1049
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
1050
				res->v.v_float = a->v.v_float - b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1051
				break;
1052

1053
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1054
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1055
				break;
1056

1057
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
1058
				if (b->v.v_float == 0.0){
1059 1060 1061
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
1062 1063 1064
					*error_string = _("Division by zero");
					return NULL;
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
1065 1066

				res->v.v_float = a->v.v_float / b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1067
				break;
1068

1069
			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
1070
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
1071 1072
				break;
			default:
1073
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1074 1075
			}
		}
1076 1077
		value_release (a);
		value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
1078
		return res;
1079

1080
	case OPER_CONCAT: {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
		char *sa, *sb, *tmp;

		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		if (!a)
			return NULL;
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
		if (!b){
			value_release (a);
			return NULL;
		}

		res = g_new (Value, 1);
		res->type = VALUE_STRING;
		sa = value_string (a);
		sb = value_string (b);
1098

Jeff Garzik's avatar
Jeff Garzik committed
1099
		tmp = g_strconcat (sa, sb, NULL);
1100 1101 1102 1103 1104 1105 1106 1107 1108
		res->v.str = string_get (tmp);
		g_free (sa);
		g_free (sb);
		g_free (tmp);

		value_release (a);
		value_release (b);
		return res;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
1109

1110
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
1111
		return eval_funcall (sheet, tree, eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
1112

1113
	case OPER_VAR: {
1114
		Sheet *cell_sheet;
1115
		CellRef *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
1116 1117
		Cell *cell;
		int col, row;
1118

Arturo Espinosa's avatar
Arturo Espinosa committed
1119 1120 1121 1122 1123 1124
		if (sheet == NULL){
			/* Only the test program requests this */
			res = g_new (Value, 1);

			res->type = VALUE_FLOAT;
			res->v.v_float = 3.14;
1125

Arturo Espinosa's avatar
Arturo Espinosa committed
1126 1127 1128
			return res;
		}

1129
		ref = &tree->u.ref;
1130
		cell_get_abs_col_row (ref, eval_col, eval_row, &col, &row);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
1131

1132
		cell_sheet = ref->sheet ? ref->sheet : sheet;
1133

1134
		cell = sheet_cell_get (cell_sheet, col, row);
1135

Arturo Espinosa's avatar
Arturo Espinosa committed
1136
		if (cell){
1137
			if (cell->generation != sheet->workbook->generation){
1138
				cell->generation = sheet->workbook->generation;
1139 1140 1141 1142 1143

				if (cell->parsed_node)
					cell_eval (cell);
			}

1144 1145