expr.c 41.6 KB
Newer Older
Miguel de Icaza's avatar
Miguel de Icaza committed
1
/*
Morten Welinder's avatar
Morten Welinder committed
2
 * expr.c: Expression evaluation in Gnumeric
Miguel de Icaza's avatar
Miguel de Icaza committed
3 4 5 6
 *
 * 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>
11
#include <locale.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "expr.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
14
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
15
#include "format.h"
16
#include "func.h"
17
#include "utils.h"
18

19
EvalPosition *
20
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, int col, int row)
21
{
22 23 24 25 26 27 28
	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);

	eval_pos->sheet = sheet;
	eval_pos->eval_col = col;
	eval_pos->eval_row = row;
29

30
	return eval_pos;
31
}
32

33 34 35 36 37 38 39 40 41 42 43 44 45
ParsePosition *
parse_pos_init (ParsePosition *pp, Workbook *wb, int col, int row)
{
	g_return_val_if_fail (wb != NULL, NULL);
	g_return_val_if_fail (pp != NULL, NULL);

	pp->wb  = wb;
	pp->col = col;
	pp->row = row;

	return pp;
}

46
EvalPosition *
47
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
48
{
49 50 51
	g_return_val_if_fail (eval_pos != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->sheet != NULL, NULL);
52 53
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

54 55 56 57 58
	return eval_pos_init (
		eval_pos,
		cell->sheet,
		cell->col->pos,
		cell->row->pos);
59
}
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
ParsePosition *
parse_pos_cell (ParsePosition *pp, Cell *cell)
{
	g_return_val_if_fail (pp != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);
	g_return_val_if_fail (cell->sheet->workbook != NULL, NULL);

	return parse_pos_init (
		pp,
		cell->sheet->workbook,
		cell->col->pos,
		cell->row->pos);
}

77
FunctionEvalInfo *
78
func_eval_info_init (FunctionEvalInfo *eval_info, Sheet *sheet, int col, int row)
79
{
80
	g_return_val_if_fail (eval_info != NULL, NULL);
81
       
82 83
	eval_pos_init (&eval_info->pos, sheet, col, row);
	eval_info->func_def = 0;
84

85
	return eval_info;
86 87
}

88
FunctionEvalInfo *
89
func_eval_info_cell (FunctionEvalInfo *eval_info, Cell *cell)
90
{
91 92
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
93

94 95 96
	return func_eval_info_init (
		eval_info, cell->sheet,
		cell->col->pos, cell->row->pos);
97 98
}

99 100
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *eval_info, const EvalPosition *eval_pos)
101
{
102 103
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
104

105 106 107 108 109
	return func_eval_info_init (
		eval_info,
		eval_pos->sheet,
		eval_pos->eval_col,
		eval_pos->eval_row);
110 111
}

112
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
113
expr_tree_new_constant (Value *v)
114
{
115 116 117
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
118 119 120 121 122 123 124
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = v;

Morten Welinder's avatar
Morten Welinder committed
125 126 127 128 129 130 131 132 133
	return ans;
}

ExprTree *
expr_tree_new_unary  (Operation op, ExprTree *e)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
134 135 136 137 138 139 140
	if (!ans)
		return NULL;

	ans->ref_count = 1;
	ans->oper = op;
	ans->u.value = e;

Morten Welinder's avatar
Morten Welinder committed
141 142 143 144 145 146 147 148 149 150
	return ans;
}


ExprTree *
expr_tree_new_binary (ExprTree *l, Operation op, ExprTree *r)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
151 152 153 154 155 156 157 158
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = op;
	ans->u.binary.value_a = l;
	ans->u.binary.value_b = r;

Morten Welinder's avatar
Morten Welinder committed
159 160 161 162 163 164 165 166 167 168
	return ans;
}

ExprTree *
expr_tree_new_funcall (Symbol *sym, GList *args)
{
	ExprTree *ans;
	g_return_val_if_fail (sym, NULL);

	ans = g_new (ExprTree, 1);
169 170 171 172 173 174 175 176
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_FUNCALL;
	ans->u.function.symbol = sym;;
	ans->u.function.arg_list = args;

Morten Welinder's avatar
Morten Welinder committed
177 178
	return ans;
}
179
       
180 181 182 183 184 185 186 187 188 189 190
int
expr_tree_get_const_int (ExprTree const * const expr)
{
	g_return_val_if_fail (expr != NULL, 0);
	g_return_val_if_fail (expr->oper == OPER_CONSTANT, 0);
	g_return_val_if_fail (expr->u.constant, 0);
	g_return_val_if_fail (expr->u.constant->type == VALUE_INTEGER, 0);

	return expr->u.constant->v.v_int;
}

191 192 193 194 195 196 197 198 199 200 201
char const *
expr_tree_get_const_str (ExprTree const *const expr)
{
	g_return_val_if_fail (expr != NULL, NULL);
	g_return_val_if_fail (expr->oper == OPER_CONSTANT, NULL);
	g_return_val_if_fail (expr->u.constant, NULL);
	g_return_val_if_fail (expr->u.constant->type == VALUE_STRING, NULL);

	return expr->u.constant->v.str->str;
}

202
ExprTree *
203
expr_parse_string (const char *expr, const ParsePosition *pp,
204 205 206 207 208
		   const char **desired_format, char **error_msg)
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

209
	switch (gnumeric_expr_parser (expr, pp, desired_format, &tree)) {
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	case PARSE_OK:
		*error_msg = NULL;
		tree->ref_count = 1;
		return tree;

	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;

	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
	default:
		g_assert_not_reached ();
		*error_msg = _("Impossible!");
		break;
	}
	return NULL;
}

Michael Meeks's avatar
Michael Meeks committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
ExprTree *
expr_tree_new_name (const ExprName *name)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_NAME;
	ans->u.name = name;

	return ans;
}
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

/*ExprTree *
expr_tree_new (void)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = NULL;
	return ans;
}*/
Morten Welinder's avatar
Morten Welinder committed
260 261 262 263 264 265 266

ExprTree *
expr_tree_new_var (const CellRef *cr)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
267 268 269 270 271 272 273
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

274 275 276
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
277 278 279 280
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
281 282 283 284 285 286 287 288 289
	GList *args = NULL;

	if (strcmp (txt, gnumeric_err_NA) == 0) {
		func = symbol_lookup (global_symbol_table, "NA");
	} else {
		func = symbol_lookup (global_symbol_table, "ERROR");
		args = g_list_prepend (NULL,
				       expr_tree_new_constant (value_new_string (txt)));
	}
Morten Welinder's avatar
Morten Welinder committed
290 291

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
292
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
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
ExprTree *
expr_tree_array_formula (int const x, int const y, int const rows, int const cols)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (ans == NULL)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_ARRAY;
	ans->u.array.x = x;
	ans->u.array.y = y;
	ans->u.array.rows = rows;
	ans->u.array.cols = cols;
	ans->u.array.corner.func.value = NULL;
	ans->u.array.corner.func.expr = NULL;
	ans->u.array.corner.cell = NULL;
	return ans;
}


static ExprTree *
318
expr_tree_array_formula_corner (ExprTree const *expr)
319 320 321 322 323 324 325 326 327 328 329 330 331
{
	Cell * const corner = expr->u.array.corner.cell;

	g_return_val_if_fail (corner, NULL);
	g_return_val_if_fail (corner->parsed_node != NULL, NULL);

	/* Sanity check incase the corner gets removed for some reason */
	g_return_val_if_fail (corner->parsed_node != (void *)0xdeadbeef, NULL);
	g_return_val_if_fail (corner->parsed_node->oper == OPER_ARRAY, NULL);

	return corner->parsed_node;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
332 333 334 335
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
336 337 338 339 340 341
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
342
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
343 344 345 346 347
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
348 349 350
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
351
	switch (tree->oper){
352
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
353
		break;
354

355
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
356
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
357
		break;
358

Morten Welinder's avatar
Morten Welinder committed
359 360 361 362 363 364 365
	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
366
		break;
Morten Welinder's avatar
Morten Welinder committed
367
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
368

Michael Meeks's avatar
Michael Meeks committed
369 370 371
	case OPER_NAME:
		break;

372
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
373 374 375 376
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

377
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
378 379
		do_expr_tree_unref (tree->u.value);
		break;
380 381 382 383
	case OPER_ARRAY:
		if (tree->u.array.x == 0 && tree->u.array.y == 0)
			do_expr_tree_unref (tree->u.array.corner.func.expr);
		break;
384 385 386
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
387
	}
388

Morten Welinder's avatar
Morten Welinder committed
389
	g_free (tree);
390 391
}

Morten Welinder's avatar
Morten Welinder committed
392 393 394 395 396 397
/*
 * 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.)
 */
398 399 400 401 402 403
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
404
	do_expr_tree_unref (tree);
405 406
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static void
encode_cellref (GString *dest, const CellRef *ref, gboolean use_relative_syntax)
{
	if (ref->sheet){
		g_string_append_c (dest, '\'');
		g_string_append (dest, ref->sheet->name);
		g_string_append (dest, "'!");
	}
	if (use_relative_syntax && !ref->col_relative)
		g_string_append_c (dest, '$');
	g_string_append (dest, col_name (ref->col));

	if (use_relative_syntax && !ref->row_relative)
		g_string_append_c (dest, '$');
	g_string_sprintfa (dest, "%d", ref->row+1);
}

424

425 426 427 428 429 430
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)
431
		cell_ref->col = eval_col + cell_ref->col;
432 433 434

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

Arturo Espinosa's avatar
Arturo Espinosa committed
436 437
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
438 439
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453
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;
	}
}

454 455 456 457
static void
free_values (Value **values, int top)
{
	int i;
458

459
	for (i = 0; i < top; i++)
460 461
		if (values[i])
			value_release (values [i]);
462 463 464
	g_free (values);
}

465
static Value *
466
eval_funcall (FunctionEvalInfo *s, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
467
{
468
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
469 470
	FunctionDefinition *fd;
	GList *l;
471
	int argc, arg;
472
	Value *v = NULL;
473 474 475 476 477 478
	Sheet *sheet;
	int eval_col;
	int eval_row;

	g_return_val_if_fail (s, NULL);
	g_return_val_if_fail (tree, NULL);
479

480
	sym = tree->u.function.symbol;
481 482 483 484 485

	sheet    = s->pos.sheet;
	eval_col = s->pos.eval_col;
	eval_row = s->pos.eval_row;

Arturo Espinosa's avatar
Arturo Espinosa committed
486 487 488
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

489
	if (sym->type != SYMBOL_FUNCTION)
490
		return value_new_error (&s->pos, _("Internal error"));
491

492
	fd = (FunctionDefinition *)sym->data;
Morten Welinder's avatar
Morten Welinder committed
493
	s->func_def = fd;
494

495 496
	if (fd->fn_type == FUNCTION_NODES) {
		/* Functions that deal with ExprNodes */		
497
		v = fd->fn.fn_nodes (s, l);
498
	} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
499 500
		/* Functions that take pre-computed Values */
		Value **values;
Arturo Espinosa's avatar
Arturo Espinosa committed
501
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
502 503
		const char *arg_type = fd->args;
		const char *argptr = fd->args;
504

505
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
506 507 508 509
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
510 511
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
512 513
				fn_argc_min++;
			fn_argc_max++;
514
		}
515

516 517
		if (argc > fn_argc_max || argc < fn_argc_min)
			return value_new_error (&s->pos, _("Invalid number of arguments"));
Arturo Espinosa's avatar
Arturo Espinosa committed
518

519
		values = g_new (Value *, fn_argc_max);
520

521
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
522
			ExprTree *t = (ExprTree *) l->data;
523
			gboolean type_mismatch = FALSE;
524
			Value *v;
525

Arturo Espinosa's avatar
Arturo Espinosa committed
526 527
			if (*arg_type=='|')
				arg_type++;
528 529 530 531

			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 */
532
				if ((v = eval_expr_real (s, t)) == NULL)
533 534 535
					goto free_list;
			} else {
				g_assert (t->oper == OPER_VAR);
536
				v = value_new_cellrange (&t->u.ref,
Michael Meeks's avatar
Michael Meeks committed
537
							 &t->u.ref);
538
			}
539

Arturo Espinosa's avatar
Arturo Espinosa committed
540
			switch (*arg_type){
541
			case 'f':
Morten Welinder's avatar
Morten Welinder committed
542
			case 'b':
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
				/*
				 * Handle the implicit union of a single row or
				 * column with the eval position
				 */
				if (v->type == VALUE_CELLRANGE) {
					CellRef a = v->v.cell_range.cell_a;
					CellRef b = v->v.cell_range.cell_b;
					cell_ref_make_absolute (&a, eval_col,
								eval_row);
					cell_ref_make_absolute (&b, eval_col,
								eval_row);

					if (a.sheet !=  b.sheet)
						type_mismatch = TRUE;
					else if (a.row == b.row) {
						int const c = s->pos.eval_col;
						if (a.col <= c && c <= b.col)
							v = value_duplicate(value_area_get_x_y (&s->pos, v, c - a.col, 0));
						else
							type_mismatch = TRUE;
					} else if (a.col == b.col) {
						int const r = s->pos.eval_row;
						if (a.row <= r && r <= b.row)
							v = value_duplicate(value_area_get_x_y (&s->pos, v, 0, r - a.row));
						else
							type_mismatch = TRUE;
					} else
						type_mismatch = TRUE;
				} else if (v->type != VALUE_INTEGER &&
					 v->type != VALUE_FLOAT &&
					 v->type != VALUE_BOOLEAN)
					type_mismatch = TRUE;
575
				break;
576
			case 's':
577
				if (v->type != VALUE_STRING)
578
					type_mismatch = TRUE;
Arturo Espinosa's avatar
Arturo Espinosa committed
579
				break;
580
			case 'r':
581
				if (v->type != VALUE_CELLRANGE)
582
					type_mismatch = TRUE;
583 584 585
				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);
586
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
587
				break;
588 589
			case 'a':
				if (v->type != VALUE_ARRAY)
590
					type_mismatch = TRUE;
591 592 593 594
				break;
			case 'A':
				if (v->type != VALUE_ARRAY &&
				    v->type != VALUE_CELLRANGE)
595
					type_mismatch = TRUE;
Michael Meeks's avatar
Michael Meeks committed
596 597 598 599 600

				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);
				}
601 602
				break;
			}
603
			values [arg] = v;
604
			if (type_mismatch){
605
				free_values (values, arg + 1);
606 607
				return value_new_error (&s->pos,
							gnumeric_err_VALUE);
608
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
609
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
610 611
		while (arg < fn_argc_max)
			values [arg++] = NULL;
612
		v = fd->fn.fn_args (s, values);
Arturo Espinosa's avatar
Arturo Espinosa committed
613 614

	free_list:
615
		free_values (values, arg);
Arturo Espinosa's avatar
Arturo Espinosa committed
616 617 618 619
	}
	return v;
}

620
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
621 622
	IS_EQUAL,
	IS_LESS,
623
	IS_GREATER,
624
	TYPE_MISMATCH
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
} 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
648

649 650 651 652 653 654
static gboolean
is_null_string (Value const *v)
{
	return v != NULL && v->type == VALUE_STRING && v->v.str->str[0] == '\0';
}

655 656 657 658
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
659
compare (const Value *a, const Value *b)
Arturo Espinosa's avatar
Arturo Espinosa committed
660
{
661 662 663 664 665 666 667 668 669
	/* Handle trivial and double NULL case */
	if (a == b)
		return IS_EQUAL;

	if (a == NULL)
		return is_null_string (b) ? IS_EQUAL : TYPE_MISMATCH;
	if (b == NULL)
		return is_null_string (a) ? IS_EQUAL : TYPE_MISMATCH;

670 671 672 673 674 675 676 677 678 679
	if (a->type == VALUE_BOOLEAN){
		gboolean err, b_val = value_get_as_bool (b, &err);

		if (err)
			return TYPE_MISMATCH;

		if (a->v.v_bool)
			return (b_val) ? IS_EQUAL : IS_GREATER;
		return (b_val) ? IS_LESS : IS_EQUAL;
	}
680 681
	if (a->type == VALUE_INTEGER){
		int f;
682

683 684 685 686 687 688 689 690
		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:
691
			f = value_get_as_float (b);
692 693 694
			return compare_float_float (a->v.v_int, f);

		default:
695
			return TYPE_MISMATCH;
696 697
		}
	}
698

699 700 701 702 703 704 705 706 707 708 709
	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:
710
			f = value_get_as_float (b);
711 712 713
			return compare_float_float (a->v.v_float, f);

		default:
714
			return TYPE_MISMATCH;
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
		}
	}

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

730
	return TYPE_MISMATCH;
Arturo Espinosa's avatar
Arturo Espinosa committed
731 732
}

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
/*
 * Utility routine to ensure that all elements of a range are recalced as
 * necessary.
 */
static void
eval_range (FunctionEvalInfo *s, Value *v)
{
	int start_col, start_row, end_col, end_row;
	CellRef * a = &v->v.cell_range.cell_a;
	CellRef * b = &v->v.cell_range.cell_b;
	Sheet * sheet = a->sheet ? a->sheet : s->pos.sheet;
	Cell * cell;
	int r, c;
	int const gen = s->pos.sheet->workbook->generation;

	cell_get_abs_col_row (a,
			      s->pos.eval_col, s->pos.eval_row,
			      &start_col, &start_row);
	cell_get_abs_col_row (b,
			      s->pos.eval_col, s->pos.eval_row,
			      &end_col, &end_row);

	if (a->sheet != b->sheet) {
		g_warning ("3D references not-fully supported.\n"
			   "Recalc may be incorrect");
		return;
	}

	for (r = start_row; r <= end_row; ++r)
		for (c = start_col; c <= end_col; ++c) {
			if ((cell = sheet_cell_get (sheet, c, r)) == NULL)
				continue;
			if (cell->generation != gen) {
				cell->generation = gen;
				if (cell->parsed_node &&
				    (cell->flags & CELL_QUEUED_FOR_RECALC))
					cell_eval (cell);
			}

		}
}

775 776
Value *
eval_expr_real (FunctionEvalInfo *s, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
777
{
778
	Value *res = NULL, *a = NULL, *b = NULL;
779
	
Arturo Espinosa's avatar
Arturo Espinosa committed
780
	g_return_val_if_fail (tree != NULL, NULL);
781
	g_return_val_if_fail (s != NULL, NULL);
782

783 784 785 786 787
	/* FIXME FIXME : We need to rework support for
	 * ranges and builtin operators to support the
	 * implicit intersection rule AND figure out
	 * how to turn it off when evaluating as an array
	 */
788
	switch (tree->oper){
789 790 791 792 793 794
	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
795
		int comp;
796

797 798
		a = eval_expr_real (s, tree->u.binary.value_a);
		b = eval_expr_real (s, tree->u.binary.value_b);
799

Arturo Espinosa's avatar
Arturo Espinosa committed
800 801
		comp = compare (a, b);

802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);

		if (comp == TYPE_MISMATCH){
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
			if (tree->oper == OPER_EQUAL)
				return value_new_bool (FALSE);
			if (tree->oper == OPER_NOT_EQUAL)
				return value_new_bool (TRUE);

817
			return value_new_error (&s->pos, _("Type Mismatch"));
818
		}
819

Arturo Espinosa's avatar
Arturo Espinosa committed
820
		switch (tree->oper){
821
		case OPER_EQUAL:
822
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
823 824
			break;

825
		case OPER_GT:
826
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
827 828
			break;

829
		case OPER_LT:
830
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
831 832
			break;

833 834
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
835 836
			break;

837 838
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
839 840
			break;

841 842
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
843
			break;
844

Arturo Espinosa's avatar
Arturo Espinosa committed
845
		default:
846
			g_assert_not_reached ();
847 848
			res = value_new_error (&s->pos,
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
849
		}
Morten Welinder's avatar
Morten Welinder committed
850
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
851
	}
852

853 854 855 856 857
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
858
		a = eval_expr_real (s, tree->u.binary.value_a);
859

860 861
		if (VALUE_IS_PROBLEM(a))
			return a;
862

863
		b = eval_expr_real (s, tree->u.binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
864

865
		if (VALUE_IS_PROBLEM(b)){
866
			value_release (a);
867
			return b;
Arturo Espinosa's avatar
Arturo Espinosa committed
868
		}
869

Arturo Espinosa's avatar
Arturo Espinosa committed
870
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
871 872
			value_release (a);
			value_release (b);
873
			return value_new_error (&s->pos, _("Type mismatch"));
Arturo Espinosa's avatar
Arturo Espinosa committed
874
		}
875

Arturo Espinosa's avatar
Arturo Espinosa committed
876
		res = g_new (Value, 1);
877 878 879
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
			int ia = value_get_as_int (a);
			int ib = value_get_as_int (b);
880

Arturo Espinosa's avatar
Arturo Espinosa committed
881
			res->type = VALUE_INTEGER;
882

883
			switch (tree->oper){
884
			case OPER_SUB:
885 886 887 888
			case OPER_ADD: {
				int sum;

				if (tree->oper == OPER_SUB){
Arturo Espinosa's avatar
0.20  
Arturo Espinosa committed
889
					ib = -ib;
890 891 892
				}

				sum = ia + ib;
893

894
				if ((ia > 0) && (ib > 0)){
895

896 897 898 899 900 901 902 903 904 905 906 907 908
					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;
909

Arturo Espinosa's avatar
Arturo Espinosa committed
910
				break;
911
			}
912

913
			case OPER_MULT:
914
				res->v.v_int = ia * ib;
Arturo Espinosa's avatar
Arturo Espinosa committed
915
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
916

917
			case OPER_DIV:
918
				if (ib == 0){
919 920 921
					value_release (a);
					value_release (b);
					value_release (res);
922
				       
923
					return value_new_error (&s->pos, gnumeric_err_DIV0);
Arturo Espinosa's avatar
Arturo Espinosa committed
924
				}
925
				res->type = VALUE_FLOAT;
926
				res->v.v_float =  ia / (float_t)ib;
Arturo Espinosa's avatar
Arturo Espinosa committed
927
				break;
928 929

			case OPER_EXP:
930
				res->v.v_int = pow (ia, ib);
Arturo Espinosa's avatar
Arturo Espinosa committed
931 932
				break;
			default:
933
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
934 935 936
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
937
			res->v.v_float = 0.0;
938 939
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
940

941
			switch (tree->oper){
942
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
943
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
944
				break;
945

946
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
947
				res->v.v_float = a->v.v_float - b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
948
				break;
949

950
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
951
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
952
				break;
953

954
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
955
				if (b->v.v_float == 0.0){
956 957 958
					value_release (a);
					value_release (b);
					value_release (res);
959

960
					return value_new_error (&s->pos, gnumeric_err_DIV0);
Arturo Espinosa's avatar
Arturo Espinosa committed
961
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
962 963

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

966
			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
967
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
968 969
				break;
			default:
970
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
971 972
			}
		}
973 974
		value_release (a);
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
975
		return res;
976

977
	case OPER_CONCAT: {
978 979
		char *sa, *sb, *tmp;

980
		a = eval_expr_real (s, tree->u.binary.value_a);
981 982
		if (VALUE_IS_PROBLEM(a))
			return a;
983
		b = eval_expr_real (s, tree->u.binary.value_b);
984
		if (VALUE_IS_PROBLEM(b)) {
985 986 987 988
			value_release (a);
			return NULL;
		}

Michael Meeks's avatar
Michael Meeks committed
989 990
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
991
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
992 993
		res = value_new_string (tmp);

994 995 996 997 998 999
		g_free (sa);
		g_free (sb);
		g_free (tmp);

		value_release (a);
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
1000
		return res;
1001
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
1002

1003
	case OPER_FUNCALL:
1004
		return eval_funcall (s, tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
1005

Michael Meeks's avatar
Michael Meeks committed
1006 1007 1008
	case OPER_NAME:
		return eval_expr_name (s, tree->u.name);

1009
	case OPER_VAR: {
1010
		Sheet *cell_sheet;
1011
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
1012 1013
		Cell *cell;
		int col, row;
1014

1015
		if (s->pos.sheet == NULL) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1016
			/* Only the test program requests this */
Morten Welinder's avatar
Morten Welinder committed
1017
			return value_new_float (3.14);
Arturo Espinosa's avatar
Arturo Espinosa committed
1018 1019
		}

1020
		ref = &tree->u.ref;
1021
		cell_get_abs_col_row (ref, s->pos.eval_col, s->pos.eval_row, &col,