expr.c 54.4 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
       
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;
}

252 253 254 255 256 257 258 259 260 261 262
int
expr_tree_get_const_int (ExprTree const * const expr)
{
	g_return_val_if_fail (expr != NULL, 0);
	g_return_val_if_fail (expr->oper == OPER_CONSTANT, 0);
	g_return_val_if_fail (expr->u.constant, 0);
	g_return_val_if_fail (expr->u.constant->type == VALUE_INTEGER, 0);

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

263 264 265 266 267 268 269 270 271 272 273
char const *
expr_tree_get_const_str (ExprTree const *const expr)
{
	g_return_val_if_fail (expr != NULL, NULL);
	g_return_val_if_fail (expr->oper == OPER_CONSTANT, NULL);
	g_return_val_if_fail (expr->u.constant, NULL);
	g_return_val_if_fail (expr->u.constant->type == VALUE_STRING, NULL);

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

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
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;
}

Michael Meeks's avatar
Michael Meeks committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
ExprTree *
expr_tree_new_name (const ExprName *name)
{
	ExprTree *ans;

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

	return ans;
}
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

/*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
332 333 334 335 336 337 338

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

	ans = g_new (ExprTree, 1);
339 340 341 342 343 344 345
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

346 347 348
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
349 350 351 352
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
353 354 355 356 357 358 359 360 361
	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
362 363

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
364
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
ExprTree *
expr_tree_array_formula (int const x, int const y, int const rows, int const cols)
{
	ExprTree *ans;

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


static ExprTree *
expr_tree_array_formula_corner (ExprTree *expr)
{
	Cell * const corner = expr->u.array.corner.cell;

	g_return_val_if_fail (corner, NULL);
	g_return_val_if_fail (corner->parsed_node != NULL, NULL);

	/* Sanity check incase the corner gets removed for some reason */
	g_return_val_if_fail (corner->parsed_node != (void *)0xdeadbeef, NULL);
	g_return_val_if_fail (corner->parsed_node->oper == OPER_ARRAY, NULL);

	return corner->parsed_node;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
404 405 406 407
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
408 409 410 411 412 413
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
414
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
415 416 417 418 419
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
420 421 422
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
423
	switch (tree->oper){
424
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
425
		break;
426

427
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
428
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
429
		break;
430

Morten Welinder's avatar
Morten Welinder committed
431 432 433 434 435 436 437
	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
438
		break;
Morten Welinder's avatar
Morten Welinder committed
439
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
440

Michael Meeks's avatar
Michael Meeks committed
441 442 443
	case OPER_NAME:
		break;

444
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
445 446 447 448
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

449
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
450 451
		do_expr_tree_unref (tree->u.value);
		break;
452 453 454 455
	case OPER_ARRAY:
		if (tree->u.array.x == 0 && tree->u.array.y == 0)
			do_expr_tree_unref (tree->u.array.corner.func.expr);
		break;
456 457 458
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
459
	}
460

Morten Welinder's avatar
Morten Welinder committed
461
	g_free (tree);
462 463
}

Morten Welinder's avatar
Morten Welinder committed
464 465 466 467 468 469
/*
 * 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.)
 */
470 471 472 473 474 475
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
476
	do_expr_tree_unref (tree);
477 478
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
static void
encode_cellref (GString *dest, const CellRef *ref, gboolean use_relative_syntax)
{
	if (ref->sheet){
		g_string_append_c (dest, '\'');
		g_string_append (dest, ref->sheet->name);
		g_string_append (dest, "'!");
	}
	if (use_relative_syntax && !ref->col_relative)
		g_string_append_c (dest, '$');
	g_string_append (dest, col_name (ref->col));

	if (use_relative_syntax && !ref->row_relative)
		g_string_append_c (dest, '$');
	g_string_sprintfa (dest, "%d", ref->row+1);
}

/*
 * value_cellrange_get_as_string:
 * @value: a value containing a VALUE_CELLRANGE
 * @use_relative_syntax: true if you want the result to contain relative indicators
 *
 * Returns: a string reprensenting the Value, for example:
 * use_relative_syntax == TRUE: $a4:$b$1
 * use_relative_syntax == FALSE: a4:b1
 */
char *
value_cellrange_get_as_string (const Value *value, gboolean use_relative_syntax)
{
	GString *str;
	char *ans;

	g_return_val_if_fail (value != NULL, NULL);
	g_return_val_if_fail (value->type == VALUE_CELLRANGE, NULL);

	str = g_string_new ("");
	encode_cellref (str, &value->v.cell_range.cell_a, use_relative_syntax);

	if ((value->v.cell_range.cell_a.col != value->v.cell_range.cell_b.col) ||
	    (value->v.cell_range.cell_a.row != value->v.cell_range.cell_b.row) ||
	    (value->v.cell_range.cell_a.col_relative != value->v.cell_range.cell_b.col_relative) ||
	    (value->v.cell_range.cell_a.sheet != value->v.cell_range.cell_b.sheet)){
		g_string_append_c (str, ':');
		
		encode_cellref (str, &value->v.cell_range.cell_b, use_relative_syntax);
	}
	ans = str->str;
	g_string_free (str, FALSE);
	return ans;
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
530 531 532 533
/*
 * simplistic value rendering
 */
char *
Michael Meeks's avatar
Michael Meeks committed
534
value_get_as_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
535 536 537 538 539 540
{
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

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

546 547 548 549 550
	case VALUE_ARRAY: {
		GString *str = g_string_new ("{");
		guint lpx, lpy;
		char *ans;

551 552 553 554
		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];

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
				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;
	}
576

577 578 579
	case VALUE_CELLRANGE: 
		return value_cellrange_get_as_string (value, TRUE);
	
580 581 582
	default:
		g_warning ("value_string problem\n");
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
583
	}
584

Arturo Espinosa's avatar
Arturo Espinosa committed
585
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
586 587
}

588 589 590
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
591
	g_return_if_fail (value != NULL);
592

593 594 595 596 597 598 599
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
600
		break;
601

602 603 604 605
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

606
	case VALUE_ARRAY:{
607
		guint lpx, lpy;
608

609
		for (lpx = 0; lpx < value->v.array.x; lpx++){
610
			for (lpy = 0; lpy < value->v.array.y; lpy++)
611
				value_release (value->v.array.vals [lpx][lpy]);
612 613
			g_free (value->v.array.vals [lpx]);
		}
614

615
		g_free (value->v.array.vals);
Morten Welinder's avatar
Morten Welinder committed
616
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
617
	}
618

Arturo Espinosa's avatar
Arturo Espinosa committed
619 620
	case VALUE_CELLRANGE:
		break;
621

622 623 624
	default:
		g_warning ("value_release problem\n");
		break;
625
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
626
	g_free (value);
627
}
628

Arturo Espinosa's avatar
Arturo Espinosa committed
629 630 631 632
/*
 * Copies a Value.
 */
void
633
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
634 635 636 637 638
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
639

Arturo Espinosa's avatar
Arturo Espinosa committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653
	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
654
	case VALUE_ARRAY: {
655
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
656 657 658 659 660
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
661 662 663
	default:
		g_warning ("value_copy_to problem\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
664 665 666
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
667 668 669 670
/*
 * Makes a copy of a Value
 */
Value *
671
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
672 673 674 675 676 677
{
	Value *new_value;

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

Arturo Espinosa's avatar
Arturo Espinosa committed
679 680 681
	return new_value;
}

682
Value *
Michael Meeks's avatar
Michael Meeks committed
683
value_new_float (float_t f)
684 685 686 687 688 689 690 691 692 693
{
	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
694
value_new_int (int i)
695 696 697 698 699 700 701 702 703
{
	Value *v = g_new (Value, 1);

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

	return v;
}

704 705 706
Value *
value_new_bool (gboolean b)
{
707 708 709 710
	/*
	 * Currently our booleans are really just ints.  This will have to
	 * change if we want Excel's ISLOGICAL.
	 */
711 712 713
	return value_new_int (b ? 1 : 0);
}

714
Value *
Michael Meeks's avatar
Michael Meeks committed
715
value_new_string (const char *str)
716 717 718 719 720 721 722 723 724
{
	Value *v = g_new (Value, 1);

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

	return v;
}

725
Value *
Michael Meeks's avatar
Michael Meeks committed
726
value_new_cellrange (const CellRef *a, const CellRef *b)
727 728
{
	Value *v = g_new (Value, 1);
Morten Welinder's avatar
Morten Welinder committed
729

730
	v->type = VALUE_CELLRANGE;
Morten Welinder's avatar
Morten Welinder committed
731 732 733
	v->v.cell_range.cell_a = *a;
	v->v.cell_range.cell_b = *b;

734 735 736
	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
737 738 739 740
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
741
Value *
742
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
743 744
{
	Value *newv;
745

Arturo Espinosa's avatar
Arturo Espinosa committed
746 747 748 749
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
751 752 753
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
754
	value_release (v);
755

Arturo Espinosa's avatar
Arturo Espinosa committed
756 757 758
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
759
int
Michael Meeks's avatar
Michael Meeks committed
760
value_get_as_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
761 762 763
{
	*err = 0;

764 765
	switch (v->type) {
	case VALUE_STRING:
Morten Welinder's avatar
Morten Welinder committed
766 767
		/* FIXME FIXME FIXME */
		/* Use locale to support TRUE, FALSE */
Arturo Espinosa's avatar
Arturo Espinosa committed
768 769
		return atoi (v->v.str->str);

770
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
771 772
		*err = 1;
		return 0;
773 774
		
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
775 776
		return v->v.v_int != 0;

777
	case VALUE_FLOAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
778 779
		return v->v.v_float != 0.0;

780
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
781
		return 0;
782 783 784 785
	default:
		g_warning ("Unhandled value in value_get_boolean");
		break;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
786 787 788
	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
789
float_t
Michael Meeks's avatar
Michael Meeks committed
790
value_get_as_float (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
791
{
792 793 794
	switch (v->type)
	{
	case VALUE_STRING:
Arturo Espinosa's avatar
Arturo Espinosa committed
795 796
		return atof (v->v.str->str);

797
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
798 799 800
		g_warning ("Getting range as a double: what to do?");
		return 0.0;

801
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
802
		return (float_t) v->v.v_int;
803 804
		
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
805
		return 0.0;
806

807 808 809
	case VALUE_FLOAT:
		return (float_t) v->v.v_float;
	default:
Michael Meeks's avatar
Michael Meeks committed
810
		g_warning ("value_get_as_float type error\n");
811 812 813
		break;
	}
	return 0.0;
Arturo Espinosa's avatar
Arturo Espinosa committed
814 815
}

816
int
817
value_get_as_int (const Value *v)
818
{
819 820 821
	switch (v->type)
	{
	case VALUE_STRING:
822 823
		return atoi (v->v.str->str);

824
	case VALUE_CELLRANGE:
Morten Welinder's avatar
Morten Welinder committed
825
		g_warning ("Getting range as a int: what to do?");
826
		return 0;
827

828
	case VALUE_INTEGER:
829 830
		return v->v.v_int;

831
	case VALUE_ARRAY:
832
		return 0;
833

834 835 836 837
	case VALUE_FLOAT:
		return (int) v->v.v_float;
	default:
		g_warning ("value_get_as_int unknown type\n");
838
		return 0;
839
	}
Michael Meeks's avatar
Michael Meeks committed
840
	return 0.0;
841 842
}

843 844 845
Value *
value_array_new (guint width, guint height)
{
846
	int x, y;
847 848 849 850 851

	Value *v = g_new (Value, 1);
	v->type = VALUE_ARRAY;
	v->v.array.x = width;
	v->v.array.y = height;
852
	v->v.array.vals = g_new (Value **, width);
853 854

	for (x = 0; x < width; x++){
855 856 857
		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);
858 859 860 861
	}
	return v;
}

Michael Meeks's avatar
Michael Meeks committed
862 863 864 865
void
value_array_set (Value *array, guint col, guint row, Value *v)
{
	g_return_if_fail (v);
866
	g_return_if_fail (array->type == VALUE_ARRAY);
Michael Meeks's avatar
Michael Meeks committed
867 868
	g_return_if_fail (col>=0);
	g_return_if_fail (row>=0);
869 870
	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
871

872 873 874
	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
875 876
}

877 878 879
void
value_array_resize (Value *v, guint width, guint height)
{
880
	int x, y, xcpy, ycpy;
881
	Value *newval;
882
	Value ***tmp;
883

Michael Meeks's avatar
Michael Meeks committed
884
	g_warning ("Totally untested");
885 886 887 888 889
	g_return_if_fail (v);
	g_return_if_fail (v->type == VALUE_ARRAY);

	newval = value_array_new (width, height);

890 891
	xcpy = MIN (width, v->v.array.x);
	ycpy = MIN (height, v->v.array.y);
892

893
	for (x = 0; x < xcpy; x++)
894
		for (y = 0; y < ycpy; y++) {
895
			value_array_set (newval, x, y, v->v.array.vals[x][y]);
896 897
			v->v.array.vals[x][y] = NULL;
		}
898 899

	tmp = v->v.array.vals;
900
	v->v.array.vals = newval->v.array.vals;
901
	newval->v.array.vals = tmp;
902 903
	newval->v.array.x = v->v.array.x;
	newval->v.array.y = v->v.array.y;
904 905
	v->v.array.x = width;
	v->v.array.y = height;
906 907

	value_release (newval);
908 909 910 911 912
}

void
value_array_copy_to (Value *v, const Value *src)
{
913
	int x, y;
914 915 916 917 918

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

921 922 923 924
	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]);
925 926 927 928
	}
}

guint
929
value_area_get_width (const EvalPosition *ep, Value const *v)
930 931 932 933
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
934

935 936
	if (v->type == VALUE_ARRAY)
		return v->v.array.x;
Michael Meeks's avatar
Michael Meeks committed
937 938 939
	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;
940 941
		guint ans = v->v.cell_range.cell_b.col -
			    v->v.cell_range.cell_a.col + 1;
Michael Meeks's avatar
Michael Meeks committed
942 943
		if (sheeta && sheeta->max_col_used < ans) /* Clip */
			ans = sheeta->max_col_used+1;
944 945
		return ans;
	}
946 947 948
}

guint
949
value_area_get_height (const EvalPosition *ep, Value const *v)
950 951 952 953
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
954

955 956
	if (v->type == VALUE_ARRAY)
		return v->v.array.y;
Michael Meeks's avatar
Michael Meeks committed
957
	else { /* FIXME: 3D references, may not clip correctly */
958
		Sheet *sheeta = eval_sheet (v->v.cell_range.cell_a.sheet, ep->sheet);
959 960
		guint ans = v->v.cell_range.cell_b.row -
		            v->v.cell_range.cell_a.row + 1;
Michael Meeks's avatar
Michael Meeks committed
961
		if (sheeta && sheeta->max_row_used < ans) /* Clip */
962
			ans = sheeta->max_row_used + 1;
963 964
		return ans;
	}
965 966
}

967 968 969 970
/*
 * An internal routine to get a cell from an array or range.  If any
 * problems occur a NULL is returned.
 */
971
const Value *
972
value_area_get_x_y (EvalPosition const *ep, Value const *v, guint x, guint y)
973
{
974
	g_return_val_if_fail (v, NULL);
975 976
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE,
977
			      NULL);
978 979

	if (v->type == VALUE_ARRAY){
980 981
		g_return_val_if_fail (x < v->v.array.x &&
				      y < v->v.array.y,
Michael Meeks's avatar
Michael Meeks committed
982
				      value_zero);
983
		return v->v.array.vals [x][y];
984
	} else {
985 986 987 988 989 990
		CellRef const * const a = &v->v.cell_range.cell_a;
		CellRef const * const b = &v->v.cell_range.cell_b;
		int a_col = a->col;
		int a_row = a->row;
		int b_col = b->col;
		int b_row = b->row;
991
		Cell *cell;
Michael Meeks's avatar
Michael Meeks committed
992
		Sheet *sheet;
993

994 995 996 997 998 999 1000 1001 1002 1003 1004
		/* Handle relative references */
		if (a->col_relative)
			a_col += ep->eval_col;
		if (a->row_relative)
			a_row += ep->eval_row;
		if (b->col_relative)
			b_col += ep->eval_col;
		if (b->row_relative)
			b_row += ep->eval_row;

		/* Handle inverted refereneces */
1005
		if (a_row > b_row) {
1006 1007 1008 1009
			int tmp = a_row;
			a_row = b_row;
			b_row = tmp;
		}
1010
		if (a_col > b_col) {
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
			int tmp = a_col;
			a_col = b_col;
			b_col = tmp;
		}

		a_col += x;
		a_row += y;

		/*
		 * FIXME FIXME FIXME
		 * This should return NA but some of the math functions may
		 * rely on this for now.
		 */
		g_return_val_if_fail (a_row<=b_row, value_zero);
1025
		g_return_val_if_fail (a_col<=b_col, value_zero);
1026

Michael Meeks's avatar
Michael Meeks committed
1027
		sheet = a->sheet?a->sheet:ep->sheet;
1028
		g_return_val_if_fail (sheet != NULL, NULL);
1029 1030

		/* Speedup */
1031 1032 1033
		if (sheet->max_col_used < a_col ||
		    sheet->max_row_used < a_row)
			return NULL;
1034

1035
		cell = sheet_cell_get (sheet, a_col, a_row);
1036

1037 1038 1039
		if (cell && cell->value)
			return cell->value;
	}
Michael Meeks's avatar
Michael Meeks committed
1040
	
1041 1042 1043 1044 1045 1046 1047 1048 1049
	return NULL;
}

const Value *
value_area_fetch_x_y (EvalPosition const *ep, Value const *v, guint x, guint y)
{
	Value const * const res = value_area_get_x_y (ep, v, x, y);
	if (res)
		return res;
1050
	return value_zero;
1051 1052
}

1053 1054 1055 1056 1057 1058
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)
1059
		cell_ref->col = eval_col + cell_ref->col;
1060 1061 1062

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1064 1065
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
1066 1067
}

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
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;
	}
}

1082 1083 1084 1085
static void
free_values (Value **values, int top)
{
	int i;
1086

1087
	for (i = 0; i < top; i++)
1088 1089
		if (values[i])
			value_release (values [i]);
1090 1091 1092
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1093
static Value *
1094
eval_funcall (FunctionEvalInfo *s, ExprTree *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
1095
{
1096
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
1097 1098
	FunctionDefinition *fd;
	GList *l;
1099
	int argc, arg;
1100
	Value *v = NULL;
1101 1102 1103 1104 1105 1106
	Sheet *sheet;
	int eval_col;
	int eval_row;

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

1108
	sym = tree->u.function.symbol;
1109 1110 1111 1112 1113

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1114 1115 1116
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

1117 1118 1119
	if (sym->type != SYMBOL_FUNCTION)
		return function_error (s, _("Internal error"));

1120
	fd = (FunctionDefinition *)sym->data;
Morten Welinder's avatar
Morten Welinder committed
1121
	s->func_def = fd;
1122

1123 1124
	if (fd->fn_type == FUNCTION_NODES) {
		/* Functions that deal with ExprNodes */		
1125
		v = fd->fn.fn_nodes (s, l);
1126
	} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
1127 1128
		/* Functions that take pre-computed Values */
		Value **values;
Arturo Espinosa's avatar
Arturo Espinosa committed
1129
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
1130 1131
		const char *arg_type = fd->args;
		const char *argptr = fd->args;
1132

1133
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
1134 1135 1136 1137
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
1138 1139
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
1140 1141
				fn_argc_min++;
			fn_argc_max++;
1142
		}
1143

1144 1145
		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
1146 1147 1148
			return NULL;
		}