expr.c 40.6 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
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
9
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
10
#include <math.h>
11
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "expr.h"
14
#include "expr-name.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
15
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
16
#include "format.h"
17
#include "func.h"
Jody Goldberg's avatar
Jody Goldberg committed
18
#include "cell.h"
Jody Goldberg's avatar
Jody Goldberg committed
19 20
#include "sheet.h"
#include "str.h"
21
#include "parse-util.h"
22
#include "ranges.h"
23
#include "number-match.h"
24
#include "workbook.h"
Jeffrey Stedfast's avatar
Jeffrey Stedfast committed
25
#include "gutils.h"
26

27 28
/***************************************************************************/

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}
80

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

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

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

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


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

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

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

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

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

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

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

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

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

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

140 141 142 143 144 145 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 *
expr_tree_new_array (int x, int y, int rows, int cols)
{
	ExprArray *ans;

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

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

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

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

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

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

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

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

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

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

213
	return tree;
214 215
}

216 217

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	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;
}

Jody Goldberg's avatar
Jody Goldberg committed
421 422
static Value *
cb_range_eval (Sheet *sheet, int col, int row, Cell *cell, void *ignore)
423
{
Jody Goldberg's avatar
Jody Goldberg committed
424 425
	cell_eval (cell);
	return NULL;
426 427
}

428
static Value *
429
eval_expr_real (EvalPos const *pos, ExprTree const *tree,
430
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
431
{
432
	Value *res = NULL, *a = NULL, *b = NULL;
433

Arturo Espinosa's avatar
Arturo Espinosa committed
434
	g_return_val_if_fail (tree != NULL, NULL);
435
	g_return_val_if_fail (pos != NULL, NULL);
436

437
	switch (tree->any.oper){
438 439 440 441 442 443
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
444
		ValueCompare comp;
445

446
		a = eval_expr_real (pos, tree->binary.value_a, flags);
Jody Goldberg's avatar
Jody Goldberg committed
447 448
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
449
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
450
				if (a == NULL)
451
					return value_new_error (pos, gnumeric_err_VALUE);
452 453 454 455
			} 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
456 457 458
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
459

460
		b = eval_expr_real (pos, tree->binary.value_b, flags);
Jody Goldberg's avatar
Jody Goldberg committed
461 462 463
		if (b != NULL) {
			Value *res = NULL;
			if (b->type == VALUE_CELLRANGE) {
464
				b = expr_implicit_intersection (pos, b);
Jody Goldberg's avatar
Jody Goldberg committed
465
				if (b == NULL)
466
					res = value_new_error (pos, gnumeric_err_VALUE);
467 468 469 470
			} 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
471 472 473 474 475 476 477 478
			} 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
479
		}
480

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

483 484 485 486
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
487

Michael Meeks's avatar
Michael Meeks committed
488
		if (comp == TYPE_MISMATCH) {
489 490 491 492
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
493
			if (tree->any.oper == OPER_EQUAL)
494
				return value_new_bool (FALSE);
495
			if (tree->any.oper == OPER_NOT_EQUAL)
496 497
				return value_new_bool (TRUE);

498
			return value_new_error (pos, gnumeric_err_VALUE);
499
		}
500

501
		switch (tree->any.oper) {
502
		case OPER_EQUAL:
503
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
504 505
			break;

506
		case OPER_GT:
507
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
508 509
			break;

510
		case OPER_LT:
511
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
512 513
			break;

514 515
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
516 517
			break;

518 519
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
520 521
			break;

522 523
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
524
			break;
525

Arturo Espinosa's avatar
Arturo Espinosa committed
526
		default:
527
			g_assert_not_reached ();
528
			res = value_new_error (pos,
529
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
530
		}
Morten Welinder's avatar
Morten Welinder committed
531
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
532
	}
533

534 535 536 537 538
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
539 540 541 542 543 544 545 546 547
		/*
		 * 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
		 */

548
	        /* Guarantees a != NULL */
549
		a = eval_expr (pos, tree->binary.value_a,
550
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
551 552 553

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
554
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
555
			if (a == NULL)
556
				return value_new_error (pos, gnumeric_err_VALUE);
557 558 559 560
		} 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
561 562
		}

563 564
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
565
			return value_new_error_err (pos, &a->v_err);
566

567
		/* 2) #!VALUE error if A is not a number */
568
		if (a->type == VALUE_STRING) {
569
			Value *tmp = format_match (a->v_str.val->str, NULL, NULL);
570 571 572 573 574 575

			value_release (a);
			if (tmp == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
			a = tmp;
		} else if (!VALUE_IS_NUMBER (a)) {
576
			value_release (a);
577
			return value_new_error (pos, gnumeric_err_VALUE);
578 579
		}

580
	        /* Guarantees that b != NULL */
581
		b = eval_expr (pos, tree->binary.value_b,
582
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
583

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

595 596
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
597
			value_release (a);
598
			return value_new_error_err (pos, &b->v_err);
Arturo Espinosa's avatar
Arturo Espinosa committed
599
		}
600

601
		/* 4) #!VALUE error if B is not a number */
602
		if (b->type == VALUE_STRING) {
603
			Value *tmp = format_match (b->v_str.val->str, NULL, NULL);
604 605 606 607 608 609 610 611

			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)) {
612 613
			value_release (a);
			value_release (b);
614
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
615
		}
616

617
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
618
			int ia = value_get_as_int (a);
619
			int ib = value_get_as_int (b);
620 621 622
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
623 624
			value_release (a);
			value_release (b);
625

626 627
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
628
			switch (tree->any.oper){
629 630 631 632 633 634
			case OPER_ADD:
				dres = (double)ia + (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
635
					return value_new_float ((gnum_float) dres);
636

637
			case OPER_SUB:
638 639 640 641 642
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
643
					return value_new_float ((gnum_float) dres);
644

645
			case OPER_MULT:
646 647 648 649 650
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
651
					return value_new_float ((gnum_float) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
652

653
			case OPER_DIV:
654
				if (ib == 0)
655
					return value_new_error (pos, gnumeric_err_DIV0);
656 657 658 659 660
				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_EXP:
664
				if (ia == 0 && ib <= 0)
665
					return value_new_error (pos, gnumeric_err_NUM);
666 667 668 669 670
				dres = pow ((double)ia, (double)ib);
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
671
					return value_new_float ((gnum_float) dres);
672

Arturo Espinosa's avatar
Arturo Espinosa committed
673
			default:
674
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
675 676
			}
		} else {
677 678
			gnum_float const va = value_get_as_float (a);
			gnum_float const vb = value_get_as_float (b);
679 680
			value_release (a);
			value_release (b);
681

682
			switch (tree->any.oper){
683
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
684
				return value_new_float (va + vb);
685

686
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
687
				return value_new_float (va - vb);
688

689
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
690
				return value_new_float (va * vb);
691

692
			case OPER_DIV:
693
				return (vb == 0.0)
694
				    ? value_new_error (pos,
695
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
696
				    : value_new_float (va / vb);
697

698
			case OPER_EXP:
699 700
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
701
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
702
				return value_new_float (pow (va, vb));
703

Arturo Espinosa's avatar
Arturo Espinosa committed
704
			default:
705
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
706 707
			}
		}
708
		return value_new_error (pos, _("Unknown operator"));
709

Jody Goldberg's avatar
Jody Goldberg committed
710
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
711 712
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
713
	        /* Garantees that a != NULL */
714
		a = eval_expr (pos, tree->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
715 716 717

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
718
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
719
			if (a == NULL)
720
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
721
		} else if (a->type == VALUE_ARRAY) {
722 723 724
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
725 726
		}

727 728
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
729

730
		if (tree->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
731 732
			return a;

733 734
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
735
			return value_new_error (pos, gnumeric_err_VALUE);
736
		}
737
		if (tree->any.oper == OPER_UNARY_NEG) {
Jody Goldberg's avatar
Jody Goldberg committed
738
			if (a->type == VALUE_INTEGER)
739
				res = value_new_int (-a->v_int.val);
Jody Goldberg's avatar
Jody Goldberg committed
740
			else if (a->type == VALUE_FLOAT)
741
				res = value_new_float (-a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
742
			else
743
				res = value_new_bool (!a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
744 745
		} else
			res = value_new_float (value_get_as_float (a) * .01);
746
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
747
		return res;
748

749
	case OPER_CONCAT:
750
		a = eval_expr_real (pos, tree->binary.value_a, flags);
751
		if (a != NULL && a->type == VALUE_ERROR)
752
			return a;
753
		b = eval_expr_real (pos, tree->binary.value_b, flags);
754 755 756
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
757
			return b;
758 759
		}

760 761 762 763 764 765 766 767 768 769 770 771 772 773
		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
774 775
			value_release (a);
			value_release (b);
776 777
		}

Morten Welinder's avatar
Morten Welinder committed
778
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
779

780
	case OPER_FUNCALL:
Jody Goldberg's avatar
Jody Goldberg committed
781
		return eval_funcall (pos, tree, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
782

Michael Meeks's avatar
Michael Meeks committed
783
	case OPER_NAME:
784
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
785

786
	case OPER_VAR: {
787
		Sheet *cell_sheet;
788
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
789 790
		Cell *cell;
		int col, row;
791

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

795
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
796
		cell = sheet_cell_get (cell_sheet, col, row);
797 798
		if (cell == NULL)
			return NULL;
799

Jody Goldberg's avatar
Jody Goldberg committed
800
		cell_eval (cell);
801

802
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
803
	}
804

805
	case OPER_CONSTANT:
806
		res = tree->constant.value;
807 808
		if (res->type != VALUE_CELLRANGE)
			return value_duplicate (res);
809
		if (flags & EVAL_PERMIT_NON_SCALAR) {
Jody Goldberg's avatar
Jody Goldberg committed
810 811
			workbook_foreach_cell_in_range (pos, res, TRUE,
							cb_range_eval, NULL);
812 813 814 815 816 817 818 819 820
			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.
			 */
821 822
			CellRef const * const a = & res->v_range.cell.a;
			CellRef const * const b = & res->v_range.cell.b;
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
			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;

Jody Goldberg's avatar
Jody Goldberg committed
848
					cell_eval (cell);
849 850 851 852 853 854

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

856 857 858
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
859 860
		int x = tree->array.x;
		int y = tree->array.y;
861
		if (x == 0 && y == 0){
862
			/* Release old value if necessary */
863
			a = tree->array.corner.value;
864 865 866
			if (a != NULL)
				value_release (a);

867 868 869 870
			/*
			 * 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
871
			 *
872 873
			 * Figure out when to iterate and when to do array
			 * operations.
Jody Goldberg's avatar
Jody Goldberg committed
874
			 * ie
875 876
			 * 	A1:A3 = '=B1:B3^2'
			 * Will iterate over all the elements and re-evaluate.
Jody Goldberg's avatar
Jody Goldberg committed
877
			 * whereas
878 879
			 *	 A1:A3 = '=bob(B1:B3)'
			 * Will call bob once if it returns an array.
Jody Goldberg's avatar
Jody Goldberg committed
880
			 *
881 882 883 884
			 * 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
885
			 */
886
			a = eval_expr_real (pos, tree->array.corner.expr,
Jody Goldberg's avatar
Jody Goldberg committed
887
					    EVAL_PERMIT_NON_SCALAR);
888 889

			/* Store real result (cast away const)*/
890
			*((Value **)&(tree->array.corner.value)) = a;
891
		} else {
892
			ExprTree const * const array =
893
			    expr_tree_array_formula_corner (tree, pos->sheet, &pos->eval);
894
			if (array)
895
				a = array->array.corner.value;