expr.c 40 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
}

Jody Goldberg's avatar
Jody Goldberg committed
398 399 400 401 402 403 404 405 406 407 408 409
/**
 * 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);
}

410
static Value *
411
eval_funcall (EvalPosition const *pos, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
412
{
413
	FunctionEvalInfo ei;
414
	Symbol const *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
415
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
416
	GList *args;
417

418
	g_return_val_if_fail (pos != NULL, NULL);
Michael Meeks's avatar
Michael Meeks committed
419
	g_return_val_if_fail (tree != NULL, NULL);
420

421
	sym = tree->func.symbol;
422 423

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

426
	fd = (FunctionDefinition *)sym->data;
427 428
	ei.func_def = fd;
	ei.pos = pos;
429
	args = tree->func.arg_list;
430

431
	return function_call_with_list (&ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
432 433
}

Jody Goldberg's avatar
Jody Goldberg committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
/**
 * 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 *
449 450
expr_implicit_intersection (EvalPosition const *pos,
			    Value *v)
Jody Goldberg's avatar
Jody Goldberg committed
451 452 453 454 455 456 457 458 459
{
	/*
	 * 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;
460 461
	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
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

	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;
}

483
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
484 485
	IS_EQUAL,
	IS_LESS,
486
	IS_GREATER,
487
	TYPE_MISMATCH
488 489 490
} compare_t;

static compare_t
491
compare_bool_bool (Value const *va, Value const *vb)
492
{
493 494 495 496 497 498 499 500 501
	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
502
compare_int_int (Value const *va, Value const *vb)
503 504 505
{
	int const a = value_get_as_int (va);
	int const b = value_get_as_int (vb);
506 507 508 509 510 511 512 513 514
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
515
compare_float_float (Value const *va, Value const *vb)
516
{
517 518
	float_t const a = value_get_as_float (va);
	float_t const b = value_get_as_float (vb);
519 520 521 522 523 524 525
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
526

527 528
/*
 * Compares two (Value *) and returns one of compare_t
Jody Goldberg's avatar
Jody Goldberg committed
529 530 531
 *
 * if pos is non null it will perform implict intersection for
 * cellranges.
532 533
 */
static compare_t
534
compare (Value const *a, Value const *b)
Arturo Espinosa's avatar
Arturo Espinosa committed
535
{
536 537
	ValueType ta, tb;

538 539 540 541
	/* Handle trivial and double NULL case */
	if (a == b)
		return IS_EQUAL;

542 543
	ta = value_is_empty_cell (a) ? VALUE_EMPTY : a->type;
	tb = value_is_empty_cell (b) ? VALUE_EMPTY : b->type;
544

545 546 547 548 549 550
	/* 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;
551

552 553 554
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_LESS;
555

556 557 558
		/* If both are strings compare as string */
		case VALUE_STRING :
		{
559
			int const t = g_strcasecmp (a->v_str.val->str, b->v_str.val->str);
560 561 562 563 564 565 566 567
			if (t == 0)
				return IS_EQUAL;
			else if (t > 0)
				return IS_GREATER;
			else
				return IS_LESS;
		}
		default :
568
			return TYPE_MISMATCH;
569
		}
570 571 572 573 574
	} else if (tb == VALUE_STRING) {
		switch (ta) {
		/* (empty, or number) < String */
		case VALUE_EMPTY : case VALUE_INTEGER : case VALUE_FLOAT :
			return IS_LESS;
575

576 577 578
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_GREATER;
579

580
		default :
581
			return TYPE_MISMATCH;
582 583 584
		}
	}

585 586 587 588 589 590
	/* 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;

591 592 593
	switch ((ta > tb) ? ta : tb) {
	case VALUE_EMPTY:	/* Empty Empty compare */
		return IS_EQUAL;
594

595 596
	case VALUE_BOOLEAN:
		return compare_bool_bool (a, b);
597

598 599 600 601 602 603 604 605
	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
606 607
}

608 609 610 611 612
/*
 * Utility routine to ensure that all elements of a range are recalced as
 * necessary.
 */
static void
613
eval_range (EvalPosition const *pos, Value *v)
614 615
{
	int start_col, start_row, end_col, end_row;
616 617
	CellRef * a = &v->v_range.cell_a;
	CellRef * b = &v->v_range.cell_b;
618
	Sheet * sheet = a->sheet ? a->sheet : pos->sheet;
619 620
	Cell * cell;
	int r, c;
621
	int const gen = pos->sheet->workbook->generation;
622

623 624
	cell_get_abs_col_row (a, &pos->eval, &start_col, &start_row);
	cell_get_abs_col_row (b, &pos->eval, &end_col, &end_row);
625

Michael Meeks's avatar
Michael Meeks committed
626
	if (b->sheet && a->sheet != b->sheet) {
627 628 629 630 631 632 633 634 635
		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;
636 637
			if (cell->generation != gen)
				cell_eval (cell);
638 639 640
		}
}

641
static Value *
642 643
eval_expr_real (EvalPosition const *pos, ExprTree const *tree,
		ExprEvalFlags flags)
Arturo Espinosa's avatar
Arturo Espinosa committed
644
{
645
	Value *res = NULL, *a = NULL, *b = NULL;
646
	
Arturo Espinosa's avatar
Arturo Espinosa committed
647
	g_return_val_if_fail (tree != NULL, NULL);
648
	g_return_val_if_fail (pos != NULL, NULL);
649

650
	switch (tree->any.oper){
651 652 653 654 655 656
	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
657
		int comp;
658

659
		a = eval_expr_real (pos, tree->binary.value_a, flags);
Jody Goldberg's avatar
Jody Goldberg committed
660 661
		if (a != NULL) {
			if (a->type == VALUE_CELLRANGE) {
662
				a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
663
				if (a == NULL)
664
					return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
665 666 667
			} else if (a->type == VALUE_ERROR)
				return a;
		}
Morten Welinder's avatar
Morten Welinder committed
668

669
		b = eval_expr_real (pos, tree->binary.value_b, flags);
Jody Goldberg's avatar
Jody Goldberg committed
670 671 672
		if (b != NULL) {
			Value *res = NULL;
			if (b->type == VALUE_CELLRANGE) {
673
				b = expr_implicit_intersection (pos, b);
Jody Goldberg's avatar
Jody Goldberg committed
674
				if (b == NULL)
675
					res = value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
676 677 678 679 680 681 682 683
			} 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
684
		}
685

Arturo Espinosa's avatar
Arturo Espinosa committed
686 687
		comp = compare (a, b);

688 689 690 691
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
692

Michael Meeks's avatar
Michael Meeks committed
693
		if (comp == TYPE_MISMATCH) {
694 695 696 697
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
698
			if (tree->any.oper == OPER_EQUAL)
699
				return value_new_bool (FALSE);
700
			if (tree->any.oper == OPER_NOT_EQUAL)
701 702
				return value_new_bool (TRUE);

703
			return value_new_error (pos, gnumeric_err_VALUE);
704
		}
705

706
		switch (tree->any.oper) {
707
		case OPER_EQUAL:
708
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
709 710
			break;

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

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

719 720
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
721 722
			break;

723 724
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
725 726
			break;

727 728
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
729
			break;
730

Arturo Espinosa's avatar
Arturo Espinosa committed
731
		default:
732
			g_assert_not_reached ();
733
			res = value_new_error (pos,
734
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
735
		}
Morten Welinder's avatar
Morten Welinder committed
736
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
737
	}
738

739 740 741 742 743
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
744 745 746 747 748 749 750 751 752
		/*
		 * 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
		 */

753
	        /* Ensure a != NULL */
754
		a = eval_expr (pos, tree->binary.value_a,
755
			       flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
756 757 758

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
759
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
760
			if (a == NULL)
761
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
762 763
		}

764 765
		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
766
			return a;
767

768 769 770
		/* 2) #!VALUE error if A is not a number */
		if (!VALUE_IS_NUMBER (a)) {
			value_release (a);
771
			return value_new_error (pos, gnumeric_err_VALUE);
772 773 774
		}

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

Jody Goldberg's avatar
Jody Goldberg committed
778 779
		/* Handle implicit intersection */
		if (b->type == VALUE_CELLRANGE) {
780
			b = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
781
			if (b == NULL)
782
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
783 784
		}

785 786
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
787
			value_release (a);
788
			return b;
Arturo Espinosa's avatar
Arturo Espinosa committed
789
		}
790

791 792
		/* 4) #!VALUE error if B is not a number */
		if (!VALUE_IS_NUMBER (b)) {
793 794
			value_release (a);
			value_release (b);
795
			return value_new_error (pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
796
		}
797

798
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
799
			int ia = value_get_as_int (a);
800
			int ib = value_get_as_int (b);
801 802 803
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
804 805
			value_release (a);
			value_release (b);
806

807 808
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
809
			switch (tree->any.oper){
810 811 812 813 814 815 816 817
			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);

818
			case OPER_SUB:
819 820 821 822 823 824
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
825

826
			case OPER_MULT:
827 828 829 830 831 832
				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
833

834
			case OPER_DIV:
835
				if (ib == 0)
836
					return value_new_error (pos, gnumeric_err_DIV0);
837 838 839 840 841 842
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
843 844

			case OPER_EXP:
845
				if (ia == 0 && ib <= 0)
846
					return value_new_error (pos, gnumeric_err_NUM);
847 848 849 850 851 852 853
				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
854
			default:
855
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
856 857
			}
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
858 859
			float_t const va = value_get_as_float (a);
			float_t const vb = value_get_as_float (b);
860 861
			value_release (a);
			value_release (b);
862

863
			switch (tree->any.oper){
864
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
865
				return value_new_float (va + vb);
866

867
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
868
				return value_new_float (va - vb);
869

870
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
871
				return value_new_float (va * vb);
872

873
			case OPER_DIV:
874
				return (vb == 0.0)
875
				    ? value_new_error (pos,
876
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
877
				    : value_new_float (va / vb);
878

879
			case OPER_EXP:
880 881
				if ((va == 0 && vb <= 0) ||
				    (va < 0 && vb != (int)vb))
882
					return value_new_error (pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
883
				return value_new_float (pow (va, vb));
884

Arturo Espinosa's avatar
Arturo Espinosa committed
885
			default:
886
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
887 888
			}
		}
889
		return value_new_error (pos, _("Unknown operator"));
890

Jody Goldberg's avatar
Jody Goldberg committed
891
	case OPER_PERCENT:
Jody Goldberg's avatar
Jody Goldberg committed
892 893
	case OPER_UNARY_NEG:
	case OPER_UNARY_PLUS:
894
	        /* Garantees that a != NULL */
895
		a = eval_expr (pos, tree->unary.value, flags & (~EVAL_PERMIT_EMPTY));
Jody Goldberg's avatar
Jody Goldberg committed
896 897 898

		/* Handle implicit intersection */
		if (a->type == VALUE_CELLRANGE) {
899
			a = expr_implicit_intersection (pos, a);
Jody Goldberg's avatar
Jody Goldberg committed
900
			if (a == NULL)
901
				return value_new_error (pos, gnumeric_err_VALUE);
Jody Goldberg's avatar
Jody Goldberg committed
902 903
		}

904 905
		if (a->type == VALUE_ERROR)
			return a;
Jody Goldberg's avatar
Jody Goldberg committed
906

907
		if (tree->any.oper == OPER_UNARY_PLUS)
Jody Goldberg's avatar
Jody Goldberg committed
908 909
			return a;

910 911
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
912
			return value_new_error (pos, gnumeric_err_VALUE);
913
		}
914
		if (tree->any.oper == OPER_UNARY_NEG) {
Jody Goldberg's avatar
Jody Goldberg committed
915
			if (a->type == VALUE_INTEGER)
916
				res = value_new_int (-a->v_int.val);
Jody Goldberg's avatar
Jody Goldberg committed
917
			else if (a->type == VALUE_FLOAT)
918
				res = value_new_float (-a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
919
			else
920
				res = value_new_bool (!a->v_float.val);
Jody Goldberg's avatar
Jody Goldberg committed
921 922
		} else
			res = value_new_float (value_get_as_float (a) * .01);
923
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
924
		return res;
925

926
	case OPER_CONCAT: {
927 928
		char *sa, *sb, *tmp;

929
		a = eval_expr_real (pos, tree->binary.value_a, flags);
930
		if (a != NULL && a->type == VALUE_ERROR)
931
			return a;
932
		b = eval_expr_real (pos, tree->binary.value_b, flags);
933 934 935
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
936
			return b;
937 938
		}

Michael Meeks's avatar
Michael Meeks committed
939 940
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
941
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
942 943
		res = value_new_string (tmp);

944 945 946 947
		g_free (sa);
		g_free (sb);
		g_free (tmp);

948
		if (a != NULL)
949
		value_release (a);
950
		if (b != NULL)
951
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
952
		return res;
953
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
954

955
	case OPER_FUNCALL:
956
		return eval_funcall (pos, tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
957

Michael Meeks's avatar
Michael Meeks committed
958
	case OPER_NAME:
959
		return eval_expr_name (pos, tree->name.name, flags);
Michael Meeks's avatar
Michael Meeks committed
960

961
	case OPER_VAR: {
962
		Sheet *cell_sheet;
963
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
964 965
		Cell *cell;
		int col, row;
966

967
		if (pos->sheet == NULL) {
Arturo Espinosa's avatar
Arturo Espinosa committed
968
			/* Only the test program requests this */
Morten Welinder's avatar
Morten Welinder committed
969
			return value_new_float (3.14);
Arturo Espinosa's avatar
Arturo Espinosa committed
970 971
		}

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

975
		cell_sheet = eval_sheet (ref->sheet, pos->sheet);
976
		cell = sheet_cell_get (cell_sheet, col, row);
977 978
		if (cell == NULL)
			return NULL;
979

980
		if (cell->generation != pos->sheet->workbook->generation)
981
			cell_eval (cell);
982

983
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
984
	}