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

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

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}
79

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

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

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

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


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

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

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

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

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

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

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

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

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

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

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_VAR;
	ans->ref = *cr;

	return (ExprTree *)ans;
}

ExprTree *
expr_tree_new_array (int x, int y, int rows, int cols)
{
	ExprArray *ans;

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

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

166 167 168 169 170 171 172 173
/**
 * 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.
 **/
174
ExprTree *
175
expr_parse_string (char const *expr_text, ParsePos const *pp,
176
		   StyleFormat **desired_format, ParseError *error)
177
{
178
	ExprTree   *expr;
Morten Welinder's avatar
Morten Welinder committed
179 180
	ParseError errrec;
	ParseError *perr = error ? error : &errrec;
181

182
	g_return_val_if_fail (expr_text != NULL, NULL);
183

184
	expr = gnumeric_expr_parser (expr_text, pp, TRUE, FALSE, desired_format,
185 186 187 188 189
				     parse_error_init (perr));

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

190
	return expr;
191 192
}

193

194 195 196
static Cell *
expr_tree_array_corner (ExprTree const *expr,
			Sheet const *sheet, CellPos const *pos)
197
{
198 199
	Cell *corner = sheet_cell_get (sheet,
		pos->col - expr->array.x, pos->row - expr->array.y);
200

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

209
	return corner;
210 211
}

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

222
	expr->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
223 224 225
}

static void
226
do_expr_tree_unref (ExprTree *expr)
Arturo Espinosa's avatar
Arturo Espinosa committed
227
{
228
	if (--expr->any.ref_count > 0)
Morten Welinder's avatar
Morten Welinder committed
229 230
		return;

231
	switch (expr->any.oper){
232
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
233
		break;
234

235
	case OPER_CONSTANT:
236
		value_release (expr->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
237
		break;
238

Morten Welinder's avatar
Morten Welinder committed
239 240 241
	case OPER_FUNCALL: {
		GList *l;

242
		for (l = expr->func.arg_list; l; l = l->next)
Morten Welinder's avatar
Morten Welinder committed
243
			do_expr_tree_unref (l->data);
244 245
		g_list_free (expr->func.arg_list);
		func_unref (expr->func.func);
Arturo Espinosa's avatar
Arturo Espinosa committed
246
		break;
Morten Welinder's avatar
Morten Welinder committed
247
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
248

Michael Meeks's avatar
Michael Meeks committed
249 250 251
	case OPER_NAME:
		break;

252
	case OPER_ANY_BINARY:
253 254
		do_expr_tree_unref (expr->binary.value_a);
		do_expr_tree_unref (expr->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
255 256
		break;

257
	case OPER_ANY_UNARY:
258
		do_expr_tree_unref (expr->unary.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
259
		break;
260
	case OPER_ARRAY:
261 262 263 264
		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);
265
		}
266
		break;
267 268 269
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
270
	}
271

272
	g_free (expr);
273 274
}

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

287
	do_expr_tree_unref (expr);
288 289
}

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

299
	return (expr->any.ref_count > 1);
Jody Goldberg's avatar
Jody Goldberg committed
300 301
}

Jody Goldberg's avatar
Jody Goldberg committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
static gboolean
cellref_equal (CellRef const *a, CellRef const *b)
{
	return	(a->col_relative == b->col_relative) &&
		(a->row_relative == b->row_relative) &&
		(a->col == b->col) &&
		(a->row == b->row) &&
		(a->sheet == b->sheet);
}

/**
 * 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);

	case OPER_FUNCALL: {
		GList *la = a->func.arg_list;
		GList *lb = b->func.arg_list;
		for (; la != NULL && lb != NULL; la = la->next, lb =lb->next)
			if (!expr_tree_equal (la->data, lb->data))
				return FALSE;
		return (la == NULL) && (lb == NULL);
	}

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

	default :
		g_assert_not_reached ();
	}

	return FALSE;
}

385
static Value *
386
eval_funcall (EvalPos const *pos, ExprTree const *expr,
Jody Goldberg's avatar
Jody Goldberg committed
387
	      ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
388
{
389
	FunctionEvalInfo ei;
Arturo Espinosa's avatar
Arturo Espinosa committed
390
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
391
	GList *args;
392

393
	g_return_val_if_fail (pos != NULL, NULL);
394
	g_return_val_if_fail (expr != NULL, NULL);
395

396
	fd = expr->func.func;
397 398
	ei.func_def = fd;
	ei.pos = pos;
399
	args = expr->func.arg_list;
400

Jody Goldberg's avatar
Jody Goldberg committed
401
	/*if (flags & EVAL_PERMIT_NON_SCALAR)*/
402
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
403 404
}

Jody Goldberg's avatar
Jody Goldberg committed
405 406 407 408
/**
 * expr_implicit_intersection :
 * @ei: EvalInfo containing valid fd!
 * @v: a VALUE_CELLRANGE
409
 *
410 411 412 413 414
 * 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
415 416 417
 *
 * Always release the value passed in.
 *
418
 * Return value:
Jody Goldberg's avatar
Jody Goldberg committed
419 420 421 422
 *     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
423
expr_implicit_intersection (EvalPos const *pos, Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
424 425
{
	Value *res = NULL;
426 427 428 429
	Range rng;
	Sheet *start_sheet, *end_sheet;

	/* handle inverted ranges */
Jody Goldberg's avatar
Jody Goldberg committed
430
	value_cellrange_normalize (pos, v, &start_sheet, &end_sheet, &rng);
431 432 433

	if (start_sheet == end_sheet) {
		if (rng.start.row == rng.end.row) {
Jody Goldberg's avatar
Jody Goldberg committed
434
			int const c = pos->eval.col;
435 436 437 438 439
			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
440 441
		}

442
		if (rng.start.col == rng.end.col) {
Jody Goldberg's avatar
Jody Goldberg committed
443
			int const r = pos->eval.row;
444 445 446 447
			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
448 449 450 451 452 453
		}
	}
	value_release (v);
	return res;
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
/**
 * 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
476 477
static Value *
cb_range_eval (Sheet *sheet, int col, int row, Cell *cell, void *ignore)
478
{
Jody Goldberg's avatar
Jody Goldberg committed
479 480
	cell_eval (cell);
	return NULL;
481 482
}

483
static Value *
484
eval_expr_real (EvalPos const *pos, ExprTree const *expr,
485
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
486
{
487
	Value *res = NULL, *a = NULL, *b = NULL;
488

489
	g_return_val_if_fail (expr != NULL, NULL);
490
	g_return_val_if_fail (pos != NULL, NULL);
491

492
	switch (expr->any.oper){
493 494 495 496 497 498
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
499
		ValueCompare comp;
500

501
		a = eval_expr_real (pos, expr->binary.value_a, flags);
Jody Goldberg's avatar
Jody Goldberg committed
502 503
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
504
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
505
				if (a == NULL)
506
					return value_new_error (pos, gnumeric_err_VALUE);
507 508 509 510
			} 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
511 512 513
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
514

515
		b = eval_expr_real (pos, expr->binary.value_b, flags);
Jody Goldberg's avatar
Jody Goldberg committed
516 517 518
		if (b != NULL) {
			Value *res = NULL;
			if (b->type == VALUE_CELLRANGE) {
519
				b = expr_implicit_intersection (pos, b);
Jody Goldberg's avatar
Jody Goldberg committed
520
				if (b == NULL)
521
					res = value_new_error (pos, gnumeric_err_VALUE);
522 523 524 525
			} 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
526 527 528 529 530 531 532 533
			} 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
534
		}
535

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

538 539 540 541
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
542

Michael Meeks's avatar
Michael Meeks committed
543
		if (comp == TYPE_MISMATCH) {
544 545 546 547
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
548
			if (expr->any.oper == OPER_EQUAL)
549
				return value_new_bool (FALSE);
550
			if (expr->any.oper == OPER_NOT_EQUAL)
551 552
				return value_new_bool (TRUE);

553
			return value_new_error (pos, gnumeric_err_VALUE);
554
		}
555

556
		switch (expr->any.oper) {
557
		case OPER_EQUAL:
558
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
559 560
			break;

561
		case OPER_GT:
562
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
563 564
			break;

565
		case OPER_LT:
566
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
567 568
			break;

569 570
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
571 572
			break;

573 574
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
575 576
			break;

577 578
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
579
			break;
580

Arturo Espinosa's avatar
Arturo Espinosa committed
581
		default:
582
			g_assert_not_reached ();
583
			res = value_new_error (pos,
584
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
585
		}
Morten Welinder's avatar
Morten Welinder committed
586
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
587
	}
588

589 590 591 592 593
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
594 595 596 597 598 599 600 601 602
		/*
		 * 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
		 */

603
	        /* Guarantees a != NULL */
604
		a = eval_expr (pos, expr->binary.value_a,
605
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
606 607 608

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

618 619
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
620
			return value_error_set_pos (&a->v_err, pos);
621

622
		/* 2) #!VALUE error if A is not a number */
623
		if (a->type == VALUE_STRING) {
624
			Value *tmp = format_match (a->v_str.val->str, NULL, NULL);
625 626 627 628 629 630

			value_release (a);
			if (tmp == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
			a = tmp;
		} else if (!VALUE_IS_NUMBER (a)) {
631
			value_release (a);
632
			return value_new_error (pos, gnumeric_err_VALUE);
633 634
		}

635
	        /* Guarantees that b != NULL */
636
		b = eval_expr (pos, expr->binary.value_b,
637
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
638

Jody Goldberg's avatar
Jody Goldberg committed
639 640
		/* Handle implicit intersection */
		if (b->type == VALUE_CELLRANGE) {
641
			b = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
642
			if (b == NULL)
643
				return value_new_error (pos, gnumeric_err_VALUE);
644 645 646 647
		} 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
648 649
		}

650 651
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
652
			value_release (a);
653
			return value_error_set_pos (&b->v_err, pos);
Arturo Espinosa's avatar
Arturo Espinosa committed
654
		}
655

656
		/* 4) #!VALUE error if B is not a number */
657
		if (b->type == VALUE_STRING) {
658
			Value *tmp = format_match (b->v_str.val->str, NULL, NULL);
659 660 661 662 663 664 665 666

			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)) {
667 668
			value_release (a);
			value_release (b);
669
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
670
		}
671

672
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
673
			int ia = value_get_as_int (a);
674
			int ib = value_get_as_int (b);
675 676 677
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
678 679
			value_release (a);
			value_release (b);
680

681 682
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
683
			switch (expr->any.oper){
684 685 686 687 688 689
			case OPER_ADD:
				dres = (double)ia + (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
690
					return value_new_float ((gnum_float) dres);
691

692
			case OPER_SUB:
693 694 695 696 697
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
698
					return value_new_float ((gnum_float) dres);
699

700
			case OPER_MULT:
701 702 703 704 705
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
706
					return value_new_float ((gnum_float) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
707

708
			case OPER_DIV:
709
				if (ib == 0)
710
					return value_new_error (pos, gnumeric_err_DIV0);
711 712 713 714 715
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
716
					return value_new_float ((gnum_float) dres);
717 718

			case OPER_EXP:
719
				if (ia == 0 && ib <= 0)
720
					return value_new_error (pos, gnumeric_err_NUM);
721 722 723 724 725
				dres = pow ((double)ia, (double)ib);
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
726
					return value_new_float ((gnum_float) dres);
727

Arturo Espinosa's avatar
Arturo Espinosa committed
728
			default:
729
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
730 731
			}
		} else {
732 733
			gnum_float const va = value_get_as_float (a);
			gnum_float const vb = value_get_as_float (b);
734 735
			value_release (a);
			value_release (b);
736

737
			switch (expr->any.oper){
738
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
739
				return value_new_float (va + vb);
740

741
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
742
				return value_new_float (va - vb);
743

744
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
745
				return value_new_float (va * vb);
746

747
			case OPER_DIV:
748
				return (vb == 0.0)
749
				    ? value_new_error (pos,
750
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
751
				    : value_new_float (va / vb);
752

753
			case OPER_EXP:
754 755
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
756
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
757
				return value_new_float (pow (va, vb));
758

Arturo Espinosa's avatar
Arturo Espinosa committed
759
			default:
760
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
761 762
			}
		}
763
		return value_new_error (pos, _("Unknown operator"));
764

Jody Goldberg's avatar
Jody Goldberg committed
765
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
766 767
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
768
	        /* Garantees that a != NULL */
769
		a = eval_expr (pos, expr->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
770 771 772

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
773
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
774
			if (a == NULL)
775
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
776
		} else if (a->type == VALUE_ARRAY) {
777 778 779
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
780 781
		}

782 783
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
784

785
		if (expr->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
786 787
			return a;

788 789
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
790
			return value_new_error (pos, gnumeric_err_VALUE);
791
		}
792
		if (expr->any.oper == OPER_UNARY_NEG) {
Jody Goldberg's avatar
Jody Goldberg committed
793
			if (a->type == VALUE_INTEGER)
794
				res = value_new_int (-a->v_int.val);
Jody Goldberg's avatar
Jody Goldberg committed
795
			else if (a->type == VALUE_FLOAT)
796
				res = value_new_float (-a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
797
			else
798
				res = value_new_bool (!a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
799 800
		} else
			res = value_new_float (value_get_as_float (a) * .01);
801
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
802
		return res;
803

804
	case OPER_CONCAT:
805
		a = eval_expr_real (pos, expr->binary.value_a, flags);
806
		if (a != NULL && a->type == VALUE_ERROR)
807
			return a;
808
		b = eval_expr_real (pos, expr->binary.value_b, flags);
809 810 811
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
812
			return b;
813 814
		}

815 816 817 818 819 820 821 822 823 824 825 826 827 828
		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
829 830
			value_release (a);
			value_release (b);
831 832
		}

Morten Welinder's avatar
Morten Welinder committed
833
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
834

835
	case OPER_FUNCALL:
836
		return eval_funcall (pos, expr, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
837

Michael Meeks's avatar
Michael Meeks committed
838
	case OPER_NAME:
839
		return eval_expr_name (pos, expr->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
840

841
	case OPER_VAR: {
842
		Sheet *cell_sheet;
843
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
844 845
		Cell *cell;
		int col, row;
846

847
		ref = &expr->var.ref;
848
		cell_get_abs_col_row (ref, &pos->eval, &col, &row);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
849

850
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
851
		cell = sheet_cell_get (cell_sheet, col, row);
852 853
		if (cell == NULL)
			return NULL;
854

Jody Goldberg's avatar
Jody Goldberg committed
855
		cell_eval (cell);
856

857
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
858
	}
859

860
	case OPER_CONSTANT:
861
		res = expr->constant.value;
862 863
		if (res->type != VALUE_CELLRANGE)
			return value_duplicate (res);
864
		if (flags & EVAL_PERMIT_NON_SCALAR) {
Jody Goldberg's avatar
Jody Goldberg committed
865 866
			workbook_foreach_cell_in_range (pos, res, TRUE,
							cb_range_eval, NULL);
867 868 869 870 871 872 873 874 875
			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.
			 */
876 877
			CellRef const * const a = & res->v_range.cell.a;
			CellRef const * const b = & res->v_range.cell.b;
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
			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
903
					cell_eval (cell);
904 905 906 907 908 909

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

911 912 913
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
914 915
		int x = expr->array.x;
		int y = expr->array.y;
916
		if (x == 0 && y == 0){
917
			/* Release old value if necessary */
918
			a = expr->array.corner.value;
919 920 921
			if (a != NULL)
				value_release (a);

922 923 924 925
			/*
			 * 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
926
			 *
927 928
			 * Figure out when to iterate and when to do array
			 * operations.
Jody Goldberg's avatar
Jody Goldberg committed
929
			 * ie
930 931
			 * 	A1:A3 = '=B1:B3^2'
			 * Will iterate over all the elements and re-evaluate.
Jody Goldberg's avatar
Jody Goldberg committed
932
			 * whereas
933 934
			 *	 A1:A3 = '=bob(B1:B3)'
			 * Will call bob once if it returns an array.
Jody Goldberg's avatar
Jody Goldberg committed
935
			 *
936 937 938 939
			 * 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
940
			 */
941
			a = eval_expr_real (pos, expr->array.corner.expr,
Jody Goldberg's avatar
Jody Goldberg committed
942
					    EVAL_PERMIT_NON_SCALAR);
Jody Goldberg's avatar