expr.c 54.2 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
EvalPosition *
21
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, int col, int row)
22
{
23 24 25 26 27 28 29
	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);

	eval_pos->sheet = sheet;
	eval_pos->eval_col = col;
	eval_pos->eval_row = row;
30

31
	return eval_pos;
32
}
33

34
EvalPosition *
35
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
36
{
37 38 39
	g_return_val_if_fail (eval_pos != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->sheet != NULL, NULL);
40 41
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

42 43 44 45 46
	return eval_pos_init (
		eval_pos,
		cell->sheet,
		cell->col->pos,
		cell->row->pos);
47
}
48

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

58
	return eval_info;
59 60
}

61
FunctionEvalInfo *
62
func_eval_info_cell (FunctionEvalInfo *eval_info, Cell *cell)
63
{
64 65
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
66

67 68 69
	return func_eval_info_init (
		eval_info, cell->sheet,
		cell->col->pos, cell->row->pos);
70 71
}

72 73
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *eval_info, const EvalPosition *eval_pos)
74
{
75 76
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
77

78 79 80 81 82
	return func_eval_info_init (
		eval_info,
		eval_pos->sheet,
		eval_pos->eval_col,
		eval_pos->eval_row);
83 84
}

85 86
ErrorMessage *
error_message_new (void)
87
{
88
	ErrorMessage *error_message = g_new (ErrorMessage, 1);
89

90 91 92
	error_message->message = NULL;
	error_message->alloc = NULL;
	error_message->small [0] = '\0';
93

94
	return error_message;
95 96 97
}

void
98
error_message_set (ErrorMessage *error_message, const char *message)
99
{
100
	g_return_if_fail (error_message != NULL);
101

102 103 104 105 106 107 108 109 110 111 112 113
	if (error_message->alloc){
		g_free (error_message->alloc);
		error_message->alloc = NULL;
	}
	
	if (strlen (message)+1 < sizeof (error_message->small)){
		strcpy (error_message->small, message);
		error_message->message = error_message->small;
		return;
	}
	error_message->alloc = g_strdup (message);
	error_message->message = error_message->alloc;
114 115 116
}

const char *
117
error_message_txt (ErrorMessage *error_message)
118
{
119 120 121
	g_return_val_if_fail (error_message != NULL, NULL);

	return error_message->message;
122 123 124 125
}

/* Can be turned into a #define for speed later */
gboolean
126
error_message_is_set (ErrorMessage *error_message)
127
{
128 129 130
	g_return_val_if_fail (error_message, FALSE);

	return error_message->message != NULL;
131 132 133
}

void
134
error_message_free (ErrorMessage *error_message)
135
{
136 137 138
	if (error_message->alloc)
		g_free (error_message->alloc);
	g_free (error_message);
139
}
140

141
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
142
expr_tree_new_constant (Value *v)
143
{
144 145 146
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
147 148 149 150 151 152 153
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = v;

Morten Welinder's avatar
Morten Welinder committed
154 155 156 157 158 159 160 161 162
	return ans;
}

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

	ans = g_new (ExprTree, 1);
163 164 165 166 167 168 169
	if (!ans)
		return NULL;

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

Morten Welinder's avatar
Morten Welinder committed
170 171 172 173 174 175 176 177 178 179
	return ans;
}


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

	ans = g_new (ExprTree, 1);
180 181 182 183 184 185 186 187
	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
188 189 190 191 192 193 194 195 196 197
	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);
198 199 200 201 202 203 204 205
	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
206 207
	return ans;
}
208 209 210 211 212 213 214 215 216 217 218
       
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;
}

219 220 221 222 223 224 225 226 227 228 229
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;
}

230 231 232 233 234 235 236 237 238 239 240
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;
}

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

/*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
299 300 301 302 303 304 305

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

	ans = g_new (ExprTree, 1);
306 307 308 309 310 311 312
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

313 314 315
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
316 317 318 319
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
320 321 322 323 324 325 326 327 328
	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
329 330

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
331
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
332 333
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
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
371 372 373 374
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
375 376 377 378 379 380
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
381
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
382 383 384 385 386
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
387 388 389
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
390
	switch (tree->oper){
391
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
392
		break;
393

394
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
395
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
396
		break;
397

Morten Welinder's avatar
Morten Welinder committed
398 399 400 401 402 403 404
	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
405
		break;
Morten Welinder's avatar
Morten Welinder committed
406
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
407

Michael Meeks's avatar
Michael Meeks committed
408 409 410
	case OPER_NAME:
		break;

411
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
412 413 414 415
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

416
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
417 418
		do_expr_tree_unref (tree->u.value);
		break;
419 420 421 422
	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;
423 424 425
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
426
	}
427

Morten Welinder's avatar
Morten Welinder committed
428
	g_free (tree);
429 430
}

Morten Welinder's avatar
Morten Welinder committed
431 432 433 434 435 436
/*
 * 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.)
 */
437 438 439 440 441 442
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
443
	do_expr_tree_unref (tree);
444 445
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
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
497 498 499 500
/*
 * simplistic value rendering
 */
char *
Michael Meeks's avatar
Michael Meeks committed
501
value_get_as_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
502
{
503 504 505 506 507 508 509 510 511
	struct lconv *locinfo;
	
	separator = ",";
	locinfo = localeconv ();
	if (locinfo->decimal_point &&
	    locinfo->decimal_point [0] == ',' &&
	    locinfo->decimal_point [1] == 0)
		separator = ";";

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
512 513 514 515 516
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

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

522 523 524 525 526
	case VALUE_ARRAY: {
		GString *str = g_string_new ("{");
		guint lpx, lpy;
		char *ans;

527 528 529 530
		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];

531 532 533 534 535
				g_return_val_if_fail (v->type == VALUE_STRING ||
						      v->type == VALUE_FLOAT ||
						      v->type == VALUE_INTEGER,
						      "Duff Array contents");
				if (lpx)
536
					g_string_sprintfa (str, separator);
537 538 539 540 541 542 543
				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));
			}
544
			if (lpy < value->v.array.y-1)
545 546 547 548 549 550 551
				g_string_sprintfa (str, ";");
		}
		g_string_sprintfa (str, "}");
		ans = str->str;
		g_string_free (str, FALSE);
		return ans;
	}
552

553 554 555
	case VALUE_CELLRANGE: 
		return value_cellrange_get_as_string (value, TRUE);
	
556 557 558
	default:
		g_warning ("value_string problem\n");
		break;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
559
	}
560

Arturo Espinosa's avatar
Arturo Espinosa committed
561
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
562 563
}

564 565 566
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
567
	g_return_if_fail (value != NULL);
568

569 570 571 572 573 574 575
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
576
		break;
577

578 579 580 581
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

582
	case VALUE_ARRAY:{
583
		guint lpx, lpy;
584

585
		for (lpx = 0; lpx < value->v.array.x; lpx++){
586
			for (lpy = 0; lpy < value->v.array.y; lpy++)
587
				value_release (value->v.array.vals [lpx][lpy]);
588 589
			g_free (value->v.array.vals [lpx]);
		}
590

591
		g_free (value->v.array.vals);
Morten Welinder's avatar
Morten Welinder committed
592
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
593
	}
594

Arturo Espinosa's avatar
Arturo Espinosa committed
595 596
	case VALUE_CELLRANGE:
		break;
597

598 599 600
	default:
		g_warning ("value_release problem\n");
		break;
601
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
602
	g_free (value);
603
}
604

Arturo Espinosa's avatar
Arturo Espinosa committed
605 606 607 608
/*
 * Copies a Value.
 */
void
609
value_copy_to (Value *dest, const Value *source)
Arturo Espinosa's avatar
Arturo Espinosa committed
610 611 612 613 614
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
615

Arturo Espinosa's avatar
Arturo Espinosa committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629
	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
630
	case VALUE_ARRAY: {
631
		value_array_copy_to (dest, source);
Arturo Espinosa's avatar
Arturo Espinosa committed
632 633 634 635 636
		break;
	}
	case VALUE_CELLRANGE:
		dest->v.cell_range = source->v.cell_range;
		break;
637 638 639
	default:
		g_warning ("value_copy_to problem\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
640 641 642
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
643 644 645 646
/*
 * Makes a copy of a Value
 */
Value *
647
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
648 649 650 651 652 653
{
	Value *new_value;

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

Arturo Espinosa's avatar
Arturo Espinosa committed
655 656 657
	return new_value;
}

658
Value *
Michael Meeks's avatar
Michael Meeks committed
659
value_new_float (float_t f)
660 661 662 663 664 665 666 667 668 669
{
	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
670
value_new_int (int i)
671 672 673 674 675 676 677 678 679
{
	Value *v = g_new (Value, 1);

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

	return v;
}

680 681 682
Value *
value_new_bool (gboolean b)
{
683 684 685 686
	/*
	 * Currently our booleans are really just ints.  This will have to
	 * change if we want Excel's ISLOGICAL.
	 */
687 688 689
	return value_new_int (b ? 1 : 0);
}

690
Value *
Michael Meeks's avatar
Michael Meeks committed
691
value_new_string (const char *str)
692 693 694 695 696 697 698 699 700
{
	Value *v = g_new (Value, 1);

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

	return v;
}

701
Value *
Michael Meeks's avatar
Michael Meeks committed
702
value_new_cellrange (const CellRef *a, const CellRef *b)
703 704
{
	Value *v = g_new (Value, 1);
Morten Welinder's avatar
Morten Welinder committed
705

706
	v->type = VALUE_CELLRANGE;
Morten Welinder's avatar
Morten Welinder committed
707 708 709
	v->v.cell_range.cell_a = *a;
	v->v.cell_range.cell_b = *b;

710 711 712
	return v;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
713 714 715 716
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
717
Value *
718
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
719 720
{
	Value *newv;
721

Arturo Espinosa's avatar
Arturo Espinosa committed
722 723 724 725
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
727 728 729
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
730
	value_release (v);
731

Arturo Espinosa's avatar
Arturo Espinosa committed
732 733 734
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
735
int
Michael Meeks's avatar
Michael Meeks committed
736
value_get_as_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
737 738 739
{
	*err = 0;

740 741
	switch (v->type) {
	case VALUE_STRING:
Morten Welinder's avatar
Morten Welinder committed
742 743
		/* FIXME FIXME FIXME */
		/* Use locale to support TRUE, FALSE */
Arturo Espinosa's avatar
Arturo Espinosa committed
744 745
		return atoi (v->v.str->str);

746
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
747 748
		*err = 1;
		return 0;
749 750
		
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
751 752
		return v->v.v_int != 0;

753
	case VALUE_FLOAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
754 755
		return v->v.v_float != 0.0;

756
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
757
		return 0;
758 759 760 761
	default:
		g_warning ("Unhandled value in value_get_boolean");
		break;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
762 763 764
	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
765
float_t
Michael Meeks's avatar
Michael Meeks committed
766
value_get_as_float (const Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
767
{
768 769 770
	switch (v->type)
	{
	case VALUE_STRING:
Arturo Espinosa's avatar
Arturo Espinosa committed
771 772
		return atof (v->v.str->str);

773
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
774 775 776
		g_warning ("Getting range as a double: what to do?");
		return 0.0;

777
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
778
		return (float_t) v->v.v_int;
779 780
		
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
781
		return 0.0;
782

783 784 785
	case VALUE_FLOAT:
		return (float_t) v->v.v_float;
	default:
Michael Meeks's avatar
Michael Meeks committed
786
		g_warning ("value_get_as_float type error\n");
787 788 789
		break;
	}
	return 0.0;
Arturo Espinosa's avatar
Arturo Espinosa committed
790 791
}

792
int
793
value_get_as_int (const Value *v)
794
{
795 796 797
	switch (v->type)
	{
	case VALUE_STRING:
798 799
		return atoi (v->v.str->str);

800
	case VALUE_CELLRANGE:
Morten Welinder's avatar
Morten Welinder committed
801
		g_warning ("Getting range as a int: what to do?");
802
		return 0;
803

804
	case VALUE_INTEGER:
805 806
		return v->v.v_int;

807
	case VALUE_ARRAY:
808
		return 0;
809

810 811 812 813
	case VALUE_FLOAT:
		return (int) v->v.v_float;
	default:
		g_warning ("value_get_as_int unknown type\n");
814
		return 0;
815
	}
Michael Meeks's avatar
Michael Meeks committed
816
	return 0.0;
817 818
}

819 820 821
Value *
value_array_new (guint width, guint height)
{
822
	int x, y;
823 824 825 826 827

	Value *v = g_new (Value, 1);
	v->type = VALUE_ARRAY;
	v->v.array.x = width;
	v->v.array.y = height;
828
	v->v.array.vals = g_new (Value **, width);
829 830

	for (x = 0; x < width; x++){
831 832 833
		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);
834 835 836 837
	}
	return v;
}

Michael Meeks's avatar
Michael Meeks committed
838 839 840 841
void
value_array_set (Value *array, guint col, guint row, Value *v)
{
	g_return_if_fail (v);
842
	g_return_if_fail (array->type == VALUE_ARRAY);
Michael Meeks's avatar
Michael Meeks committed
843 844
	g_return_if_fail (col>=0);
	g_return_if_fail (row>=0);
845 846
	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
847

848 849 850
	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
851 852
}

853 854 855
void
value_array_resize (Value *v, guint width, guint height)
{
856
	int x, y, xcpy, ycpy;
857
	Value *newval;
858
	Value ***tmp;
859

Michael Meeks's avatar
Michael Meeks committed
860
	g_warning ("Totally untested");
861 862 863 864 865
	g_return_if_fail (v);
	g_return_if_fail (v->type == VALUE_ARRAY);

	newval = value_array_new (width, height);

866 867
	xcpy = MIN (width, v->v.array.x);
	ycpy = MIN (height, v->v.array.y);
868

869
	for (x = 0; x < xcpy; x++)
870
		for (y = 0; y < ycpy; y++) {
871
			value_array_set (newval, x, y, v->v.array.vals[x][y]);
872 873
			v->v.array.vals[x][y] = NULL;
		}
874 875

	tmp = v->v.array.vals;
876
	v->v.array.vals = newval->v.array.vals;
877
	newval->v.array.vals = tmp;
878 879
	newval->v.array.x = v->v.array.x;
	newval->v.array.y = v->v.array.y;
880 881
	v->v.array.x = width;
	v->v.array.y = height;
882 883

	value_release (newval);
884 885 886 887 888
}

void
value_array_copy_to (Value *v, const Value *src)
{
889
	int x, y;
890 891 892 893 894

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

897 898 899 900
	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]);
901 902 903 904
	}
}

guint
905
value_area_get_width (const EvalPosition *ep, Value const *v)
906 907 908 909
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
910

911 912
	if (v->type == VALUE_ARRAY)
		return v->v.array.x;
Michael Meeks's avatar
Michael Meeks committed
913 914 915
	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;
916 917
		guint ans = v->v.cell_range.cell_b.col -
			    v->v.cell_range.cell_a.col + 1;
Michael Meeks's avatar
Michael Meeks committed
918 919
		if (sheeta && sheeta->max_col_used < ans) /* Clip */
			ans = sheeta->max_col_used+1;
920 921
		return ans;
	}
922 923 924
}

guint
925
value_area_get_height (const EvalPosition *ep, Value const *v)
926 927 928 929
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
930

931 932
	if (v->type == VALUE_ARRAY)
		return v->v.array.y;
Michael Meeks's avatar
Michael Meeks committed
933
	else { /* FIXME: 3D references, may not clip correctly */
934
		Sheet *sheeta = eval_sheet (v->v.cell_range.cell_a.sheet, ep->sheet);
935 936
		guint ans = v->v.cell_range.cell_b.row -
		            v->v.cell_range.cell_a.row + 1;
Michael Meeks's avatar
Michael Meeks committed
937
		if (sheeta && sheeta->max_row_used < ans) /* Clip */
938
			ans = sheeta->max_row_used + 1;
939 940
		return ans;
	}
941 942
}

943 944 945 946
/*
 * An internal routine to get a cell from an array or range.  If any
 * problems occur a NULL is returned.
 */
947
const Value *
948
value_area_get_x_y (EvalPosition const *ep, Value const *v, guint x, guint y)
949
{
950
	g_return_val_if_fail (v, NULL);
951 952
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE,
953
			      NULL);
954 955

	if (v->type == VALUE_ARRAY){
956 957
		g_return_val_if_fail (x < v->v.array.x &&
				      y < v->v.array.y,
Michael Meeks's avatar
Michael Meeks committed
958
				      value_zero);
959
		return v->v.array.vals [x][y];
960
	} else {
961 962 963 964 965 966
		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;
967
		Cell *cell;
Michael Meeks's avatar
Michael Meeks committed
968
		Sheet *sheet;
969

970 971 972 973 974 975 976 977 978 979 980
		/* 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 */
981
		if (a_row > b_row) {
982 983 984 985
			int tmp = a_row;
			a_row = b_row;
			b_row = tmp;
		}
986
		if (a_col > b_col) {
987 988 989 990 991 992 993 994 995 996 997 998 999 1000
			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);
1001
		g_return_val_if_fail (a_col<=b_col, value_zero);
1002

Michael Meeks's avatar
Michael Meeks committed
1003
		sheet = a->sheet?a->sheet:ep->sheet;
1004
		g_return_val_if_fail (sheet != NULL, NULL);
1005 1006

		/* Speedup */
1007 1008 1009
		if (sheet->max_col_used < a_col ||
		    sheet->max_row_used < a_row)
			return NULL;
1010

1011
		cell = sheet_cell_get (sheet, a_col, a_row);
1012

1013 1014 1015
		if (cell && cell->value)
			return cell->value;
	}
Michael Meeks's avatar
Michael Meeks committed
1016
	
1017 1018 1019 1020 1021 1022 1023 1024 1025
	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;
1026
	return value_zero;
1027 1028
}

1029 1030 1031 1032 1033 1034
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)
1035
		cell_ref->col = eval_col + cell_ref->col;
1036 1037 1038

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1040 1041
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
1042 1043
}

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
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;
	}
}

1058 1059 1060 1061
static void
free_values (Value **values, int top)
{
	int i;
1062

1063
	for (i = 0; i < top; i++)
1064 1065
		if (values[i])
			value_release (values [i]);
1066 1067 1068
	g_free (values);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1069
static Value *
1070
eval_funcall (FunctionEvalInfo *s, ExprTree *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
1071
{
1072
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
1073 1074
	FunctionDefinition *fd;
	GList *l;
1075
	int argc, arg;
1076
	Value *v = NULL;
1077 1078 1079 1080 1081 1082
	Sheet *sheet;
	int eval_col;
	int eval_row;

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

1084
	sym = tree->u.function.symbol;
1085 1086 1087 1088 1089

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1090 1091 1092
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

1093 1094 1095
	if (sym->type != SYMBOL_FUNCTION)
		return function_error (s, _("Internal error"));

1096
	fd = (FunctionDefinition *)sym->data;
Morten Welinder's avatar
Morten Welinder committed
1097
	s->func_def = fd;
1098

1099 1100
	if (fd->fn_type == FUNCTION_NODES) {
		/* Functions that deal with ExprNodes */		
1101
		v = fd->fn.fn_nodes (s, l);
1102
	} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
1103 1104
		/* Functions that take pre-computed Values */
		Value **values;
Arturo Espinosa's avatar
Arturo Espinosa committed
1105
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
1106 1107
		const char *arg_type = fd->args;
		const char *argptr = fd->args;
1108

1109
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
1110 1111 1112 1113
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
1114 1115
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
1116 1117
				fn_argc_min++;
			fn_argc_max++;
1118
		}
1119

1120 1121
		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
1122 1123 1124
			return NULL;
		}

1125
		values = g_new (Value *, fn_argc_max);
1126

1127
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
1128
			ExprTree *t = (ExprTree *) l->data;
1129
			int type_mismatch = 0;
1130
			Value *v;
1131

Arturo Espinosa's avatar
Arturo Espinosa committed
1132 1133
			if (*arg_type=='|')
				arg_type++;
1134 1135 1136 1137

			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
1138
				if ((v = eval_expr (s, t)) == NULL)
1139 1140 1141
					goto free_list;
			} else {
				g_assert (t->oper == OPER_VAR);
1142
				v = value_new_cellrange (&t->u.ref,
Michael Meeks's avatar
Michael Meeks committed
1143
							 &t->u.ref);
1144
			}
1145

Arturo Espinosa's avatar
Arturo Espinosa committed
1146
			switch (*arg_type){
1147
			case 'f':
Morten Welinder's avatar
Morten Welinder committed
1148
			case 'b':
1149 1150 1151 1152
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
1153
			case 's':
1154 1155
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
1156
				break;
1157
			case 'r':
1158 1159 1160 1161 1162
				if (v->type != VALUE_CELLRANGE)
					type_mismatch = 1;
				else {
					cell_ref_make_absolute (&v->v.cell_range.cell_a, eval_col, eval