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

24 25
/***************************************************************************/

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}
77

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}

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

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

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

164
int
165
expr_tree_get_const_int (ExprTree const *expr)
166 167
{
	g_return_val_if_fail (expr != NULL, 0);
168 169 170
	g_return_val_if_fail (expr->any.oper == OPER_CONSTANT, 0);
	g_return_val_if_fail (expr->constant.value, 0);
	g_return_val_if_fail (expr->constant.value->type == VALUE_INTEGER, 0);
171

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

175
char const *
176
expr_tree_get_const_str (ExprTree const *expr)
177 178
{
	g_return_val_if_fail (expr != NULL, NULL);
179 180 181
	g_return_val_if_fail (expr->any.oper == OPER_CONSTANT, NULL);
	g_return_val_if_fail (expr->constant.value, NULL);
	g_return_val_if_fail (expr->constant.value->type == VALUE_STRING, NULL);
182

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

186
ExprTree *
187
expr_parse_string (char const *expr_text, ParsePos const *pp,
188
		   StyleFormat **desired_format, char **error_msg)
189
{
190 191
	ExprTree   *tree;
	ParseError  perr;
192

193
	g_return_val_if_fail (expr_text != NULL, NULL);
194

195 196
	tree = gnumeric_expr_parser (expr_text, pp, TRUE, FALSE, desired_format,
				     parse_error_init (&perr));
197

198 199
	/* TODO : use perr when we populate it */
	if (tree == NULL)
200
		*error_msg = perr.message;
201 202 203 204
	else
		*error_msg = NULL;
	parse_error_free (&perr);
	return tree;
205 206
}

207 208

static ExprTree *
209 210
expr_tree_array_formula_corner (ExprTree const *expr,
				Sheet const *sheet, CellPos const *pos)
211
{
212
	Cell *corner;
213

214
	g_return_val_if_fail (pos != NULL, NULL);
215

216 217 218
	corner = sheet_cell_get (sheet,
				 pos->col - expr->array.x,
				 pos->row - expr->array.y);
219

220
	/* Sanity check incase the corner gets removed for some reason */
221
	g_return_val_if_fail (corner != NULL, NULL);
222
	g_return_val_if_fail (cell_has_expr (corner), NULL);
223 224 225 226
	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);
227

228
	return corner->base.expression;
229 230
}

Arturo Espinosa's avatar
Arturo Espinosa committed
231 232 233 234
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
235 236 237 238
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
239
	g_return_if_fail (tree->any.ref_count > 0);
240

241
	tree->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
242 243 244 245 246
}

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

250
	switch (tree->any.oper){
251
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
252
		break;
253

254
	case OPER_CONSTANT:
255
		value_release (tree->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
256
		break;
257

Morten Welinder's avatar
Morten Welinder committed
258 259 260
	case OPER_FUNCALL: {
		GList *l;

261
		for (l = tree->func.arg_list; l; l = l->next)
Morten Welinder's avatar
Morten Welinder committed
262
			do_expr_tree_unref (l->data);
263
		g_list_free (tree->func.arg_list);
Jody Goldberg's avatar
Jody Goldberg committed
264
		func_unref (tree->func.func);
Arturo Espinosa's avatar
Arturo Espinosa committed
265
		break;
Morten Welinder's avatar
Morten Welinder committed
266
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
267

Michael Meeks's avatar
Michael Meeks committed
268 269 270
	case OPER_NAME:
		break;

271
	case OPER_ANY_BINARY:
272 273
		do_expr_tree_unref (tree->binary.value_a);
		do_expr_tree_unref (tree->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
274 275
		break;

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

Morten Welinder's avatar
Morten Welinder committed
291
	g_free (tree);
292 293
}

Morten Welinder's avatar
Morten Welinder committed
294 295 296 297 298 299
/*
 * 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.)
 */
300 301 302 303
void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
304
	g_return_if_fail (tree->any.ref_count > 0);
305

Arturo Espinosa's avatar
Arturo Espinosa committed
306
	do_expr_tree_unref (tree);
307 308
}

Jody Goldberg's avatar
Jody Goldberg committed
309 310 311 312 313 314 315 316 317 318 319 320
/**
 * 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);
}

321
static Value *
Jody Goldberg's avatar
Jody Goldberg committed
322 323
eval_funcall (EvalPos const *pos, ExprTree const *tree,
	      ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
324
{
325
	FunctionEvalInfo ei;
Arturo Espinosa's avatar
Arturo Espinosa committed
326
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
327
	GList *args;
328

329
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
330
	g_return_val_if_fail (tree != NULL, NULL);
331

Jody Goldberg's avatar
Jody Goldberg committed
332
	fd = tree->func.func;
333 334
	ei.func_def = fd;
	ei.pos = pos;
335
	args = tree->func.arg_list;
336

Jody Goldberg's avatar
Jody Goldberg committed
337
	/*if (flags & EVAL_PERMIT_NON_SCALAR)*/
338
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
339 340
}

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

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

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/**
 * 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;
}

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

428 429
	cell_get_abs_col_row (a, &pos->eval, &start_col, &start_row);
	cell_get_abs_col_row (b, &pos->eval, &end_col, &end_row);
430

Michael Meeks's avatar
Michael Meeks committed
431
	if (b->sheet && a->sheet != b->sheet) {
432 433 434 435 436 437 438 439 440
		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;
441
			if (cell->base.generation != gen)
442
				cell_eval (cell);
443 444 445
		}
}

446
static Value *
447
eval_expr_real (EvalPos const *pos, ExprTree const *tree,
448
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
449
{
450
	Value *res = NULL, *a = NULL, *b = NULL;
451

Arturo Espinosa's avatar
Arturo Espinosa committed
452
	g_return_val_if_fail (tree != NULL, NULL);
453
	g_return_val_if_fail (pos != NULL, NULL);
454

455
	switch (tree->any.oper){
456 457 458 459 460 461
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
462
		ValueCompare comp;
463

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

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

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

501 502 503 504
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
505

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

516
			return value_new_error (pos, gnumeric_err_VALUE);
517
		}
518

519
		switch (tree->any.oper) {
520
		case OPER_EQUAL:
521
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
522 523
			break;

524
		case OPER_GT:
525
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
526 527
			break;

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

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

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

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

Arturo Espinosa's avatar
Arturo Espinosa committed
544
		default:
545
			g_assert_not_reached ();
546
			res = value_new_error (pos,
547
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
548
		}
Morten Welinder's avatar
Morten Welinder committed
549
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
550
	}
551

552 553 554 555 556
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
557 558 559 560 561 562 563 564 565
		/*
		 * 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
		 */

566
	        /* Guarantees a != NULL */
567
		a = eval_expr (pos, tree->binary.value_a,
568
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
569 570 571

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

581 582
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
583
			return value_new_error_err (pos, &a->v_err);
584

585
		/* 2) #!VALUE error if A is not a number */
586
		if (a->type == VALUE_STRING) {
587
			Value *tmp = format_match (a->v_str.val->str, NULL, NULL);
588 589 590 591 592 593

			value_release (a);
			if (tmp == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
			a = tmp;
		} else if (!VALUE_IS_NUMBER (a)) {
594
			value_release (a);
595
			return value_new_error (pos, gnumeric_err_VALUE);
596 597
		}

598
	        /* Guarantees that b != NULL */
599
		b = eval_expr (pos, tree->binary.value_b,
600
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
601

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

613 614
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
615
			value_release (a);
616
			return value_new_error_err (pos, &b->v_err);
Arturo Espinosa's avatar
Arturo Espinosa committed
617
		}
618

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

			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)) {
630 631
			value_release (a);
			value_release (b);
632
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
633
		}
634

635
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
636
			int ia = value_get_as_int (a);
637
			int ib = value_get_as_int (b);
638 639 640
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
641 642
			value_release (a);
			value_release (b);
643

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

655
			case OPER_SUB:
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_MULT:
664 665 666 667 668
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
669
					return value_new_float ((gnum_float) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
670

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

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

Arturo Espinosa's avatar
Arturo Espinosa committed
691
			default:
692
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
693 694
			}
		} else {
695 696
			gnum_float const va = value_get_as_float (a);
			gnum_float const vb = value_get_as_float (b);
697 698
			value_release (a);
			value_release (b);
699

700
			switch (tree->any.oper){
701
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
702
				return value_new_float (va + vb);
703

704
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
705
				return value_new_float (va - vb);
706

707
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
708
				return value_new_float (va * vb);
709

710
			case OPER_DIV:
711
				return (vb == 0.0)
712
				    ? value_new_error (pos,
713
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
714
				    : value_new_float (va / vb);
715

716
			case OPER_EXP:
717 718
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
719
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
720
				return value_new_float (pow (va, vb));
721

Arturo Espinosa's avatar
Arturo Espinosa committed
722
			default:
723
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
724 725
			}
		}
726
		return value_new_error (pos, _("Unknown operator"));
727

Jody Goldberg's avatar
Jody Goldberg committed
728
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
729 730
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
731
	        /* Garantees that a != NULL */
732
		a = eval_expr (pos, tree->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
733 734 735

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
736
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
737
			if (a == NULL)
738
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
739
		} else if (a->type == VALUE_ARRAY) {
740 741 742
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
743 744
		}

745 746
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
747

748
		if (tree->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
749 750
			return a;

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

767
	case OPER_CONCAT: {
768 769
		char *sa, *sb, *tmp;

770
		a = eval_expr_real (pos, tree->binary.value_a, flags);
771
		if (a != NULL && a->type == VALUE_ERROR)
772
			return a;
773
		b = eval_expr_real (pos, tree->binary.value_b, flags);
774 775 776
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
777
			return b;
778 779
		}

Michael Meeks's avatar
Michael Meeks committed
780 781
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
782
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
783 784
		res = value_new_string (tmp);

785 786 787 788
		g_free (sa);
		g_free (sb);
		g_free (tmp);

789
		if (a != NULL)
790
		value_release (a);
791
		if (b != NULL)
792
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
793
		return res;
794
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
795

796
	case OPER_FUNCALL:
Jody Goldberg's avatar
Jody Goldberg committed
797
		return eval_funcall (pos, tree, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
798

Michael Meeks's avatar
Michael Meeks committed
799
	case OPER_NAME:
800
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
801

802
	case OPER_VAR: {
803
		Sheet *cell_sheet;
804
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
805 806
		Cell *cell;
		int col, row;
807

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

811
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
812
		cell = sheet_cell_get (cell_sheet, col, row);
813 814
		if (cell == NULL)
			return NULL;
815

816
		if (cell->base.generation != pos->sheet->workbook->generation)
817
			cell_eval (cell);
818

819
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
820
	}
821

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

864
					if (cell->base.generation != pos->sheet->workbook->generation)
865 866 867 868 869 870 871
						cell_eval (cell);

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

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

884 885 886 887
			/*
			 * 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
888
			 *