expr.c 41.2 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"
22

23 24
/***************************************************************************/

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}
76

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

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

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

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


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

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

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

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

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

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

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

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

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

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	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;
151

152 153 154 155 156 157 158 159 160 161 162 163
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_ARRAY;
	ans->x = x;
	ans->y = y;
	ans->rows = rows;
	ans->cols = cols;
	ans->corner.func.value = NULL;
	ans->corner.func.expr = NULL;
	ans->corner.cell = NULL;
	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
ExprTree *
187
expr_parse_string (char const *expr, ParsePos const *pp,
188
		   StyleFormat **desired_format, char **error_msg)
189 190 191 192
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

193
	switch (gnumeric_expr_parser (expr, pp, TRUE, FALSE, desired_format, &tree)) {
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	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;
}

213 214

static ExprTree *
215
expr_tree_array_formula_corner (ExprTree const *expr, EvalPos const *pos)
216
{
217
	Cell * corner = expr->array.corner.cell;
218

219 220 221 222 223 224
	/* 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,
225 226 227
					 pos->eval.col - expr->array.x,
					 pos->eval.row - expr->array.y);
		((ExprTree *)expr)->array.corner.cell = corner;
228 229 230
	}

	g_return_val_if_fail (corner != NULL, NULL);
231
	g_return_val_if_fail (cell_has_expr (corner), NULL);
232 233

	/* Sanity check incase the corner gets removed for some reason */
234 235 236 237
	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);
238

239
	return corner->base.expression;
240 241
}

Arturo Espinosa's avatar
Arturo Espinosa committed
242 243 244 245
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
246 247 248 249
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
250
	g_return_if_fail (tree->any.ref_count > 0);
251

252
	tree->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
253 254 255 256 257
}

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

261
	switch (tree->any.oper){
262
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
263
		break;
264

265
	case OPER_CONSTANT:
266
		value_release (tree->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
267
		break;
268

Morten Welinder's avatar
Morten Welinder committed
269 270 271
	case OPER_FUNCALL: {
		GList *l;

272
		for (l = tree->func.arg_list; l; l = l->next)
Morten Welinder's avatar
Morten Welinder committed
273
			do_expr_tree_unref (l->data);
274
		g_list_free (tree->func.arg_list);
Jody Goldberg's avatar
Jody Goldberg committed
275
		func_unref (tree->func.func);
Arturo Espinosa's avatar
Arturo Espinosa committed
276
		break;
Morten Welinder's avatar
Morten Welinder committed
277
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
278

Michael Meeks's avatar
Michael Meeks committed
279 280 281
	case OPER_NAME:
		break;

282
	case OPER_ANY_BINARY:
283 284
		do_expr_tree_unref (tree->binary.value_a);
		do_expr_tree_unref (tree->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
285 286
		break;

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

Morten Welinder's avatar
Morten Welinder committed
302
	g_free (tree);
303 304
}

Morten Welinder's avatar
Morten Welinder committed
305 306 307 308 309 310
/*
 * 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.)
 */
311 312 313 314
void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
315
	g_return_if_fail (tree->any.ref_count > 0);
316

Arturo Espinosa's avatar
Arturo Espinosa committed
317
	do_expr_tree_unref (tree);
318 319
}

Jody Goldberg's avatar
Jody Goldberg committed
320 321 322 323 324 325 326 327 328 329 330 331
/**
 * 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);
}

332
static Value *
Jody Goldberg's avatar
Jody Goldberg committed
333 334
eval_funcall (EvalPos const *pos, ExprTree const *tree,
	      ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
335
{
336
	FunctionEvalInfo ei;
Arturo Espinosa's avatar
Arturo Espinosa committed
337
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
338
	GList *args;
339

340
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
341
	g_return_val_if_fail (tree != NULL, NULL);
342

Jody Goldberg's avatar
Jody Goldberg committed
343
	fd = tree->func.func;
344 345
	ei.func_def = fd;
	ei.pos = pos;
346
	args = tree->func.arg_list;
347

Jody Goldberg's avatar
Jody Goldberg committed
348
	/*if (flags & EVAL_PERMIT_NON_SCALAR)*/
349
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
350 351
}

Jody Goldberg's avatar
Jody Goldberg committed
352 353 354 355
/**
 * expr_implicit_intersection :
 * @ei: EvalInfo containing valid fd!
 * @v: a VALUE_CELLRANGE
356
 *
357 358 359 360 361
 * 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
362 363 364
 *
 * Always release the value passed in.
 *
365
 * Return value:
Jody Goldberg's avatar
Jody Goldberg committed
366 367 368 369
 *     If the intersection succeeded return a duplicate of the value
 *     at the intersection point.  This value needs to be freed.
 **/
Value *
370
expr_implicit_intersection (EvalPos const *pos,
371
			    Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
372 373
{
	Value *res = NULL;
374 375 376 377 378 379 380 381
	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
382
			int const c = pos->eval.col;
383 384 385 386 387
			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
388 389
		}

390
		if (rng.start.col == rng.end.col) {
Jody Goldberg's avatar
Jody Goldberg committed
391
			int const r = pos->eval.row;
392 393 394 395
			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
396 397 398 399 400 401
		}
	}
	value_release (v);
	return res;
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
/**
 * 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;
}

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

439 440
	cell_get_abs_col_row (a, &pos->eval, &start_col, &start_row);
	cell_get_abs_col_row (b, &pos->eval, &end_col, &end_row);
441

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

457
static Value *
458
eval_expr_real (EvalPos const *pos, ExprTree const *tree,
459
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
460
{
461
	Value *res = NULL, *a = NULL, *b = NULL;
462

Arturo Espinosa's avatar
Arturo Espinosa committed
463
	g_return_val_if_fail (tree != NULL, NULL);
464
	g_return_val_if_fail (pos != NULL, NULL);
465

466
	switch (tree->any.oper){
467 468 469 470 471 472
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
473
		ValueCompare comp;
474

475
		a = eval_expr_real (pos, tree->binary.value_a, flags);
Jody Goldberg's avatar
Jody Goldberg committed
476 477
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
478
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
479
				if (a == NULL)
480
					return value_new_error (pos, gnumeric_err_VALUE);
481 482 483 484
			} 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
485 486 487
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
488

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

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

512 513 514 515
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
516

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

527
			return value_new_error (pos, gnumeric_err_VALUE);
528
		}
529

530
		switch (tree->any.oper) {
531
		case OPER_EQUAL:
532
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
533 534
			break;

535
		case OPER_GT:
536
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
537 538
			break;

539
		case OPER_LT:
540
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
541 542
			break;

543 544
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
545 546
			break;

547 548
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
549 550
			break;

551 552
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
553
			break;
554

Arturo Espinosa's avatar
Arturo Espinosa committed
555
		default:
556
			g_assert_not_reached ();
557
			res = value_new_error (pos,
558
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
559
		}
Morten Welinder's avatar
Morten Welinder committed
560
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
561
	}
562

563 564 565 566 567
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
568 569 570 571 572 573 574 575 576
		/*
		 * 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
		 */

577
	        /* Ensure a != NULL */
578
		a = eval_expr (pos, tree->binary.value_a,
579
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
580 581 582

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
583
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
584
			if (a == NULL)
585
				return value_new_error (pos, gnumeric_err_VALUE);
586 587 588 589
		} 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
590 591
		}

592 593
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
594
			return value_new_error_err (pos, &a->v_err);
595

596
		/* 2) #!VALUE error if A is not a number */
597
		if (a->type == VALUE_STRING) {
598
			Value *tmp = format_match (a->v_str.val->str, NULL, NULL);
599 600 601 602 603 604

			value_release (a);
			if (tmp == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
			a = tmp;
		} else if (!VALUE_IS_NUMBER (a)) {
605
			value_release (a);
606
			return value_new_error (pos, gnumeric_err_VALUE);
607 608 609
		}

	        /* Garantees that b != NULL */
610
		b = eval_expr (pos, tree->binary.value_b,
611
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
612

Jody Goldberg's avatar
Jody Goldberg committed
613 614
		/* Handle implicit intersection */
		if (b->type == VALUE_CELLRANGE) {
615
			b = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
616
			if (b == NULL)
617
				return value_new_error (pos, gnumeric_err_VALUE);
618 619 620 621
		} 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
622 623
		}

624 625
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
626
			value_release (a);
627
			return value_new_error_err (pos, &b->v_err);
Arturo Espinosa's avatar
Arturo Espinosa committed
628
		}
629

630
		/* 4) #!VALUE error if B is not a number */
631
		if (b->type == VALUE_STRING) {
632
			Value *tmp = format_match (b->v_str.val->str, NULL, NULL);
633 634 635 636 637 638 639 640

			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)) {
641 642
			value_release (a);
			value_release (b);
643
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
644
		}
645

646
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
647
			int ia = value_get_as_int (a);
648
			int ib = value_get_as_int (b);
649 650 651
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
652 653
			value_release (a);
			value_release (b);
654

655 656
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
657
			switch (tree->any.oper){
658 659 660 661 662 663 664 665
			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);

666
			case OPER_SUB:
667 668 669 670 671 672
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
673

674
			case OPER_MULT:
675 676 677 678 679 680
				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
681

682
			case OPER_DIV:
683
				if (ib == 0)
684
					return value_new_error (pos, gnumeric_err_DIV0);
685 686 687 688 689 690
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
691 692

			case OPER_EXP:
693
				if (ia == 0 && ib <= 0)
694
					return value_new_error (pos, gnumeric_err_NUM);
695 696 697 698 699 700 701
				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
702
			default:
703
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
704 705
			}
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
706 707
			float_t const va = value_get_as_float (a);
			float_t const vb = value_get_as_float (b);
708 709
			value_release (a);
			value_release (b);
710

711
			switch (tree->any.oper){
712
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
713
				return value_new_float (va + vb);
714

715
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
716
				return value_new_float (va - vb);
717

718
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
719
				return value_new_float (va * vb);
720

721
			case OPER_DIV:
722
				return (vb == 0.0)
723
				    ? value_new_error (pos,
724
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
725
				    : value_new_float (va / vb);
726

727
			case OPER_EXP:
728 729
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
730
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
731
				return value_new_float (pow (va, vb));
732

Arturo Espinosa's avatar
Arturo Espinosa committed
733
			default:
734
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
735 736
			}
		}
737
		return value_new_error (pos, _("Unknown operator"));
738

Jody Goldberg's avatar
Jody Goldberg committed
739
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
740 741
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
742
	        /* Garantees that a != NULL */
743
		a = eval_expr (pos, tree->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
744 745 746

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
747
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
748
			if (a == NULL)
749
				return value_new_error (pos, gnumeric_err_VALUE);
750 751 752 753
		} else if (a->type == VALUE_CELLRANGE) {
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
754 755
		}

756 757
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
758

759
		if (tree->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
760 761
			return a;

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

778
	case OPER_CONCAT: {
779 780
		char *sa, *sb, *tmp;

781
		a = eval_expr_real (pos, tree->binary.value_a, flags);
782
		if (a != NULL && a->type == VALUE_ERROR)
783
			return a;
784
		b = eval_expr_real (pos, tree->binary.value_b, flags);
785 786 787
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
788
			return b;
789 790
		}

Michael Meeks's avatar
Michael Meeks committed
791 792
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
793
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
794 795
		res = value_new_string (tmp);

796 797 798 799
		g_free (sa);
		g_free (sb);
		g_free (tmp);

800
		if (a != NULL)
801
		value_release (a);
802
		if (b != NULL)
803
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
804
		return res;
805
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
806

807
	case OPER_FUNCALL:
Jody Goldberg's avatar
Jody Goldberg committed
808
		return eval_funcall (pos, tree, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
809

Michael Meeks's avatar
Michael Meeks committed
810
	case OPER_NAME:
811
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
812

813
	case OPER_VAR: {
814
		Sheet *cell_sheet;
815
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
816 817
		Cell *cell;
		int col, row;
818

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

822
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
823
		cell = sheet_cell_get (cell_sheet, col, row);
824 825
		if (cell == NULL)
			return NULL;
826

827
		if (cell->base.generation != pos->sheet->workbook->generation)
828
			cell_eval (cell);
829

830
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
831
	}
832

833
	case OPER_CONSTANT:
834
		res = tree->constant.value;
835 836
		if (res->type != VALUE_CELLRANGE)
			return value_duplicate (res);
837
		if (flags & EVAL_PERMIT_NON_SCALAR) {
838
			eval_range (pos, res);
839 840 841 842 843 844 845 846 847
			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.
			 */
848 849
			CellRef const * const a = & res->v_range.cell.a;
			CellRef const * const b = & res->v_range.cell.b;
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
			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;

875
					if (cell->base.generation != pos->sheet->workbook->generation)
876 877 878 879 880 881 882
						cell_eval (cell);

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

884 885 886
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
887 888
		int x = tree->array.x;
		int y = tree->array.y;
889
		if (x == 0 && y == 0){
890
			/* Release old value if necessary */
891
			a = tree->array.corner.func.value;
892 893 894
			if (a != NULL)
				value_release (a);

895 896 897 898
			/*
			 * 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
899
			 *
900 901
			 * Figure out when to iterate and when to do array
			 * operations.
Jody Goldberg's avatar
Jody Goldberg committed
902
			 * ie
903 904
			 * 	A1:A3 = '=B1:B3^2'
			 * Will iterate over all the elements and re-evaluate.
Jody Goldberg's avatar
Jody Goldberg committed
905
			 * whereas
906 907
			 *	 A1:A3 = '=bob(B1:B3)'
			 * Will call bob once if it returns an array.
Jody Goldberg's avatar
Jody Goldberg committed
908
			 *
909 910 911 912
			 * 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.
Jody Goldberg's avatar
Jody Goldberg committed
913
			 */
914
			a = eval_expr_real (pos, tree->array.corner.func.expr,
Jody Goldberg's avatar
Jody Goldberg committed
915
					    EVAL_PERMIT_NON_SCALAR);
916 917

			/* Store real result (cast away const)*/
918
			*((Value **)&(tree->array.corner.func.value)) = a;
919
		} else {
920
			ExprTree const * const array =
921
			    expr_tree_array_formula_corner (tree, pos);
922
			if (array)