expr.c 28 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
	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
65
	default:
Arturo Espinosa's avatar
Arturo Espinosa committed
66 67 68
		g_assert_not_reached ();
		*error_msg = _("Impossible!");
		break;
69
	}
70 71 72
	return NULL;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
73 74
static void
do_expr_tree_ref (ExprTree *tree)
75
{
76
	g_return_if_fail (tree != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
77 78 79
	g_return_if_fail (tree->ref_count > 0);

	tree->ref_count++;
80
	switch (tree->oper){
81 82 83
	case OPER_VAR:
	case OPER_CONSTANT:
	case OPER_FUNCALL:
84 85
		break;

86 87 88 89 90 91 92 93 94 95 96 97
	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
98 99
		do_expr_tree_ref (tree->u.binary.value_a);
		do_expr_tree_ref (tree->u.binary.value_b);
100 101
		break;

102
	case OPER_NEG:
Arturo Espinosa's avatar
Arturo Espinosa committed
103
		do_expr_tree_ref (tree->u.value);
104 105 106 107
		break;
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
108 109 110 111
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
112 113 114 115 116 117
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
118 119 120 121 122 123 124 125
	do_expr_tree_ref (tree);
}

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

139 140 141 142 143 144 145 146 147 148 149 150
	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
151 152 153 154
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

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

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
170
	do_expr_tree_unref (tree);
171 172
}

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

	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
184
		return g_strdup_printf ("%d", value->v.v_int);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
185 186

	case VALUE_FLOAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
187
		return g_strdup_printf ("%g", value->v.v_float);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
188 189

	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
190
		return g_strdup ("ARRAY");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
191 192
		
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
193
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
194
	}
195

Arturo Espinosa's avatar
Arturo Espinosa committed
196
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
197 198
}

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

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
211 212
		break;
		
213 214 215 216
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

217 218 219 220 221 222 223
	case VALUE_ARRAY:{
		guint i;
		
		for (i = 0; i < value->v.array.x; i++)
			g_free (value->v.array.vals [i]);
		
		g_free (value->v.array.vals);
Arturo Espinosa's avatar
Arturo Espinosa committed
224
	}
225

Arturo Espinosa's avatar
Arturo Espinosa committed
226 227
	case VALUE_CELLRANGE:
		break;
228

229
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
230
	g_free (value);
231
}
232

Arturo Espinosa's avatar
Arturo Espinosa committed
233 234 235 236
/*
 * Copies a Value.
 */
void
237
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
	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
258
	case VALUE_ARRAY: {
259
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
260 261 262 263 264
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
265 266 267
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
268 269 270 271
/*
 * Makes a copy of a Value
 */
Value *
272
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
273 274 275 276 277 278 279 280 281 282
{
	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;
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
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;
}

305 306 307 308 309 310 311 312 313 314 315
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
316 317 318 319
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
320
Value *
321
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
322 323 324 325 326 327 328
{
	Value *newv;
	
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
330 331 332
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
333
	value_release (v);
Arturo Espinosa's avatar
Arturo Espinosa committed
334 335 336 337
	
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
338
int
339
value_get_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
{
	*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
365
float_t
366
value_get_as_double (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
367 368 369 370 371 372 373 374 375 376 377 378 379
{
	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
380 381 382
	if (v->type == VALUE_ARRAY)
		return 0.0;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
383
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
384 385
}

386
int
387
value_get_as_int (const Value *v)
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
{
	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;
}

407 408 409
Value *
value_array_new (guint width, guint height)
{
410
	int x, y;
411 412 413 414 415 416

	Value *v = g_new (Value, 1);
	v->type = VALUE_ARRAY;
	v->v.array.x = width;
	v->v.array.y = height;
	v->v.array.vals = g_new (Value *, width);
417 418 419 420 421 422 423

	for (x = 0; x < width; x++){
		v->v.array.vals [x] = g_new (Value, height);

		for (y = 0; y < height; y++){
			v->v.array.vals[x][y].type = VALUE_INTEGER;
			v->v.array.vals[x][y].v.v_int = 0;
424 425 426 427 428 429 430 431
		}
	}
	return v;
}

void
value_array_resize (Value *v, guint width, guint height)
{
432
	int x, xcpy, ycpy;
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	Value *newval;

	g_return_if_fail (v);
	g_return_if_fail (v->type == VALUE_ARRAY);

	newval = value_array_new (width, height);

	if (width>v->v.array.x)
		xcpy = v->v.array.x;
	else
		ycpy = width;

	if (height>v->v.array.y)
		ycpy = v->v.array.y;
	else
		ycpy = height;

450 451 452 453
	for (x = 0; x < xcpy; x++){
		memcpy (newval->v.array.vals [x],
			v->v.array.vals [x],
			sizeof (Value) * ycpy);
454 455 456 457
	}
	v->v.array.vals = newval->v.array.vals;
	v->v.array.x = width;
	v->v.array.y = height;
458
	value_release (newval);
459 460 461 462 463
}

void
value_array_copy_to (Value *v, const Value *src)
{
464
	int x;
465 466 467 468 469 470

	g_return_if_fail (src->type == VALUE_ARRAY);
	v->type = VALUE_ARRAY;
	v->v.array.x = src->v.array.x;
	v->v.array.y = src->v.array.y;
	v->v.array.vals = g_new (Value *, v->v.array.x);
471 472 473 474 475 476 477

	for (x = 0; x < v->v.array.x; x++){
		v->v.array.vals [x] = g_new (Value,v->v.array.y);
		
		memcpy (v->v.array.vals   [x],
			src->v.array.vals [x],
			sizeof(Value)*v->v.array.y);
478 479 480 481 482 483 484 485 486
	}
}

guint
value_area_get_width (Value *v)
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
487

488 489 490 491 492 493 494 495 496 497 498 499 500
	if (v->type == VALUE_ARRAY)
		return v->v.array.x;
	else
		return v->v.cell_range.cell_b.col -
		       v->v.cell_range.cell_a.col + 1;
}

guint
value_area_get_height (Value *v)
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
501

502 503 504 505 506 507 508 509 510 511 512 513 514
	if (v->type == VALUE_ARRAY)
		return v->v.array.y;
	else
		return v->v.cell_range.cell_b.row -
		       v->v.cell_range.cell_a.row + 1;
}

const Value *
value_area_get_at_x_y (Value *v, guint x, guint y)
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE,
515 516 517
			      value_int (0));

	if (v->type == VALUE_ARRAY){
518
		g_return_val_if_fail (v->v.array.x < x &&
519 520 521
				      v->v.array.y < y,
				      value_int (0));
		return &v->v.array.vals [x][y];
522 523 524
	} else {
		CellRef *a, *b;
		Cell *cell;
525

526 527 528
		a = &v->v.cell_range.cell_a;
		b = &v->v.cell_range.cell_b;
		g_return_val_if_fail (!a->col_relative,
529
				      value_int (0));
530
		g_return_val_if_fail (!b->col_relative,
531
				      value_int (0));
532
		g_return_val_if_fail (!a->row_relative,
533
				      value_int (0));
534
		g_return_val_if_fail (!b->row_relative,
535
				      value_int (0));
536
		g_return_val_if_fail (a->col<=b->col,
537
				      value_int (0));
538
		g_return_val_if_fail (a->row<=b->row,
539 540
				      value_int (0));
		
541
		cell = sheet_cell_get (a->sheet, a->col+x, a->row+y);
542

543 544 545
		if (cell && cell->value)
			return cell->value;
		else
546
			return value_int (0);
547
	}
548
	return value_int (0);
549 550
}

551 552 553 554 555 556 557 558 559 560
static void
cell_ref_make_absolute (CellRef *cell_ref, int eval_col, int eval_row)
{
	g_return_if_fail (cell_ref != NULL);

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

	if (cell_ref->row_relative)
		cell_ref->row = eval_row + cell_ref->row;
561

Arturo Espinosa's avatar
Arturo Espinosa committed
562 563
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
564 565
}

Arturo Espinosa's avatar
Arturo Espinosa committed
566 567
static Value *
eval_cell_value (Sheet *sheet, Value *value)
568
{
Arturo Espinosa's avatar
Arturo Espinosa committed
569
	Value *res;
570

Arturo Espinosa's avatar
Arturo Espinosa committed
571 572 573 574 575 576
	res = g_new (Value, 1);
	res->type = value->type;
	
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
577
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
578 579 580
		break;
		
	case VALUE_INTEGER:
581
		res->v.v_int = value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
582 583 584
		break;
		
	case VALUE_FLOAT:
585
		res->v.v_float = value->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
586
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
587 588 589 590 591 592 593

	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
594 595 596 597 598 599 600
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

601 602 603 604 605 606 607 608 609 610
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
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
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;
Arturo Espinosa's avatar
Arturo Espinosa committed
633 634 635
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
		char *arg_type = fd->args;
		char *argptr = fd->args;
636 637
	       
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
638 639 640 641
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
642 643
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
644 645
				fn_argc_min++;
			fn_argc_max++;
646
		}
647

Arturo Espinosa's avatar
Arturo Espinosa committed
648
		if (argc > fn_argc_max || argc < fn_argc_min){
Arturo Espinosa's avatar
Arturo Espinosa committed
649 650 651 652
			*error_string = _("Invalid number of arguments");
			return NULL;
		}

653
		values = g_new (Value *, fn_argc_max);
Arturo Espinosa's avatar
Arturo Espinosa committed
654
		
655
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
656
			ExprTree *t = (ExprTree *) l->data;
657
			int type_mismatch = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
658 659 660 661
			
			v = eval_expr (sheet, t, eval_col, eval_row, error_string);
			if (v == NULL)
				goto free_list;
Arturo Espinosa's avatar
Arturo Espinosa committed
662 663 664 665 666
			
			if (*arg_type=='|')
				arg_type++;
			
			switch (*arg_type){
667
			case 'f':
668 669 670 671
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
672
			case 's':
673 674
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
675
				break;
676
			case 'r':
677 678 679 680 681
				if (v->type != VALUE_CELLRANGE)
					type_mismatch = 1;
				else {
					cell_ref_make_absolute (&v->v.cell_range.cell_a, eval_col, eval_row);
					cell_ref_make_absolute (&v->v.cell_range.cell_b, eval_col, eval_row);
682
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
683
				break;
684 685 686 687 688 689 690 691 692 693
			case 'a':
				if (v->type != VALUE_ARRAY)
					type_mismatch = 1;
				break;
			case 'A':
				if (v->type != VALUE_ARRAY &&
				    v->type != VALUE_CELLRANGE)
					type_mismatch = 1;
				break;
			}
694
			if (type_mismatch){
695 696 697
				free_values (values, arg);
				*error_string = _("Type mismatch");
				return NULL;
698
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
699 700
			values [arg] = v;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
701 702
		while (arg < fn_argc_max)
			values [arg++] = NULL;
703
		v = fd->fn (fd, values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
704 705

	free_list:
706
		for (i = 0; i < arg; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
707 708
			if (values [i] != NULL)
				value_release (values [i]);
709
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
710 711 712 713 714 715
		g_free (values);
		return v;
	}
	return v;
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
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++){
735
				tree [i].oper = OPER_CONSTANT;
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
				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
771
	sym = symbol_lookup (global_symbol_table, name);
772 773 774 775
	if (sym == NULL){
		*error_string = "Function does not exist";
		return NULL;
	}
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	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;
}

791
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
792 793
	IS_EQUAL,
	IS_LESS,
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	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
819

820 821 822 823
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
Arturo Espinosa's avatar
Arturo Espinosa committed
824 825
compare (Value *a, Value *b)
{
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
	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
877 878
}

Arturo Espinosa's avatar
Arturo Espinosa committed
879
Value *
880
eval_expr (void *asheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
881 882 883
{
	Value *a, *b, *res;
	Sheet *sheet = asheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
884 885 886 887 888

	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
889
	
890
	switch (tree->oper){
891 892 893 894 895 896
	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
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
		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);

915 916 917 918 919 920 921
		if (comp == TYPE_ERROR){
			value_release (a);
			value_release (b);
			*error_string = _("Type error");
			return NULL;
		}
		
Arturo Espinosa's avatar
Arturo Espinosa committed
922
		switch (tree->oper){
923
		case OPER_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
924 925 926
			res->v.v_int = comp == IS_EQUAL;
			break;

927
		case OPER_GT:
928
			res->v.v_int = comp == IS_GREATER;
Arturo Espinosa's avatar
Arturo Espinosa committed
929 930
			break;

931
		case OPER_LT:
Arturo Espinosa's avatar
Arturo Espinosa committed
932 933 934
			res->v.v_int = comp == IS_LESS;
			break;

935
		case OPER_LTE:
Arturo Espinosa's avatar
Arturo Espinosa committed
936 937 938
			res->v.v_int = (comp == IS_EQUAL || comp == IS_LESS);
			break;

939
		case OPER_GTE:
940
			res->v.v_int = (comp == IS_EQUAL || comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
941 942
			break;

943
		case OPER_NOT_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
944 945 946 947 948 949 950 951 952 953 954
			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;
	}
	
955 956 957 958 959
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
960 961
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
962 963 964 965

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

969 970
		if (!b){
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
971 972
			return NULL;
		}
973

Arturo Espinosa's avatar
Arturo Espinosa committed
974
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
975 976
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
977 978 979 980 981 982
			*error_string = _("Type mismatch");
			return NULL;
		}
		
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
983 984 985
			int ia = a->v.v_int;
			int ib = b->v.v_int;
			
Arturo Espinosa's avatar
Arturo Espinosa committed
986 987
			res->type = VALUE_INTEGER;
			
988
			switch (tree->oper){
989
			case OPER_SUB:
990 991 992 993
			case OPER_ADD: {
				int sum;

				if (tree->oper == OPER_SUB){
Arturo Espinosa's avatar
0.20  
Arturo Espinosa committed
994
					ib = -ib;
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
				}

				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
1015
				break;
1016
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
1017
				
1018
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1019
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1020
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1021

1022
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
1023
				if (b->v.v_int == 0){
1024 1025 1026
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
1027 1028 1029
					*error_string = _("Division by zero");
					return NULL;
				}
1030 1031
				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
1032 1033
				break;
				
1034
			case OPER_EXP: 
Arturo Espinosa's avatar
Arturo Espinosa committed
1035
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
1036 1037
				break;
			default:
1038
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1039 1040 1041
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
1042
			res->v.v_float = 0.0;
1043 1044
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
1045
			
1046
			switch (tree->oper){
1047
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
1048
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1049 1050
				break;
				
1051
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
1052
				res->v.v_float = a->v.v_float - b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1053 1054
				break;
				
1055
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1056
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1057 1058
				break;
				
1059
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
1060
				if (b->v.v_float == 0.0){
1061 1062 1063
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
1064 1065 1066
					*error_string = _("Division by zero");
					return NULL;
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
1067 1068

				res->v.v_float = a->v.v_float / b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1069 1070
				break;
				
1071
			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
1072
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
1073 1074
				break;
			default:
1075
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1076 1077
			}
		}
1078 1079
		value_release (a);
		value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
1080 1081
		return res;
		
1082
	case OPER_CONCAT: {
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
		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
1101
		tmp = g_strconcat (sa, sb, NULL);
1102 1103 1104 1105 1106 1107 1108 1109 1110
		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
1111

1112
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
1113
		return eval_funcall (sheet, tree, eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
1114

1115
	case OPER_VAR: {
1116
		Sheet *cell_sheet;
1117
		CellRef *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		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;
		}

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

1134 1135 1136
		cell_sheet = ref->sheet ? ref->sheet : sheet;
		
		cell = sheet_cell_get (cell_sheet, col, row);
1137

Arturo Espinosa's avatar
Arturo Espinosa committed
1138
		if (cell){
1139
			if (cell->generation != sheet->workbook->generation){
1140
				cell->generation = sheet->workbook->generation;
1141 1142 1143 1144 1145

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

1146 1147
			if (cell->value)
				return eval_cell_value (sheet, cell->value);
1148
			else {
1149 1150 1151 1152
				if (cell->text)
					*error_string = cell->text->str;
				else
					*error_string = _("Reference to newborn cell");
1153 1154
				return NULL;
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
1155
		}
1156 1157 1158 1159
		res = g_new (Value, 1);
			
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
1160 1161
		return res;
	}
1162 1163 1164 1165
	
	case OPER_CONSTANT:
		return eval_cell_value (sheet, tree->u.constant);

1166
	case OPER_NEG:
1167 1168
		a = eval_expr (sheet, tree->u.value,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
1169 1170 1171 1172
		if (!a)
			return NULL;
		if (!VALUE_IS_NUMBER (a)){
			*error_string = _("Type mismatch");
1173
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
1174 1175 1176
			return NULL;
		}
		res = g_new (Value, 1);
1177
		res->type = a->type;
Arturo Espinosa's avatar
Arturo Espinosa committed
1178
		if (a->type == VALUE_INTEGER){
Arturo Espinosa's avatar
Arturo Espinosa committed
1179