expr.c 54.8 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>
11
#include <locale.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "expr.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
14
#include "eval.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
15
#include "format.h"
16
#include "func.h"
17
#include "utils.h"
18

19 20
Value *value_zero = NULL;

21
EvalPosition *
22
eval_pos_init (EvalPosition *eval_pos, Sheet *sheet, int col, int row)
23
{
24 25 26 27 28 29 30
	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;
31

32
	return eval_pos;
33
}
34

35
EvalPosition *
36
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
37
{
38 39 40
	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);
41 42
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

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

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

59
	return eval_info;
60 61
}

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

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

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

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

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

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

95
	return error_message;
96 97 98
}

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

103 104 105 106 107 108 109 110 111 112 113 114
	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;
115 116 117
}

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

	return error_message->message;
123 124 125 126
}

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

314 315 316
	return ans;
}

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

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
332
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
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 371
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
372 373 374 375
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
376 377 378 379 380 381
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
382
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
383 384 385 386 387
}

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

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

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

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

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

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

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

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

Morten Welinder's avatar
Morten Welinder committed
432 433 434 435 436 437
/*
 * 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.)
 */
438 439 440 441 442 443
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
444
	do_expr_tree_unref (tree);
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 497
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
498 499 500 501
/*
 * simplistic value rendering
 */
char *
Michael Meeks's avatar
Michael Meeks committed
502
value_get_as_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
503
{
504 505
	struct lconv *locinfo;
	
506
	char const * separator = ",";
507 508 509 510 511 512
	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
513 514 515 516 517
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	dest->type = source->type;
616

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

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

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

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

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

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

	return v;
}

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

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

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

	return v;
}

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

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

711 712 713
	return v;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

808
	case VALUE_ARRAY:
809
		return 0;
810

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

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

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

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

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

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

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

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

	newval = value_array_new (width, height);

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

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

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

	value_release (newval);
885 886 887 888 889
}

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

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

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

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

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

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

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

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

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

971 972 973 974 975 976 977 978 979 980 981
		/* 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 */
982
		if (a_row > b_row) {
983 984 985 986
			int tmp = a_row;
			a_row = b_row;
			b_row = tmp;
		}
987
		if (a_col > b_col) {
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.
		 */
1001 1002
		g_return_val_if_fail (a_row<=b_row, NULL);
		g_return_val_if_fail (a_col<=b_col, NULL);
1003

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1147
			switch (*arg_type){
1148
			case 'f':
Morten Welinder's avatar
Morten Welinder committed
1149
			case 'b':
1150 1151 1152 1153
				if (v->type != VALUE_INTEGER &&
				    v->type != VALUE_FLOAT)
					type_mismatch = 1;
				break;
1154
			case 's':
1155 1156
				if (v->type != VALUE_STRING)
					type_mismatch = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
1157
				break;