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"
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;
199
	ParseError *perr = error;
200

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

203 204 205
	if (error == NULL)
		perr = g_new0 (ParseError, 1);
		
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 *
367
expr_implicit_intersection (EvalPos const *pos,
368
			    Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
369 370
{
	Value *res = NULL;
371 372 373 374 375 376 377 378
	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
379
			int const c = pos->eval.col;
380 381 382 383 384
			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
385 386
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

775
	case OPER_CONCAT: {
776 777
		char *sa, *sb, *tmp;

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

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

793 794 795 796
		g_free (sa);
		g_free (sb);
		g_free (tmp);

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

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

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

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

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

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

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

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

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

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

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

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