expr.c 38 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
#include "ranges.h"
19
#include "workbook.h"
20

21
EvalPosition *
22
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, int col, int row)
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;
Jody Goldberg's avatar
Jody Goldberg committed
29 30
	eval_pos->eval.col = col;
	eval_pos->eval.row = row;
31

32
	return eval_pos;
33
}
34

35 36 37 38 39 40 41 42 43 44 45 46 47
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;
}

48
EvalPosition *
49
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
50
{
51 52 53
	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);
54 55
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

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

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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);
}

79
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
80
expr_tree_new_constant (Value *v)
81
{
82 83 84
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
85 86 87 88 89 90 91
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = v;

Morten Welinder's avatar
Morten Welinder committed
92 93 94 95 96 97 98 99 100
	return ans;
}

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

	ans = g_new (ExprTree, 1);
101 102 103 104 105 106 107
	if (!ans)
		return NULL;

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

Morten Welinder's avatar
Morten Welinder committed
108 109 110 111 112 113 114 115 116 117
	return ans;
}


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

	ans = g_new (ExprTree, 1);
118 119 120 121 122 123 124 125
	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
126 127 128 129 130 131 132 133 134 135
	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);
136 137 138 139 140 141 142 143
	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
144 145
	return ans;
}
146
       
147 148 149 150 151 152 153 154 155 156 157
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;
}

158 159 160 161 162 163 164 165 166 167 168
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;
}

169
ExprTree *
170
expr_parse_string (const char *expr, const ParsePosition *pp,
Morten Welinder's avatar
Morten Welinder committed
171
		   char **desired_format, char **error_msg)
172 173 174 175
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

176
	switch (gnumeric_expr_parser (expr, pp, desired_format, &tree)) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	case PARSE_OK:
		*error_msg = NULL;
		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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
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;
}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

/*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
226 227 228 229 230 231 232

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

	ans = g_new (ExprTree, 1);
233 234 235 236 237 238 239
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

240 241 242
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
243 244 245 246
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
247 248 249 250 251 252 253 254 255
	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
256 257

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
258
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
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 *
284
expr_tree_array_formula_corner (ExprTree const *expr, EvalPosition const *pos)
285
{
286
	Cell * corner = expr->u.array.corner.cell;
287

288 289 290 291 292 293
	/* Attempt to set the corner if it is not already set */
	if (corner == NULL) {
		g_return_val_if_fail (pos != NULL, NULL);
		g_return_val_if_fail (pos->sheet != NULL, NULL);

		corner = sheet_cell_get (pos->sheet,
Jody Goldberg's avatar
Jody Goldberg committed
294 295
					 pos->eval.col - expr->u.array.x,
					 pos->eval.row - expr->u.array.y);
296 297 298 299
		((ExprTree *)expr)->u.array.corner.cell = corner;
	}

	g_return_val_if_fail (corner != NULL, NULL);
300 301 302 303 304
	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);
305 306
	g_return_val_if_fail (corner->parsed_node->u.array.x == 0, NULL);
	g_return_val_if_fail (corner->parsed_node->u.array.y == 0, NULL);
307 308 309 310

	return corner->parsed_node;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
311 312 313 314
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
315 316 317 318 319 320
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
321
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
322 323 324 325 326
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
327 328 329
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
330
	switch (tree->oper){
331
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
332
		break;
333

334
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
335
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
336
		break;
337

Morten Welinder's avatar
Morten Welinder committed
338 339 340 341 342 343 344
	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
345
		break;
Morten Welinder's avatar
Morten Welinder committed
346
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
347

Michael Meeks's avatar
Michael Meeks committed
348 349 350
	case OPER_NAME:
		break;

351
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
352 353 354 355
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

356
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
357 358
		do_expr_tree_unref (tree->u.value);
		break;
359
	case OPER_ARRAY:
360 361 362
		if (tree->u.array.x == 0 && tree->u.array.y == 0) {
			if (tree->u.array.corner.func.value)
				value_release (tree->u.array.corner.func.value);
363
			do_expr_tree_unref (tree->u.array.corner.func.expr);
364
		}
365
		break;
366 367 368
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
369
	}
370

Morten Welinder's avatar
Morten Welinder committed
371
	g_free (tree);
372 373
}

Morten Welinder's avatar
Morten Welinder committed
374 375 376 377 378 379
/*
 * 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.)
 */
380 381 382 383 384 385
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
386
	do_expr_tree_unref (tree);
387 388
}

389
static Value *
390
eval_funcall (EvalPosition const * const pos, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
391
{
392
	FunctionEvalInfo ei;
393
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
394
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
395
	GList *args;
396

397
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
398
	g_return_val_if_fail (tree != NULL, NULL);
399

400
	sym = tree->u.function.symbol;
401 402

	if (sym->type != SYMBOL_FUNCTION)
403
		return value_new_error (pos, _("Internal error"));
404

405
	fd = (FunctionDefinition *)sym->data;
406 407
	ei.func_def = fd;
	ei.pos = pos;
Michael Meeks's avatar
Michael Meeks committed
408
	args = tree->u.function.arg_list;
409

410
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
411 412
}

Jody Goldberg's avatar
Jody Goldberg committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
/**
 * expr_implicit_intersection :
 * @ei: EvalInfo containing valid fd!
 * @v: a VALUE_CELLRANGE
 * 
 * Attempt to find the intersection between the calling cell and
 * some element of the 1 one unit wide or tall range.
 *
 * Always release the value passed in.
 *
 * Return value: 
 *     If the intersection succeeded return a duplicate of the value
 *     at the intersection point.  This value needs to be freed.
 **/
Value *
expr_implicit_intersection (EvalPosition const * const pos,
			    Value * const v)
{
	/*
	 * Handle the implicit union of a single row or
	 * column with the eval position.
	 * NOTE : We do not need to know if this is expression is
	 * being evaluated as an array or not because we can differentiate
	 * based on the required type for the argument.
	 */
	Value *res = NULL;
	CellRef const * const a = & v->v.cell_range.cell_a;
	CellRef const * const b = & v->v.cell_range.cell_b;

	if (a->sheet == b->sheet) {
		int a_col, a_row, b_col, b_row;
		cell_get_abs_col_row (a, &pos->eval, &a_col, &a_row);
		cell_get_abs_col_row (b, &pos->eval, &b_col, &b_row);
		if (a_row == b_row) {
			int const c = pos->eval.col;
			if (a_col <= c && c <= b_col)
				res = value_duplicate (value_area_get_x_y (pos, v, c - a_col, 0));
		}

		if (a_col == b_col) {
			int const r = pos->eval.row;
			if (a_row <= r && r <= b_row)
				res = value_duplicate (value_area_get_x_y (pos, v, 0, r - a_row));
		}
	}
	value_release (v);
	return res;
}

462
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
463 464
	IS_EQUAL,
	IS_LESS,
465
	IS_GREATER,
466
	TYPE_MISMATCH
467 468 469
} compare_t;

static compare_t
470
compare_bool_bool (Value const * const va, Value const * const vb)
471
{
472 473 474 475 476 477 478 479 480 481 482 483 484
	gboolean err; /* Ignored */
	gboolean const a = value_get_as_bool (va, &err);
	gboolean const b = value_get_as_bool (vb, &err);
	if (a)
		return b ? IS_EQUAL : IS_GREATER;
	return b ? IS_LESS : IS_EQUAL;
}

static compare_t
compare_int_int (Value const * const va, Value const * const vb)
{
	int const a = value_get_as_int (va);
	int const b = value_get_as_int (vb);
485 486 487 488 489 490 491 492 493
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
494
compare_float_float (Value const * const va, Value const * const vb)
495
{
496 497
	float_t const a = value_get_as_float (va);
	float_t const b = value_get_as_float (vb);
498 499 500 501 502 503 504
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
505

506 507
/*
 * Compares two (Value *) and returns one of compare_t
Jody Goldberg's avatar
Jody Goldberg committed
508 509 510
 *
 * if pos is non null it will perform implict intersection for
 * cellranges.
511 512
 */
static compare_t
Jody Goldberg's avatar
Jody Goldberg committed
513
compare (Value const * const a, Value const * const b)
Arturo Espinosa's avatar
Arturo Espinosa committed
514
{
515 516
	ValueType ta, tb;

517 518 519 520
	/* Handle trivial and double NULL case */
	if (a == b)
		return IS_EQUAL;

521 522
	ta = value_is_empty_cell (a) ? VALUE_EMPTY : a->type;
	tb = value_is_empty_cell (b) ? VALUE_EMPTY : b->type;
523

524 525 526 527 528 529
	/* string > empty */
	if (ta == VALUE_STRING) {
		switch (tb) {
		/* Strings are > (empty, or number) */
		case VALUE_EMPTY : case VALUE_INTEGER : case VALUE_FLOAT :
			return IS_GREATER;
530

531 532 533
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_LESS;
534

535 536 537 538 539 540 541 542 543 544 545 546
		/* If both are strings compare as string */
		case VALUE_STRING :
		{
			int const 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;
		}
		default :
547
			return TYPE_MISMATCH;
548
		}
549 550 551 552 553
	} else if (tb == VALUE_STRING) {
		switch (ta) {
		/* (empty, or number) < String */
		case VALUE_EMPTY : case VALUE_INTEGER : case VALUE_FLOAT :
			return IS_LESS;
554

555 556 557
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_GREATER;
558

559
		default :
560
			return TYPE_MISMATCH;
561 562 563
		}
	}

564 565 566 567 568 569
	/* Booleans > all numbers (Why did excel do this ??) */
	if (ta == VALUE_BOOLEAN && (tb == VALUE_INTEGER || tb == VALUE_FLOAT))
		return IS_GREATER;
	if (tb == VALUE_BOOLEAN && (ta == VALUE_INTEGER || ta == VALUE_FLOAT))
		return IS_LESS;

570 571 572
	switch ((ta > tb) ? ta : tb) {
	case VALUE_EMPTY:	/* Empty Empty compare */
		return IS_EQUAL;
573

574 575
	case VALUE_BOOLEAN:
		return compare_bool_bool (a, b);
576

577 578 579 580 581 582 583 584
	case VALUE_INTEGER:
		return compare_int_int (a, b);

	case VALUE_FLOAT:
		return compare_float_float (a, b);
	default:
		return TYPE_MISMATCH;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
585 586
}

587 588 589 590 591
/*
 * Utility routine to ensure that all elements of a range are recalced as
 * necessary.
 */
static void
592
eval_range (EvalPosition const * const pos, Value *v)
593 594 595 596
{
	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;
597
	Sheet * sheet = a->sheet ? a->sheet : pos->sheet;
598 599
	Cell * cell;
	int r, c;
600
	int const gen = pos->sheet->workbook->generation;
601

602 603
	cell_get_abs_col_row (a, &pos->eval, &start_col, &start_row);
	cell_get_abs_col_row (b, &pos->eval, &end_col, &end_row);
604

Michael Meeks's avatar
Michael Meeks committed
605
	if (b->sheet && a->sheet != b->sheet) {
606 607 608 609 610 611 612 613 614
		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;
615 616
			if (cell->generation != gen)
				cell_eval (cell);
617 618 619
		}
}

620
static Value *
621
eval_expr_real (EvalPosition const * const pos, ExprTree const * const tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
622
{
623
	Value *res = NULL, *a = NULL, *b = NULL;
624
	
Arturo Espinosa's avatar
Arturo Espinosa committed
625
	g_return_val_if_fail (tree != NULL, NULL);
626
	g_return_val_if_fail (pos != NULL, NULL);
627

628
	switch (tree->oper){
629 630 631 632 633 634
	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
635
		int comp;
636

637
		a = eval_expr_real (pos, tree->u.binary.value_a);
Jody Goldberg's avatar
Jody Goldberg committed
638 639
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
640
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
641
				if (a == NULL)
642
					return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
643 644 645
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
646

647
		b = eval_expr_real (pos, tree->u.binary.value_b);
Jody Goldberg's avatar
Jody Goldberg committed
648 649 650
		if (b != NULL) {
			Value *res = NULL;
			if (b->type == VALUE_CELLRANGE) {
651
				b = expr_implicit_intersection (pos, b);
Jody Goldberg's avatar
Jody Goldberg committed
652
				if (b == NULL)
653
					res = value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
654 655 656 657 658 659 660 661
			} else if (b->type == VALUE_ERROR)
				res = b;

			if (res != NULL) {
				if (a != NULL)
					value_release (a);
				return res;
			}
Morten Welinder's avatar
Morten Welinder committed
662
		}
663

Arturo Espinosa's avatar
Arturo Espinosa committed
664 665
		comp = compare (a, b);

666 667 668 669
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
670

Michael Meeks's avatar
Michael Meeks committed
671
		if (comp == TYPE_MISMATCH) {
672 673 674 675 676 677 678 679 680
			/* 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);

681
			return value_new_error (pos, gnumeric_err_VALUE);
682
		}
683

Michael Meeks's avatar
Michael Meeks committed
684
		switch (tree->oper) {
685
		case OPER_EQUAL:
686
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
687 688
			break;

689
		case OPER_GT:
690
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
691 692
			break;

693
		case OPER_LT:
694
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
695 696
			break;

697 698
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
699 700
			break;

701 702
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
703 704
			break;

705 706
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
707
			break;
708

Arturo Espinosa's avatar
Arturo Espinosa committed
709
		default:
710
			g_assert_not_reached ();
711
			res = value_new_error (pos,
712
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
713
		}
Morten Welinder's avatar
Morten Welinder committed
714
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
715
	}
716

717 718 719 720 721
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
722 723 724 725 726 727 728 729 730
		/*
		 * Priority
		 * 1) Error from A
		 * 2) #!VALUE error if A is not a number
		 * 3) Error from B
		 * 4) #!VALUE error if B is not a number
		 * 5) result of operation, or error specific to the operation
		 */

Jody Goldberg's avatar
Jody Goldberg committed
731
	        /* Garantees that a != NULL */
732
		a = eval_expr (pos, tree->u.binary.value_a);
Jody Goldberg's avatar
Jody Goldberg committed
733 734 735

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
736
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
737
			if (a == NULL)
738
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
739 740
		}

741 742
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
743
			return a;
744

745 746 747
		/* 2) #!VALUE error if A is not a number */
		if (!VALUE_IS_NUMBER (a)) {
			value_release (a);
748
			return value_new_error (pos, gnumeric_err_VALUE);
749 750 751
		}

	        /* Garantees that b != NULL */
752
		b = eval_expr (pos, tree->u.binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
753

Jody Goldberg's avatar
Jody Goldberg committed
754 755
		/* Handle implicit intersection */
		if (b->type == VALUE_CELLRANGE) {
756
			b = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
757
			if (b == NULL)
758
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
759 760
		}

761 762
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
763
			value_release (a);
764
			return b;
Arturo Espinosa's avatar
Arturo Espinosa committed
765
		}
766

767 768
		/* 4) #!VALUE error if B is not a number */
		if (!VALUE_IS_NUMBER (b)) {
769 770
			value_release (a);
			value_release (b);
771
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
772
		}
773

774
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
775
			int ia = value_get_as_int (a);
776
			int ib = value_get_as_int (b);
777 778 779
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
780 781
			value_release (a);
			value_release (b);
782

783 784
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
785
			switch (tree->oper){
786 787 788 789 790 791 792 793
			case OPER_ADD:
				dres = (double)ia + (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);

794
			case OPER_SUB:
795 796 797 798 799 800
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
801

802
			case OPER_MULT:
803 804 805 806 807 808
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
809

810
			case OPER_DIV:
811
				if (ib == 0)
812
					return value_new_error (pos, gnumeric_err_DIV0);
813 814 815 816 817 818
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
819 820

			case OPER_EXP:
821
				if (ia == 0 && ib <= 0)
822
					return value_new_error (pos, gnumeric_err_NUM);
823 824 825 826 827 828 829
				dres = pow ((double)ia, (double)ib);
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);

Arturo Espinosa's avatar
Arturo Espinosa committed
830
			default:
831
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
832 833
			}
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
834 835
			float_t const va = value_get_as_float (a);
			float_t const vb = value_get_as_float (b);
836 837
			value_release (a);
			value_release (b);
838

839
			switch (tree->oper){
840
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
841
				return value_new_float (va + vb);
842

843
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
844
				return value_new_float (va - vb);
845

846
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
847
				return value_new_float (va * vb);
848

849
			case OPER_DIV:
850
				return (vb == 0.0)
851
				    ? value_new_error (pos,
852
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
853
				    : value_new_float (va / vb);
854

855
			case OPER_EXP:
856 857
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
858
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
859
				return value_new_float (pow (va, vb));
860

Arturo Espinosa's avatar
Arturo Espinosa committed
861
			default:
862
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
863 864
			}
		}
865
		return value_new_error (pos, _("Unknown operator"));
866

Jody Goldberg's avatar
Jody Goldberg committed
867
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
868 869
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
870
	        /* Garantees that a != NULL */
871
		a = eval_expr (pos, tree->u.value);
Jody Goldberg's avatar
Jody Goldberg committed
872 873 874

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
875
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
876
			if (a == NULL)
877
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
878 879
		}

880 881
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
882

Jody Goldberg's avatar
Jody Goldberg committed
883 884 885
		if (tree->oper == OPER_UNARY_PLUS)
			return a;

886 887
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
888
			return value_new_error (pos, gnumeric_err_VALUE);
889
		}
Jody Goldberg's avatar
Jody Goldberg committed
890
		if (tree->oper == OPER_UNARY_NEG) {
Jody Goldberg's avatar
Jody Goldberg committed
891 892 893 894 895 896 897 898
			if (a->type == VALUE_INTEGER)
				res = value_new_int (-a->v.v_int);
			else if (a->type == VALUE_FLOAT)
				res = value_new_float (-a->v.v_float);
			else
				res = value_new_bool (!a->v.v_float);
		} else
			res = value_new_float (value_get_as_float (a) * .01);
899
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
900
		return res;
901

902
	case OPER_CONCAT: {
903 904
		char *sa, *sb, *tmp;

905
		a = eval_expr_real (pos, tree->u.binary.value_a);
906
		if (a != NULL && a->type == VALUE_ERROR)
907
			return a;
908
		b = eval_expr_real (pos, tree->u.binary.value_b);
909 910 911
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
912
			return b;
913 914
		}

Michael Meeks's avatar
Michael Meeks committed
915 916
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
917
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
918 919
		res = value_new_string (tmp);

920 921 922 923
		g_free (sa);
		g_free (sb);
		g_free (tmp);

924
		if (a != NULL)
925
		value_release (a);
926
		if (b != NULL)
927
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
928
		return res;
929
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
930

931
	case OPER_FUNCALL:
932
		return eval_funcall (pos, tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
933

Michael Meeks's avatar
Michael Meeks committed
934
	case OPER_NAME:
935
		return eval_expr_name (pos, tree->u.name);
Michael Meeks's avatar
Michael Meeks committed
936

937
	case OPER_VAR: {
938
		Sheet *cell_sheet;
939
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
940 941
		Cell *cell;
		int col, row;
942

943
		if (pos->sheet == NULL) {
Arturo Espinosa's avatar
Arturo Espinosa committed
944
			/* Only the test program requests this */
Morten Welinder's avatar
Morten Welinder committed
945
			return value_new_float (3.14);
Arturo Espinosa's avatar
Arturo Espinosa committed
946 947
		}

948
		ref = &tree->u.ref;
949
		cell_get_abs_col_row (ref, &pos->eval, &col, &row);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
950

951
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
952
		cell = sheet_cell_get (cell_sheet, col, row);
953 954
		if (cell == NULL)
			return NULL;
955

956
		if (cell->generation != pos->sheet->workbook->generation)
957
			cell_eval (cell);
958

959
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
960
	}
961

962
	case OPER_CONSTANT:
963 964
		res = tree->u.constant;
		if (res->type == VALUE_CELLRANGE)
965
			eval_range (pos, res);
966
		return value_duplicate (res);
967

968 969 970
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
971 972
		int x = tree->u.array.x;
		int y = tree->u.array.y;
973
		if (x == 0 && y == 0){
974 975 976 977 978 979
			/* Release old value if necessary */
			a = tree->u.array.corner.func.value;
			if (a != NULL)
				value_release (a);

			/* Store real result (cast away const)*/
Jody Goldberg's avatar
Jody Goldberg committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
			/* FIXME : Call a wrapper routine that will iterate over the
			 * the array and evaluate the expression for all elements
			 *
			 * Figure out when to iterate and when to do array operations.
			 * ie
			 * A1:A3 = '=B1:B3^2'  Will iterate over all the elements and
			 *     re-evaluate.
			 * whereas
			 * A1:A3 = '=bob(B1:B3)'  Will call bob once if it returns an
			 *     array.
			 *
			 * This may be as simple as evaluating the corner.  If that is
			 * is an array return the result, else build an array and
			 * iterate over the elements, but that theory needs validation.
			 */
995
			a = eval_expr_real (pos, tree->u.array.corner.func.expr);
996
			*((Value **)&(tree->u.array.corner.func.value)) = a;
997
		} else {
998
			ExprTree const * const array =
999
			    expr_tree_array_formula_corner (tree, pos);
1000 1001 1002 1003
			if (array)
				a = array->u.array.corner.func.value;
			else
				a = NULL;
1004
		}
1005

1006 1007
		if (a != NULL &&
		    (a->type == VALUE_CELLRANGE || a->type == VALUE_ARRAY)) {
1008 1009
			int const num_x = value_area_get_width (pos, a);
			int const num_y = value_area_get_height (pos, a);
1010

1011
			/* Evaluate relative to the upper left corner */
1012
			EvalPosition tmp_ep = *pos;
Jody Goldberg's avatar
Jody Goldberg committed
1013 1014
			tmp_ep.eval.col -= x;
			tmp_ep.eval.row -= y;
1015 1016 1017 1018 1019 1020 1021

			/* If the src array is 1 element wide or tall we wrap */
			if (x >= 1 && num_x == 1)
				x = 0;
			if (y >= 1 && num_y == 1)
				y = 0;
			if (x >= num_x || y >= num_y)
1022
				return value_new_error (pos, gnumeric_err_NA);
1023 1024 1025

			a = (Value *)value_area_get_x_y (&tmp_ep, a, x, y);
		}
1026 1027 1028

		if (a == NULL)
			return NULL;
1029 1030
		return value_duplicate (a);
	}
1031
	}
1032

1033
	return value_new_error (pos, _("Unknown evaluation error"));
1034 1035 1036
}

Value *
1037
eval_expr (EvalPosition const * const pos, ExprTree const *tree)
1038
{
1039
	Value * res = eval_expr_real (pos, tree);
1040 1041 1042 1043 1044 1045 1046 1047
	if (res == NULL)
		return value_new_int (0);

	if (res->type == VALUE_EMPTY) {
		value_release (res);
		return value_new_int (0);
	}
	return res;
1048
}
Arturo Espinosa's avatar
Arturo Espinosa committed
1049

Morten Welinder's avatar
Morten Welinder committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
Value *
eval_expr_empty (EvalPosition const * const pos, ExprTree const * const tree)
{
	Value * res = eval_expr_real (pos, tree);

	if (res && res->type == VALUE_EMPTY) {
		value_release (res);
		res = NULL;
	}

	return res;
}


1064
int
Jody Goldberg's avatar
Jody Goldberg committed
1065
cell_ref_get_abs_col (CellRef const * const ref, EvalPosition const * const pos)
1066 1067 1068 1069 1070
{
	g_return_val_if_fail (ref != NULL, 0);
	g_return_val_if_fail (pos != NULL, 0);

	if (ref->col_relative)
Jody Goldberg's avatar
Jody Goldberg committed
1071
		return pos->eval.col + ref->col;
1072 1073 1074 1075 1076
	return ref->col;

}

int
Jody Goldberg's avatar
Jody Goldberg committed
1077
cell_ref_get_abs_row (CellRef const * const ref, EvalPosition const * const pos)
1078 1079 1080 1081 1082
{
	g_return_val_if_fail (ref != NULL, 0);
	g_return_val_if_fail (pos != NULL, 0);

	if (ref->row_relative)
Jody Goldberg's avatar
Jody Goldberg committed
1083
		return pos->eval.row + ref->row;
1084 1085 1086 1087
	return ref->row;
}


Arturo Espinosa's avatar
Arturo Espinosa committed
1088
void
Jody Goldberg's avatar
Jody Goldberg committed
1089 1090 1091
cell_get_abs_col_row (CellRef const * const cell_ref,
		      CellPos const * const pos,
		      int * const col, int * const row)
Arturo Espinosa's avatar
Arturo Espinosa committed
1092 1093 1094 1095
{
	g_return_if_fail (cell_ref != NULL);

	if (cell_ref->col_relative)