expr.c 44 KB
Newer Older
Miguel de Icaza's avatar
Miguel de Icaza committed
1
/*
Morten Welinder's avatar
Morten Welinder committed
2
 * expr.c: Expression evaluation in Gnumeric
Miguel de Icaza's avatar
Miguel de Icaza committed
3 4 5 6
 *
 * Author:
 *   Miguel de Icaza (miguel@gnu.org).
 */
7
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
8
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
9
#include <math.h>
10
#include <string.h>
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
#include "utils.h"
17

18 19
Value *value_zero = NULL;

20 21
EvalPosition *
eval_pos_init (EvalPosition *fp, Sheet *s, int col, int row)
22
{
23 24 25 26 27 28 29 30 31
	g_return_val_if_fail (s, NULL);
	g_return_val_if_fail (fp, NULL);
	g_return_val_if_fail (IS_SHEET (s), NULL);

	fp->sheet = s;
	fp->eval_col = col;
	fp->eval_row = row;
	return fp;
}
32

33 34 35 36 37 38 39 40 41 42 43 44 45
EvalPosition *
eval_pos_cell (EvalPosition *fp, Cell *cell)
{
	g_return_val_if_fail (fp, NULL);
	g_return_val_if_fail (cell, NULL);
	g_return_val_if_fail (cell->sheet, NULL);
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

	return eval_pos_init (fp,
			      cell->sheet,
			      cell->col->pos,
			      cell->row->pos);
}
46

47 48 49 50 51 52 53 54 55 56
FunctionEvalInfo *
func_eval_info_init (FunctionEvalInfo *s, Sheet *sheet, int col, int row)
{
	g_return_val_if_fail (s, NULL);
       
	eval_pos_init (&s->pos, sheet, col, row);
	s->error = error_message_new ();
	s->func_def = 0;
	return s;
}
57

58 59 60 61 62 63 64 65 66 67
FunctionEvalInfo *
func_eval_info_cell (FunctionEvalInfo *s, Cell *cell)
{
	g_return_val_if_fail (s, NULL);
	g_return_val_if_fail (cell, NULL);

	return func_eval_info_init (s,
				  cell->sheet,
				  cell->col->pos,
				  cell->row->pos);
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *s, const EvalPosition *fp)
{
	g_return_val_if_fail (s, NULL);
	g_return_val_if_fail (fp, NULL);

	return func_eval_info_init (s,
				    fp->sheet,
				    fp->eval_col,
				    fp->eval_row);
}

ErrorMessage *error_message_new (void)
{
	ErrorMessage *em = g_new (ErrorMessage, 1);

86
	em->err_msg      = 0;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	em->err_alloced  = 0;
	em->small_err[0] = '\0';
	return em;
}

#define ERROR_MESSAGE_CLEAN(em) do { em->err_msg = NULL; \
                                     if (em->err_alloced) { \
					     g_free (em->err_alloced); \
					     em->err_alloced = NULL; \
				     } \
                                     em->small_err[0] = '\0'; \
				} while (0)

void
error_message_set (ErrorMessage *em, const char *message)
{
	g_return_if_fail (em);
	ERROR_MESSAGE_CLEAN (em);

	em->err_msg = message;
}

void
error_message_set_alloc (ErrorMessage *em, char *message)
{
	g_return_if_fail (em);
	ERROR_MESSAGE_CLEAN (em);

	em->err_alloced  = message;
}

void
error_message_set_small (ErrorMessage *em, const char *message)
{
	g_return_if_fail (em);
	g_return_if_fail (message);
	g_return_if_fail (strlen(message) < 19);
	ERROR_MESSAGE_CLEAN (em);

	strcpy (em->small_err, message);
}

const char *
error_message_txt (ErrorMessage *em)
{
	if (!em)
		return _("Internal Error");

	if (em->err_msg)
		return em->err_msg;
	if (em->err_alloced)
		return em->err_msg;
	return em->small_err;
}

/* Can be turned into a #define for speed later */
gboolean
error_message_is_set (ErrorMessage *em)
{
	g_return_val_if_fail (em, FALSE);
	
	if (em->err_msg || em->err_alloced ||
	    em->small_err[0] != '\0')
		return TRUE;
	return FALSE;
}

void
error_message_free (ErrorMessage *em)
{
	if (em->err_alloced) {
		g_free (em->err_alloced);
		em->err_alloced = 0;
	}
	g_free (em);
}
163

164
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
165
expr_tree_new_constant (Value *v)
166
{
167 168 169
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
170 171 172 173 174 175 176
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = v;

Morten Welinder's avatar
Morten Welinder committed
177 178 179 180 181 182 183 184 185
	return ans;
}

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

	ans = g_new (ExprTree, 1);
186 187 188 189 190 191 192
	if (!ans)
		return NULL;

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

Morten Welinder's avatar
Morten Welinder committed
193 194 195 196 197 198 199 200 201 202
	return ans;
}


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

	ans = g_new (ExprTree, 1);
203 204 205 206 207 208 209 210
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = op;
	ans->u.binary.value_a = l;
	ans->u.binary.value_b = r;

Morten Welinder's avatar
Morten Welinder committed
211 212 213 214 215 216 217 218 219 220
	return ans;
}

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

	ans = g_new (ExprTree, 1);
221 222 223 224 225 226 227 228
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_FUNCALL;
	ans->u.function.symbol = sym;;
	ans->u.function.arg_list = args;

Morten Welinder's avatar
Morten Welinder committed
229 230
	return ans;
}
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
       
Value *
function_error (FunctionEvalInfo *fe, char *error_string)
{
	g_return_val_if_fail (fe, NULL);
	g_return_val_if_fail (error_string, NULL);

	error_message_set (fe->error, error_string);
	return NULL;
}

Value *
function_error_alloc (FunctionEvalInfo *fe, char *error_string)
{
	g_return_val_if_fail (fe, NULL);
	g_return_val_if_fail (error_string, NULL);

	error_message_set_alloc (fe->error, error_string);
	return NULL;
}

ExprTree *
expr_parse_string (const char *expr, const EvalPosition *fp,
		   const char **desired_format, char **error_msg)
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

	switch (gnumeric_expr_parser (expr, fp, desired_format, &tree)) {
	case PARSE_OK:
		*error_msg = NULL;
		tree->ref_count = 1;
		return tree;

	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;

	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
	default:
		g_assert_not_reached ();
		*error_msg = _("Impossible!");
		break;
	}
	return NULL;
}


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

	ans = g_new (ExprTree, 1);
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = NULL;
	return ans;
}*/
Morten Welinder's avatar
Morten Welinder committed
295 296 297 298 299 300 301

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

	ans = g_new (ExprTree, 1);
302 303 304 305 306 307 308
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

309 310 311
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
312 313 314 315
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
316 317 318 319 320 321 322 323 324
	GList *args = NULL;

	if (strcmp (txt, gnumeric_err_NA) == 0) {
		func = symbol_lookup (global_symbol_table, "NA");
	} else {
		func = symbol_lookup (global_symbol_table, "ERROR");
		args = g_list_prepend (NULL,
				       expr_tree_new_constant (value_new_string (txt)));
	}
Morten Welinder's avatar
Morten Welinder committed
325 326

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
327
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
328 329
}

Arturo Espinosa's avatar
Arturo Espinosa committed
330 331 332 333
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
334 335 336 337 338 339
void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

Morten Welinder's avatar
Morten Welinder committed
340
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
341 342 343 344 345
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
346 347 348
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
349
	switch (tree->oper){
350
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
351
		break;
352

353
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
354
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
355
		break;
356

Morten Welinder's avatar
Morten Welinder committed
357 358 359 360 361 362 363
	case OPER_FUNCALL: {
		GList *l;

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

367
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
368 369 370 371
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

372
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
373 374
		do_expr_tree_unref (tree->u.value);
		break;
375 376 377
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
378
	}
379

Morten Welinder's avatar
Morten Welinder committed
380
	g_free (tree);
381 382
}

Morten Welinder's avatar
Morten Welinder committed
383 384 385 386 387 388
/*
 * expr_tree_unref:
 * Decrements the ref_count for part of a tree.  (All trees are expected
 * to have been created with a ref-count of one, so when we hit zero, we
 * go down over the tree and unref the tree and its leaves stuff.)
 */
389 390 391 392 393 394
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
395
	do_expr_tree_unref (tree);
396 397
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
398 399 400 401
/*
 * simplistic value rendering
 */
char *
Michael Meeks's avatar
Michael Meeks committed
402
value_get_as_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
403 404 405 406 407 408
{
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

	case VALUE_FLOAT:
Morten Welinder's avatar
Morten Welinder committed
412
		return g_strdup_printf ("%.*g", DBL_DIG, value->v.v_float);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
413

414 415 416 417 418
	case VALUE_ARRAY: {
		GString *str = g_string_new ("{");
		guint lpx, lpy;
		char *ans;

419 420 421 422
		for (lpy = 0; lpy < value->v.array.y; lpy++){
			for (lpx = 0; lpx < value->v.array.x; lpx++){
				const Value *v = value->v.array.vals [lpx][lpy];

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
				g_return_val_if_fail (v->type == VALUE_STRING ||
						      v->type == VALUE_FLOAT ||
						      v->type == VALUE_INTEGER,
						      "Duff Array contents");
				if (lpx)
					g_string_sprintfa (str, ",");
				if (v->type == VALUE_STRING)
					g_string_sprintfa (str, "\"%s\"",
							   v->v.str->str);
				else
					g_string_sprintfa (str, "%g",
							   value_get_as_float (v));
			}
			if (lpy<value->v.array.y-1)
				g_string_sprintfa (str, ";");
		}
		g_string_sprintfa (str, "}");
		ans = str->str;
		g_string_free (str, FALSE);
		return ans;
	}
444

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
445
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
446
		break;
447 448 449
	default:
		g_warning ("value_string problem\n");
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
450
	}
451

Arturo Espinosa's avatar
Arturo Espinosa committed
452
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
453 454
}

455 456 457
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
458
	g_return_if_fail (value != NULL);
459

460 461 462 463 464 465 466
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
467
		break;
468

469 470 471 472
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

473
	case VALUE_ARRAY:{
474
		guint lpx, lpy;
475

476
		for (lpx = 0; lpx < value->v.array.x; lpx++){
477
			for (lpy = 0; lpy < value->v.array.y; lpy++)
478
				value_release (value->v.array.vals [lpx][lpy]);
479 480
			g_free (value->v.array.vals [lpx]);
		}
481

482
		g_free (value->v.array.vals);
Morten Welinder's avatar
Morten Welinder committed
483
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
484
	}
485

Arturo Espinosa's avatar
Arturo Espinosa committed
486 487
	case VALUE_CELLRANGE:
		break;
488

489 490 491
	default:
		g_warning ("value_release problem\n");
		break;
492
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
493
	g_free (value);
494
}
495

Arturo Espinosa's avatar
Arturo Espinosa committed
496 497 498 499
/*
 * Copies a Value.
 */
void
500
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
501 502 503 504 505
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
506

Arturo Espinosa's avatar
Arturo Espinosa committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520
	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
521
	case VALUE_ARRAY: {
522
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
523 524 525 526 527
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
528 529 530
	default:
		g_warning ("value_copy_to problem\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
531 532 533
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
534 535 536 537
/*
 * Makes a copy of a Value
 */
Value *
538
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
539 540 541 542 543 544
{
	Value *new_value;

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

Arturo Espinosa's avatar
Arturo Espinosa committed
546 547 548
	return new_value;
}

549
Value *
Michael Meeks's avatar
Michael Meeks committed
550
value_new_float (float_t f)
551 552 553 554 555 556 557 558 559 560
{
	Value *v = g_new (Value, 1);

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

	return v;
}

Value *
Michael Meeks's avatar
Michael Meeks committed
561
value_new_int (int i)
562 563 564 565 566 567 568 569 570
{
	Value *v = g_new (Value, 1);

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

	return v;
}

571 572 573
Value *
value_new_bool (gboolean b)
{
574 575 576 577
	/*
	 * Currently our booleans are really just ints.  This will have to
	 * change if we want Excel's ISLOGICAL.
	 */
578 579 580
	return value_new_int (b ? 1 : 0);
}

581
Value *
Michael Meeks's avatar
Michael Meeks committed
582
value_new_string (const char *str)
583 584 585 586 587 588 589 590 591
{
	Value *v = g_new (Value, 1);

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

	return v;
}

592
Value *
Michael Meeks's avatar
Michael Meeks committed
593
value_new_cellrange (const CellRef *a, const CellRef *b)
594 595
{
	Value *v = g_new (Value, 1);
Morten Welinder's avatar
Morten Welinder committed
596

597
	v->type = VALUE_CELLRANGE;
Morten Welinder's avatar
Morten Welinder committed
598 599 600
	v->v.cell_range.cell_a = *a;
	v->v.cell_range.cell_b = *b;

601 602 603
	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
604 605 606 607
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
608
Value *
609
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
610 611
{
	Value *newv;
612

Arturo Espinosa's avatar
Arturo Espinosa committed
613 614 615 616
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
618 619 620
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
621
	value_release (v);
622

Arturo Espinosa's avatar
Arturo Espinosa committed
623 624 625
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
626
int
Michael Meeks's avatar
Michael Meeks committed
627
value_get_as_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
628 629 630
{
	*err = 0;

631 632
	switch (v->type) {
	case VALUE_STRING:
Morten Welinder's avatar
Morten Welinder committed
633 634
		/* FIXME FIXME FIXME */
		/* Use locale to support TRUE, FALSE */
Arturo Espinosa's avatar
Arturo Espinosa committed
635 636
		return atoi (v->v.str->str);

637
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
638 639
		*err = 1;
		return 0;
640 641
		
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
642 643
		return v->v.v_int != 0;

644
	case VALUE_FLOAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
645 646
		return v->v.v_float != 0.0;

647
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
648
		return 0;
649 650 651 652
	default:
		g_warning ("Unhandled value in value_get_boolean");
		break;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
653 654 655
	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
656
float_t
Michael Meeks's avatar
Michael Meeks committed
657
value_get_as_float (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
658
{
659 660 661
	switch (v->type)
	{
	case VALUE_STRING:
Arturo Espinosa's avatar
Arturo Espinosa committed
662 663
		return atof (v->v.str->str);

664
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
665 666 667
		g_warning ("Getting range as a double: what to do?");
		return 0.0;

668
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
669
		return (float_t) v->v.v_int;
670 671
		
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
672
		return 0.0;
673

674 675 676
	case VALUE_FLOAT:
		return (float_t) v->v.v_float;
	default:
Michael Meeks's avatar
Michael Meeks committed
677
		g_warning ("value_get_as_float type error\n");
678 679 680
		break;
	}
	return 0.0;
Arturo Espinosa's avatar
Arturo Espinosa committed
681 682
}

683
int
684
value_get_as_int (const Value *v)
685
{
686 687 688
	switch (v->type)
	{
	case VALUE_STRING:
689 690
		return atoi (v->v.str->str);

691
	case VALUE_CELLRANGE:
Morten Welinder's avatar
Morten Welinder committed
692
		g_warning ("Getting range as a int: what to do?");
693
		return 0;
694

695
	case VALUE_INTEGER:
696 697
		return v->v.v_int;

698
	case VALUE_ARRAY:
699
		return 0;
700

701 702 703 704
	case VALUE_FLOAT:
		return (int) v->v.v_float;
	default:
		g_warning ("value_get_as_int unknown type\n");
705
		return 0;
706
	}
Michael Meeks's avatar
Michael Meeks committed
707
	return 0.0;
708 709
}

710 711 712
Value *
value_array_new (guint width, guint height)
{
713
	int x, y;
714 715 716 717 718

	Value *v = g_new (Value, 1);
	v->type = VALUE_ARRAY;
	v->v.array.x = width;
	v->v.array.y = height;
719
	v->v.array.vals = g_new (Value **, width);
720 721

	for (x = 0; x < width; x++){
722 723 724
		v->v.array.vals [x] = g_new (Value *, height);
		for (y = 0; y < height; y++)
			v->v.array.vals[x][y] = value_new_int (0);
725 726 727 728
	}
	return v;
}

Michael Meeks's avatar
Michael Meeks committed
729 730 731 732
void
value_array_set (Value *array, guint col, guint row, Value *v)
{
	g_return_if_fail (v);
733
	g_return_if_fail (array->type == VALUE_ARRAY);
Michael Meeks's avatar
Michael Meeks committed
734 735
	g_return_if_fail (col>=0);
	g_return_if_fail (row>=0);
736 737
	g_return_if_fail (array->v.array.y > row);
	g_return_if_fail (array->v.array.x > col);
Michael Meeks's avatar
Michael Meeks committed
738

739 740 741
	if (array->v.array.vals[col][row])
		value_release (array->v.array.vals[col][row]);
	array->v.array.vals[col][row] = v;
Michael Meeks's avatar
Michael Meeks committed
742 743
}

744 745 746
void
value_array_resize (Value *v, guint width, guint height)
{
747
	int x, y, xcpy, ycpy;
748
	Value *newval;
749
	Value ***tmp;
750

Michael Meeks's avatar
Michael Meeks committed
751
	g_warning ("Totally untested");
752 753 754 755 756 757 758 759
	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
760
		xcpy = width;
761 762 763 764 765 766

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

767 768 769 770 771
	for (x = 0; x < xcpy; x++)
		for (y = 0; y < ycpy; y++)
			value_array_set (newval, x, y, v->v.array.vals[x][y]);

	tmp = v->v.array.vals;
772
	v->v.array.vals = newval->v.array.vals;
773
	newval->v.array.vals = tmp;
774 775
	value_release (newval);

776 777 778 779 780 781 782
	v->v.array.x = width;
	v->v.array.y = height;
}

void
value_array_copy_to (Value *v, const Value *src)
{
783
	int x, y;
784 785 786 787 788

	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;
789
	v->v.array.vals = g_new (Value **, v->v.array.x);
790

791 792 793 794
	for (x = 0; x < v->v.array.x; x++) {
		v->v.array.vals [x] = g_new (Value *, v->v.array.y);
		for (y = 0; y < v->v.array.y; y++)
			v->v.array.vals [x][y] = value_duplicate (src->v.array.vals [x][y]);
795 796 797 798
	}
}

guint
Michael Meeks's avatar
Michael Meeks committed
799
value_area_get_width (const EvalPosition *ep, Value *v)
800 801 802 803
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
804

805 806
	if (v->type == VALUE_ARRAY)
		return v->v.array.x;
Michael Meeks's avatar
Michael Meeks committed
807 808 809
	else { /* FIXME: 3D references, may not clip correctly */
		Sheet *sheeta = v->v.cell_range.cell_a.sheet ?
			v->v.cell_range.cell_a.sheet:ep->sheet;
810 811
		guint ans = v->v.cell_range.cell_b.col -
			    v->v.cell_range.cell_a.col + 1;
Michael Meeks's avatar
Michael Meeks committed
812 813
		if (sheeta && sheeta->max_col_used < ans) /* Clip */
			ans = sheeta->max_col_used+1;
814 815
		return ans;
	}
816 817 818
}

guint
Michael Meeks's avatar
Michael Meeks committed
819
value_area_get_height (const EvalPosition *ep, Value *v)
820 821 822 823
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
824

825 826
	if (v->type == VALUE_ARRAY)
		return v->v.array.y;
Michael Meeks's avatar
Michael Meeks committed
827 828 829
	else { /* FIXME: 3D references, may not clip correctly */
		Sheet *sheeta = v->v.cell_range.cell_a.sheet ?
			v->v.cell_range.cell_a.sheet:ep->sheet;
830 831
		guint ans = v->v.cell_range.cell_b.row -
		            v->v.cell_range.cell_a.row + 1;
Michael Meeks's avatar
Michael Meeks committed
832 833
		if (sheeta && sheeta->max_row_used < ans) /* Clip */
			ans = sheeta->max_row_used+1;
834 835
		return ans;
	}
836 837 838
}

const Value *
Michael Meeks's avatar
Michael Meeks committed
839
value_area_get_at_x_y (const EvalPosition *ep, Value *v, guint x, guint y)
840 841 842 843
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE,
Michael Meeks's avatar
Michael Meeks committed
844
			      value_zero);
845 846

	if (v->type == VALUE_ARRAY){
847
		g_return_val_if_fail (v->v.array.x < x &&
848
				      v->v.array.y < y,
Michael Meeks's avatar
Michael Meeks committed
849
				      value_zero);
850
		return v->v.array.vals [x][y];
851 852 853
	} else {
		CellRef *a, *b;
		Cell *cell;
Michael Meeks's avatar
Michael Meeks committed
854
		Sheet *sheet;
855

856 857
		a = &v->v.cell_range.cell_a;
		b = &v->v.cell_range.cell_b;
Michael Meeks's avatar
Michael Meeks committed
858 859
		/* Fixme: these need to be altered to use 'ep' to calc.
		   non relative values if necessary */
860 861 862 863 864 865
		g_return_val_if_fail (!a->col_relative, value_zero);
		g_return_val_if_fail (!b->col_relative, value_zero);
		g_return_val_if_fail (!a->row_relative, value_zero);
		g_return_val_if_fail (!b->row_relative, value_zero);
		g_return_val_if_fail (a->col<=b->col, value_zero);
		g_return_val_if_fail (a->row<=b->row, value_zero);
Michael Meeks's avatar
Michael Meeks committed
866 867
		sheet = a->sheet?a->sheet:ep->sheet;
		g_return_val_if_fail (sheet,          value_zero);
868 869 870 871 872

		/* Speedup */
		if (a->sheet->max_col_used < a->col+x ||
		    a->sheet->max_row_used < a->row+y)
			return value_zero;
873

Michael Meeks's avatar
Michael Meeks committed
874
		cell = sheet_cell_get (sheet, a->col+x, a->row+y);
875

876 877 878
		if (cell && cell->value)
			return cell->value;
	}
Michael Meeks's avatar
Michael Meeks committed
879
	
880
	return value_zero;
881 882
}

883 884 885 886 887 888
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)
889
		cell_ref->col = eval_col + cell_ref->col;
890 891 892

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

Arturo Espinosa's avatar
Arturo Espinosa committed
894 895
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
896 897
}

898 899 900 901 902 903 904 905 906 907 908 909 910 911
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;
	}
}

912 913 914 915
static void
free_values (Value **values, int top)
{
	int i;
916

917
	for (i = 0; i < top; i++)
918 919
		if (values[i])
			value_release (values [i]);
920 921 922
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
923
static Value *
924
eval_funcall (FunctionEvalInfo *s, ExprTree *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
925
{
926
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
927 928
	FunctionDefinition *fd;
	GList *l;
929
	int argc, arg;
930
	Value *v = NULL;
931 932 933 934 935 936
	Sheet *sheet;
	int eval_col;
	int eval_row;

	g_return_val_if_fail (s, NULL);
	g_return_val_if_fail (tree, NULL);
937

938
	sym = tree->u.function.symbol;
939 940 941 942 943

	sheet    = s->pos.sheet;
	eval_col = s->pos.eval_col;
	eval_row = s->pos.eval_row;

Arturo Espinosa's avatar
Arturo Espinosa committed
944 945 946
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

947 948 949
	if (sym->type != SYMBOL_FUNCTION)
		return function_error (s, _("Internal error"));

950
	fd = (FunctionDefinition *)sym->data;
Morten Welinder's avatar
Morten Welinder committed
951
	s->func_def = fd;
952

953 954 955 956
	if (fd->fn_type == FUNCTION_NODES) {
		/* Functions that deal with ExprNodes */		
		v = ((FunctionNodes *)fd->fn) (s, l);
	} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
957 958
		/* Functions that take pre-computed Values */
		Value **values;
Arturo Espinosa's avatar
Arturo Espinosa committed
959
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
960 961
		const char *arg_type = fd->args;
		const char *argptr = fd->args;
962

963
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
964 965 966 967
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
968 969
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
970 971
				fn_argc_min++;
			fn_argc_max++;
972
		}
973

974 975
		if (argc > fn_argc_max || argc < fn_argc_min) {
			error_message_set (s->error, _("Invalid number of arguments"));
Arturo Espinosa's avatar
Arturo Espinosa committed
976 977 978
			return NULL;
		}

979
		values = g_new (Value *, fn_argc_max);
980

981
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
982
			ExprTree *t = (ExprTree *) l->data;
983
			int type_mismatch = 0;
984
			Value *v;
985

Arturo Espinosa's avatar
Arturo Espinosa committed
986 987
			if (*arg_type=='|')
				arg_type++;
988 989 990 991

			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 */
Morten Welinder's avatar
Morten Welinder committed
992
				if ((v = eval_expr (s, t)) == NULL)
993 994 995
					goto free_list;
			} else {
				g_assert (t->oper == OPER_VAR);
996
				v = value_new_cellrange (&t->u.ref,
Michael Meeks's avatar
Michael Meeks committed
997
							 &t->u.ref);
998 999 1000 1001 1002
				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;
			}
1003

Arturo Espinosa's avatar
Arturo Espinosa committed
1004
			switch (*arg_type){
1005
			case 'f':
Morten Welinder's avatar
Morten Welinder committed
1006
			case 'b':
1007 1008 1009 1010
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
1011
			case 's':
1012 1013
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
1014
				break;
1015
			case 'r':
1016 1017 1018 1019 1020
				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);
1021
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
1022
				break;
1023 1024 1025 1026 1027 1028 1029 1030
			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
1031 1032 1033 1034 1035

				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);
				}
1036 1037
				break;
			}
1038
			values [arg] = v;
1039
			if (type_mismatch){
1040
				free_values (values, arg + 1);
1041
				error_message_set (s->error, _("Type mismatch"));
1042
				return NULL;
1043
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
1044
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
1045 1046
		while (arg < fn_argc_max)
			values [arg++] = NULL;
1047
		v = ((FunctionArgs *)fd->fn) (s, values);
Arturo Espinosa's avatar
Arturo Espinosa committed
1048 1049

	free_list:
1050
		free_values (values, arg);
Arturo Espinosa's avatar
Arturo Espinosa committed
1051 1052 1053 1054
	}
	return v;
}

1055
Value *
1056 1057 1058 1059 1060
function_def_call_with_values (const EvalPosition *ep,
			       FunctionDefinition *fd,
			       int                 argc,
			       Value              *values [],
			       ErrorMessage       *error)
1061 1062
{
	Value *retval;
1063 1064 1065 1066
	FunctionEvalInfo s;

	func_eval_info_pos (&s, ep);
	s.error = error;
1067

1068
	if (fd->fn_type == FUNCTION_NODES) {
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
		/*
		 * 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);
1079

1080
			for (i = 0; i < argc; i++){
1081
				tree [i].oper = OPER_CONSTANT;
Miguel de Icaza's avatar