expr.c 23.9 KB
Newer Older
Miguel de Icaza's avatar
Miguel de Icaza committed
1 2 3 4 5 6
/*
 * expr.c: Expression evaluation in Gnumeriuc
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org).
 */
7
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
8
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
9
#include <math.h>
10
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "expr.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
14
#include "format.h"
15
#include "func.h"
16

Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
17 18 19 20 21 22 23 24 25 26 27 28
/* Shared variables with parser.y */

/*       The expression being parsed */
char     *parser_expr;

/*        The suggested format to use for this expression */
char     *parser_desired_format;

/*        The error returned from the */
ParseErr  parser_error;

/*        The expression tree returned from the parser */
29
ExprTree *parser_result;
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
30

31 32 33
/*        The sheet where the parsing takes place */
void     *parser_sheet;

Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
34 35
/*        Location where the parsing is taking place */
int       parser_col, parser_row;
36

37
ExprTree *
38
expr_parse_string (char *expr, void *sheet, int col, int row, char **desired_format, char **error_msg)
39
{
40 41 42
	g_return_val_if_fail (expr != NULL, NULL);
	
	parser_expr  = expr;
43
	parser_error = PARSE_OK;
44 45 46
	parser_col   = col;
	parser_row   = row;
	parser_sheet = sheet;
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
47 48
	parser_desired_format = NULL;
	
49
	yyparse ();
50 51 52
	switch (parser_error){
	case PARSE_OK:
		*error_msg = NULL;
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
53 54
		if (desired_format)
			*desired_format = parser_desired_format;
55
		parser_result->ref_count = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
56
		return parser_result;
57

Arturo Espinosa's avatar
Arturo Espinosa committed
58 59 60 61
	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;
			
62 63 64 65
	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
	}
66 67 68
	return NULL;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
69 70
static void
do_expr_tree_ref (ExprTree *tree)
71
{
72
	g_return_if_fail (tree != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
73 74 75
	g_return_if_fail (tree->ref_count > 0);

	tree->ref_count++;
76
	switch (tree->oper){
77 78 79
	case OPER_VAR:
	case OPER_CONSTANT:
	case OPER_FUNCALL:
80 81
		break;

82 83 84 85 86 87 88 89 90 91 92 93
	case OPER_EQUAL:
	case OPER_GT:
	case OPER_LT:
	case OPER_GTE:
	case OPER_LTE:
	case OPER_NOT_EQUAL:
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
	case OPER_CONCAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
94 95
		do_expr_tree_ref (tree->u.binary.value_a);
		do_expr_tree_ref (tree->u.binary.value_b);
96 97
		break;

98
	case OPER_NEG:
Arturo Espinosa's avatar
Arturo Espinosa committed
99
		do_expr_tree_ref (tree->u.value);
100 101 102 103
		break;
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
104 105 106 107
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
108 109 110 111 112 113
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Arturo Espinosa's avatar
Arturo Espinosa committed
114 115 116 117 118 119 120 121
	do_expr_tree_ref (tree);
}

static void
do_expr_tree_unref (ExprTree *tree)
{
	tree->ref_count--;
	switch (tree->oper){
122
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
123 124
		break;
		
125
	case OPER_CONSTANT:
Arturo Espinosa's avatar
Arturo Espinosa committed
126 127 128 129
		if (tree->ref_count == 0)
			value_release (tree->u.constant);
		break;
		
130
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
131 132 133 134
		if (tree->ref_count == 0)
			symbol_unref (tree->u.function.symbol);
		break;

135 136 137 138 139 140 141 142 143 144 145 146
	case OPER_EQUAL:
	case OPER_GT:
	case OPER_LT:
	case OPER_GTE:
	case OPER_LTE:
	case OPER_NOT_EQUAL:
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
	case OPER_CONCAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
147 148 149 150
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

151
	case OPER_NEG:
Arturo Espinosa's avatar
Arturo Espinosa committed
152 153 154 155 156 157
		do_expr_tree_unref (tree->u.value);
		break;
	}
	
	if (tree->ref_count == 0)
		g_free (tree);
158 159 160 161 162 163 164 165
}

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
166
	do_expr_tree_unref (tree);
167 168
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
169 170 171 172 173 174 175 176 177 178 179
/*
 * simplistic value rendering
 */
char *
value_string (Value *value)
{
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

	case VALUE_INTEGER:
180
		return g_strdup_printf ("%d", value->v.v_int) ;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
181 182

	case VALUE_FLOAT:
183
		return g_strdup_printf ("%g", value->v.v_float) ;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
184 185

	case VALUE_ARRAY:
186
		return g_strdup ("ARRAY") ;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
187 188
		
	case VALUE_CELLRANGE:
189
		break ;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
190
	}
191 192

	return g_strdup ("Internal problem") ;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
193 194
}

195 196 197
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
198 199
	g_return_if_fail (value != NULL);
	
200 201 202 203 204 205 206
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
207 208
		break;
		
209 210 211 212
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

Arturo Espinosa's avatar
Arturo Espinosa committed
213 214 215 216 217 218 219 220 221 222
	case VALUE_ARRAY: {
		GList *l;

		for (l = value->v.array; l; l = l->next)
			value_release (l->data);
		g_list_free (l);
	}
	
	case VALUE_CELLRANGE:
		break;
223

224
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
225
	g_free (value);
226
}
227

Arturo Espinosa's avatar
Arturo Espinosa committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/*
 * Copies a Value.
 */
void
value_copy_to (Value *dest, Value *source)
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
	
	switch (source->type){
	case VALUE_STRING:
		dest->v.str = source->v.str;
		string_ref (dest->v.str);
		break;

	case VALUE_INTEGER:
		dest->v.v_int = source->v.v_int;
		break;

	case VALUE_FLOAT:
		dest->v.v_float = source->v.v_float;
		break;

Arturo Espinosa's avatar
Arturo Espinosa committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	case VALUE_ARRAY: {
		GList *l, *new = NULL;

		for (l = source->v.array; l; l = l->next){
			Value *copy;

			copy = value_duplicate (l->data);
			
			new = g_list_append (new, copy);
		}
		dest->v.array = new;
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
269 270 271
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/*
 * Makes a copy of a Value
 */
Value *
value_duplicate (Value *value)
{
	Value *new_value;

	g_return_val_if_fail (value != NULL, NULL);
	new_value = g_new (Value, 1);
	value_copy_to (new_value, value);
	
	return new_value;
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
Value *
value_float (float_t f)
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_FLOAT;
	v->v.v_float = f;

	return v;
}

Value *
value_int (int i)
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_INTEGER;
	v->v.v_int = i;

	return v;
}

309 310 311 312 313 314 315 316 317 318 319
Value *
value_str (char *str)
{
	Value *v = g_new (Value, 1);

	v->type = VALUE_STRING;
	v->v.str = string_get (str);

	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
320 321 322 323
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
324
Value *
325
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
326 327 328 329 330 331 332
{
	Value *newv;
	
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

	if (v->type == VALUE_FLOAT)
		return v;
333

Arturo Espinosa's avatar
Arturo Espinosa committed
334 335 336
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
337
	value_release (v);
Arturo Espinosa's avatar
Arturo Espinosa committed
338 339 340 341
	
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
int
value_get_bool (Value *v, int *err)
{
	*err = 0;

	if (v->type == VALUE_STRING)
		return atoi (v->v.str->str);

	if (v->type == VALUE_CELLRANGE){
		*err = 1;
		return 0;
	}

	if (v->type == VALUE_INTEGER)
		return v->v.v_int != 0;

	if (v->type == VALUE_FLOAT)
		return v->v.v_float != 0.0;

	if (v->type == VALUE_ARRAY)
		return 0;
	
	g_warning ("Unhandled value in value_get_boolean");

	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
float_t
value_get_as_double (Value *v)
{
	if (v->type == VALUE_STRING){
		return atof (v->v.str->str);
	}

	if (v->type == VALUE_CELLRANGE){
		g_warning ("Getting range as a double: what to do?");
		return 0.0;
	}

	if (v->type == VALUE_INTEGER)
		return (float_t) v->v.v_int;

Arturo Espinosa's avatar
Arturo Espinosa committed
384 385 386
	if (v->type == VALUE_ARRAY)
		return 0.0;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
387
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
388 389
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
int
value_get_as_int (Value *v)
{
	if (v->type == VALUE_STRING){
		return atoi (v->v.str->str);
	}

	if (v->type == VALUE_CELLRANGE){
		g_warning ("Getting range as a double: what to do?");
		return 0.0;
	}

	if (v->type == VALUE_INTEGER)
		return v->v.v_int;

	if (v->type == VALUE_ARRAY)
		return 0.0;
	
	return (int) v->v.v_float;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
411 412
static Value *
eval_cell_value (Sheet *sheet, Value *value)
413
{
Arturo Espinosa's avatar
Arturo Espinosa committed
414
	Value *res;
415

Arturo Espinosa's avatar
Arturo Espinosa committed
416 417 418 419 420 421
	res = g_new (Value, 1);
	res->type = value->type;
	
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
422
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
423 424 425
		break;
		
	case VALUE_INTEGER:
426
		res->v.v_int = value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
427 428 429
		break;
		
	case VALUE_FLOAT:
430
		res->v.v_float = value->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
431
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
432 433 434 435 436 437 438

	case VALUE_ARRAY:
		g_warning ("VALUE_ARRAY not handled in eval_cell_value\n");
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
		break;
			
Arturo Espinosa's avatar
Arturo Espinosa committed
439 440 441 442 443 444 445
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

446 447 448 449 450 451 452 453 454 455
static void
free_values (Value **values, int top)
{
	int i;
	
	for (i = 0; i < top; i++)
		value_release (values [i]);
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
static Value *
eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
{
	FunctionDefinition *fd;
	GList *l;
	int argc, arg, i;
	Value *v;
	
	fd = (FunctionDefinition *) tree->u.function.symbol->data;
	
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

	if (fd->expr_fn)
	{
		/* Functions that deal with ExprNodes */
		v = fd->expr_fn (sheet, l, eval_col, eval_row, error_string);
	}
	else
	{
		/* Functions that take pre-computed Values */
		Value **values;
		int fn_argc;
		char *arg_type = fd->args;
480

Arturo Espinosa's avatar
Arturo Espinosa committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
		fn_argc = strlen (fd->args);
		
		if (fn_argc != argc){
			*error_string = _("Invalid number of arguments");
			return NULL;
		}

		values = g_new (Value *, argc);
		
		for (arg = 0; l; l = l->next, arg++, arg_type++){
			ExprTree *t = (ExprTree *) l->data;
			
			v = eval_expr (sheet, t, eval_col, eval_row, error_string);
			if (v == NULL)
				goto free_list;
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

			switch (*arg_type){
			case 'f':
				if (v->type == VALUE_INTEGER ||
				    v->type == VALUE_FLOAT)
					break;
				
				free_values (values, arg);
				*error_string = _("Type mismatch");
				return NULL;
			case 's':
				if (v->type != VALUE_STRING){
					free_values (values, arg);
					*error_string = _("Type mismatch");
					return NULL;
				}
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
513 514 515
			
			values [arg] = v;
		}
516
		v = fd->fn (fd, values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
517 518 519 520 521 522 523 524 525 526

	free_list:
		for (i = 0; i < arg; i++)
			value_release (values [i]);
		g_free (values);
		return v;
	}
	return v;
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
Value *
function_def_call_with_values (Sheet *sheet, FunctionDefinition *fd, int argc,
			       Value *values [], char **error_string)
{
	Value *retval;
	
	if (fd->expr_fn){
		/*
		 * If function deals with ExprNodes, create some
		 * temporary ExprNodes with constants.
		 */
		ExprTree *tree = NULL;
		GList *l = NULL;
		int i;

		if (argc){
			tree = g_new (ExprTree, argc);
		
			for (i = 0; i < argc; i++){
546
				tree [i].oper = OPER_CONSTANT;
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
				tree [i].ref_count = 1;
				tree [i].u.constant = values [i];
				
				l = g_list_append (l, &(tree[i]));
			}
		}
		
		retval = fd->expr_fn (sheet, l, 0, 0, error_string);

		if (tree){
			g_free (tree);
			g_list_free (l);
		}

	} else 
		retval = fd->fn (fd, values, error_string);

	return retval;
}

/*
 * Use this to invoke a register function: the only drawback is that
 * you have to compute/expand all of the values to use this
 */
Value *
function_call_with_values (Sheet *sheet, char *name, int argc, Value *values[], char **error_string)
{
	FunctionDefinition *fd;
	Value *retval;
	Symbol *sym;

	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
	g_return_val_if_fail (name != NULL, NULL);

Miguel de Icaza's avatar
Miguel de Icaza committed
582
	sym = symbol_lookup (global_symbol_table, name);
583 584 585 586
	if (sym == NULL){
		*error_string = "Function does not exist";
		return NULL;
	}
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	if (sym->type != SYMBOL_FUNCTION){
		*error_string = "Calling non-function";
		return NULL;
	}
	
	fd = sym->data;

	symbol_ref (sym);
	retval = function_def_call_with_values (sheet, fd, argc, values, error_string);

	symbol_unref (sym);

	return retval;
}

602
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
603 604
	IS_EQUAL,
	IS_LESS,
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	IS_GREATER,
	TYPE_ERROR
} compare_t;

static compare_t
compare_int_int (int a, int b)
{
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
compare_float_float (float_t a, float_t b)
{
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
630

631 632 633 634
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
Arturo Espinosa's avatar
Arturo Espinosa committed
635 636
compare (Value *a, Value *b)
{
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	if (a->type == VALUE_INTEGER){
		int f;
		
		switch (b->type){
		case VALUE_INTEGER:
			return compare_int_int (a->v.v_int, b->v.v_int);

		case VALUE_FLOAT:
			return compare_float_float (a->v.v_int, b->v.v_float);

		case VALUE_STRING:
			f = value_get_as_double (b);
			return compare_float_float (a->v.v_int, f);

		default:
			return TYPE_ERROR;
		}
	}
	
	if (a->type == VALUE_FLOAT){
		float_t f;

		switch (b->type){
		case VALUE_INTEGER:
			return compare_float_float (a->v.v_float, b->v.v_int);

		case VALUE_FLOAT:
			return compare_float_float (a->v.v_float, b->v.v_float);

		case VALUE_STRING:
			f = value_get_as_double (b);
			return compare_float_float (a->v.v_float, f);

		default:
			return TYPE_ERROR;
		}
	}

	if (a->type == VALUE_STRING && b->type == VALUE_STRING){
		int t;

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

	return TYPE_ERROR;
Arturo Espinosa's avatar
Arturo Espinosa committed
688 689
}

Arturo Espinosa's avatar
Arturo Espinosa committed
690
Value *
691
eval_expr (void *asheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
692 693 694
{
	Value *a, *b, *res;
	Sheet *sheet = asheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
695 696 697 698 699

	g_return_val_if_fail (tree != NULL, NULL);
	g_return_val_if_fail (asheet != NULL, NULL);
	g_return_val_if_fail (error_string != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (asheet), NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
700
	
701
	switch (tree->oper){
702 703 704 705 706 707
	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
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
		int comp;
		
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
		if (!(a && b)){
			if (a)
				value_release (a);
			if (b)
				value_release (b);
			return NULL;
		}
		res = g_new (Value, 1);
		res->type = VALUE_INTEGER;

		comp = compare (a, b);

726 727 728 729 730 731 732
		if (comp == TYPE_ERROR){
			value_release (a);
			value_release (b);
			*error_string = _("Type error");
			return NULL;
		}
		
Arturo Espinosa's avatar
Arturo Espinosa committed
733
		switch (tree->oper){
734
		case OPER_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
735 736 737
			res->v.v_int = comp == IS_EQUAL;
			break;

738
		case OPER_GT:
739
			res->v.v_int = comp == IS_GREATER;
Arturo Espinosa's avatar
Arturo Espinosa committed
740 741
			break;

742
		case OPER_LT:
Arturo Espinosa's avatar
Arturo Espinosa committed
743 744 745
			res->v.v_int = comp == IS_LESS;
			break;

746
		case OPER_LTE:
Arturo Espinosa's avatar
Arturo Espinosa committed
747 748 749
			res->v.v_int = (comp == IS_EQUAL || comp == IS_LESS);
			break;

750
		case OPER_GTE:
751
			res->v.v_int = (comp == IS_EQUAL || comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
752 753
			break;

754
		case OPER_NOT_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
755 756 757 758 759 760 761 762 763 764 765
			res->v.v_int = comp != IS_EQUAL;
			break;
			
		default:
			g_warning ("This should never be reached: comparission ops\n");
		}
		value_release (a);
		value_release (b);
		return res;
	}
	
766 767 768 769 770
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
771 772
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
773 774 775 776

		if (!a)
			return NULL;
		
777 778
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
779

780 781
		if (!b){
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
782 783
			return NULL;
		}
784

Arturo Espinosa's avatar
Arturo Espinosa committed
785
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
786 787
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
788 789 790 791 792 793
			*error_string = _("Type mismatch");
			return NULL;
		}
		
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
794 795 796
			int ia = a->v.v_int;
			int ib = b->v.v_int;
			
Arturo Espinosa's avatar
Arturo Espinosa committed
797 798
			res->type = VALUE_INTEGER;
			
799
			switch (tree->oper){
800
			case OPER_SUB:
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
			case OPER_ADD: {
				int sum;

				if (tree->oper == OPER_SUB){
					int t = ia;
					
					ia = ib;
					ib = -t;
				}

				sum = ia + ib;
				
				if ((ia > 0) && (ib > 0)){
					
					if (sum < ia){
						res->type = VALUE_FLOAT;
						res->v.v_float = ((double)ia) + ib;
					} else
						res->v.v_int = sum;
				} else if ((ia < 0) && (ib < 0)){
					if (sum > ia){
						res->type = VALUE_FLOAT;
						res->v.v_float = ((double)ia) + ib;
					} else
						res->v.v_int = sum;
				} else
					res->v.v_int = sum;
				
Arturo Espinosa's avatar
Arturo Espinosa committed
829
				break;
830
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
831
				
832
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
833
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
834
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
835

836
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
837
				if (b->v.v_int == 0){
838 839 840
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
841 842 843
					*error_string = _("Division by zero");
					return NULL;
				}
844 845
				res->type = VALUE_FLOAT;
				res->v.v_float =  a->v.v_int / (float_t)b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
846 847
				break;
				
848
			case OPER_EXP: 
Arturo Espinosa's avatar
Arturo Espinosa committed
849
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
850 851
				break;
			default:
852
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
853 854 855
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
856
			res->v.v_float = 0.0;
857 858
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
859
			
860
			switch (tree->oper){
861
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
862
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
863 864
				break;
				
865
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
866
				res->v.v_float = a->v.v_float - b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
867 868
				break;
				
869
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
870
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
871 872
				break;
				
873
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
874
				if (b->v.v_float == 0.0){
875 876 877
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
878 879 880
					*error_string = _("Division by zero");
					return NULL;
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
881 882

				res->v.v_float = a->v.v_float / b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
883 884
				break;
				
885
			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
886
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
887 888
				break;
			default:
889
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
890 891
			}
		}
892 893
		value_release (a);
		value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
894 895
		return res;
		
896
	case OPER_CONCAT: {
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
		char *sa, *sb, *tmp;

		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		if (!a)
			return NULL;
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
		if (!b){
			value_release (a);
			return NULL;
		}

		res = g_new (Value, 1);
		res->type = VALUE_STRING;
		sa = value_string (a);
		sb = value_string (b);
		
Jeff Garzik's avatar
Jeff Garzik committed
915
		tmp = g_strconcat (sa, sb, NULL);
916 917 918 919 920 921 922 923 924
		res->v.str = string_get (tmp);
		g_free (sa);
		g_free (sb);
		g_free (tmp);

		value_release (a);
		value_release (b);
		return res;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
925

926
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
927
		return eval_funcall (sheet, tree, eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
928

929
	case OPER_VAR: {
930
		Sheet *cell_sheet;
931
		CellRef *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
932 933 934 935 936 937 938 939 940 941 942 943 944
		Cell *cell;
		int col, row;
		
		if (sheet == NULL){
			/* Only the test program requests this */
			res = g_new (Value, 1);

			res->type = VALUE_FLOAT;
			res->v.v_float = 3.14;
			
			return res;
		}

945
		ref = &tree->u.ref;
946
		cell_get_abs_col_row (ref, eval_col, eval_row, &col, &row);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
947

948 949 950
		cell_sheet = ref->sheet ? ref->sheet : sheet;
		
		cell = sheet_cell_get (cell_sheet, col, row);
951

Arturo Espinosa's avatar
Arturo Espinosa committed
952
		if (cell){
953
			if (cell->generation != sheet->workbook->generation){
954
				cell->generation = sheet->workbook->generation;
955 956 957 958 959

				if (cell->parsed_node)
					cell_eval (cell);
			}

960 961
			if (cell->value)
				return eval_cell_value (sheet, cell->value);
962
			else {
963 964 965 966
				if (cell->text)
					*error_string = cell->text->str;
				else
					*error_string = _("Reference to newborn cell");
967 968
				return NULL;
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
969
		}
970 971 972 973
		res = g_new (Value, 1);
			
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
974 975
		return res;
	}
976 977 978 979
	
	case OPER_CONSTANT:
		return eval_cell_value (sheet, tree->u.constant);

980
	case OPER_NEG:
981 982
		a = eval_expr (sheet, tree->u.value,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
983 984 985 986
		if (!a)
			return NULL;
		if (!VALUE_IS_NUMBER (a)){
			*error_string = _("Type mismatch");
987
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
988 989 990
			return NULL;
		}
		res = g_new (Value, 1);
991
		res->type = a->type;
Arturo Espinosa's avatar
Arturo Espinosa committed
992
		if (a->type == VALUE_INTEGER){
Arturo Espinosa's avatar
Arturo Espinosa committed
993
			res->v.v_int = -a->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
994
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
995
			res->v.v_float = -a->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
996
		}
997
		value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
998
		return res;
999
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
1000 1001 1002
	
	*error_string = _("Unknown evaluation error");
	return NULL;
1003
}
Arturo Espinosa's avatar
Arturo Espinosa committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

void
cell_get_abs_col_row (CellRef *cell_ref, int eval_col, int eval_row, int *col, int *row)
{
	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;
}

1021 1022 1023
static int
evaluate_level (Operation x)
{
1024
	if (x == OPER_EXP)
1025
		return 3;
1026
	if ((x==OPER_MULT) || (x==OPER_DIV))
1027
		return 2;
1028
	if ((x==OPER_ADD)  || (x==OPER_SUB)   || (x==OPER_CONCAT))
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
		return 1;
	return 0;
}

static int
bigger_prec (Operation parent, Operation this)
{
	int parent_level, this_level;

	parent_level = evaluate_level (parent);
	this_level   = evaluate_level (this);

1041
	return parent_level >= this_level;
1042 1043
}

1044 1045 1046 1047 1048 1049 1050 1051 1052
/*
 * Converts a parsed tree into its string representation
 * assuming that we are evaluating at col, row (This is
 * only used during copying to "render" a new text
 * representation for a copied cell.
 *
 * This routine is pretty simple: it walks the ExprTree and
 * create a string representation.
 */
1053
static char *
1054
do_expr_decode_tree (ExprTree *tree, void *sheet, int col, int row, Operation parent_op)
1055 1056 1057 1058 1059 1060 1061 1062 1063
{
	static const char *binary_operation_names [] = {
		"=", ">", "<", ">=", "<=", "<>",
		"+", "-", "*", "/",  "^",  "&"
	};

	switch (tree->oper){

		/* The binary operations */
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE:
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
	case OPER_CONCAT: {	
1076 1077 1078
		char *a, *b, *res;
		char const *op;
		
1079 1080
		a = do_expr_decode_tree (tree->u.binary.value_a, sheet, col, row, tree->oper);
		b = do_expr_decode_tree (tree->u.binary.value_b, sheet, col, row, tree->oper);
1081
		op = binary_operation_names [tree->oper];
1082 1083

		if (bigger_prec (parent_op, tree->oper))
Jeff Garzik's avatar
Jeff Garzik committed
1084
			res = g_strconcat ("(", a, op, b, ")", NULL);
1085
		else
Jeff Garzik's avatar
Jeff Garzik committed
1086
			res = g_strconcat (a, op, b, NULL);
1087
		
1088 1089 1090 1091 1092
		g_free (a);
		g_free (b);
		return res;
	}
	
1093
	case OPER_NEG: {
1094 1095
		char *res, *a;

1096
		a = do_expr_decode_tree (tree->u.value, sheet, col, row, tree->oper);
1097
		res = g_strconcat ("-", a, NULL);
1098 1099 1100 1101
		g_free (a);
		return res;
	}
	
1102
	case OPER_FUNCALL: {
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
		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);

		if (argc){
			int i, len = 0;
			args = g_malloc (sizeof (char *) * argc);

			i = 0;
1118
			for (l = arg_list; l; l = l->next, i++){
1119 1120
				ExprTree *t = l->data;
				
1121
				args [i] = do_expr_decode_tree (t, sheet, col, row, OPER_CONSTANT);
1122 1123 1124
				len += strlen (args [i]) + 1;
			}
			len++;
1125
			sum = g_malloc (len + 2);
1126 1127 1128
			
			i = 0;
			sum [0] = 0;
1129
			for (l = arg_list; l; l = l->next, i++){
1130
				strcat (sum, args [i]);
1131 1132 1133 1134
				if (l->next)
					strcat (sum, ",");
			}
			
Jeff Garzik's avatar
Jeff Garzik committed
1135
			res = g_strconcat (
1136 1137 1138 1139 1140 1141 1142 1143
				fd->name, "(", sum, ")", NULL);

			for (i = 0; i < argc; i++)
				g_free (args [i]);
			g_free (args);

			return res;
		} else
Jeff Garzik's avatar
Jeff Garzik committed
1144
			return g_strconcat (fd->name, "()", NULL);
1145 1146
	}
	
1147 1148 1149
	case OPER_VAR: {
		CellRef *cell_ref;
		
1150
		cell_ref = &tree->u.ref;
1151 1152 1153
		return cellref_name (cell_ref, sheet, col, row);
	}

1154
	case OPER_CONSTANT: {
1155 1156 1157
		Value *v = tree->u.constant;

		if (v->type == VALUE_CELLRANGE){
1158 1159 1160 1161
			char *a, *b, *res;

			a = cellref_name (&v->v.cell_range.cell_a, sheet, col, row);
			b = cellref_name (&v->v.cell_range.cell_b, sheet, col, row);
1162

Jeff Garzik's avatar
Jeff Garzik committed
1163
			res = g_strconcat (a, ":", b, NULL);
1164

1165 1166 1167 1168
			g_free (a);
			g_free (b);
			
			return res;
1169 1170
		} else {
			if (v->type == VALUE_STRING){
Jeff Garzik's avatar
Jeff Garzik committed
1171
				return g_strconcat ("\"", v->v.str->str, "\"", NULL);
1172 1173 1174
			} else
				return value_string (v);
		}
1175 1176 1177 1178 1179 1180 1181
	}
	}

	g_warning ("ExprTree: This should not happen\n");
	return g_strdup ("0");
}

1182
char *
1183
expr_decode_tree (ExprTree *tree, void *sheet, int col, int row)
1184 1185
{
	g_return_val_if_fail (tree != NULL, NULL);
1186 1187 1188 1189
	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
	
	return do_expr_decode_tree (tree, sheet, col, row, OPER_CONSTANT);
1190
}
1191

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
static ExprTree *
do_expr_tree_relocate (ExprTree *tree, int coldiff, int rowdiff)
{
	ExprTree *new_tree;

	new_tree = g_new (ExprTree, 1);
	*new_tree = *tree;
	
	new_tree->ref_count = 1;
	
	switch (tree->oper){
		/* The binary operations */
	case OPER_EQUAL:
	case OPER_NOT_EQUAL:
	case OPER_GT:
	case OPER_GTE:
	case OPER_LT:
	case OPER_LTE:
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
	case OPER_CONCAT: {
		ExprTree *a, *b;
		
		a = do_expr_tree_relocate (tree->u.binary.value_a, coldiff, rowdiff);
		b = do_expr_tree_relocate (tree->u.binary.value_b, coldiff, rowdiff);

		new_tree->u.binary.value_a = a;
		new_tree->u.binary.value_b = b;
		break;
	}

	case OPER_FUNCALL: {
		GList *l, *arg_list;
		GList *new_list = NULL;

		tree->ref_count++;
		
		arg_list = tree->u.function.arg_list;

		for (l = arg_list; l; l = l->next){
			ExprTree *tree = l->data;

			new_list = g_list_append (
				new_list,
				do_expr_tree_relocate (tree, coldiff, rowdiff));
			new_tree->u.function.arg_list = new_list;
		}
		symbol_ref (tree->u.function.symbol);
		break;
	}

1246
	case OPER_VAR: {
1247 1248
		CellRef *ref;
		
1249 1250 1251 1252 1253 1254 1255
		ref = &new_tree->u.ref;
		ref->col -= coldiff;
		ref->row -= rowdiff;
		break;
	}
	
	case OPER_CONSTANT:
1256 1257
		new_tree->u.constant = value_duplicate (tree->u.constant);

1258 1259 1260
		if (new_tree->u.constant->type == VALUE_CELLRANGE){
			CellRef *ref;
		
1261 1262