expr.c 38.1 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

17
ExprTree *
18 19
expr_parse_string (const char *expr, Sheet *sheet, int col, int row,
		   const char **desired_format, char **error_msg)
20
{
21
	ExprTree *tree;
22
	g_return_val_if_fail (expr != NULL, NULL);
23 24

	switch (gnumeric_expr_parser (expr, sheet, col, row, desired_format, &tree)) {
25 26
	case PARSE_OK:
		*error_msg = NULL;
27 28
		tree->ref_count = 1;
		return tree;
29

Arturo Espinosa's avatar
Arturo Espinosa committed
30 31 32
	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;
33

34 35 36
	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
37
	default:
Arturo Espinosa's avatar
Arturo Espinosa committed
38 39 40
		g_assert_not_reached ();
		*error_msg = _("Impossible!");
		break;
41
	}
42 43 44
	return NULL;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
45 46
static void
do_expr_tree_ref (ExprTree *tree)
47
{
48
	g_return_if_fail (tree != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
49 50 51
	g_return_if_fail (tree->ref_count > 0);

	tree->ref_count++;
52
	switch (tree->oper){
53 54 55
	case OPER_VAR:
	case OPER_CONSTANT:
	case OPER_FUNCALL:
56 57
		break;

58
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
59 60
		do_expr_tree_ref (tree->u.binary.value_a);
		do_expr_tree_ref (tree->u.binary.value_b);
61 62
		break;

63
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
64
		do_expr_tree_ref (tree->u.value);
65 66 67 68
		break;
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
69 70 71 72
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
73 74 75 76 77 78
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
79 80 81 82 83 84 85 86
	do_expr_tree_ref (tree);
}

static void
do_expr_tree_unref (ExprTree *tree)
{
	tree->ref_count--;
	switch (tree->oper){
87
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
88
		break;
89

90
	case OPER_CONSTANT:
Arturo Espinosa's avatar
Arturo Espinosa committed
91 92 93
		if (tree->ref_count == 0)
			value_release (tree->u.constant);
		break;
94

95
	case OPER_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
96 97 98 99
		if (tree->ref_count == 0)
			symbol_unref (tree->u.function.symbol);
		break;

100
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
101 102 103 104
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

105
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
106 107 108
		do_expr_tree_unref (tree->u.value);
		break;
	}
109

Arturo Espinosa's avatar
Arturo Espinosa committed
110 111
	if (tree->ref_count == 0)
		g_free (tree);
112 113 114 115 116 117 118 119
}

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
120
	do_expr_tree_unref (tree);
121 122
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
123 124 125 126
/*
 * simplistic value rendering
 */
char *
127
value_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
128 129 130 131 132 133
{
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

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

	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
140
		return g_strdup ("ARRAY");
141

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
142
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
143
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
144
	}
145

Arturo Espinosa's avatar
Arturo Espinosa committed
146
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
147 148
}

149 150 151
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
152
	g_return_if_fail (value != NULL);
153

154 155 156 157 158 159 160
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
161
		break;
162

163 164 165 166
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

167 168
	case VALUE_ARRAY:{
		guint i;
169

170 171
		for (i = 0; i < value->v.array.x; i++)
			g_free (value->v.array.vals [i]);
172

173
		g_free (value->v.array.vals);
Arturo Espinosa's avatar
Arturo Espinosa committed
174
	}
175

Arturo Espinosa's avatar
Arturo Espinosa committed
176 177
	case VALUE_CELLRANGE:
		break;
178

179
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
180
	g_free (value);
181
}
182

Arturo Espinosa's avatar
Arturo Espinosa committed
183 184 185 186
/*
 * Copies a Value.
 */
void
187
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
188 189 190 191 192
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
193

Arturo Espinosa's avatar
Arturo Espinosa committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
	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
208
	case VALUE_ARRAY: {
209
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
210 211 212 213 214
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
215 216 217
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
218 219 220 221
/*
 * Makes a copy of a Value
 */
Value *
222
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
223 224 225 226 227 228
{
	Value *new_value;

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

Arturo Espinosa's avatar
Arturo Espinosa committed
230 231 232
	return new_value;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
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;
}

255
Value *
256
value_str (const char *str)
257 258 259 260 261 262 263 264 265
{
	Value *v = g_new (Value, 1);

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

	return v;
}

266 267 268 269 270 271 272 273 274 275
Value *
value_cellrange (CellRef *a, CellRef *b)
{
	Value *v = g_new (Value, 1);
	v->type = VALUE_CELLRANGE;
	memcpy (&v->v.cell_range.cell_a, a, sizeof(CellRef));
	memcpy (&v->v.cell_range.cell_b, b, sizeof(CellRef));
	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
276 277 278 279
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
280
Value *
281
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
282 283
{
	Value *newv;
284

Arturo Espinosa's avatar
Arturo Espinosa committed
285 286 287 288
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
290 291 292
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
293
	value_release (v);
294

Arturo Espinosa's avatar
Arturo Espinosa committed
295 296 297
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
298
int
299
value_get_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
	*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;
319

Arturo Espinosa's avatar
Arturo Espinosa committed
320 321 322 323 324
	g_warning ("Unhandled value in value_get_boolean");

	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
325
float_t
326
value_get_as_double (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
327 328 329 330 331 332 333 334 335 336 337 338 339
{
	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
340 341
	if (v->type == VALUE_ARRAY)
		return 0.0;
342

Arturo Espinosa's avatar
Arturo Espinosa committed
343
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
344 345
}

346
int
347
value_get_as_int (const Value *v)
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
{
	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;
363

364 365 366
	return (int) v->v.v_float;
}

367 368 369
Value *
value_array_new (guint width, guint height)
{
370
	int x, y;
371 372 373 374 375 376

	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);
377 378 379 380 381 382 383

	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;
384 385 386 387 388 389 390 391
		}
	}
	return v;
}

void
value_array_resize (Value *v, guint width, guint height)
{
392
	int x, xcpy, ycpy;
393 394 395 396 397 398 399 400 401 402
	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
403
		xcpy = width;
404 405 406 407 408 409

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

410 411 412 413
	for (x = 0; x < xcpy; x++){
		memcpy (newval->v.array.vals [x],
			v->v.array.vals [x],
			sizeof (Value) * ycpy);
414 415 416 417
	}
	v->v.array.vals = newval->v.array.vals;
	v->v.array.x = width;
	v->v.array.y = height;
418
	value_release (newval);
419 420 421 422 423
}

void
value_array_copy_to (Value *v, const Value *src)
{
424
	int x;
425 426 427 428 429 430

	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);
431 432 433

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

435 436 437
		memcpy (v->v.array.vals   [x],
			src->v.array.vals [x],
			sizeof(Value)*v->v.array.y);
438 439 440 441 442 443 444 445 446
	}
}

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);
447

448 449 450 451 452 453 454 455 456 457 458 459 460
	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);
461

462 463 464 465 466 467 468 469 470 471 472 473 474
	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,
475 476 477
			      value_int (0));

	if (v->type == VALUE_ARRAY){
478
		g_return_val_if_fail (v->v.array.x < x &&
479 480 481
				      v->v.array.y < y,
				      value_int (0));
		return &v->v.array.vals [x][y];
482 483 484
	} else {
		CellRef *a, *b;
		Cell *cell;
485

486 487 488
		a = &v->v.cell_range.cell_a;
		b = &v->v.cell_range.cell_b;
		g_return_val_if_fail (!a->col_relative,
489
				      value_int (0));
490
		g_return_val_if_fail (!b->col_relative,
491
				      value_int (0));
492
		g_return_val_if_fail (!a->row_relative,
493
				      value_int (0));
494
		g_return_val_if_fail (!b->row_relative,
495
				      value_int (0));
496
		g_return_val_if_fail (a->col<=b->col,
497
				      value_int (0));
498
		g_return_val_if_fail (a->row<=b->row,
499
				      value_int (0));
500

501
		cell = sheet_cell_get (a->sheet, a->col+x, a->row+y);
502

503 504 505
		if (cell && cell->value)
			return cell->value;
		else
506
			return value_int (0);
507
	}
508
	return value_int (0);
509 510
}

511 512 513 514 515 516
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)
517
		cell_ref->col = eval_col + cell_ref->col;
518 519 520

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

Arturo Espinosa's avatar
Arturo Espinosa committed
522 523
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
524 525
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539
static void
cell_ref_restore_absolute (CellRef *cell_ref, const CellRef *orig, int eval_col, int eval_row)
{
	if (orig->col_relative) {
		cell_ref->col -= eval_col;
		cell_ref->col_relative = 1;
	}

	if (orig->row_relative) {
		cell_ref->row -= eval_row;
		cell_ref->row_relative = 1;
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
540 541
static Value *
eval_cell_value (Sheet *sheet, Value *value)
542
{
Arturo Espinosa's avatar
Arturo Espinosa committed
543
	Value *res;
544

Arturo Espinosa's avatar
Arturo Espinosa committed
545 546
	res = g_new (Value, 1);
	res->type = value->type;
547

Arturo Espinosa's avatar
Arturo Espinosa committed
548 549 550
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
551
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
552
		break;
553

Arturo Espinosa's avatar
Arturo Espinosa committed
554
	case VALUE_INTEGER:
555
		res->v.v_int = value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
556
		break;
557

Arturo Espinosa's avatar
Arturo Espinosa committed
558
	case VALUE_FLOAT:
559
		res->v.v_float = value->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
560
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
561 562 563 564 565 566

	case VALUE_ARRAY:
		g_warning ("VALUE_ARRAY not handled in eval_cell_value\n");
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
		break;
567

Arturo Espinosa's avatar
Arturo Espinosa committed
568 569 570 571 572 573 574
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

575 576 577 578
static void
free_values (Value **values, int top)
{
	int i;
579

580 581 582 583 584
	for (i = 0; i < top; i++)
		value_release (values [i]);
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
585 586 587 588 589 590 591
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;
592

Arturo Espinosa's avatar
Arturo Espinosa committed
593
	fd = (FunctionDefinition *) tree->u.function.symbol->data;
594

Arturo Espinosa's avatar
Arturo Espinosa committed
595 596 597 598 599 600 601 602 603 604 605 606
	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
607 608 609
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
		char *arg_type = fd->args;
		char *argptr = fd->args;
610

611
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
612 613 614 615
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
616 617
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
618 619
				fn_argc_min++;
			fn_argc_max++;
620
		}
621

Arturo Espinosa's avatar
Arturo Espinosa committed
622
		if (argc > fn_argc_max || argc < fn_argc_min){
Arturo Espinosa's avatar
Arturo Espinosa committed
623 624 625 626
			*error_string = _("Invalid number of arguments");
			return NULL;
		}

627
		values = g_new (Value *, fn_argc_max);
628

629
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
630
			ExprTree *t = (ExprTree *) l->data;
631
			int type_mismatch = 0;
632

Arturo Espinosa's avatar
Arturo Espinosa committed
633 634
			if (*arg_type=='|')
				arg_type++;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

			if ((*arg_type != 'A' &&          /* This is so a cell reference */
			     *arg_type != 'r') ||         /* can be converted to a cell range */
			    !t || (t->oper != OPER_VAR)) { /* without being evaluated */
				if ((v = eval_expr (sheet, t, eval_col,
						    eval_row, error_string))==NULL)
					goto free_list;
			} else {
				g_assert (t->oper == OPER_VAR);
				v = value_cellrange (&t->u.ref,
						     &t->u.ref);
				if (!v->v.cell_range.cell_a.sheet)
					v->v.cell_range.cell_a.sheet = sheet;
				if (!v->v.cell_range.cell_b.sheet)
					v->v.cell_range.cell_b.sheet = sheet;
			}
651

Arturo Espinosa's avatar
Arturo Espinosa committed
652
			switch (*arg_type){
653
			case 'f':
654

655 656 657 658
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
659
			case 's':
660 661
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
662
				break;
663
			case 'r':
664 665 666 667 668
				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);
669
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
670
				break;
671 672 673 674 675 676 677 678
			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;
Michael Meeks's avatar
Michael Meeks committed
679 680 681 682 683

				if (v->type == VALUE_CELLRANGE) {
					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);
				}
684 685
				break;
			}
686
			if (type_mismatch){
687 688 689
				free_values (values, arg);
				*error_string = _("Type mismatch");
				return NULL;
690
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
691 692
			values [arg] = v;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
693 694
		while (arg < fn_argc_max)
			values [arg++] = NULL;
695
		v = fd->fn (fd, values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
696 697

	free_list:
698
		for (i = 0; i < arg; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
699 700
			if (values [i] != NULL)
				value_release (values [i]);
701
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
702 703 704 705 706 707
		g_free (values);
		return v;
	}
	return v;
}

708 709 710 711 712
Value *
function_def_call_with_values (Sheet *sheet, FunctionDefinition *fd, int argc,
			       Value *values [], char **error_string)
{
	Value *retval;
713

714 715 716 717 718 719 720 721 722 723 724
	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);
725

726
			for (i = 0; i < argc; i++){
727
				tree [i].oper = OPER_CONSTANT;
728 729
				tree [i].ref_count = 1;
				tree [i].u.constant = values [i];
730

731 732 733
				l = g_list_append (l, &(tree[i]));
			}
		}
734

735 736 737 738 739 740 741
		retval = fd->expr_fn (sheet, l, 0, 0, error_string);

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

742
	} else
743 744 745 746 747 748 749 750 751 752
		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 *
753
function_call_with_values (Sheet *sheet, const char *name, int argc, Value *values[], char **error_string)
754 755 756 757 758 759 760 761 762
{
	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
763
	sym = symbol_lookup (global_symbol_table, name);
764
	if (sym == NULL){
765
		*error_string = _("Function does not exist");
766 767
		return NULL;
	}
768
	if (sym->type != SYMBOL_FUNCTION){
769
		*error_string = _("Calling non-function");
770 771
		return NULL;
	}
772

773 774 775 776 777 778 779 780 781 782
	fd = sym->data;

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

	symbol_unref (sym);

	return retval;
}

783
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
784 785
	IS_EQUAL,
	IS_LESS,
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	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
811

812 813 814 815
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
Arturo Espinosa's avatar
Arturo Espinosa committed
816 817
compare (Value *a, Value *b)
{
818 819
	if (a->type == VALUE_INTEGER){
		int f;
820

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
		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;
		}
	}
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
	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
869 870
}

Arturo Espinosa's avatar
Arturo Espinosa committed
871
Value *
872
eval_expr (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
873 874
{
	Value *a, *b, *res;
Arturo Espinosa's avatar
Arturo Espinosa committed
875 876

	g_return_val_if_fail (tree != NULL, NULL);
877
	g_return_val_if_fail (sheet != NULL, NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
878
	g_return_val_if_fail (error_string != NULL, NULL);
879
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
880

881
	switch (tree->oper){
882 883 884 885 886 887
	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
888
		int comp;
889

Arturo Espinosa's avatar
Arturo Espinosa committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
		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);

906 907 908 909 910 911
		if (comp == TYPE_ERROR){
			value_release (a);
			value_release (b);
			*error_string = _("Type error");
			return NULL;
		}
912

Arturo Espinosa's avatar
Arturo Espinosa committed
913
		switch (tree->oper){
914
		case OPER_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
915 916 917
			res->v.v_int = comp == IS_EQUAL;
			break;

918
		case OPER_GT:
919
			res->v.v_int = comp == IS_GREATER;
Arturo Espinosa's avatar
Arturo Espinosa committed
920 921
			break;

922
		case OPER_LT:
Arturo Espinosa's avatar
Arturo Espinosa committed
923 924 925
			res->v.v_int = comp == IS_LESS;
			break;

926
		case OPER_LTE:
Arturo Espinosa's avatar
Arturo Espinosa committed
927 928 929
			res->v.v_int = (comp == IS_EQUAL || comp == IS_LESS);
			break;

930
		case OPER_GTE:
931
			res->v.v_int = (comp == IS_EQUAL || comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
932 933
			break;

934
		case OPER_NOT_EQUAL:
Arturo Espinosa's avatar
Arturo Espinosa committed
935 936
			res->v.v_int = comp != IS_EQUAL;
			break;
937

Arturo Espinosa's avatar
Arturo Espinosa committed
938 939 940 941 942 943 944
		default:
			g_warning ("This should never be reached: comparission ops\n");
		}
		value_release (a);
		value_release (b);
		return res;
	}
945

946 947 948 949 950
	case OPER_ADD:
	case OPER_SUB:
	case OPER_MULT:
	case OPER_DIV:
	case OPER_EXP:
951 952
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
953 954 955

		if (!a)
			return NULL;
956

957 958
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
959

960 961
		if (!b){
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
962 963
			return NULL;
		}
964

Arturo Espinosa's avatar
Arturo Espinosa committed
965
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
966 967
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
968 969 970
			*error_string = _("Type mismatch");
			return NULL;
		}
971

Arturo Espinosa's avatar
Arturo Espinosa committed
972 973
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
974 975
			int ia = a->v.v_int;
			int ib = b->v.v_int;
976

Arturo Espinosa's avatar
Arturo Espinosa committed
977
			res->type = VALUE_INTEGER;
978

979
			switch (tree->oper){
980
			case OPER_SUB:
981 982 983 984
			case OPER_ADD: {
				int sum;

				if (tree->oper == OPER_SUB){
Arturo Espinosa's avatar
0.20  
Arturo Espinosa committed
985
					ib = -ib;
986 987 988
				}

				sum = ia + ib;
989

990
				if ((ia > 0) && (ib > 0)){
991

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
					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;
1005

Arturo Espinosa's avatar
Arturo Espinosa committed
1006
				break;
1007
			}
1008

1009
			case OPER_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
1010
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
1011
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1012

1013
			case OPER_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
1014
				if (b->v.v_int == 0){
1015 1016 1017
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
1018 1019 1020
					*error_string = _("Division by zero");
					return NULL;
				}
1021 1022
				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
1023
				break;
1024 1025

			case OPER_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
1026
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
1027 1028
				break;
			default:
1029
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
1030 1031 1032
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
1033
			res->v.v_float = 0.0;
1034 1035
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
1036

1037
			switch (tree->oper){
1038
			case OPER_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
1039
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
1040
				break;
1041

1042
			case OPER_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
1043
				res->v.v_float = a->v.v_float - b->v.v_float;