expr.c 40.9 KB
Newer Older
Miguel de Icaza's avatar
Miguel de Icaza committed
1
/*
Morten Welinder's avatar
Morten Welinder committed
2
 * expr.c: Expression evaluation in Gnumeric
Miguel de Icaza's avatar
Miguel de Icaza committed
3 4 5 6
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org).
 */
7
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
8
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
9
#include <math.h>
10
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "expr.h"
13
#include "expr-name.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
14
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
15
#include "format.h"
16
#include "func.h"
Jody Goldberg's avatar
Jody Goldberg committed
17
#include "cell.h"
18
#include "parse-util.h"
19
#include "ranges.h"
20
#include "number-match.h"
21
#include "workbook.h"
22

23 24
/***************************************************************************/

25
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
26
expr_tree_new_constant (Value *v)
27
{
28
	ExprConstant *ans;
29

30
	ans = g_new (ExprConstant, 1);
31 32
	if (!ans)
		return NULL;
33

34
	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
35
	*((Operation *)&(ans->oper)) = OPER_CONSTANT;
36
	ans->value = v;
37

38
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
39 40
}

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

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

Jody Goldberg's avatar
Jody Goldberg committed
54
	func_ref (func);
55 56 57 58
	return expr_tree_new_funcall (func, args);
}

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

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

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

	return (ExprTree *)ans;
}
76

Morten Welinder's avatar
Morten Welinder committed
77 78 79
ExprTree *
expr_tree_new_unary  (Operation op, ExprTree *e)
{
80
	ExprUnary *ans;
Morten Welinder's avatar
Morten Welinder committed
81

82
	ans = g_new (ExprUnary, 1);
83 84 85 86
	if (!ans)
		return NULL;

	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
87
	*((Operation *)&(ans->oper)) = op;
88
	ans->value = e;
89

90
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
91 92 93 94 95 96
}


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

99
	ans = g_new (ExprBinary, 1);
100 101
	if (!ans)
		return NULL;
102

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

108
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
109 110 111
}

ExprTree *
112
expr_tree_new_name (NamedExpression const *name)
Morten Welinder's avatar
Morten Welinder committed
113
{
114
	ExprName *ans;
Morten Welinder's avatar
Morten Welinder committed
115

116
	ans = g_new (ExprName, 1);
117 118
	if (!ans)
		return NULL;
119

120
	ans->ref_count = 1;
121 122
	*((Operation *)&(ans->oper)) = OPER_NAME;
	ans->name = name;
123

124
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
125
}
126 127 128 129 130 131 132 133 134

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

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	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;
151

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

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

171
	return expr->constant.value->v_int.val;
172 173
}

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

182
	return expr->constant.value->v_str.val->str;
183 184
}

185
ExprTree *
186
expr_parse_string (char const *expr, ParsePos const *pp,
187
		   StyleFormat **desired_format, char **error_msg)
188 189 190 191
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

192
	switch (gnumeric_expr_parser (expr, pp, TRUE, FALSE, desired_format, &tree)) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	case PARSE_OK:
		*error_msg = NULL;
		return tree;

	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;

	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
	default:
		g_assert_not_reached ();
		*error_msg = _("Impossible!");
		break;
	}
	return NULL;
}

212 213

static ExprTree *
214 215
expr_tree_array_formula_corner (ExprTree const *expr,
				Sheet const *sheet, CellPos const *pos)
216
{
217
	Cell *corner;
218

219
	g_return_val_if_fail (pos != NULL, NULL);
220

221 222 223
	corner = sheet_cell_get (sheet,
				 pos->col - expr->array.x,
				 pos->row - expr->array.y);
224

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

233
	return corner->base.expression;
234 235
}

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

246
	tree->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
247 248 249 250 251
}

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

255
	switch (tree->any.oper){
256
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
257
		break;
258

259
	case OPER_CONSTANT:
260
		value_release (tree->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
261
		break;
262

Morten Welinder's avatar
Morten Welinder committed
263 264 265
	case OPER_FUNCALL: {
		GList *l;

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

Michael Meeks's avatar
Michael Meeks committed
273 274 275
	case OPER_NAME:
		break;

276
	case OPER_ANY_BINARY:
277 278
		do_expr_tree_unref (tree->binary.value_a);
		do_expr_tree_unref (tree->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
279 280
		break;

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

Morten Welinder's avatar
Morten Welinder committed
296
	g_free (tree);
297 298
}

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

Arturo Espinosa's avatar
Arturo Espinosa committed
311
	do_expr_tree_unref (tree);
312 313
}

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

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

334
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
335
	g_return_val_if_fail (tree != NULL, NULL);
336

Jody Goldberg's avatar
Jody Goldberg committed
337
	fd = tree->func.func;
338 339
	ei.func_def = fd;
	ei.pos = pos;
340
	args = tree->func.arg_list;
341

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

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

	/* handle inverted ranges */
	range_ref_normalize  (&rng, &start_sheet, &end_sheet, v, pos);

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

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

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

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

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

Michael Meeks's avatar
Michael Meeks committed
436
	if (b->sheet && a->sheet != b->sheet) {
437 438 439 440 441 442 443 444 445
		g_warning ("3D references not-fully supported.\n"
			   "Recalc may be incorrect");
		return;
	}

	for (r = start_row; r <= end_row; ++r)
		for (c = start_col; c <= end_col; ++c) {
			if ((cell = sheet_cell_get (sheet, c, r)) == NULL)
				continue;
446
			if (cell->base.generation != gen)
447
				cell_eval (cell);
448 449 450
		}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Jody Goldberg's avatar
Jody Goldberg committed
733
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
734 735
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
736
	        /* Garantees that a != NULL */
737
		a = eval_expr (pos, tree->unary.value, 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);
744 745 746 747
		} else if (a->type == VALUE_CELLRANGE) {
			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 (tree->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 (tree->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 774
		char *sa, *sb, *tmp;

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

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

790 791 792 793
		g_free (sa);
		g_free (sb);
		g_free (tmp);

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

801
	case OPER_FUNCALL:
Jody Goldberg's avatar
Jody Goldberg committed
802
		return eval_funcall (pos, tree, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
803

Michael Meeks's avatar
Michael Meeks committed
804
	case OPER_NAME:
805
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
806

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

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

816
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
817
		cell = sheet_cell_get (cell_sheet, col, row);
818 819
		if (cell == NULL)
			return NULL;
820

821
		if (cell->base.generation != pos->sheet->workbook->generation)
822
			cell_eval (cell);
823

824
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
825
	}
826

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

869
					if (cell->base.generation != pos->sheet->workbook->generation)
870 871 872 873 874 875 876
						cell_eval (cell);

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

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

889 890 891 892
			/*
			 * 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
893
			 *
894 895
			 * Figure out when to iterate and when to do array
			 * operations.
Jody Goldberg's avatar
Jody Goldberg committed
896
			 * ie
897 898
			 * 	A1:A3 = '=B1:B3^2'
			 * Will iterate over all the elements and re-evaluate.
Jody Goldberg's avatar
Jody Goldberg committed
899
			 * whereas
900 901
			 *	 A1:A3 = '=bob(B1:B3)'
			 * Will call bob once if it returns an array.
Jody Goldberg's avatar
Jody Goldberg committed
902
			 *
903 904 905 906
			 * 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
907
			 */