expr.c 35.1 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"
Arturo Espinosa's avatar
Arturo Espinosa committed
14
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
15
#include "format.h"
16
#include "func.h"
17
#include "utils.h"
18
#include "ranges.h"
19

20 21
static Value *eval_expr_real (FunctionEvalInfo *s, ExprTree const *tree);

22
EvalPosition *
23
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, int col, int row)
24
{
25 26 27 28 29 30 31
	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);

	eval_pos->sheet = sheet;
	eval_pos->eval_col = col;
	eval_pos->eval_row = row;
32

33
	return eval_pos;
34
}
35

36 37 38 39 40 41 42 43 44 45 46 47 48
ParsePosition *
parse_pos_init (ParsePosition *pp, Workbook *wb, int col, int row)
{
	g_return_val_if_fail (wb != NULL, NULL);
	g_return_val_if_fail (pp != NULL, NULL);

	pp->wb  = wb;
	pp->col = col;
	pp->row = row;

	return pp;
}

49
EvalPosition *
50
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
51
{
52 53 54
	g_return_val_if_fail (eval_pos != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->sheet != NULL, NULL);
55 56
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

57 58 59 60 61
	return eval_pos_init (
		eval_pos,
		cell->sheet,
		cell->col->pos,
		cell->row->pos);
62
}
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
ParsePosition *
parse_pos_cell (ParsePosition *pp, Cell *cell)
{
	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,
		cell->sheet->workbook,
		cell->col->pos,
		cell->row->pos);
}

80
FunctionEvalInfo *
81
func_eval_info_init (FunctionEvalInfo *eval_info, Sheet *sheet, int col, int row)
82
{
83
	g_return_val_if_fail (eval_info != NULL, NULL);
84
       
85 86
	eval_pos_init (&eval_info->pos, sheet, col, row);
	eval_info->func_def = 0;
87

88
	return eval_info;
89 90
}

91
FunctionEvalInfo *
92
func_eval_info_cell (FunctionEvalInfo *eval_info, Cell *cell)
93
{
94 95
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
96

97 98 99
	return func_eval_info_init (
		eval_info, cell->sheet,
		cell->col->pos, cell->row->pos);
100 101
}

102 103
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *eval_info, const EvalPosition *eval_pos)
104
{
105 106
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
107

108 109 110 111 112
	return func_eval_info_init (
		eval_info,
		eval_pos->sheet,
		eval_pos->eval_col,
		eval_pos->eval_row);
113 114
}

115
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
116
expr_tree_new_constant (Value *v)
117
{
118 119 120
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
121 122 123 124 125 126 127
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = v;

Morten Welinder's avatar
Morten Welinder committed
128 129 130 131 132 133 134 135 136
	return ans;
}

ExprTree *
expr_tree_new_unary  (Operation op, ExprTree *e)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
137 138 139 140 141 142 143
	if (!ans)
		return NULL;

	ans->ref_count = 1;
	ans->oper = op;
	ans->u.value = e;

Morten Welinder's avatar
Morten Welinder committed
144 145 146 147 148 149 150 151 152 153
	return ans;
}


ExprTree *
expr_tree_new_binary (ExprTree *l, Operation op, ExprTree *r)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
154 155 156 157 158 159 160 161
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = op;
	ans->u.binary.value_a = l;
	ans->u.binary.value_b = r;

Morten Welinder's avatar
Morten Welinder committed
162 163 164 165 166 167 168 169 170 171
	return ans;
}

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

	ans = g_new (ExprTree, 1);
172 173 174 175 176 177 178 179
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_FUNCALL;
	ans->u.function.symbol = sym;;
	ans->u.function.arg_list = args;

Morten Welinder's avatar
Morten Welinder committed
180 181
	return ans;
}
182
       
183 184 185 186 187 188 189 190 191 192 193
int
expr_tree_get_const_int (ExprTree const * const expr)
{
	g_return_val_if_fail (expr != NULL, 0);
	g_return_val_if_fail (expr->oper == OPER_CONSTANT, 0);
	g_return_val_if_fail (expr->u.constant, 0);
	g_return_val_if_fail (expr->u.constant->type == VALUE_INTEGER, 0);

	return expr->u.constant->v.v_int;
}

194 195 196 197 198 199 200 201 202 203 204
char const *
expr_tree_get_const_str (ExprTree const *const expr)
{
	g_return_val_if_fail (expr != NULL, NULL);
	g_return_val_if_fail (expr->oper == OPER_CONSTANT, NULL);
	g_return_val_if_fail (expr->u.constant, NULL);
	g_return_val_if_fail (expr->u.constant->type == VALUE_STRING, NULL);

	return expr->u.constant->v.str->str;
}

205
ExprTree *
206
expr_parse_string (const char *expr, const ParsePosition *pp,
207 208 209 210 211
		   const char **desired_format, char **error_msg)
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

212
	switch (gnumeric_expr_parser (expr, pp, desired_format, &tree)) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	case PARSE_OK:
		*error_msg = NULL;
		tree->ref_count = 1;
		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;
}

Michael Meeks's avatar
Michael Meeks committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
ExprTree *
expr_tree_new_name (const ExprName *name)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_NAME;
	ans->u.name = name;

	return ans;
}
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

/*ExprTree *
expr_tree_new (void)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = NULL;
	return ans;
}*/
Morten Welinder's avatar
Morten Welinder committed
263 264 265 266 267 268 269

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

	ans = g_new (ExprTree, 1);
270 271 272 273 274 275 276
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

277 278 279
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
280 281 282 283
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
284 285 286 287 288 289 290 291 292
	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)));
	}
Morten Welinder's avatar
Morten Welinder committed
293 294

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
295
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
ExprTree *
expr_tree_array_formula (int const x, int const y, int const rows, int const cols)
{
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
	if (ans == NULL)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_ARRAY;
	ans->u.array.x = x;
	ans->u.array.y = y;
	ans->u.array.rows = rows;
	ans->u.array.cols = cols;
	ans->u.array.corner.func.value = NULL;
	ans->u.array.corner.func.expr = NULL;
	ans->u.array.corner.cell = NULL;
	return ans;
}


static ExprTree *
321
expr_tree_array_formula_corner (ExprTree const *expr, EvalPosition const *pos)
322
{
323
	Cell * corner = expr->u.array.corner.cell;
324

325 326 327 328 329 330 331 332 333 334 335 336
	/* 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,
					 pos->eval_col - expr->u.array.x,
					 pos->eval_row - expr->u.array.y);
		((ExprTree *)expr)->u.array.corner.cell = corner;
	}

	g_return_val_if_fail (corner != NULL, NULL);
337 338 339 340 341
	g_return_val_if_fail (corner->parsed_node != NULL, NULL);

	/* Sanity check incase the corner gets removed for some reason */
	g_return_val_if_fail (corner->parsed_node != (void *)0xdeadbeef, NULL);
	g_return_val_if_fail (corner->parsed_node->oper == OPER_ARRAY, NULL);
342 343
	g_return_val_if_fail (corner->parsed_node->u.array.x == 0, NULL);
	g_return_val_if_fail (corner->parsed_node->u.array.y == 0, NULL);
344 345 346 347

	return corner->parsed_node;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
348 349 350 351
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
352 353 354 355 356 357
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Morten Welinder's avatar
Morten Welinder committed
358
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
359 360 361 362 363
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
364 365 366
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
367
	switch (tree->oper){
368
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
369
		break;
370

371
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
372
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
373
		break;
374

Morten Welinder's avatar
Morten Welinder committed
375 376 377 378 379 380 381
	case OPER_FUNCALL: {
		GList *l;

		for (l = tree->u.function.arg_list; l; l = l->next)
			do_expr_tree_unref (l->data);
		g_list_free (tree->u.function.arg_list);
		symbol_unref (tree->u.function.symbol);
Arturo Espinosa's avatar
Arturo Espinosa committed
382
		break;
Morten Welinder's avatar
Morten Welinder committed
383
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
384

Michael Meeks's avatar
Michael Meeks committed
385 386 387
	case OPER_NAME:
		break;

388
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
389 390 391 392
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

393
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
394 395
		do_expr_tree_unref (tree->u.value);
		break;
396
	case OPER_ARRAY:
397 398 399
		if (tree->u.array.x == 0 && tree->u.array.y == 0) {
			if (tree->u.array.corner.func.value)
				value_release (tree->u.array.corner.func.value);
400
			do_expr_tree_unref (tree->u.array.corner.func.expr);
401
		}
402
		break;
403 404 405
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
406
	}
407

Morten Welinder's avatar
Morten Welinder committed
408
	g_free (tree);
409 410
}

Morten Welinder's avatar
Morten Welinder committed
411 412 413 414 415 416
/*
 * 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.)
 */
417 418 419 420 421 422
void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Arturo Espinosa's avatar
Arturo Espinosa committed
423
	do_expr_tree_unref (tree);
424 425
}

426
static Value *
Michael Meeks's avatar
Michael Meeks committed
427
eval_funcall (FunctionEvalInfo *ei, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
428
{
429
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
430
	FunctionDefinition *fd;
Michael Meeks's avatar
Michael Meeks committed
431
	GList *args;
432

Michael Meeks's avatar
Michael Meeks committed
433 434
	g_return_val_if_fail (ei != NULL, NULL);
	g_return_val_if_fail (tree != NULL, NULL);
435

436
	sym = tree->u.function.symbol;
437 438

	if (sym->type != SYMBOL_FUNCTION)
Michael Meeks's avatar
Michael Meeks committed
439
		return value_new_error (&ei->pos, _("Internal error"));
440

441
	fd = (FunctionDefinition *)sym->data;
Michael Meeks's avatar
Michael Meeks committed
442 443
	ei->func_def = fd;
	args = tree->u.function.arg_list;
444

Michael Meeks's avatar
Michael Meeks committed
445
	return function_call_with_list (ei, args);
Arturo Espinosa's avatar
Arturo Espinosa committed
446 447
}

448
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
449 450
	IS_EQUAL,
	IS_LESS,
451
	IS_GREATER,
452
	TYPE_MISMATCH
453 454 455
} compare_t;

static compare_t
456
compare_bool_bool (Value const * const va, Value const * const vb)
457
{
458 459 460 461 462 463 464 465 466 467 468 469 470
	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
compare_int_int (Value const * const va, Value const * const vb)
{
	int const a = value_get_as_int (va);
	int const b = value_get_as_int (vb);
471 472 473 474 475 476 477 478 479
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
480
compare_float_float (Value const * const va, Value const * const vb)
481
{
482 483
	float_t const a = value_get_as_float (va);
	float_t const b = value_get_as_float (vb);
484 485 486 487 488 489 490
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
491

492 493 494 495
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
496
compare (const Value *a, const Value *b)
Arturo Espinosa's avatar
Arturo Espinosa committed
497
{
498 499
	ValueType ta, tb;

500 501 502 503
	/* Handle trivial and double NULL case */
	if (a == b)
		return IS_EQUAL;

504 505
	ta = value_is_empty_cell (a) ? VALUE_EMPTY : a->type;
	tb = value_is_empty_cell (b) ? VALUE_EMPTY : b->type;
506

507 508 509 510 511 512
	/* 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;
513

514 515 516
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_LESS;
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		/* If both are strings compare as string */
		case VALUE_STRING :
		{
			int const t = strcasecmp (a->v.str->str, b->v.str->str);
			if (t == 0)
				return IS_EQUAL;
			else if (t > 0)
				return IS_GREATER;
			else
				return IS_LESS;
		}
		default :
			/*
			 * TODO : Add implicit intersection here if not in
			 * array mode
			 * */
534
			return TYPE_MISMATCH;
535
		}
536 537 538 539 540
	} else if (tb == VALUE_STRING) {
		switch (ta) {
		/* (empty, or number) < String */
		case VALUE_EMPTY : case VALUE_INTEGER : case VALUE_FLOAT :
			return IS_LESS;
541

542 543 544
		/* Strings are < FALSE ?? */
		case VALUE_BOOLEAN :
			return IS_GREATER;
545

546 547 548 549 550
		default :
			/*
			 * TODO : Add implicit intersection here if not in
			 * array mode
			 * */
551
			return TYPE_MISMATCH;
552 553 554
		}
	}

555 556 557 558 559 560
	/* 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;

561 562 563
	switch ((ta > tb) ? ta : tb) {
	case VALUE_EMPTY:	/* Empty Empty compare */
		return IS_EQUAL;
564

565 566
	case VALUE_BOOLEAN:
		return compare_bool_bool (a, b);
567

568 569 570 571 572 573 574 575
	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
576 577
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
/*
 * Utility routine to ensure that all elements of a range are recalced as
 * necessary.
 */
static void
eval_range (FunctionEvalInfo *s, Value *v)
{
	int start_col, start_row, end_col, end_row;
	CellRef * a = &v->v.cell_range.cell_a;
	CellRef * b = &v->v.cell_range.cell_b;
	Sheet * sheet = a->sheet ? a->sheet : s->pos.sheet;
	Cell * cell;
	int r, c;
	int const gen = s->pos.sheet->workbook->generation;

	cell_get_abs_col_row (a,
			      s->pos.eval_col, s->pos.eval_row,
			      &start_col, &start_row);
	cell_get_abs_col_row (b,
			      s->pos.eval_col, s->pos.eval_row,
			      &end_col, &end_row);

	if (a->sheet != b->sheet) {
		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;
610 611
			if (cell->generation != gen)
				cell_eval (cell);
612 613 614
		}
}

615
static Value *
616
eval_expr_real (FunctionEvalInfo *s, ExprTree const *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
617
{
618
	Value *res = NULL, *a = NULL, *b = NULL;
619
	
Arturo Espinosa's avatar
Arturo Espinosa committed
620
	g_return_val_if_fail (tree != NULL, NULL);
621
	g_return_val_if_fail (s != NULL, NULL);
622

623 624 625 626 627
	/* FIXME FIXME : We need to rework support for
	 * ranges and builtin operators to support the
	 * implicit intersection rule AND figure out
	 * how to turn it off when evaluating as an array
	 */
628
	switch (tree->oper){
629 630 631 632 633 634
	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
635
		int comp;
636

637
		a = eval_expr_real (s, tree->u.binary.value_a);
638
		if (a != NULL && a->type == VALUE_ERROR)
Morten Welinder's avatar
Morten Welinder committed
639 640
			return a;

641
		b = eval_expr_real (s, tree->u.binary.value_b);
642 643 644
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
Morten Welinder's avatar
Morten Welinder committed
645 646
			return b;
		}
647

Arturo Espinosa's avatar
Arturo Espinosa committed
648 649
		comp = compare (a, b);

650 651 652 653
		if (a != NULL)
			value_release (a);
		if (b != NULL)
			value_release (b);
654

Michael Meeks's avatar
Michael Meeks committed
655
		if (comp == TYPE_MISMATCH) {
656 657 658 659 660 661 662 663 664
			/* TODO TODO TODO : Make error more informative
			 *    regarding what is comparing to what
			 */
			/* For equality comparisons even errors are ok */
			if (tree->oper == OPER_EQUAL)
				return value_new_bool (FALSE);
			if (tree->oper == OPER_NOT_EQUAL)
				return value_new_bool (TRUE);

665
			return value_new_error (&s->pos, gnumeric_err_VALUE);
666
		}
667

Michael Meeks's avatar
Michael Meeks committed
668
		switch (tree->oper) {
669
		case OPER_EQUAL:
670
			res = value_new_bool (comp == IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
671 672
			break;

673
		case OPER_GT:
674
			res = value_new_bool (comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
675 676
			break;

677
		case OPER_LT:
678
			res = value_new_bool (comp == IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
679 680
			break;

681 682
		case OPER_NOT_EQUAL:
			res = value_new_bool (comp != IS_EQUAL);
Arturo Espinosa's avatar
Arturo Espinosa committed
683 684
			break;

685 686
		case OPER_LTE:
			res = value_new_bool (comp != IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
687 688
			break;

689 690
		case OPER_GTE:
			res = value_new_bool (comp != IS_LESS);
Arturo Espinosa's avatar
Arturo Espinosa committed
691
			break;
692

Arturo Espinosa's avatar
Arturo Espinosa committed
693
		default:
694
			g_assert_not_reached ();
695 696
			res = value_new_error (&s->pos,
						_("Internal type error"));
Arturo Espinosa's avatar
Arturo Espinosa committed
697
		}
Morten Welinder's avatar
Morten Welinder committed
698
		return res;
Arturo Espinosa's avatar
Arturo Espinosa committed
699
	}
700

701 702 703 704 705
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
706 707 708 709 710 711 712 713 714 715 716 717 718 719
	        /* Garantees that a != NULL */
		a = eval_expr (s, tree->u.binary.value_a);

		/*
		 * 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
		 */

		/* 1) Error from A */
		if (a->type == VALUE_ERROR)
720
			return a;
721

722 723 724 725 726 727 728 729
		/* 2) #!VALUE error if A is not a number */
		if (!VALUE_IS_NUMBER (a)) {
			value_release (a);
			return value_new_error (&s->pos, gnumeric_err_VALUE);
		}

	        /* Garantees that b != NULL */
		b = eval_expr (s, tree->u.binary.value_b);
Arturo Espinosa's avatar
Arturo Espinosa committed
730

731 732
		/* 3) Error from B */
		if (b->type == VALUE_ERROR) {
733
			value_release (a);
734
			return b;
Arturo Espinosa's avatar
Arturo Espinosa committed
735
		}
736

737 738
		/* 4) #!VALUE error if B is not a number */
		if (!VALUE_IS_NUMBER (b)) {
739 740
			value_release (a);
			value_release (b);
741
			return value_new_error (&s->pos, gnumeric_err_VALUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
742
		}
743

744
		if (a->type != VALUE_FLOAT && b->type != VALUE_FLOAT){
Morten Welinder's avatar
Morten Welinder committed
745
			int ia = value_get_as_int (a);
746
			int ib = value_get_as_int (b);
747 748 749
			double dres;
			int ires;

Morten Welinder's avatar
Morten Welinder committed
750 751
			value_release (a);
			value_release (b);
752

753 754
			/* FIXME: we could use simple (cheap) heuristics to
			   catch most cases where overflow will not happen.  */
755
			switch (tree->oper){
756 757 758 759 760 761 762 763
			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);

764
			case OPER_SUB:
765 766 767 768 769 770
				dres = (double)ia - (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
771

772
			case OPER_MULT:
773 774 775 776 777 778
				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
779

780
			case OPER_DIV:
781 782 783 784 785 786 787 788
				if (ib == 0)
					return value_new_error (&s->pos, gnumeric_err_DIV0);
				dres = (double)ia / (double)ib;
				ires = (int)dres;
				if (dres == ires)
					return value_new_int (ires);
				else
					return value_new_float ((float_t) dres);
789 790

			case OPER_EXP:
791
				if (ia == 0 && ib <= 0)
792 793 794 795 796 797 798 799
					return value_new_error (&s->pos, gnumeric_err_NUM);
				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
800
			default:
801
				abort ();
Arturo Espinosa's avatar
Arturo Espinosa committed
802 803
			}
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
804 805
			float_t const va = value_get_as_float (a);
			float_t const vb = value_get_as_float (b);
806 807
			value_release (a);
			value_release (b);
808

809
			switch (tree->oper){
810
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
811
				return value_new_float (va + vb);
812

813
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
814
				return value_new_float (va - vb);
815

816
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
817
				return value_new_float (va * vb);
818

819
			case OPER_DIV:
820 821 822
				return (vb == 0.0)
				    ? value_new_error (&s->pos,
						       gnumeric_err_DIV0)
Arturo Espinosa's avatar
Arturo Espinosa committed
823
				    : value_new_float (va / vb);
824

825
			case OPER_EXP:
826 827
				if (va == 0 && vb <= 0)
					return value_new_error (&s->pos, gnumeric_err_NUM);
Arturo Espinosa's avatar
Arturo Espinosa committed
828
				return value_new_float (pow (va, vb));
829

Arturo Espinosa's avatar
Arturo Espinosa committed
830
			default:
831
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
832 833
			}
		}
834 835
		return value_new_error (&s->pos, _("Unknown operator"));

Jody Goldberg's avatar
Jody Goldberg committed
836
	case OPER_PERCENT:
837 838 839 840 841 842 843 844 845
	case OPER_NEG:
	        /* Garantees that a != NULL */
		a = eval_expr (s, tree->u.value);
		if (a->type == VALUE_ERROR)
			return a;
		if (!VALUE_IS_NUMBER (a)){
			value_release (a);
			return value_new_error (&s->pos, gnumeric_err_VALUE);
		}
Jody Goldberg's avatar
Jody Goldberg committed
846 847 848 849 850 851 852 853 854
		if (tree->oper == OPER_NEG) {
			if (a->type == VALUE_INTEGER)
				res = value_new_int (-a->v.v_int);
			else if (a->type == VALUE_FLOAT)
				res = value_new_float (-a->v.v_float);
			else
				res = value_new_bool (!a->v.v_float);
		} else
			res = value_new_float (value_get_as_float (a) * .01);
855
		value_release (a);
Morten Welinder's avatar
Morten Welinder committed
856
		return res;
857

858
	case OPER_CONCAT: {
859 860
		char *sa, *sb, *tmp;

861
		a = eval_expr_real (s, tree->u.binary.value_a);
862
		if (a != NULL && a->type == VALUE_ERROR)
863
			return a;
864
		b = eval_expr_real (s, tree->u.binary.value_b);
865 866 867
		if (b != NULL && b->type == VALUE_ERROR) {
			if (a != NULL)
				value_release (a);
868
			return b;
869 870
		}

Michael Meeks's avatar
Michael Meeks committed
871 872
		sa = value_get_as_string (a);
		sb = value_get_as_string (b);
Jeff Garzik's avatar
Jeff Garzik committed
873
		tmp = g_strconcat (sa, sb, NULL);
Morten Welinder's avatar
Morten Welinder committed
874 875
		res = value_new_string (tmp);

876 877 878 879
		g_free (sa);
		g_free (sb);
		g_free (tmp);

880
		if (a != NULL)
881
		value_release (a);
882
		if (b != NULL)
883
		value_release (b);
Morten Welinder's avatar
Morten Welinder committed
884
		return res;
885
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
886

887
	case OPER_FUNCALL:
888
		return eval_funcall (s, tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
889

Michael Meeks's avatar
Michael Meeks committed
890 891 892
	case OPER_NAME:
		return eval_expr_name (s, tree->u.name);

893
	case OPER_VAR: {
894
		Sheet *cell_sheet;
895
		CellRef const *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
896 897
		Cell *cell;
		int col, row;
898

899
		if (s->pos.sheet == NULL) {
Arturo Espinosa's avatar
Arturo Espinosa committed
900
			/* Only the test program requests this */
Morten Welinder's avatar
Morten Welinder committed
901
			return value_new_float (3.14);
Arturo Espinosa's avatar
Arturo Espinosa committed
902 903
		}

904
		ref = &tree->u.ref;
905
		cell_get_abs_col_row (ref, s->pos.eval_col, s->pos.eval_row, &col, &row);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
906

907
		cell_sheet = eval_sheet (ref->sheet, s->pos.sheet);
908
		cell = sheet_cell_get (cell_sheet, col, row);
909 910
		if (cell == NULL)
			return NULL;
911

912 913
		if (cell->generation != s->pos.sheet->workbook->generation)
			cell_eval (cell);
914

915
		return value_duplicate (cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
916
	}
917

918
	case OPER_CONSTANT:
919 920 921 922
		res = tree->u.constant;
		if (res->type == VALUE_CELLRANGE)
			eval_range (s, res);
		return value_duplicate (res);
923

924 925 926
	case OPER_ARRAY:
	{
		/* The upper left corner manages the recalc of the expr */
927 928
		int x = tree->u.array.x;
		int y = tree->u.array.y;
929
		if (x == 0 && y == 0){
930 931 932 933 934 935
			/* Release old value if necessary */
			a = tree->u.array.corner.func.value;
			if (a != NULL)
				value_release (a);

			/* Store real result (cast away const)*/
936
			a = eval_expr_real (s, tree->u.array.corner.func.expr);
937
			*((Value **)&(tree->u.array.corner.func.value)) = a;
938
		} else {
939
			ExprTree const * const array =
940
			    expr_tree_array_formula_corner (tree, &s->pos);
941 942 943 944
			if (array)
				a = array->u.array.corner.func.value;
			else
				a = NULL;
945
		}
946

947 948
		if (a != NULL &&
		    (a->type == VALUE_CELLRANGE || a->type == VALUE_ARRAY)) {
949 950
			int const num_x = value_area_get_width (&s->pos, a);
			int const num_y = value_area_get_height (&s->pos, a);
951

952 953 954 955 956 957 958 959 960 961 962
			/* Evaluate relative to the upper left corner */
			EvalPosition tmp_ep = s->pos;
			tmp_ep.eval_col -= x;
			tmp_ep.eval_row -= y;

			/* If the src array is 1 element wide or tall we wrap */
			if (x >= 1 && num_x == 1)
				x = 0;
			if (y >= 1 && num_y == 1)
				y = 0;
			if (x >= num_x || y >= num_y)
963
				return value_new_error (&s->pos, gnumeric_err_NA);
964 965 966

			a = (Value *)value_area_get_x_y (&tmp_ep, a, x, y);
		}
967 968 969

		if (a == NULL)
			return NULL;
970 971
		return value_duplicate (a);
	}
972
	}
973

974
	return value_new_error (&s->pos, _("Unknown evaluation error"));
975 976 977
}

Value *
978
eval_expr (FunctionEvalInfo *s, ExprTree const *tree)
979 980
{
	Value * res = eval_expr_real (s, tree);
981 982 983 984 985 986 987 988
	if (res == NULL)
		return value_new_int (0);

	if (res->type == VALUE_EMPTY) {
		value_release (res);
		return value_new_int (0);
	}
	return res;
989
}
Arturo Espinosa's avatar
Arturo Espinosa committed
990

991
int
Arturo Espinosa's avatar
Arturo Espinosa committed
992
cell_ref_get_abs_col (CellRef const *ref, EvalPosition const *pos)
993 994 995 996 997 998 999 1000 1001 1002 1003
{
	g_return_val_if_fail (ref != NULL, 0);
	g_return_val_if_fail (pos != NULL, 0);

	if (ref->col_relative)
		return pos->eval_col + ref->col;
	return ref->col;

}

int
Arturo Espinosa's avatar
Arturo Espinosa committed
1004
cell_ref_get_abs_row (CellRef const *ref, EvalPosition const *pos)
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
{
	g_return_val_if_fail (ref != NULL, 0);
	g_return_val_if_fail (pos != NULL, 0);

	if (ref->row_relative)
		return pos->eval_row + ref->row;
	return ref->row;
}


Arturo Espinosa's avatar
Arturo Espinosa committed
1015
void
1016
cell_get_abs_col_row (const CellRef *cell_ref, int eval_col, int eval_row, int *col, int *row)
Arturo Espinosa's avatar
Arturo Espinosa committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
{
	g_return_if_fail (cell_ref != NULL);

	if (cell_ref->col_relative)
		*col = eval_col + cell_ref->col;
	else
		*col = cell_ref->col;

	if (cell_ref->row_relative)
		*row = eval_row + cell_ref->row;
	else
		*row = cell_ref->row;
}

1031 1032
/*
 * Converts a parsed tree into its string representation
1033
 * assuming that we are evaluating at col, row
1034 1035
 *
 * This routine is pretty simple: it walks the ExprTree and
1036 1037 1038
 * creates a string representation.
 *
 * FIXME: strings containing quotes will come out wrong.
1039
 */
1040
static char *
Jody Goldberg's avatar
Jody Goldberg committed
1041
do_expr_decode_tree (ExprTree *tree, ParsePosition const *pp,
1042
		     int paren_level)
1043
{
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	static struct {
		const char *name;
		int prec;	              /* Precedences -- should match parser.y  */
		int assoc_left, assoc_right;  /* 0: no, 1: yes.  */
	} operations [] = {
		{ "=",  1, 1, 0 },
		{ ">",  1, 1, 0 },
		{ "<",  1, 1, 0 },
		{ ">=", 1, 1, 0 },
		{ "<=", 1, 1, 0 },
		{ "<>", 1, 1, 0 },
		{ "+",  3, 1, 0 },
		{ "-",  3, 1, 0 },
		{ "*",  4, 1, 0 },
		{ "/",  4, 1, 0 },
		{ "^",  6, 0, 1 },
		{ "&",  2, 1, 0 },
Michael Meeks's avatar
Michael Meeks committed
1061 1062 1063 1064 1065
		{ NULL, 0, 0, 0 }, /* Funcall  */
		{ NULL, 0, 0, 0 }, /* Name     */
		{ NULL, 0, 0, 0 }, /* Constant */
		{ NULL, 0, 0, 0 }, /* Var      */
		{ "-",  5, 0, 0 },
Jody Goldberg's avatar
Jody Goldberg committed
1066
		{ "%",  5, 0, 0 },
Michael Meeks's avatar
Michael Meeks committed
1067
		{ NULL, 0, 0, 0 }  /* Array    */
1068
	};
1069
	int op;
1070

1071 1072
	op = tree->oper;

Michael Meeks's avatar
Michael Meeks committed
1073
	switch (op) {
1074
	case OPER_ANY_BINARY: {
1075
		char *a, *b, *res;
1076 1077 1078 1079
		char const *opname;
		int prec;

		prec = operations[op].prec;
1080

1081 1082
		a = do_expr_decode_tree (tree->u.binary.value_a, pp, prec - operations[op].assoc_left);
		b = do_expr_decode_tree (tree->u.binary.value_b, pp, prec - operations[op].assoc_right);
1083
		opname = operations[op].name;
1084

1085 1086
		if (prec <= paren_level)
			res = g_strconcat ("(", a, opname, b, ")", NULL);
1087
		else
1088
			res = g_strconcat (a, opname, b, NULL);
1089

1090 1091 1092 1093
		g_free (a);
		g_free (b);
		return res;
	}
1094

1095
	case OPER_ANY_UNARY: {
1096
		char *res, *a;
1097 1098
		char const *opname;
		int prec;
1099

1100
		prec = operations[op].prec;
1101
		a = do_expr_decode_tree (tree->u.value, pp, operations[op].prec);
1102 1103
		opname = operations[op].name;

Jody Goldberg's avatar
Jody Goldberg committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
		if (tree->oper == OPER_NEG) {
			if (prec <= paren_level)
				res = g_strconcat ("(", opname, a, ")", NULL);
			else
				res = g_strconcat (opname, a, NULL);
		} else {
			if (prec <= paren_level)
				res = g_strconcat ("(", a, opname, ")", NULL);
			else
				res = g_strconcat (a, opname, NULL);
		}
1115 1116 1117
		g_free (a);
		return res;
	}
1118

1119
	case OPER_FUNCALL: {
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		FunctionDefinition *fd;
		GList *arg_list, *l;
		char *res, *sum;
		char **args;
		int  argc;

		fd = tree->u.function.symbol->data;
		arg_list = tree->u.function.arg_list;
		argc = g_list_length (arg_list);

Michael Meeks's avatar
Michael Meeks committed
1130
		if (argc) {
1131 1132 1133 1134
			int i, len = 0;
			args = g_malloc (sizeof (char *) * argc);

			i = 0;
Michael Meeks's avatar
Michael Meeks committed
1135
			for (l = arg_list; l; l = l->next, i++) {