expr.c 40.5 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Miguel de Icaza's avatar
Miguel de Icaza committed
2
/*
Morten Welinder's avatar
Morten Welinder committed
3
 * expr.c: Expression evaluation in Gnumeric
Miguel de Icaza's avatar
Miguel de Icaza committed
4 5 6 7
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org).
 */
8
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
9
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
10
#include <math.h>
11
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "expr.h"
14
#include "expr-name.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
15
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
16
#include "format.h"
17
#include "func.h"
Jody Goldberg's avatar
Jody Goldberg committed
18
#include "cell.h"
Jody Goldberg's avatar
Jody Goldberg committed
19 20
#include "sheet.h"
#include "str.h"
21
#include "parse-util.h"
22
#include "ranges.h"
23
#include "number-match.h"
24
#include "workbook.h"
Jeffrey Stedfast's avatar
Jeffrey Stedfast committed
25
#include "gutils.h"
26

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

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}
80

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

	return (ExprTree *)ans;
}

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

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

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

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

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

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

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

189 190 191 192 193 194 195 196
/**
 * expr_parse_string:
 *
 * Parse a string. if @error is non-null it will be assumed that the
 * caller has passed a pointer to a ParseError struct AND that it will
 * take responsibility for freeing that struct and it's contents.
 * with parse_error_free.
 **/
197
ExprTree *
198
expr_parse_string (char const *expr_text, ParsePos const *pp,
199
		   StyleFormat **desired_format, ParseError *error)
200
{
201
	ExprTree   *expr;
Morten Welinder's avatar
Morten Welinder committed
202 203
	ParseError errrec;
	ParseError *perr = error ? error : &errrec;
204

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

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

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

213
	return expr;
214 215
}

216

217 218 219
static Cell *
expr_tree_array_corner (ExprTree const *expr,
			Sheet const *sheet, CellPos const *pos)
220
{
221 222
	Cell *corner = sheet_cell_get (sheet,
		pos->col - expr->array.x, pos->row - expr->array.y);
223

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

232
	return corner;
233 234
}

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

245
	expr->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
246 247 248
}

static void
249
do_expr_tree_unref (ExprTree *expr)
Arturo Espinosa's avatar
Arturo Espinosa committed
250
{
251
	if (--expr->any.ref_count > 0)
Morten Welinder's avatar
Morten Welinder committed
252 253
		return;

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

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

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

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

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

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

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

295
	g_free (expr);
296 297
}

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

310
	do_expr_tree_unref (expr);
311 312
}

Jody Goldberg's avatar
Jody Goldberg committed
313 314 315 316 317
/**
 * expr_tree_shared : Returns TRUE if the reference count
 *   for the supplied expression is > 1
 */
gboolean
318
expr_tree_shared (ExprTree const *expr)
Jody Goldberg's avatar
Jody Goldberg committed
319
{
320
	g_return_val_if_fail (expr != NULL, FALSE);
Jody Goldberg's avatar
Jody Goldberg committed
321

322
	return (expr->any.ref_count > 1);
Jody Goldberg's avatar
Jody Goldberg committed
323 324
}

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

333
	g_return_val_if_fail (pos != NULL, NULL);
334
	g_return_val_if_fail (expr != NULL, NULL);
335

336
	fd = expr->func.func;
337 338
	ei.func_def = fd;
	ei.pos = pos;
339
	args = expr->func.arg_list;
340

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

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

	/* handle inverted ranges */
Jody Goldberg's avatar
Jody Goldberg committed
370
	value_cellrange_normalize (pos, v, &start_sheet, &end_sheet, &rng);
371 372 373

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

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

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
/**
 * expr_array_intersection :
 * @v: a VALUE_ARRAY
 *
 * Returns the upper left corner of an array.
 *
 * Always release the value passed in.
 *
 * FIXME FIXME FIXME : This will need to be reworked
 * cellrange for array expressions
 *
 * Return value:
 *     duplicate of the value in the upper left of the array
 **/
Value *
expr_array_intersection (Value *a)
{
	Value *tmp = value_duplicate (a->v_array.vals [0][0]);
	value_release (a);
	return tmp;
}

Jody Goldberg's avatar
Jody Goldberg committed
416 417
static Value *
cb_range_eval (Sheet *sheet, int col, int row, Cell *cell, void *ignore)
418
{
Jody Goldberg's avatar
Jody Goldberg committed
419 420
	cell_eval (cell);
	return NULL;
421 422
}

423
static Value *
424
eval_expr_real (EvalPos const *pos, ExprTree const *expr,
425
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
426
{
427
	Value *res = NULL, *a = NULL, *b = NULL;
428

429
	g_return_val_if_fail (expr != NULL, NULL);
430
	g_return_val_if_fail (pos != NULL, NULL);
431

432
	switch (expr->any.oper){
433 434 435 436 437 438
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
439
		ValueCompare comp;
440

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

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

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

478 479 480 481
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
482

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

493
			return value_new_error (pos, gnumeric_err_VALUE);
494
		}
495

496
		switch (expr->any.oper) {
497
		case OPER_EQUAL:
498
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
499 500
			break;

501
		case OPER_GT:
502
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
503 504
			break;

505
		case OPER_LT:
506
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
507 508
			break;

509 510
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
511 512
			break;

513 514
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
515 516
			break;

517 518
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
519
			break;
520

Arturo Espinosa's avatar
Arturo Espinosa committed
521
		default:
522
			g_assert_not_reached ();
523
			res = value_new_error (pos,
524
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
525
		}
Morten Welinder's avatar
Morten Welinder committed
526
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
527
	}
528

529 530 531 532 533
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
534 535 536 537 538 539 540 541 542
		/*
		 * 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
		 */

543
	        /* Guarantees a != NULL */
544
		a = eval_expr (pos, expr->binary.value_a,
545
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
546 547 548

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

558 559
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
560
			return value_new_error_err (pos, &a->v_err);
561

562
		/* 2) #!VALUE error if A is not a number */
563
		if (a->type == VALUE_STRING) {
564
			Value *tmp = format_match (a->v_str.val->str, NULL, NULL);
565 566 567 568 569 570

			value_release (a);
			if (tmp == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
			a = tmp;
		} else if (!VALUE_IS_NUMBER (a)) {
571
			value_release (a);
572
			return value_new_error (pos, gnumeric_err_VALUE);
573 574
		}

575
	        /* Guarantees that b != NULL */
576
		b = eval_expr (pos, expr->binary.value_b,
577
			       flags & (~EVAL_PERMIT_EMPTY));
Arturo Espinosa's avatar
Arturo Espinosa committed
578

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

590 591
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
592
			value_release (a);
593
			return value_new_error_err (pos, &b->v_err);
Arturo Espinosa's avatar
Arturo Espinosa committed
594
		}
595

596
		/* 4) #!VALUE error if B is not a number */
597
		if (b->type == VALUE_STRING) {
598
			Value *tmp = format_match (b->v_str.val->str, NULL, NULL);
599 600 601 602 603 604 605 606

			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)) {
607 608
			value_release (a);
			value_release (b);
609
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
610
		}
611

612
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
613
			int ia = value_get_as_int (a);
614
			int ib = value_get_as_int (b);
615 616 617
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
618 619
			value_release (a);
			value_release (b);
620

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

632
			case OPER_SUB:
633 634 635 636 637
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
638
					return value_new_float ((gnum_float) dres);
639

640
			case OPER_MULT:
641 642 643 644 645
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
646
					return value_new_float ((gnum_float) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
647

648
			case OPER_DIV:
649
				if (ib == 0)
650
					return value_new_error (pos, gnumeric_err_DIV0);
651 652 653 654 655
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
656
					return value_new_float ((gnum_float) dres);
657 658

			case OPER_EXP:
659
				if (ia == 0 && ib <= 0)
660
					return value_new_error (pos, gnumeric_err_NUM);
661 662 663 664 665
				dres = pow ((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

Arturo Espinosa's avatar
Arturo Espinosa committed
668
			default:
669
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
670 671
			}
		} else {
672 673
			gnum_float const va = value_get_as_float (a);
			gnum_float const vb = value_get_as_float (b);
674 675
			value_release (a);
			value_release (b);
676

677
			switch (expr->any.oper){
678
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
679
				return value_new_float (va + vb);
680

681
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
682
				return value_new_float (va - vb);
683

684
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
685
				return value_new_float (va * vb);
686

687
			case OPER_DIV:
688
				return (vb == 0.0)
689
				    ? value_new_error (pos,
690
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
691
				    : value_new_float (va / vb);
692

693
			case OPER_EXP:
694 695
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
696
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
697
				return value_new_float (pow (va, vb));
698

Arturo Espinosa's avatar
Arturo Espinosa committed
699
			default:
700
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
701 702
			}
		}
703
		return value_new_error (pos, _("Unknown operator"));
704

Jody Goldberg's avatar
Jody Goldberg committed
705
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
706 707
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
708
	        /* Garantees that a != NULL */
709
		a = eval_expr (pos, expr->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
710 711 712

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
713
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
714
			if (a == NULL)
715
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
716
		} else if (a->type == VALUE_ARRAY) {
717 718 719
			a = expr_array_intersection (a);
			if (a == NULL)
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
720 721
		}

722 723
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
724

725
		if (expr->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
726 727
			return a;

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

744
	case OPER_CONCAT:
745
		a = eval_expr_real (pos, expr->binary.value_a, flags);
746
		if (a != NULL && a->type == VALUE_ERROR)
747
			return a;
748
		b = eval_expr_real (pos, expr->binary.value_b, flags);
749 750 751
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
752
			return b;
753 754
		}

755 756 757 758 759 760 761 762 763 764 765 766 767 768
		if (a == NULL) {
			if (b != NULL) {
				res = value_new_string (value_peek_string (b));
				value_release (b);
			} else
				res = value_new_string ("");
		} else if (b == NULL) {
			res = value_new_string (value_peek_string (a));
			value_release (a);
		} else {
			char *tmp = g_strconcat (value_peek_string (a),
						 value_peek_string (b), NULL);
			res = value_new_string (tmp);
			g_free (tmp);
Morten Welinder's avatar
Morten Welinder committed
769 770
			value_release (a);
			value_release (b);
771 772
		}

Morten Welinder's avatar
Morten Welinder committed
773
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
774

775
	case OPER_FUNCALL:
776
		return eval_funcall (pos, expr, flags);
Arturo Espinosa's avatar
Arturo Espinosa committed
777

Michael Meeks's avatar
Michael Meeks committed
778
	case OPER_NAME:
779
		return eval_expr_name (pos, expr->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
780

781
	case OPER_VAR: {
782
		Sheet *cell_sheet;
783
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
784 785
		Cell *cell;
		int col, row;
786

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

790
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
791
		cell = sheet_cell_get (cell_sheet, col, row);
792 793
		if (cell == NULL)
			return NULL;
794

Jody Goldberg's avatar
Jody Goldberg committed
795
		cell_eval (cell);
796

797
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
798
	}
799

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

			if (a->sheet == b->sheet) {
				int a_col, a_row, b_col, b_row;
				int c = pos->eval.col;
				int r = pos->eval.row;

				cell_get_abs_col_row (a, &pos->eval, &a_col, &a_row);
				cell_get_abs_col_row (b, &pos->eval, &b_col, &b_row);
				if (a_row == b_row) {
					if (a_col <= c && c <= b_col) {
						r = a_row;
						found = TRUE;
					}
				} else if (a_col == b_col) {
					if (a_row <= r && r <= b_row) {
						c = a_col;
						found = TRUE;
					}
				}
				if (found) {
					Cell * cell = sheet_cell_get (pos->sheet, c, r);
					if (cell == NULL)
						return NULL;

Jody Goldberg's avatar
Jody Goldberg committed
843
					cell_eval (cell);
844 845 846 847 848 849

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

851 852 853
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
854 855
		int x = expr->array.x;
		int y = expr->array.y;
856
		if (x == 0 && y == 0){
857
			/* Release old value if necessary */
858
			a = expr->array.corner.value;
859 860 861
			if (a != NULL)
				value_release (a);

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

			/* Store real result (cast away const)*/
885