expr.c 39.8 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>
11
#include <locale.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"
18
#include "parse-util.h"
19
#include "ranges.h"
20
#include "workbook.h"
21

22 23
inline EvalPosition *
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, CellPos const *pos)
24
{
25
	g_return_val_if_fail (eval_pos != NULL, NULL);
26
	g_return_val_if_fail (sheet != NULL, NULL);
27 28 29
	g_return_val_if_fail (IS_SHEET (sheet), NULL);

	eval_pos->sheet = sheet;
30
	eval_pos->eval  = *pos;
31

32
	return eval_pos;
33
}
34

35
EvalPosition *
Jody Goldberg's avatar
Jody Goldberg committed
36
eval_pos_cell (EvalPosition *eval_pos, Cell const *cell)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
{
	CellPos pos;
	g_return_val_if_fail (cell != NULL, NULL);

	pos.col = cell->col_info->pos;
	pos.row = cell->row_info->pos;
	return eval_pos_init (eval_pos, cell->sheet, &pos);
}

EvalPosition *
eval_pos_cellref (EvalPosition *dest, EvalPosition const *src, 
		  CellRef const *ref)
{
	/* FIXME : This is a place to catch all of the strange
	 * usages.  Please figure out what they were trying to do.
	 */
	*dest = *src;
	return dest;
}

57 58 59 60
/*
 * Supply either a sheet (preferred) or a workbook.
 */

61
ParsePosition *
62
parse_pos_init (ParsePosition *pp, Workbook *wb, Sheet *sheet, int col, int row)
63
{
64
	g_return_val_if_fail (wb || sheet, NULL);
65 66
	g_return_val_if_fail (pp != NULL, NULL);

67 68
	pp->sheet = sheet;
	pp->wb = sheet ? sheet->workbook : wb;
69 70 71 72 73 74 75
	pp->col = col;
	pp->row = row;

	return pp;
}

ParsePosition *
Jody Goldberg's avatar
Jody Goldberg committed
76
parse_pos_cell (ParsePosition *pp, Cell const *cell)
77 78 79 80 81 82 83 84 85
{
	g_return_val_if_fail (pp != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);
	g_return_val_if_fail (cell->sheet->workbook != NULL, NULL);

	return parse_pos_init (
		pp,
86 87
		NULL,
		cell->sheet,
88 89
		cell->col_info->pos,
		cell->row_info->pos);
90 91
}

92 93 94 95 96 97 98 99 100 101 102
ParsePosition *
parse_pos_evalpos (ParsePosition *pp, EvalPosition const *ep)
{
	g_return_val_if_fail (pp != NULL, NULL);
	g_return_val_if_fail (ep != NULL, NULL);

	return parse_pos_init (pp, NULL, ep->sheet, ep->eval.col, ep->eval.row);
}

/***************************************************************************/

103
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
104
expr_tree_new_constant (Value *v)
105
{
106
	ExprConstant *ans;
107

108
	ans = g_new (ExprConstant, 1);
109 110 111 112
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
113
	*((Operation *)&(ans->oper)) = OPER_CONSTANT;
114
	ans->value= v;
115

116
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
ExprTree *
expr_tree_new_error (char const *txt)
{
	Symbol *func;
	GList *args = NULL;

	if (strcmp (txt, gnumeric_err_NA) == 0) {
		func = symbol_lookup (global_symbol_table, "NA");
	} else {
		func = symbol_lookup (global_symbol_table, "ERROR");
		args = g_list_prepend (NULL,
				       expr_tree_new_constant (value_new_string (txt)));
	}

	symbol_ref (func);
	return expr_tree_new_funcall (func, args);
}

ExprTree *
expr_tree_new_funcall (Symbol *sym, GList *args)
{
	ExprFunction *ans;
	g_return_val_if_fail (sym, NULL);

	ans = g_new (ExprFunction, 1);
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_FUNCALL;
	ans->symbol = sym;;
	ans->arg_list = args;

	return (ExprTree *)ans;
}
       
Morten Welinder's avatar
Morten Welinder committed
155 156 157
ExprTree *
expr_tree_new_unary  (Operation op, ExprTree *e)
{
158
	ExprUnary *ans;
Morten Welinder's avatar
Morten Welinder committed
159

160
	ans = g_new (ExprUnary, 1);
161 162 163 164
	if (!ans)
		return NULL;

	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
165
	*((Operation *)&(ans->oper)) = op;
166
	ans->value = e;
167

168
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
169 170 171 172 173 174
}


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

177
	ans = g_new (ExprBinary, 1);
178 179 180 181
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
Jody Goldberg's avatar
Jody Goldberg committed
182
	*((Operation *)&(ans->oper)) = op;
183 184
	ans->value_a = l;
	ans->value_b = r;
185

186
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
187 188 189
}

ExprTree *
190
expr_tree_new_name (NamedExpression const *name)
Morten Welinder's avatar
Morten Welinder committed
191
{
192
	ExprName *ans;
Morten Welinder's avatar
Morten Welinder committed
193

194
	ans = g_new (ExprName, 1);
195 196 197 198
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
199 200
	*((Operation *)&(ans->oper)) = OPER_NAME;
	ans->name = name;
201

202
	return (ExprTree *)ans;
Morten Welinder's avatar
Morten Welinder committed
203
}
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

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

	ans = g_new (ExprVar, 1);
	if (!ans)
		return NULL;
	
	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;
	
	ans->ref_count = 1;
	*((Operation *)&(ans->oper)) = OPER_ARRAY;
	ans->x = x;
	ans->y = y;
	ans->rows = rows;
	ans->cols = cols;
	ans->corner.func.value = NULL;
	ans->corner.func.expr = NULL;
	ans->corner.cell = NULL;
	return (ExprTree *)ans;
}

242
int
243
expr_tree_get_const_int (ExprTree const *expr)
244 245
{
	g_return_val_if_fail (expr != NULL, 0);
246 247 248
	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);
249

250
	return expr->constant.value->v_int.val;
251 252
}

253
char const *
254
expr_tree_get_const_str (ExprTree const *expr)
255 256
{
	g_return_val_if_fail (expr != NULL, NULL);
257 258 259
	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);
260

261
	return expr->constant.value->v_str.val->str;
262 263
}

264
ExprTree *
265
expr_parse_string (char const *expr, ParsePosition const *pp,
Morten Welinder's avatar
Morten Welinder committed
266
		   char **desired_format, char **error_msg)
267 268 269 270
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

271
	switch (gnumeric_expr_parser (expr, pp, TRUE, desired_format, &tree)) {
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	case PARSE_OK:
		*error_msg = NULL;
		return tree;

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

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

291 292

static ExprTree *
293
expr_tree_array_formula_corner (ExprTree const *expr, EvalPosition const *pos)
294
{
295
	Cell * corner = expr->array.corner.cell;
296

297 298 299 300 301 302
	/* Attempt to set the corner if it is not already set */
	if (corner == NULL) {
		g_return_val_if_fail (pos != NULL, NULL);
		g_return_val_if_fail (pos->sheet != NULL, NULL);

		corner = sheet_cell_get (pos->sheet,
303 304 305
					 pos->eval.col - expr->array.x,
					 pos->eval.row - expr->array.y);
		((ExprTree *)expr)->array.corner.cell = corner;
306 307 308
	}

	g_return_val_if_fail (corner != NULL, NULL);
309
	g_return_val_if_fail (cell_has_expr (corner), NULL);
310 311

	/* Sanity check incase the corner gets removed for some reason */
312
	g_return_val_if_fail (corner->u.expression != (void *)0xdeadbeef, NULL);
313 314 315
	g_return_val_if_fail (corner->u.expression->any.oper == OPER_ARRAY, NULL);
	g_return_val_if_fail (corner->u.expression->array.x == 0, NULL);
	g_return_val_if_fail (corner->u.expression->array.y == 0, NULL);
316

317
	return corner->u.expression;
318 319
}

Arturo Espinosa's avatar
Arturo Espinosa committed
320 321 322 323
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
324 325 326 327
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
328
	g_return_if_fail (tree->any.ref_count > 0);
329

330
	tree->any.ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
331 332 333 334 335
}

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

339
	switch (tree->any.oper){
340
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
341
		break;
342

343
	case OPER_CONSTANT:
344
		value_release (tree->constant.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
345
		break;
346

Morten Welinder's avatar
Morten Welinder committed
347 348 349
	case OPER_FUNCALL: {
		GList *l;

350
		for (l = tree->func.arg_list; l; l = l->next)
Morten Welinder's avatar
Morten Welinder committed
351
			do_expr_tree_unref (l->data);
352 353
		g_list_free (tree->func.arg_list);
		symbol_unref (tree->func.symbol);
Arturo Espinosa's avatar
Arturo Espinosa committed
354
		break;
Morten Welinder's avatar
Morten Welinder committed
355
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
356

Michael Meeks's avatar
Michael Meeks committed
357 358 359
	case OPER_NAME:
		break;

360
	case OPER_ANY_BINARY:
361 362
		do_expr_tree_unref (tree->binary.value_a);
		do_expr_tree_unref (tree->binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
363 364
		break;

365
	case OPER_ANY_UNARY:
366
		do_expr_tree_unref (tree->unary.value);
Arturo Espinosa's avatar
Arturo Espinosa committed
367
		break;
368
	case OPER_ARRAY:
369 370 371 372
		if (tree->array.x == 0 && tree->array.y == 0) {
			if (tree->array.corner.func.value)
				value_release (tree->array.corner.func.value);
			do_expr_tree_unref (tree->array.corner.func.expr);
373
		}
374
		break;
375 376 377
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
378
	}
379

Morten Welinder's avatar
Morten Welinder committed
380
	g_free (tree);
381 382
}

Morten Welinder's avatar
Morten Welinder committed
383 384 385 386 387 388
/*
 * 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.)
 */
389 390 391 392
void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
393
	g_return_if_fail (tree->any.ref_count > 0);
394

Arturo Espinosa's avatar
Arturo Espinosa committed
395
	do_expr_tree_unref (tree);
396 397
}

398
static Value *
399
eval_funcall (EvalPosition const *pos, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
400
{
401
	FunctionEvalInfo ei;
402
	Symbol const *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
403
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
404
	GList *args;
405

406
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
407
	g_return_val_if_fail (tree != NULL, NULL);
408

409
	sym = tree->func.symbol;
410 411

	if (sym->type != SYMBOL_FUNCTION)
412
		return value_new_error (pos, _("Internal error"));
413

414
	fd = (FunctionDefinition *)sym->data;
415 416
	ei.func_def = fd;
	ei.pos = pos;
417
	args = tree->func.arg_list;
418

419
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
420 421
}

Jody Goldberg's avatar
Jody Goldberg committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
/**
 * expr_implicit_intersection :
 * @ei: EvalInfo containing valid fd!
 * @v: a VALUE_CELLRANGE
 * 
 * Attempt to find the intersection between the calling cell and
 * some element of the 1 one unit wide or tall range.
 *
 * Always release the value passed in.
 *
 * Return value: 
 *     If the intersection succeeded return a duplicate of the value
 *     at the intersection point.  This value needs to be freed.
 **/
Value *
437 438
expr_implicit_intersection (EvalPosition const *pos,
			    Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
439 440 441 442 443 444 445 446 447
{
	/*
	 * 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.
	 */
	Value *res = NULL;
448 449
	CellRef const * const a = & v->v_range.cell_a;
	CellRef const * const b = & v->v_range.cell_b;
Jody Goldberg's avatar
Jody Goldberg committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

	if (a->sheet == b->sheet) {
		int a_col, a_row, b_col, b_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) {
			int const c = pos->eval.col;
			if (a_col <= c && c <= b_col)
				res = value_duplicate (value_area_get_x_y (pos, v, c - a_col, 0));
		}

		if (a_col == b_col) {
			int const r = pos->eval.row;
			if (a_row <= r && r <= b_row)
				res = value_duplicate (value_area_get_x_y (pos, v, 0, r - a_row));
		}
	}
	value_release (v);
	return res;
}

471
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
472 473
	IS_EQUAL,
	IS_LESS,
474
	IS_GREATER,
475
	TYPE_MISMATCH
476 477 478
} compare_t;

static compare_t
479
compare_bool_bool (Value const *va, Value const *vb)
480
{
481 482 483 484 485 486 487 488 489
	gboolean err; /* Ignored */
	gboolean const a = value_get_as_bool (va, &err);
	gboolean const b = value_get_as_bool (vb, &err);
	if (a)
		return b ? IS_EQUAL : IS_GREATER;
	return b ? IS_LESS : IS_EQUAL;
}

static compare_t
490
compare_int_int (Value const *va, Value const *vb)
491 492 493
{
	int const a = value_get_as_int (va);
	int const b = value_get_as_int (vb);
494 495 496 497 498 499 500 501 502
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
503
compare_float_float (Value const *va, Value const *vb)
504
{
505 506
	float_t const a = value_get_as_float (va);
	float_t const b = value_get_as_float (vb);
507 508 509 510 511 512 513
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
514

515 516
/*
 * Compares two (Value *) and returns one of compare_t
Jody Goldberg's avatar
Jody Goldberg committed
517 518 519
 *
 * if pos is non null it will perform implict intersection for
 * cellranges.
520 521
 */
static compare_t
522
compare (Value const *a, Value const *b)
Arturo Espinosa's avatar
Arturo Espinosa committed
523
{
524 525
	ValueType ta, tb;

526 527 528 529
	/* Handle trivial and double NULL case */
	if (a == b)
		return IS_EQUAL;

530 531
	ta = value_is_empty_cell (a) ? VALUE_EMPTY : a->type;
	tb = value_is_empty_cell (b) ? VALUE_EMPTY : b->type;
532

533 534 535 536 537 538
	/* string > empty */
	if (ta == VALUE_STRING) {
		switch (tb) {
		/* Strings are > (empty, or number) */
		case VALUE_EMPTY : case VALUE_INTEGER : case VALUE_FLOAT :
			return IS_GREATER;
539

540 541 542
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_LESS;
543

544 545 546
		/* If both are strings compare as string */
		case VALUE_STRING :
		{
547
			int const t = strcasecmp (a->v_str.val->str, b->v_str.val->str);
548 549 550 551 552 553 554 555
			if (t == 0)
				return IS_EQUAL;
			else if (t > 0)
				return IS_GREATER;
			else
				return IS_LESS;
		}
		default :
556
			return TYPE_MISMATCH;
557
		}
558 559 560 561 562
	} else if (tb == VALUE_STRING) {
		switch (ta) {
		/* (empty, or number) < String */
		case VALUE_EMPTY : case VALUE_INTEGER : case VALUE_FLOAT :
			return IS_LESS;
563

564 565 566
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_GREATER;
567

568
		default :
569
			return TYPE_MISMATCH;
570 571 572
		}
	}

573 574 575 576 577 578
	/* Booleans > all numbers (Why did excel do this ??) */
	if (ta == VALUE_BOOLEAN && (tb == VALUE_INTEGER || tb == VALUE_FLOAT))
		return IS_GREATER;
	if (tb == VALUE_BOOLEAN && (ta == VALUE_INTEGER || ta == VALUE_FLOAT))
		return IS_LESS;

579 580 581
	switch ((ta > tb) ? ta : tb) {
	case VALUE_EMPTY:	/* Empty Empty compare */
		return IS_EQUAL;
582

583 584
	case VALUE_BOOLEAN:
		return compare_bool_bool (a, b);
585

586 587 588 589 590 591 592 593
	case VALUE_INTEGER:
		return compare_int_int (a, b);

	case VALUE_FLOAT:
		return compare_float_float (a, b);
	default:
		return TYPE_MISMATCH;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
594 595
}

596 597 598 599 600
/*
 * Utility routine to ensure that all elements of a range are recalced as
 * necessary.
 */
static void
601
eval_range (EvalPosition const *pos, Value *v)
602 603
{
	int start_col, start_row, end_col, end_row;
604 605
	CellRef * a = &v->v_range.cell_a;
	CellRef * b = &v->v_range.cell_b;
606
	Sheet * sheet = a->sheet ? a->sheet : pos->sheet;
607 608
	Cell * cell;
	int r, c;
609
	int const gen = pos->sheet->workbook->generation;
610

611 612
	cell_get_abs_col_row (a, &pos->eval, &start_col, &start_row);
	cell_get_abs_col_row (b, &pos->eval, &end_col, &end_row);
613

Michael Meeks's avatar
Michael Meeks committed
614
	if (b->sheet && a->sheet != b->sheet) {
615 616 617 618 619 620 621 622 623
		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;
624 625
			if (cell->generation != gen)
				cell_eval (cell);
626 627 628
		}
}

629
static Value *
630 631
eval_expr_real (EvalPosition const *pos, ExprTree const *tree,
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
632
{
633
	Value *res = NULL, *a = NULL, *b = NULL;
634
	
Arturo Espinosa's avatar
Arturo Espinosa committed
635
	g_return_val_if_fail (tree != NULL, NULL);
636
	g_return_val_if_fail (pos != NULL, NULL);
637

638
	switch (tree->any.oper){
639 640 641 642 643 644
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE: {
Arturo Espinosa's avatar
Arturo Espinosa committed
645
		int comp;
646

647
		a = eval_expr_real (pos, tree->binary.value_a, flags);
Jody Goldberg's avatar
Jody Goldberg committed
648 649
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
650
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
651
				if (a == NULL)
652
					return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
653 654 655
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
656

657
		b = eval_expr_real (pos, tree->binary.value_b, flags);
Jody Goldberg's avatar
Jody Goldberg committed
658 659 660
		if (b != NULL) {
			Value *res = NULL;
			if (b->type == VALUE_CELLRANGE) {
661
				b = expr_implicit_intersection (pos, b);
Jody Goldberg's avatar
Jody Goldberg committed
662
				if (b == NULL)
663
					res = value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
664 665 666 667 668 669 670 671
			} 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
672
		}
673

Arturo Espinosa's avatar
Arturo Espinosa committed
674 675
		comp = compare (a, b);

676 677 678 679
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
680

Michael Meeks's avatar
Michael Meeks committed
681
		if (comp == TYPE_MISMATCH) {
682 683 684 685
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
686
			if (tree->any.oper == OPER_EQUAL)
687
				return value_new_bool (FALSE);
688
			if (tree->any.oper == OPER_NOT_EQUAL)
689 690
				return value_new_bool (TRUE);

691
			return value_new_error (pos, gnumeric_err_VALUE);
692
		}
693

694
		switch (tree->any.oper) {
695
		case OPER_EQUAL:
696
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
697 698
			break;

699
		case OPER_GT:
700
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
701 702
			break;

703
		case OPER_LT:
704
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
705 706
			break;

707 708
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
709 710
			break;

711 712
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
713 714
			break;

715 716
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
717
			break;
718

Arturo Espinosa's avatar
Arturo Espinosa committed
719
		default:
720
			g_assert_not_reached ();
721
			res = value_new_error (pos,
722
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
723
		}
Morten Welinder's avatar
Morten Welinder committed
724
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
725
	}
726

727 728 729 730 731
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
732 733 734 735 736 737 738 739 740
		/*
		 * 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
		 */

741
	        /* Ensure a != NULL */
742
		a = eval_expr (pos, tree->binary.value_a,
743
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
744 745 746

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
747
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
748
			if (a == NULL)
749
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
750 751
		}

752 753
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
754
			return a;
755

756 757 758
		/* 2) #!VALUE error if A is not a number */
		if (!VALUE_IS_NUMBER (a)) {
			value_release (a);
759
			return value_new_error (pos, gnumeric_err_VALUE);
760 761 762
		}

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

Jody Goldberg's avatar
Jody Goldberg committed
766 767
		/* Handle implicit intersection */
		if (b->type == VALUE_CELLRANGE) {
768
			b = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
769
			if (b == NULL)
770
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
771 772
		}

773 774
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
775
			value_release (a);
776
			return b;
Arturo Espinosa's avatar
Arturo Espinosa committed
777
		}
778

779 780
		/* 4) #!VALUE error if B is not a number */
		if (!VALUE_IS_NUMBER (b)) {
781 782
			value_release (a);
			value_release (b);
783
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
784
		}
785

786
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
787
			int ia = value_get_as_int (a);
788
			int ib = value_get_as_int (b);
789 790 791
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
792 793
			value_release (a);
			value_release (b);
794

795 796
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
797
			switch (tree->any.oper){
798 799 800 801 802 803 804 805
			case OPER_ADD:
				dres = (double)ia + (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);

806
			case OPER_SUB:
807 808 809 810 811 812
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
813

814
			case OPER_MULT:
815 816 817 818 819 820
				dres = (double)ia * (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
Arturo Espinosa's avatar
Arturo Espinosa committed
821

822
			case OPER_DIV:
823
				if (ib == 0)
824
					return value_new_error (pos, gnumeric_err_DIV0);
825 826 827 828 829 830
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
831 832

			case OPER_EXP:
833
				if (ia == 0 && ib <= 0)
834
					return value_new_error (pos, gnumeric_err_NUM);
835 836 837 838 839 840 841
				dres = pow ((double)ia, (double)ib);
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);

Arturo Espinosa's avatar
Arturo Espinosa committed
842
			default:
843
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
844 845
			}
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
846 847
			float_t const va = value_get_as_float (a);
			float_t const vb = value_get_as_float (b);
848 849
			value_release (a);
			value_release (b);
850

851
			switch (tree->any.oper){
852
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
853
				return value_new_float (va + vb);
854

855
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
856
				return value_new_float (va - vb);
857

858
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
859
				return value_new_float (va * vb);
860

861
			case OPER_DIV:
862
				return (vb == 0.0)
863
				    ? value_new_error (pos,
864
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
865
				    : value_new_float (va / vb);
866

867
			case OPER_EXP:
868 869
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
870
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
871
				return value_new_float (pow (va, vb));
872

Arturo Espinosa's avatar
Arturo Espinosa committed
873
			default:
874
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
875 876
			}
		}
877
		return value_new_error (pos, _("Unknown operator"));
878

Jody Goldberg's avatar
Jody Goldberg committed
879
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
880 881
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
882
	        /* Garantees that a != NULL */
883
		a = eval_expr (pos, tree->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
884 885 886

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
887
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
888
			if (a == NULL)
889
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
890 891
		}

892 893
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
894

895
		if (tree->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
896 897
			return a;

898 899
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
900
			return value_new_error (pos, gnumeric_err_VALUE);
901
		}
902
		if (tree->any.oper == OPER_UNARY_NEG) {
Jody Goldberg's avatar
Jody Goldberg committed
903
			if (a->type == VALUE_INTEGER)
904
				res = value_new_int (-a->v_int.val);
Jody Goldberg's avatar
Jody Goldberg committed
905
			else if (a->type == VALUE_FLOAT)
906
				res = value_new_float (-a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
907
			else
908
				res = value_new_bool (!a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
909 910
		} else
			res = value_new_float (value_get_as_float (a) * .01);
911
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
912
		return res;
913

914
	case OPER_CONCAT: {
915 916
		char *sa, *sb, *tmp;

917
		a = eval_expr_real (pos, tree->binary.value_a, flags);
918
		if (a != NULL && a->type == VALUE_ERROR)
919
			return a;
920
		b = eval_expr_real (pos, tree->binary.value_b, flags);
921 922 923
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
924
			return b;
925 926
		}

Michael Meeks's avatar
Michael Meeks committed
927 928
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
929
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
930 931
		res = value_new_string (tmp);

932 933 934 935
		g_free (sa);
		g_free (sb);
		g_free (tmp);

936
		if (a != NULL)
937
		value_release (a);
938
		if (b != NULL)
939
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
940
		return res;
941
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
942

943
	case OPER_FUNCALL:
944
		return eval_funcall (pos, tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
945

Michael Meeks's avatar
Michael Meeks committed
946
	case OPER_NAME:
947
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
948

949
	case OPER_VAR: {
950
		Sheet *cell_sheet;
951
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
952 953
		Cell *cell;
		int col, row;
954

955
		if (pos->sheet == NULL) {
Arturo Espinosa's avatar
Arturo Espinosa committed
956
			/* Only the test program requests this */
Morten Welinder's avatar
Morten Welinder committed
957
			return value_new_float (3.14);
Arturo Espinosa's avatar
Arturo Espinosa committed
958 959
		}

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

963
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
964
		cell = sheet_cell_get (cell_sheet, col, row);
965 966
		if (cell == NULL)
			return NULL;
967

968
		if (cell->generation != pos->sheet->workbook->generation)
969
			cell_eval (cell);
970

971
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
972
	}
973

974
	case OPER_CONSTANT: