expr.c 44.5 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Miguel de Icaza's avatar
Miguel de Icaza committed
2
/*
Morten Welinder's avatar
Morten Welinder committed
3
 * expr.c: Expression evaluation in Gnumeric
Miguel de Icaza's avatar
Miguel de Icaza committed
4 5 6 7
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org).
 */
8 9
#include <gnumeric-config.h>
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
10
#include "expr.h"
11

12
#include "expr-name.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
14
#include "format.h"
15
#include "func.h"
Jody Goldberg's avatar
Jody Goldberg committed
16
#include "cell.h"
Jody Goldberg's avatar
Jody Goldberg committed
17 18
#include "sheet.h"
#include "str.h"
19
#include "parse-util.h"
20
#include "ranges.h"
21
#include "number-match.h"
22
#include "workbook.h"
Jeffrey Stedfast's avatar
Jeffrey Stedfast committed
23
#include "gutils.h"
24
#include "parse-util.h"
25

26 27 28
#include <math.h>
#include <string.h>

29 30
/***************************************************************************/

31
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
32
expr_tree_new_constant (Value *v)
33
{
34
	ExprConstant *ans;
35

36
	ans = g_new (ExprConstant, 1);
37 38
	if (!ans)
		return NULL;
39

40
	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
41
	*((Operation *)&(ans->oper)) = OPER_CONSTANT;
42
	ans->value = v;
43

44
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
45 46
}

47 48 49
ExprTree *
expr_tree_new_error (char const *txt)
{
Jody Goldberg's avatar
Jody Goldberg committed
50
	FunctionDefinition *func;
51
	ExprList *args = NULL;
52

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

Jody Goldberg's avatar
Jody Goldberg committed
60
	func_ref (func);
61 62 63 64
	return expr_tree_new_funcall (func, args);
}

ExprTree *
65
expr_tree_new_funcall (FunctionDefinition *func, ExprList *args)
66 67
{
	ExprFunction *ans;
Jody Goldberg's avatar
Jody Goldberg committed
68
	g_return_val_if_fail (func, NULL);
69 70 71 72

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

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

	return (ExprTree *)ans;
}
82

Morten Welinder's avatar
Morten Welinder committed
83 84 85
ExprTree *
expr_tree_new_unary  (Operation op, ExprTree *e)
{
86
	ExprUnary *ans;
Morten Welinder's avatar
Morten Welinder committed
87

88
	ans = g_new (ExprUnary, 1);
89 90 91 92
	if (!ans)
		return NULL;

	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
93
	*((Operation *)&(ans->oper)) = op;
94
	ans->value = e;
95

96
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
97 98 99 100 101 102
}


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

105
	ans = g_new (ExprBinary, 1);
106 107
	if (!ans)
		return NULL;
108

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

114
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
115 116 117
}

ExprTree *
118 119
expr_tree_new_name (NamedExpression *name,
		    Sheet *optional_scope, Workbook *optional_wb_scope)
Morten Welinder's avatar
Morten Welinder committed
120
{
121
	ExprName *ans;
Morten Welinder's avatar
Morten Welinder committed
122

123
	ans = g_new (ExprName, 1);
124 125
	if (!ans)
		return NULL;
126

127
	ans->ref_count = 1;
128 129
	*((Operation *)&(ans->oper)) = OPER_NAME;
	ans->name = name;
130
	expr_name_ref (name);
131

132 133 134
	ans->optional_scope = optional_scope;
	ans->optional_wb_scope = optional_wb_scope;

135
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
136
}
137 138 139 140 141 142 143 144 145

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

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

147 148 149 150 151 152 153 154
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_VAR;
	ans->ref = *cr;

	return (ExprTree *)ans;
}

ExprTree *
155
expr_tree_new_array (int x, int y, int cols, int rows)
156 157 158 159 160 161
{
	ExprArray *ans;

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

163 164 165 166 167 168
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_ARRAY;
	ans->x = x;
	ans->y = y;
	ans->rows = rows;
	ans->cols = cols;
169 170
	ans->corner.value = NULL;
	ans->corner.expr = NULL;
171 172 173
	return (ExprTree *)ans;
}

174
ExprTree *
175
expr_tree_new_set (ExprList *set)
176
{
177
	ExprSet *ans;
178

179 180 181
	ans = g_new (ExprSet, 1);
	if (!ans)
		return NULL;
182

183 184 185
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_SET;
	ans->set = set;
186

187
	return (ExprTree *)ans;
188 189
}

190 191 192
static Cell *
expr_tree_array_corner (ExprTree const *expr,
			Sheet const *sheet, CellPos const *pos)
193
{
194 195
	Cell *corner = sheet_cell_get (sheet,
		pos->col - expr->array.x, pos->row - expr->array.y);
196

197
	/* Sanity check incase the corner gets removed for some reason */
198
	g_return_val_if_fail (corner != NULL, NULL);
199
	g_return_val_if_fail (cell_has_expr (corner), NULL);
200 201 202 203
	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);
204

205
	return corner;
206 207
}

Arturo Espinosa's avatar
Arturo Espinosa committed
208 209 210 211
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
212
void
213
expr_tree_ref (ExprTree *expr)
214
{
215 216
	g_return_if_fail (expr != NULL);
	g_return_if_fail (expr->any.ref_count > 0);
217

218
	expr->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
219 220 221
}

static void
222
do_expr_tree_unref (ExprTree *expr)
Arturo Espinosa's avatar
Arturo Espinosa committed
223
{
224
	if (--expr->any.ref_count > 0)
Morten Welinder's avatar
Morten Welinder committed
225 226
		return;

227
	switch (expr->any.oper){
228
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
229
		break;
230

231
	case OPER_CONSTANT:
232
		value_release (expr->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
233
		break;
234

235 236
	case OPER_FUNCALL:
		expr_list_unref (expr->func.arg_list);
237
		func_unref (expr->func.func);
Arturo Espinosa's avatar
Arturo Espinosa committed
238 239
		break;

Michael Meeks's avatar
Michael Meeks committed
240
	case OPER_NAME:
241
		expr_name_unref (expr->name.name);
Michael Meeks's avatar
Michael Meeks committed
242 243
		break;

244
	case OPER_ANY_BINARY:
245 246
		do_expr_tree_unref (expr->binary.value_a);
		do_expr_tree_unref (expr->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
247 248
		break;

249
	case OPER_ANY_UNARY:
250
		do_expr_tree_unref (expr->unary.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
251
		break;
252
	case OPER_ARRAY:
253 254 255 256
		if (expr->array.x == 0 && expr->array.y == 0) {
			if (expr->array.corner.value)
				value_release (expr->array.corner.value);
			do_expr_tree_unref (expr->array.corner.expr);
257
		}
258
		break;
259 260 261 262
	case OPER_SET:
		expr_list_unref (expr->set.set);
		break;

263 264 265
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
266
	}
267

268
	g_free (expr);
269 270
}

Morten Welinder's avatar
Morten Welinder committed
271 272 273 274 275 276
/*
 * 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.)
 */
277
void
278
expr_tree_unref (ExprTree *expr)
279
{
280 281
	g_return_if_fail (expr != NULL);
	g_return_if_fail (expr->any.ref_count > 0);
282

283
	do_expr_tree_unref (expr);
284 285
}

Jody Goldberg's avatar
Jody Goldberg committed
286
/**
Jody Goldberg's avatar
Jody Goldberg committed
287
 * expr_tree_is_shared : Returns TRUE if the reference count
Jody Goldberg's avatar
Jody Goldberg committed
288 289 290
 *   for the supplied expression is > 1
 */
gboolean
Jody Goldberg's avatar
Jody Goldberg committed
291
expr_tree_is_shared (ExprTree const *expr)
Jody Goldberg's avatar
Jody Goldberg committed
292
{
293
	g_return_val_if_fail (expr != NULL, FALSE);
Jody Goldberg's avatar
Jody Goldberg committed
294

295
	return (expr->any.ref_count > 1);
Jody Goldberg's avatar
Jody Goldberg committed
296 297
}

Jody Goldberg's avatar
Jody Goldberg committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/**
 * expr_tree_equal : Returns TRUE if the supplied expressions are exactly the
 *   same.  No eval position is used to see if they are effectively the same.
 *   Named expressions must refer the the same name, having equivalent names is
 *   insufficeient.
 */
gboolean
expr_tree_equal (ExprTree const *a, ExprTree const *b)
{
	if (a == b)
		return TRUE;

	g_return_val_if_fail (a != NULL, FALSE);
	g_return_val_if_fail (b != NULL, FALSE);

	if (a->any.oper != b->any.oper)
		return FALSE;

	switch (a->any.oper) {
	case OPER_ANY_BINARY:
		return	expr_tree_equal (a->binary.value_a, b->binary.value_a) &&
			expr_tree_equal (a->binary.value_b, b->binary.value_b);

	case OPER_ANY_UNARY:
		return expr_tree_equal (a->unary.value, b->unary.value);

324 325 326
	case OPER_FUNCALL:
		return (a->func.func == b->func.func) &&
			expr_list_equal (a->func.arg_list, b->func.arg_list);
Jody Goldberg's avatar
Jody Goldberg committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

	case OPER_NAME:
		return a->name.name == b->name.name;

	case OPER_VAR:
		return cellref_equal (&a->var.ref, &b->var.ref);

	case OPER_CONSTANT: {
		Value const *va = a->constant.value;
		Value const *vb = b->constant.value;

		if (va->type != vb->type)
			return FALSE;

		if (va->type == VALUE_CELLRANGE)
			return	cellref_equal (&va->v_range.cell.a, &vb->v_range.cell.a) &&
				cellref_equal (&va->v_range.cell.b, &vb->v_range.cell.b);

		return value_compare (va, vb, TRUE) == IS_EQUAL;
	}

	case OPER_ARRAY: {
		ExprArray const *aa = &a->array;
		ExprArray const *ab = &b->array;

		return	aa->cols == ab->cols &&
			aa->rows == ab->rows &&
			aa->x == ab->x &&
			aa->y == ab->y &&
			expr_tree_equal (aa->corner.expr, ab->corner.expr);
	}

359 360 361
	case OPER_SET:
		return expr_list_equal (a->set.set, b->set.set);

Jody Goldberg's avatar
Jody Goldberg committed
362 363 364 365 366 367 368
	default :
		g_assert_not_reached ();
	}

	return FALSE;
}

Jody Goldberg's avatar
Jody Goldberg committed
369 370 371 372
/**
 * expr_implicit_intersection :
 * @ei: EvalInfo containing valid fd!
 * @v: a VALUE_CELLRANGE
373
 *
374 375 376 377 378
 * 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
379 380 381
 *
 * Always release the value passed in.
 *
382
 * Return value:
Jody Goldberg's avatar
Jody Goldberg committed
383 384 385 386
 *     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
387
expr_implicit_intersection (EvalPos const *pos, Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
388 389
{
	Value *res = NULL;
390 391 392 393
	Range rng;
	Sheet *start_sheet, *end_sheet;

	/* handle inverted ranges */
Jody Goldberg's avatar
Jody Goldberg committed
394
	value_cellrange_normalize (pos, v, &start_sheet, &end_sheet, &rng);
395 396 397

	if (start_sheet == end_sheet) {
		if (rng.start.row == rng.end.row) {
Jody Goldberg's avatar
Jody Goldberg committed
398
			int const c = pos->eval.col;
Jody Goldberg's avatar
Jody Goldberg committed
399 400 401 402 403 404
			if (rng.start.col <= c && c <= rng.end.col) {
				Value const *tmp = value_area_get_x_y (pos, v,
					c - rng.start.col, 0);
				if (tmp != NULL)
					res = value_duplicate (tmp);
			}
Jody Goldberg's avatar
Jody Goldberg committed
405 406
		}

407
		if (rng.start.col == rng.end.col) {
Jody Goldberg's avatar
Jody Goldberg committed
408
			int const r = pos->eval.row;
Jody Goldberg's avatar
Jody Goldberg committed
409 410 411 412 413 414
			if (rng.start.row <= r && r <= rng.end.row) {
				Value const *tmp = value_area_get_x_y (pos, v,
					0, r - rng.start.row);
				if (tmp != NULL)
					res = value_duplicate (tmp);
			}
Jody Goldberg's avatar
Jody Goldberg committed
415 416 417 418 419 420
		}
	}
	value_release (v);
	return res;
}

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/**
 * 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;
}

Jody Goldberg's avatar
Jody Goldberg committed
443 444
static Value *
cb_range_eval (Sheet *sheet, int col, int row, Cell *cell, void *ignore)
445
{
Jody Goldberg's avatar
Jody Goldberg committed
446 447
	cell_eval (cell);
	return NULL;
448 449
}

450
static Value *
451
expr_eval_real (ExprTree const *expr, EvalPos const *pos,
452
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
453
{
454
	Value *res = NULL, *a = NULL, *b = NULL;
455

456
	g_return_val_if_fail (expr != NULL, NULL);
457
	g_return_val_if_fail (pos != NULL, NULL);
458

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

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

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

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

505 506 507 508
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
509

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

520
			return value_new_error (pos, gnumeric_err_VALUE);
521
		}
522

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

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

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

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

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

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

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

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

570
	        /* Guarantees a != NULL */
571
		a = expr_eval (expr->binary.value_a, pos,
572
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
573 574 575

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

585 586
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
587
			return value_error_set_pos (&a->v_err, pos);
588

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

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

602
	        /* Guarantees that b != NULL */
603
		b = expr_eval (expr->binary.value_b, pos,
604
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
605

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

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

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

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

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

Morten Welinder's avatar
Morten Welinder committed
645 646
			value_release (a);
			value_release (b);
647

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

783 784 785 786 787 788 789 790 791 792 793 794 795 796
		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
797 798
			value_release (a);
			value_release (b);
799 800
		}

Morten Welinder's avatar
Morten Welinder committed
801
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
802

803 804 805 806 807 808 809 810
	case OPER_FUNCALL: {
		FunctionEvalInfo ei;
		ei.func_def = expr->func.func;
		ei.pos = pos;

		/*if (flags & EVAL_PERMIT_NON_SCALAR)*/
		return function_call_with_list (&ei, expr->func.arg_list);
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
811

Michael Meeks's avatar
Michael Meeks committed
812
	case OPER_NAME:
813 814 815
		if (expr->name.name->active)
			return expr_name_eval (expr->name.name, pos, flags);
		return value_new_error (pos, gnumeric_err_REF);
Michael Meeks's avatar
Michael Meeks committed
816

817
	case OPER_VAR: {
818
		CellRef const * const ref = &expr->var.ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
819
		Cell *cell;
820
		CellPos dest;
821

822
		cellref_get_abs_pos (ref, &pos->eval, &dest);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
823

824 825
		cell = sheet_cell_get (eval_sheet (ref->sheet, pos->sheet),
			dest.col, dest.row);
826 827
		if (cell == NULL)
			return NULL;
828

Jody Goldberg's avatar
Jody Goldberg committed
829
		cell_eval (cell);
830

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

834
	case OPER_CONSTANT:
835
		res = expr->constant.value;
836 837
		if (res->type != VALUE_CELLRANGE)
			return value_duplicate (res);
838
		if (flags & EVAL_PERMIT_NON_SCALAR) {
Jody Goldberg's avatar
Jody Goldberg committed
839 840
			workbook_foreach_cell_in_range (pos, res, TRUE,
							cb_range_eval, NULL);
841 842 843 844 845 846 847 848 849
			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.
			 */
850 851
			CellRef const * const ref_a = & res->v_range.cell.a;
			CellRef const * const ref_b = & res->v_range.cell.b;
852 853
			gboolean found = FALSE;

854 855
			if (ref_a->sheet == ref_b->sheet) {
				CellPos a, b;
856 857 858
				int c = pos->eval.col;
				int r = pos->eval.row;

859 860 861 862 863
				cellref_get_abs_pos (ref_a, &pos->eval, &a);
				cellref_get_abs_pos (ref_b, &pos->eval, &b);
				if (a.row == b.row) {
					if (a.col <= c && c <= b.col) {
						r = a.row;
864 865
						found = TRUE;
					}
866 867 868
				} else if (a.col == b.col) {
					if (a.row <= r && r <= b.row) {
						c = a.col;
869 870 871 872 873 874 875 876
						found = TRUE;
					}
				}
				if (found) {
					Cell * cell = sheet_cell_get (pos->sheet, c, r);
					if (cell == NULL)
						return NULL;

Jody Goldberg's avatar
Jody Goldberg committed
877
					cell_eval (cell);
878 879 880 881 882 883

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

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

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

			/* Store real result (cast away const)*/
919
			*((Value **)&(expr->array.corner.value)) = a;
920
		} else {
921 922 923 924 925 926
			Cell *corner = expr_tree_array_corner (expr,
				pos->sheet, &pos->eval);
			if (corner != NULL) {
				cell_eval (corner);
				a = corner->base.expression->array.corner.value;
			} else
927
				a = NULL;
928
		}
929

930 931
		if (a != NULL &&
		    (a->type == VALUE_CELLRANGE || a->type == VALUE_ARRAY)) {
932 933
			int const num_x = value_area_get_width (pos, a);
			int const num_y = value_area_get_height (pos, a);
934

935
			/* Evaluate relative to the upper left corner */
936
			EvalPos tmp_ep = *pos;