expr.c 41.5 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>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "expr.h"
13
#include "expr-name.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"
Jody Goldberg's avatar
Jody Goldberg committed
17
#include "cell.h"
18
#include "parse-util.h"
19
#include "ranges.h"
20
#include "number-match.h"
21
#include "workbook.h"
Jeffrey Stedfast's avatar
Jeffrey Stedfast committed
22
#include "gutils.h"
23

24 25
/***************************************************************************/

26
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
27
expr_tree_new_constant (Value *v)
28
{
29
	ExprConstant *ans;
30

31
	ans = g_new (ExprConstant, 1);
32 33
	if (!ans)
		return NULL;
34

35
	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
36
	*((Operation *)&(ans->oper)) = OPER_CONSTANT;
37
	ans->value = v;
38

39
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
40 41
}

42 43 44
ExprTree *
expr_tree_new_error (char const *txt)
{
Jody Goldberg's avatar
Jody Goldberg committed
45
	FunctionDefinition *func;
46 47
	GList *args = NULL;

Jody Goldberg's avatar
Jody Goldberg committed
48 49
	if (strcmp (txt, gnumeric_err_NA) != 0) {
		func = func_lookup_by_name ("ERROR", NULL);
50 51
		args = g_list_prepend (NULL,
				       expr_tree_new_constant (value_new_string (txt)));
Jody Goldberg's avatar
Jody Goldberg committed
52 53
	} else
		func = func_lookup_by_name ("NA", NULL);
54

Jody Goldberg's avatar
Jody Goldberg committed
55
	func_ref (func);
56 57 58 59
	return expr_tree_new_funcall (func, args);
}

ExprTree *
Jody Goldberg's avatar
Jody Goldberg committed
60
expr_tree_new_funcall (FunctionDefinition *func, GList *args)
61 62
{
	ExprFunction *ans;
Jody Goldberg's avatar
Jody Goldberg committed
63
	g_return_val_if_fail (func, NULL);
64 65 66 67

	ans = g_new (ExprFunction, 1);
	if (!ans)
		return NULL;
68

69 70
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_FUNCALL;
Jody Goldberg's avatar
Jody Goldberg committed
71
	func_ref (func);
Jody Goldberg's avatar
Jody Goldberg committed
72
	ans->func = func;;
73 74 75 76
	ans->arg_list = args;

	return (ExprTree *)ans;
}
77

Morten Welinder's avatar
Morten Welinder committed
78 79 80
ExprTree *
expr_tree_new_unary  (Operation op, ExprTree *e)
{
81
	ExprUnary *ans;
Morten Welinder's avatar
Morten Welinder committed
82

83
	ans = g_new (ExprUnary, 1);
84 85 86 87
	if (!ans)
		return NULL;

	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
88
	*((Operation *)&(ans->oper)) = op;
89
	ans->value = e;
90

91
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
92 93 94 95 96 97
}


ExprTree *
expr_tree_new_binary (ExprTree *l, Operation op, ExprTree *r)
{
98
	ExprBinary *ans;
Morten Welinder's avatar
Morten Welinder committed
99

100
	ans = g_new (ExprBinary, 1);
101 102
	if (!ans)
		return NULL;
103

104
	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
105
	*((Operation *)&(ans->oper)) = op;
106 107
	ans->value_a = l;
	ans->value_b = r;
108

109
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
110 111 112
}

ExprTree *
113
expr_tree_new_name (NamedExpression const *name)
Morten Welinder's avatar
Morten Welinder committed
114
{
115
	ExprName *ans;
Morten Welinder's avatar
Morten Welinder committed
116

117
	ans = g_new (ExprName, 1);
118 119
	if (!ans)
		return NULL;
120

121
	ans->ref_count = 1;
122 123
	*((Operation *)&(ans->oper)) = OPER_NAME;
	ans->name = name;
124

125
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
126
}
127 128 129 130 131 132 133 134 135

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

	ans = g_new (ExprVar, 1);
	if (!ans)
		return NULL;
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_VAR;
	ans->ref = *cr;

	return (ExprTree *)ans;
}

ExprTree *
expr_tree_new_array (int x, int y, int rows, int cols)
{
	ExprArray *ans;

	ans = g_new (ExprArray, 1);
	if (ans == NULL)
		return NULL;
152

153 154 155 156 157 158
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_ARRAY;
	ans->x = x;
	ans->y = y;
	ans->rows = rows;
	ans->cols = cols;
159 160
	ans->corner.value = NULL;
	ans->corner.expr = NULL;
161 162 163
	return (ExprTree *)ans;
}

164
int
165
expr_tree_get_const_int (ExprTree const *expr)
166 167
{
	g_return_val_if_fail (expr != NULL, 0);
168 169 170
	g_return_val_if_fail (expr->any.oper == OPER_CONSTANT, 0);
	g_return_val_if_fail (expr->constant.value, 0);
	g_return_val_if_fail (expr->constant.value->type == VALUE_INTEGER, 0);
171

172
	return expr->constant.value->v_int.val;
173 174
}

175
char const *
176
expr_tree_get_const_str (ExprTree const *expr)
177 178
{
	g_return_val_if_fail (expr != NULL, NULL);
179 180 181
	g_return_val_if_fail (expr->any.oper == OPER_CONSTANT, NULL);
	g_return_val_if_fail (expr->constant.value, NULL);
	g_return_val_if_fail (expr->constant.value->type == VALUE_STRING, NULL);
182

183
	return expr->constant.value->v_str.val->str;
184 185
}

186 187 188 189 190 191 192 193
/**
 * expr_parse_string:
 *
 * Parse a string. if @error is non-null it will be assumed that the
 * caller has passed a pointer to a ParseError struct AND that it will
 * take responsibility for freeing that struct and it's contents.
 * with parse_error_free.
 **/
194
ExprTree *
195
expr_parse_string (char const *expr_text, ParsePos const *pp,
196
		   StyleFormat **desired_format, ParseError *error)
197
{
198
	ExprTree   *tree;
Morten Welinder's avatar
Morten Welinder committed
199 200
	ParseError errrec;
	ParseError *perr = error ? error : &errrec;
201

202
	g_return_val_if_fail (expr_text != NULL, NULL);
203

204
	tree = gnumeric_expr_parser (expr_text, pp, TRUE, FALSE, desired_format,
205 206 207 208 209
				     parse_error_init (perr));

	if (error == NULL)
		parse_error_free (perr);

210
	return tree;
211 212
}

213 214

static ExprTree *
215 216
expr_tree_array_formula_corner (ExprTree const *expr,
				Sheet const *sheet, CellPos const *pos)
217
{
218
	Cell *corner;
219

220
	g_return_val_if_fail (pos != NULL, NULL);
221

222 223 224
	corner = sheet_cell_get (sheet,
				 pos->col - expr->array.x,
				 pos->row - expr->array.y);
225

226
	/* Sanity check incase the corner gets removed for some reason */
227
	g_return_val_if_fail (corner != NULL, NULL);
228
	g_return_val_if_fail (cell_has_expr (corner), NULL);
229 230 231 232
	g_return_val_if_fail (corner->base.expression != (void *)0xdeadbeef, NULL);
	g_return_val_if_fail (corner->base.expression->any.oper == OPER_ARRAY, NULL);
	g_return_val_if_fail (corner->base.expression->array.x == 0, NULL);
	g_return_val_if_fail (corner->base.expression->array.y == 0, NULL);
233

234
	return corner->base.expression;
235 236
}

Arturo Espinosa's avatar
Arturo Espinosa committed
237 238 239 240
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
241 242 243 244
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
245
	g_return_if_fail (tree->any.ref_count > 0);
246

247
	tree->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
248 249 250 251 252
}

static void
do_expr_tree_unref (ExprTree *tree)
{
253
	if (--tree->any.ref_count > 0)
Morten Welinder's avatar
Morten Welinder committed
254 255
		return;

256
	switch (tree->any.oper){
257
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
258
		break;
259

260
	case OPER_CONSTANT:
261
		value_release (tree->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
262
		break;
263

Morten Welinder's avatar
Morten Welinder committed
264 265 266
	case OPER_FUNCALL: {
		GList *l;

267
		for (l = tree->func.arg_list; l; l = l->next)
Morten Welinder's avatar
Morten Welinder committed
268
			do_expr_tree_unref (l->data);
269
		g_list_free (tree->func.arg_list);
Jody Goldberg's avatar
Jody Goldberg committed
270
		func_unref (tree->func.func);
Arturo Espinosa's avatar
Arturo Espinosa committed
271
		break;
Morten Welinder's avatar
Morten Welinder committed
272
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
273

Michael Meeks's avatar
Michael Meeks committed
274 275 276
	case OPER_NAME:
		break;

277
	case OPER_ANY_BINARY:
278 279
		do_expr_tree_unref (tree->binary.value_a);
		do_expr_tree_unref (tree->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
280 281
		break;

282
	case OPER_ANY_UNARY:
283
		do_expr_tree_unref (tree->unary.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
284
		break;
285
	case OPER_ARRAY:
286
		if (tree->array.x == 0 && tree->array.y == 0) {
287 288 289
			if (tree->array.corner.value)
				value_release (tree->array.corner.value);
			do_expr_tree_unref (tree->array.corner.expr);
290
		}
291
		break;
292 293 294
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
295
	}
296

Morten Welinder's avatar
Morten Welinder committed
297
	g_free (tree);
298 299
}

Morten Welinder's avatar
Morten Welinder committed
300 301 302 303 304 305
/*
 * 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.)
 */
306 307 308 309
void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
310
	g_return_if_fail (tree->any.ref_count > 0);
311

Arturo Espinosa's avatar
Arturo Espinosa committed
312
	do_expr_tree_unref (tree);
313 314
}

Jody Goldberg's avatar
Jody Goldberg committed
315 316 317 318 319 320 321 322 323 324 325 326
/**
 * expr_tree_shared : Returns TRUE if the reference count
 *   for the supplied expression is > 1
 */
gboolean
expr_tree_shared (ExprTree const *tree)
{
	g_return_val_if_fail (tree != NULL, FALSE);

	return (tree->any.ref_count > 1);
}

327
static Value *
Jody Goldberg's avatar
Jody Goldberg committed
328 329
eval_funcall (EvalPos const *pos, ExprTree const *tree,
	      ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
330
{
331
	FunctionEvalInfo ei;
Arturo Espinosa's avatar
Arturo Espinosa committed
332
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
333
	GList *args;
334

335
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
336
	g_return_val_if_fail (tree != NULL, NULL);
337

Jody Goldberg's avatar
Jody Goldberg committed
338
	fd = tree->func.func;
339 340
	ei.func_def = fd;
	ei.pos = pos;
341
	args = tree->func.arg_list;
342

Jody Goldberg's avatar
Jody Goldberg committed
343
	/*if (flags & EVAL_PERMIT_NON_SCALAR)*/
344
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
345 346
}

Jody Goldberg's avatar
Jody Goldberg committed
347 348 349 350
/**
 * expr_implicit_intersection :
 * @ei: EvalInfo containing valid fd!
 * @v: a VALUE_CELLRANGE
351
 *
352 353 354 355 356
 * 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.
Jody Goldberg's avatar
Jody Goldberg committed
357 358 359
 *
 * Always release the value passed in.
 *
360
 * Return value:
Jody Goldberg's avatar
Jody Goldberg committed
361 362 363 364
 *     If the intersection succeeded return a duplicate of the value
 *     at the intersection point.  This value needs to be freed.
 **/
Value *
365
expr_implicit_intersection (EvalPos const *pos,
366
			    Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
367 368
{
	Value *res = NULL;
369 370 371 372 373 374 375 376
	Range rng;
	Sheet *start_sheet, *end_sheet;

	/* handle inverted ranges */
	range_ref_normalize  (&rng, &start_sheet, &end_sheet, v, pos);

	if (start_sheet == end_sheet) {
		if (rng.start.row == rng.end.row) {
Jody Goldberg's avatar
Jody Goldberg committed
377
			int const c = pos->eval.col;
378 379 380 381 382
			if (rng.start.col <= c && c <= rng.end.col)
				res = value_duplicate (
					value_area_get_x_y (pos, v,
							    c - rng.start.col,
							    0));
Jody Goldberg's avatar
Jody Goldberg committed
383 384
		}

385
		if (rng.start.col == rng.end.col) {
Jody Goldberg's avatar
Jody Goldberg committed
386
			int const r = pos->eval.row;
387 388 389 390
			if (rng.start.row <= r && r <= rng.end.row)
				res = value_duplicate (
					value_area_get_x_y (pos, v, 0,
							    r - rng.start.row));
Jody Goldberg's avatar
Jody Goldberg committed
391 392 393 394 395 396
		}
	}
	value_release (v);
	return res;
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
/**
 * expr_array_intersection :
 * @v: a VALUE_ARRAY
 *
 * Returns the upper left corner of an array.
 *
 * Always release the value passed in.
 *
 * FIXME FIXME FIXME : This will need to be reworked
 * cellrange for array expressions
 *
 * Return value:
 *     duplicate of the value in the upper left of the array
 **/
Value *
expr_array_intersection (Value *a)
{
	Value *tmp = value_duplicate (a->v_array.vals [0][0]);
	value_release (a);
	return tmp;
}

419 420 421 422 423
/*
 * Utility routine to ensure that all elements of a range are recalced as
 * necessary.
 */
static void
424
eval_range (EvalPos const *pos, Value *v)
425 426
{
	int start_col, start_row, end_col, end_row;
427 428
	CellRef * a = &v->v_range.cell.a;
	CellRef * b = &v->v_range.cell.b;
429
	Sheet * sheet = a->sheet ? a->sheet : pos->sheet;
430 431
	Cell * cell;
	int r, c;
432
	int const gen = pos->sheet->workbook->generation;
433

434 435
	cell_get_abs_col_row (a, &pos->eval, &start_col, &start_row);
	cell_get_abs_col_row (b, &pos->eval, &end_col, &end_row);
436

Michael Meeks's avatar
Michael Meeks committed
437
	if (b->sheet && a->sheet != b->sheet) {
438 439 440 441 442 443 444 445 446
		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;
447
			if (cell->base.generation != gen)
448
				cell_eval (cell);
449 450 451
		}
}

452
static Value *
453
eval_expr_real (EvalPos const *pos, ExprTree const *tree,
454
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
455
{
456
	Value *res = NULL, *a = NULL, *b = NULL;
457

Arturo Espinosa's avatar
Arturo Espinosa committed
458
	g_return_val_if_fail (tree != NULL, NULL);
459
	g_return_val_if_fail (pos != NULL, NULL);
460

461
	switch (tree->any.oper){
462 463 464 465 466 467
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
468
		ValueCompare comp;
469

470
		a = eval_expr_real (pos, tree->binary.value_a, flags);
Jody Goldberg's avatar
Jody Goldberg committed
471 472
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
473
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
474
				if (a == NULL)
475
					return value_new_error (pos, gnumeric_err_VALUE);
476 477 478 479
			} else if (a->type == VALUE_ARRAY) {
				a = expr_array_intersection (a);
				if (a == NULL)
					return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
480 481 482
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
483

484
		b = eval_expr_real (pos, tree->binary.value_b, flags);
Jody Goldberg's avatar
Jody Goldberg committed
485 486 487
		if (b != NULL) {
			Value *res = NULL;
			if (b->type == VALUE_CELLRANGE) {
488
				b = expr_implicit_intersection (pos, b);
Jody Goldberg's avatar
Jody Goldberg committed
489
				if (b == NULL)
490
					res = value_new_error (pos, gnumeric_err_VALUE);
491 492 493 494
			} else if (b->type == VALUE_ARRAY) {
				b = expr_array_intersection (b);
				if (b == NULL)
					return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
495 496 497 498 499 500 501 502
			} 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
503
		}
504

JP Rosevear's avatar
JP Rosevear committed
505
		comp = value_compare (a, b, FALSE);
Arturo Espinosa's avatar
Arturo Espinosa committed
506

507 508 509 510
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
511

Michael Meeks's avatar
Michael Meeks committed
512
		if (comp == TYPE_MISMATCH) {
513 514 515 516
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
517
			if (tree->any.oper == OPER_EQUAL)
518
				return value_new_bool (FALSE);
519
			if (tree->any.oper == OPER_NOT_EQUAL)
520 521
				return value_new_bool (TRUE);

522
			return value_new_error (pos, gnumeric_err_VALUE);
523
		}
524

525
		switch (tree->any.oper) {
526
		case OPER_EQUAL:
527
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
528 529
			break;

530
		case OPER_GT:
531
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
532 533
			break;

534
		case OPER_LT:
535
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
536 537
			break;

538 539
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
540 541
			break;

542 543
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
544 545
			break;

546 547
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
548
			break;
549

Arturo Espinosa's avatar
Arturo Espinosa committed
550
		default:
551
			g_assert_not_reached ();
552
			res = value_new_error (pos,
553
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
554
		}
Morten Welinder's avatar
Morten Welinder committed
555
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
556
	}
557

558 559 560 561 562
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
563 564 565 566 567 568 569 570 571
		/*
		 * 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
		 */

572
	        /* Guarantees a != NULL */
573
		a = eval_expr (pos, tree->binary.value_a,
574
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
575 576 577

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
578
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
579
			if (a == NULL)
580
				return value_new_error (pos, gnumeric_err_VALUE);
581 582 583 584
		} else if (a->type == VALUE_ARRAY) {
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
585 586
		}

587 588
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
589
			return value_new_error_err (pos, &a->v_err);
590

591
		/* 2) #!VALUE error if A is not a number */
592
		if (a->type == VALUE_STRING) {
593
			Value *tmp = format_match (a->v_str.val->str, NULL, NULL);
594 595 596 597 598 599

			value_release (a);
			if (tmp == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
			a = tmp;
		} else if (!VALUE_IS_NUMBER (a)) {
600
			value_release (a);
601
			return value_new_error (pos, gnumeric_err_VALUE);
602 603
		}

604
	        /* Guarantees that b != NULL */
605
		b = eval_expr (pos, tree->binary.value_b,
606
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
607

Jody Goldberg's avatar
Jody Goldberg committed
608 609
		/* Handle implicit intersection */
		if (b->type == VALUE_CELLRANGE) {
610
			b = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
611
			if (b == NULL)
612
				return value_new_error (pos, gnumeric_err_VALUE);
613 614 615 616
		} else if (b->type == VALUE_ARRAY) {
			b = expr_array_intersection (b);
			if (b == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
617 618
		}

619 620
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
621
			value_release (a);
622
			return value_new_error_err (pos, &b->v_err);
Arturo Espinosa's avatar
Arturo Espinosa committed
623
		}
624

625
		/* 4) #!VALUE error if B is not a number */
626
		if (b->type == VALUE_STRING) {
627
			Value *tmp = format_match (b->v_str.val->str, NULL, NULL);
628 629 630 631 632 633 634 635

			value_release (b);
			if (tmp == NULL) {
				value_release (a);
				return value_new_error (pos, gnumeric_err_VALUE);
			}
			b = tmp;
		} else if (!VALUE_IS_NUMBER (b)) {
636 637
			value_release (a);
			value_release (b);
638
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
639
		}
640

641
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
642
			int ia = value_get_as_int (a);
643
			int ib = value_get_as_int (b);
644 645 646
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
647 648
			value_release (a);
			value_release (b);
649

650 651
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
652
			switch (tree->any.oper){
653 654 655 656 657 658
			case OPER_ADD:
				dres = (double)ia + (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
659
					return value_new_float ((gnum_float) dres);
660

661
			case OPER_SUB:
662 663 664 665 666
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
667
					return value_new_float ((gnum_float) dres);
668

669
			case OPER_MULT:
670 671 672 673 674
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
675
					return value_new_float ((gnum_float) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
676

677
			case OPER_DIV:
678
				if (ib == 0)
679
					return value_new_error (pos, gnumeric_err_DIV0);
680 681 682 683 684
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
685
					return value_new_float ((gnum_float) dres);
686 687

			case OPER_EXP:
688
				if (ia == 0 && ib <= 0)
689
					return value_new_error (pos, gnumeric_err_NUM);
690 691 692 693 694
				dres = pow ((double)ia, (double)ib);
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
695
					return value_new_float ((gnum_float) dres);
696

Arturo Espinosa's avatar
Arturo Espinosa committed
697
			default:
698
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
699 700
			}
		} else {
701 702
			gnum_float const va = value_get_as_float (a);
			gnum_float const vb = value_get_as_float (b);
703 704
			value_release (a);
			value_release (b);
705

706
			switch (tree->any.oper){
707
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
708
				return value_new_float (va + vb);
709

710
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
711
				return value_new_float (va - vb);
712

713
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
714
				return value_new_float (va * vb);
715

716
			case OPER_DIV:
717
				return (vb == 0.0)
718
				    ? value_new_error (pos,
719
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
720
				    : value_new_float (va / vb);
721

722
			case OPER_EXP:
723 724
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
725
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
726
				return value_new_float (pow (va, vb));
727

Arturo Espinosa's avatar
Arturo Espinosa committed
728
			default:
729
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
730 731
			}
		}
732
		return value_new_error (pos, _("Unknown operator"));
733

Jody Goldberg's avatar
Jody Goldberg committed
734
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
735 736
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
737
	        /* Garantees that a != NULL */
738
		a = eval_expr (pos, tree->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
739 740 741

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
742
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
743
			if (a == NULL)
744
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
745
		} else if (a->type == VALUE_ARRAY) {
746 747 748
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
749 750
		}

751 752
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
753

754
		if (tree->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
755 756
			return a;

757 758
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
759
			return value_new_error (pos, gnumeric_err_VALUE);
760
		}
761
		if (tree->any.oper == OPER_UNARY_NEG) {
Jody Goldberg's avatar
Jody Goldberg committed
762
			if (a->type == VALUE_INTEGER)
763
				res = value_new_int (-a->v_int.val);
Jody Goldberg's avatar
Jody Goldberg committed
764
			else if (a->type == VALUE_FLOAT)
765
				res = value_new_float (-a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
766
			else
767
				res = value_new_bool (!a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
768 769
		} else
			res = value_new_float (value_get_as_float (a) * .01);
770
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
771
		return res;
772

773
	case OPER_CONCAT: {
Morten Welinder's avatar
Morten Welinder committed
774
		char *tmp;
775

776
		a = eval_expr_real (pos, tree->binary.value_a, flags);
777
		if (a != NULL && a->type == VALUE_ERROR)
778
			return a;
779
		b = eval_expr_real (pos, tree->binary.value_b, flags);
780 781 782
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
783
			return b;
784 785
		}

Morten Welinder's avatar
Morten Welinder committed
786 787
		tmp = g_strconcat (value_peek_string (a),
				   value_peek_string (b), NULL);
Morten Welinder's avatar
Morten Welinder committed
788
		res = value_new_string (tmp);
789 790
		g_free (tmp);

791
		if (a != NULL)
Morten Welinder's avatar
Morten Welinder committed
792
			value_release (a);
793
		if (b != NULL)
Morten Welinder's avatar
Morten Welinder committed
794
			value_release (b);
Morten Welinder's avatar
Morten Welinder committed
795
		return res;
796
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
797

798
	case OPER_FUNCALL:
Jody Goldberg's avatar
Jody Goldberg committed
799
		return eval_funcall (pos, tree, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
800

Michael Meeks's avatar
Michael Meeks committed
801
	case OPER_NAME:
802
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
803

804
	case OPER_VAR: {
805
		Sheet *cell_sheet;
806
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
807 808
		Cell *cell;
		int col, row;
809

810
		ref = &tree->var.ref;
811
		cell_get_abs_col_row (ref, &pos->eval, &col, &row);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
812

813
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
814
		cell = sheet_cell_get (cell_sheet, col, row);
815 816
		if (cell == NULL)
			return NULL;
817

818
		if (cell->base.generation != pos->sheet->workbook->generation)
819
			cell_eval (cell);
820

821
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
822
	}
823

824
	case OPER_CONSTANT:
825
		res = tree->constant.value;
826 827
		if (res->type != VALUE_CELLRANGE)
			return value_duplicate (res);
828
		if (flags & EVAL_PERMIT_NON_SCALAR) {
829
			eval_range (pos, res);
830 831 832 833 834 835 836 837 838
			return value_duplicate (res);
		} else {
			/*
			 * 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.
			 */
839 840
			CellRef const * const a = & res->v_range.cell.a;
			CellRef const * const b = & res->v_range.cell.b;
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
			gboolean found = FALSE;

			if (a->sheet == b->sheet) {
				int a_col, a_row, b_col, b_row;
				int c = pos->eval.col;
				int r = pos->eval.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) {
					if (a_col <= c && c <= b_col) {
						r = a_row;
						found = TRUE;
					}
				} else if (a_col == b_col) {
					if (a_row <= r && r <= b_row) {
						c = a_col;
						found = TRUE;
					}
				}
				if (found) {
					Cell * cell = sheet_cell_get (pos->sheet, c, r);
					if (cell == NULL)
						return NULL;

866
					if (cell->base.generation != pos->sheet->workbook->generation)
867 868 869 870 871 872 873
						cell_eval (cell);

					return value_duplicate (cell->value);
				}
			}
			return value_new_error (pos, gnumeric_err_VALUE);
		}
874

875 876 877
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
878 879
		int x = tree->array.x;
		int y = tree->array.y;
880
		if (x == 0 && y == 0){
881
			/* Release old value if necessary */
882
			a = tree->array.corner.value;
883 884 885
			if (a != NULL)
				value_release (a);

886 887 888 889
			/*
			 * FIXME : Call a wrapper routine that will iterate
			 * over the the array and evaluate the expression for
			 * all elements
Jody Goldberg's avatar
Jody Goldberg committed
890
			 *