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 197 198
	tree = gnumeric_expr_parser (expr_text, pp, TRUE, FALSE, desired_format,
				     parse_error_init (&perr));
	/* TODO : use perr when we populate it */
	if (tree == NULL)
199
		*error_msg = _("Syntax error");
200 201 202 203
	else
		*error_msg = NULL;
	parse_error_free (&perr);
	return tree;
204 205
}

206 207

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

654
			case OPER_SUB:
655 656 657 658 659
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
660
					return value_new_float ((gnum_float) dres);
661

662
			case OPER_MULT:
663 664 665 666 667
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
668
					return value_new_float ((gnum_float) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
669

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

883 884 885 886
			/*
			 * 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
887
			 *
888 889
			 * Figure out when to iterate and when to do array
			 * operations.
Jody Goldberg's avatar
Jody Goldberg committed
890
			 * ie
891 892