expr.c 40.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"
Jody Goldberg's avatar
Jody Goldberg committed
18 19
#include "sheet.h"
#include "str.h"
20
#include "parse-util.h"
21
#include "ranges.h"
22
#include "number-match.h"
23
#include "workbook.h"
Jeffrey Stedfast's avatar
Jeffrey Stedfast committed
24
#include "gutils.h"
25

26 27
/***************************************************************************/

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

33
	ans = g_new (ExprConstant, 1);
34 35
	if (!ans)
		return NULL;
36

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

41
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
42 43
}

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

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

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

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

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

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

	return (ExprTree *)ans;
}
79

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

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

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

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


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

102
	ans = g_new (ExprBinary, 1);
103 104
	if (!ans)
		return NULL;
105

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

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

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

119
	ans = g_new (ExprName, 1);
120 121
	if (!ans)
		return NULL;
122

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

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

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

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

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	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;
154

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

166
int
167
expr_tree_get_const_int (ExprTree const *expr)
168 169
{
	g_return_val_if_fail (expr != NULL, 0);
170 171 172
	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);
173

174
	return expr->constant.value->v_int.val;
175 176
}

177
char const *
178
expr_tree_get_const_str (ExprTree const *expr)
179 180
{
	g_return_val_if_fail (expr != NULL, NULL);
181 182 183
	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);
184

185
	return expr->constant.value->v_str.val->str;
186 187
}

188 189 190 191 192 193 194 195
/**
 * 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.
 **/
196
ExprTree *
197
expr_parse_string (char const *expr_text, ParsePos const *pp,
198
		   StyleFormat **desired_format, ParseError *error)
199
{
200
	ExprTree   *tree;
Morten Welinder's avatar
Morten Welinder committed
201 202
	ParseError errrec;
	ParseError *perr = error ? error : &errrec;
203

204
	g_return_val_if_fail (expr_text != NULL, NULL);
205

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

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

212
	return tree;
213 214
}

215 216

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

222
	g_return_val_if_fail (pos != NULL, NULL);
223

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

228
	/* Sanity check incase the corner gets removed for some reason */
229
	g_return_val_if_fail (corner != NULL, NULL);
230
	g_return_val_if_fail (cell_has_expr (corner), NULL);
231 232 233 234
	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);
235

236
	return corner->base.expression;
237 238
}

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

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

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

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

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

Morten Welinder's avatar
Morten Welinder committed
266 267 268
	case OPER_FUNCALL: {
		GList *l;

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

Michael Meeks's avatar
Michael Meeks committed
276 277 278
	case OPER_NAME:
		break;

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

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

Morten Welinder's avatar
Morten Welinder committed
299
	g_free (tree);
300 301
}

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

Arturo Espinosa's avatar
Arturo Espinosa committed
314
	do_expr_tree_unref (tree);
315 316
}

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

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

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

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

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

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

	/* handle inverted ranges */
Jody Goldberg's avatar
Jody Goldberg committed
374
	value_cellrange_normalize (pos, v, &start_sheet, &end_sheet, &rng);
375 376 377

	if (start_sheet == end_sheet) {
		if (rng.start.row == rng.end.row) {
Jody Goldberg's avatar
Jody Goldberg committed
378
			int const c = pos->eval.col;
379 380 381 382 383
			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
384 385
		}

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

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/**
 * 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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			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)) {
637 638
			value_release (a);
			value_release (b);
639
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
640
		}
641

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

785 786 787 788 789 790 791 792 793 794 795 796 797 798
		if (a == NULL) {
			if (b != NULL) {
				res = value_new_string (value_peek_string (b));
				value_release (b);
			} else
				res = value_new_string ("");
		} else if (b == NULL) {
			res = value_new_string (value_peek_string (a));
			value_release (a);
		} else {
			char *tmp = g_strconcat (value_peek_string (a),
						 value_peek_string (b), NULL);
			res = value_new_string (tmp);
			g_free (tmp);
Morten Welinder's avatar
Morten Welinder committed
799 800
			value_release (a);
			value_release (b);
801 802
		}

Morten Welinder's avatar
Morten Welinder committed
803
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
804

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

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

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

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

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

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

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

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

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

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

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

893 894 895 896
			/*
			 * 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
897
			 *
898 899
			 * Figure out when to iterate and when to do array
			 * operations.
Jody Goldberg's avatar
Jody Goldberg committed
900
			 * ie
901 902
			 * 	A1:A3 = '=B1:B3^2'
			 * Will iterate over all the elements and re-evaluate.
Jody Goldberg's avatar