expr.c 38.3 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;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
46 47
static void
do_expr_tree_ref (ExprTree *tree)
48
{
49
	g_return_if_fail (tree != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
50 51 52
	g_return_if_fail (tree->ref_count > 0);

	tree->ref_count++;
53
	switch (tree->oper){
54 55 56
	case OPER_VAR:
	case OPER_CONSTANT:
	case OPER_FUNCALL:
57 58
		break;

59
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
60 61
		do_expr_tree_ref (tree->u.binary.value_a);
		do_expr_tree_ref (tree->u.binary.value_b);
62 63
		break;

64
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
65
		do_expr_tree_ref (tree->u.value);
66 67 68 69
		break;
	}
}

70 71 72 73 74 75 76 77 78 79 80
ExprTree *
expr_tree_new ()
{
	ExprTree *ans = g_new (ExprTree, 1);
	if (!ans) return NULL;
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = NULL;
	return ans;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
81 82 83 84
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
85 86 87 88 89 90
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Arturo Espinosa's avatar
Arturo Espinosa committed
91 92 93 94 95 96 97 98
	do_expr_tree_ref (tree);
}

static void
do_expr_tree_unref (ExprTree *tree)
{
	tree->ref_count--;
	switch (tree->oper){
99
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
100
		break;
101

102
	case OPER_CONSTANT:
Arturo Espinosa's avatar
Arturo Espinosa committed
103 104 105
		if (tree->ref_count == 0)
			value_release (tree->u.constant);
		break;
106

107
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
108 109 110 111
		if (tree->ref_count == 0)
			symbol_unref (tree->u.function.symbol);
		break;

112
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
113 114 115 116
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

117
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
118 119 120
		do_expr_tree_unref (tree->u.value);
		break;
	}
121

Arturo Espinosa's avatar
Arturo Espinosa committed
122 123
	if (tree->ref_count == 0)
		g_free (tree);
124 125 126 127 128 129 130 131
}

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
132
	do_expr_tree_unref (tree);
133 134
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
135 136 137 138
/*
 * simplistic value rendering
 */
char *
139
value_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
140 141 142 143 144 145
{
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

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

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

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
154
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
155
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
156
	}
157

Arturo Espinosa's avatar
Arturo Espinosa committed
158
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
159 160
}

161 162 163
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
164
	g_return_if_fail (value != NULL);
165

166 167 168 169 170 171 172
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
173
		break;
174

175 176 177 178
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

179 180
	case VALUE_ARRAY:{
		guint i;
181

182 183
		for (i = 0; i < value->v.array.x; i++)
			g_free (value->v.array.vals [i]);
184

185
		g_free (value->v.array.vals);
Arturo Espinosa's avatar
Arturo Espinosa committed
186
	}
187

Arturo Espinosa's avatar
Arturo Espinosa committed
188 189
	case VALUE_CELLRANGE:
		break;
190

191
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
192
	g_free (value);
193
}
194

Arturo Espinosa's avatar
Arturo Espinosa committed
195 196 197 198
/*
 * Copies a Value.
 */
void
199
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
200 201 202 203 204
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
205

Arturo Espinosa's avatar
Arturo Espinosa committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219
	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
220
	case VALUE_ARRAY: {
221
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
222 223 224 225 226
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
227 228 229
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
230 231 232 233
/*
 * Makes a copy of a Value
 */
Value *
234
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
235 236 237 238 239 240
{
	Value *new_value;

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

Arturo Espinosa's avatar
Arturo Espinosa committed
242 243 244
	return new_value;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
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;
}

267
Value *
268
value_str (const char *str)
269 270 271 272 273 274 275 276 277
{
	Value *v = g_new (Value, 1);

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

	return v;
}

278 279 280 281 282 283 284 285 286 287
Value *
value_cellrange (CellRef *a, CellRef *b)
{
	Value *v = g_new (Value, 1);
	v->type = VALUE_CELLRANGE;
	memcpy (&v->v.cell_range.cell_a, a, sizeof(CellRef));
	memcpy (&v->v.cell_range.cell_b, b, sizeof(CellRef));
	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
288 289 290 291
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
292
Value *
293
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
294 295
{
	Value *newv;
296

Arturo Espinosa's avatar
Arturo Espinosa committed
297 298 299 300
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
302 303 304
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
305
	value_release (v);
306

Arturo Espinosa's avatar
Arturo Espinosa committed
307 308 309
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
310
int
311
value_get_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
{
	*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;
331

Arturo Espinosa's avatar
Arturo Espinosa committed
332 333 334 335 336
	g_warning ("Unhandled value in value_get_boolean");

	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
337
float_t
338
value_get_as_double (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
339 340 341 342 343 344 345 346 347 348 349 350 351
{
	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
352 353
	if (v->type == VALUE_ARRAY)
		return 0.0;
354

Arturo Espinosa's avatar
Arturo Espinosa committed
355
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
356 357
}

358
int
359
value_get_as_int (const Value *v)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
{
	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;
375

376 377 378
	return (int) v->v.v_float;
}

379 380 381
Value *
value_array_new (guint width, guint height)
{
382
	int x, y;
383 384 385 386 387 388

	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);
389 390 391 392 393 394 395

	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;
396 397 398 399 400 401 402 403
		}
	}
	return v;
}

void
value_array_resize (Value *v, guint width, guint height)
{
404
	int x, xcpy, ycpy;
405 406 407 408 409 410 411 412 413 414
	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
415
		xcpy = width;
416 417 418 419 420 421

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

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

void
value_array_copy_to (Value *v, const Value *src)
{
436
	int x;
437 438 439 440 441 442

	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);
443 444 445

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

447 448 449
		memcpy (v->v.array.vals   [x],
			src->v.array.vals [x],
			sizeof(Value)*v->v.array.y);
450 451 452 453 454 455 456 457 458
	}
}

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);
459

460 461 462 463 464 465 466 467 468 469 470 471 472
	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);
473

474 475 476 477 478 479 480 481 482 483 484 485 486
	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,
487 488 489
			      value_int (0));

	if (v->type == VALUE_ARRAY){
490
		g_return_val_if_fail (v->v.array.x < x &&
491 492 493
				      v->v.array.y < y,
				      value_int (0));
		return &v->v.array.vals [x][y];
494 495 496
	} else {
		CellRef *a, *b;
		Cell *cell;
497

498 499 500
		a = &v->v.cell_range.cell_a;
		b = &v->v.cell_range.cell_b;
		g_return_val_if_fail (!a->col_relative,
501
				      value_int (0));
502
		g_return_val_if_fail (!b->col_relative,
503
				      value_int (0));
504
		g_return_val_if_fail (!a->row_relative,
505
				      value_int (0));
506
		g_return_val_if_fail (!b->row_relative,
507
				      value_int (0));
508
		g_return_val_if_fail (a->col<=b->col,
509
				      value_int (0));
510
		g_return_val_if_fail (a->row<=b->row,
511
				      value_int (0));
512

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

515 516 517
		if (cell && cell->value)
			return cell->value;
		else
518
			return value_int (0);
519
	}
520
	return value_int (0);
521 522
}

523 524 525 526 527 528
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)
529
		cell_ref->col = eval_col + cell_ref->col;
530 531 532

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

Arturo Espinosa's avatar
Arturo Espinosa committed
534 535
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
536 537
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551
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
552 553
static Value *
eval_cell_value (Sheet *sheet, Value *value)
554
{
Arturo Espinosa's avatar
Arturo Espinosa committed
555
	Value *res;
556

Arturo Espinosa's avatar
Arturo Espinosa committed
557 558
	res = g_new (Value, 1);
	res->type = value->type;
559

Arturo Espinosa's avatar
Arturo Espinosa committed
560 561 562
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
563
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
564
		break;
565

Arturo Espinosa's avatar
Arturo Espinosa committed
566
	case VALUE_INTEGER:
567
		res->v.v_int = value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
568
		break;
569

Arturo Espinosa's avatar
Arturo Espinosa committed
570
	case VALUE_FLOAT:
571
		res->v.v_float = value->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
572
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
573 574 575 576 577 578

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

Arturo Espinosa's avatar
Arturo Espinosa committed
580 581 582 583 584 585 586
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

587 588 589 590
static void
free_values (Value **values, int top)
{
	int i;
591

592 593 594 595 596
	for (i = 0; i < top; i++)
		value_release (values [i]);
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
597 598 599 600 601 602 603
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;
604

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

Arturo Espinosa's avatar
Arturo Espinosa committed
607 608 609 610 611 612 613 614 615 616 617 618
	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
619 620 621
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
		char *arg_type = fd->args;
		char *argptr = fd->args;
622

623
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
624 625 626 627
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
628 629
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
630 631
				fn_argc_min++;
			fn_argc_max++;
632
		}
633

Arturo Espinosa's avatar
Arturo Espinosa committed
634
		if (argc > fn_argc_max || argc < fn_argc_min){
Arturo Espinosa's avatar
Arturo Espinosa committed
635 636 637 638
			*error_string = _("Invalid number of arguments");
			return NULL;
		}

639
		values = g_new (Value *, fn_argc_max);
640

641
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
642
			ExprTree *t = (ExprTree *) l->data;
643
			int type_mismatch = 0;
644

Arturo Espinosa's avatar
Arturo Espinosa committed
645 646
			if (*arg_type=='|')
				arg_type++;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

			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;
			}
663

Arturo Espinosa's avatar
Arturo Espinosa committed
664
			switch (*arg_type){
665
			case 'f':
666

667 668 669 670
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
671
			case 's':
672 673
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
674
				break;
675
			case 'r':
676 677 678 679 680
				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);
681
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
682
				break;
683 684 685 686 687 688 689 690
			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
691 692 693 694 695

				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);
				}
696 697
				break;
			}
698
			if (type_mismatch){
699 700 701
				free_values (values, arg);
				*error_string = _("Type mismatch");
				return NULL;
702
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
703 704
			values [arg] = v;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
705 706
		while (arg < fn_argc_max)
			values [arg++] = NULL;
707
		v = fd->fn (fd, values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
708 709

	free_list:
710
		for (i = 0; i < arg; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
711 712
			if (values [i] != NULL)
				value_release (values [i]);
713
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
714 715 716 717 718 719
		g_free (values);
		return v;
	}
	return v;
}

720 721 722 723 724
Value *
function_def_call_with_values (Sheet *sheet, FunctionDefinition *fd, int argc,
			       Value *values [], char **error_string)
{
	Value *retval;
725

726 727 728 729 730 731 732 733 734 735 736
	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);
737

738
			for (i = 0; i < argc; i++){
739
				tree [i].oper = OPER_CONSTANT;
740 741
				tree [i].ref_count = 1;
				tree [i].u.constant = values [i];
742

743 744 745
				l = g_list_append (l, &(tree[i]));
			}
		}
746

747 748 749 750 751 752 753
		retval = fd->expr_fn (sheet, l, 0, 0, error_string);

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

754
	} else
755 756 757 758 759 760 761 762 763 764
		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 *
765
function_call_with_values (Sheet *sheet, const char *name, int argc, Value *values[], char **error_string)
766 767 768 769 770 771 772 773 774
{
	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
775
	sym = symbol_lookup (global_symbol_table, name);
776
	if (sym == NULL){
777
		*error_string = _("Function does not exist");
778 779
		return NULL;
	}
780
	if (sym->type != SYMBOL_FUNCTION){
781
		*error_string = _("Calling non-function");
782 783
		return NULL;
	}
784

785 786 787 788 789 790 791 792 793 794
	fd = sym->data;

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

	symbol_unref (sym);

	return retval;
}

795
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
796 797
	IS_EQUAL,
	IS_LESS,
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
	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
823

824 825 826 827
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
Arturo Espinosa's avatar
Arturo Espinosa committed
828 829
compare (Value *a, Value *b)
{
830 831
	if (a->type == VALUE_INTEGER){
		int f;
832

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
		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;
		}
	}
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 876 877 878 879 880
	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
881 882
}

Arturo Espinosa's avatar
Arturo Espinosa committed
883
Value *
884
eval_expr (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
885 886
{
	Value *a, *b, *res;
Arturo Espinosa's avatar
Arturo Espinosa committed
887 888

	g_return_val_if_fail (tree != NULL, NULL);
889
	g_return_val_if_fail (sheet != NULL, NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
890
	g_return_val_if_fail (error_string != NULL, NULL);
891
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
892

893
	switch (tree->oper){
894 895 896 897 898 899
	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
900
		int comp;
901

Arturo Espinosa's avatar
Arturo Espinosa committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
		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);

918 919 920 921 922 923
		if (comp == TYPE_ERROR){
			value_release (a);
			value_release (b);
			*error_string = _("Type error");
			return NULL;
		}
924

Arturo Espinosa's avatar
Arturo Espinosa committed
925
		switch (tree->oper){
926
		case OPER_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
927 928 929
			res->v.v_int = comp == IS_EQUAL;
			break;

930
		case OPER_GT:
931
			res->v.v_int = comp == IS_GREATER;
Arturo Espinosa's avatar
Arturo Espinosa committed
932 933
			break;

934
		case OPER_LT:
Arturo Espinosa's avatar
Arturo Espinosa committed
935 936 937
			res->v.v_int = comp == IS_LESS;
			break;

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

942
		case OPER_GTE:
943
			res->v.v_int = (comp == IS_EQUAL || comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
944 945
			break;

946
		case OPER_NOT_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
947 948
			res->v.v_int = comp != IS_EQUAL;
			break;
949

Arturo Espinosa's avatar
Arturo Espinosa committed
950 951 952 953 954 955 956
		default:
			g_warning ("This should never be reached: comparission ops\n");
		}
		value_release (a);
		value_release (b);
		return res;
	}
957

958 959 960 961 962
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
963 964
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
965 966 967

		if (!a)
			return NULL;
968

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

972 973
		if (!b){
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
974 975
			return NULL;
		}
976

Arturo Espinosa's avatar
Arturo Espinosa committed
977
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
978 979
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
980 981 982
			*error_string = _("Type mismatch");
			return NULL;
		}
983

Arturo Espinosa's avatar
Arturo Espinosa committed
984 985
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
986 987
			int ia = a->v.v_int;
			int ib = b->v.v_int;
988

Arturo Espinosa's avatar
Arturo Espinosa committed
989
			res->type = VALUE_INTEGER;
990

991
			switch (tree->oper){
992
			case OPER_SUB:
993 994 995 996
			case OPER_ADD: {
				int sum;

				if (tree->oper == OPER_SUB){
Arturo Espinosa's avatar
0.20  
Arturo Espinosa committed
997
					ib = -ib;
998 999 1000
				}

				sum = ia + ib;
1001

1002
				if ((ia > 0) && (ib > 0)){
1003

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
					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;
1017

Arturo Espinosa's avatar
Arturo Espinosa committed
1018
				break;
1019
			}
1020

1021
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1022
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1023
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1024

1025
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
1026
				if (b->v.v_int == 0){
1027 1028 1029
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
1030 1031 1032
					*error_string = _("Division by zero");
					return NULL;
				}
1033 1034
				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
1035
				break;
1036 1037

			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
1038
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
1039 1040
				break;
			default:
1041
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1042 1043 1044
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
1045
			res->v.v_float = 0.0;
1046 1047
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
1048

1049
			switch (tree->oper){
1050
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
1051
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1052
				break;
1053

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

1058
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1059
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1060
				break;
1061

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

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

1074
			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
1075
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
1076 1077
				break;
			default:
1078
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1079 1080
			}
		}
1081 1082
		value_release (a);
		value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
1083
		return res;
1084

1085
	case OPER_CONCAT: {
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
		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);
1103

Jeff Garzik's avatar
Jeff Garzik committed
1104
		tmp = g_strconcat (sa, sb, NULL);
1105 1106 1107 1108 1109 1110 1111 1112 1113
		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
1114

1115
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
1116
		return eval_funcall (sheet, tree, eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
1117

1118
	case OPER_VAR: {
1119
		Sheet *cell_sheet;
1120
		CellRef *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
1121 1122
		Cell *cell;
		int col, row;
1123

Arturo Espinosa's avatar
Arturo Espinosa committed
1124 1125 1126 1127 1128 1129
		if (sheet == NULL){
			/* Only the test program requests this */
			res = g_new (Value, 1);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1131 1132 1133
			return res;
		}

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

1137
		cell_sheet = ref->sheet ? ref->sheet : sheet;
1138

1139
		cell = sheet_cell_get (cell_sheet, col, row);
1140

Arturo Espinosa's avatar
Arturo Espinosa committed
1141
		if (cell){
1142
			if (cell->generation != sheet->workbook->generation){
1143
				cell->generation = sheet->workbook->generation;
1144 1145 1146 1147 1148

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

1149 1150
			if (cell->value)
				return eval_cell_value (sheet, cell->value);
1151
			else {
1152 1153 1154 1155
				if (cell->text)
					*error_string = cell->text->str;
				else
					*error_string = _("Reference to newborn cell");