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

30
	return eval_pos;
31
}
32

33 34 35 36 37 38 39 40 41 42 43 44 45
ParsePosition *
parse_pos_init (ParsePosition *pp, Workbook *wb, int col, int row)
{
	g_return_val_if_fail (wb != NULL, NULL);
	g_return_val_if_fail (pp != NULL, NULL);

	pp->wb  = wb;
	pp->col = col;
	pp->row = row;

	return pp;
}

46
EvalPosition *
47
eval_pos_cell (EvalPosition *eval_pos, Cell *cell)
48
{
49 50 51
	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);
52 53
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);

54 55 56 57 58
	return eval_pos_init (
		eval_pos,
		cell->sheet,
		cell->col->pos,
		cell->row->pos);
59
}
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
ParsePosition *
parse_pos_cell (ParsePosition *pp, Cell *cell)
{
	g_return_val_if_fail (pp != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (cell->sheet), NULL);
	g_return_val_if_fail (cell->sheet->workbook != NULL, NULL);

	return parse_pos_init (
		pp,
		cell->sheet->workbook,
		cell->col->pos,
		cell->row->pos);
}

77
FunctionEvalInfo *
78
func_eval_info_init (FunctionEvalInfo *eval_info, Sheet *sheet, int col, int row)
79
{
80
	g_return_val_if_fail (eval_info != NULL, NULL);
81
       
82 83 84
	eval_pos_init (&eval_info->pos, sheet, col, row);
	eval_info->error = error_message_new ();
	eval_info->func_def = 0;
85

86
	return eval_info;
87 88
}

89
FunctionEvalInfo *
90
func_eval_info_cell (FunctionEvalInfo *eval_info, Cell *cell)
91
{
92 93
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (cell != NULL, NULL);
94

95 96 97
	return func_eval_info_init (
		eval_info, cell->sheet,
		cell->col->pos, cell->row->pos);
98 99
}

100 101
FunctionEvalInfo *
func_eval_info_pos (FunctionEvalInfo *eval_info, const EvalPosition *eval_pos)
102
{
103 104
	g_return_val_if_fail (eval_info != NULL, NULL);
	g_return_val_if_fail (eval_pos != NULL, NULL);
105

106 107 108 109 110
	return func_eval_info_init (
		eval_info,
		eval_pos->sheet,
		eval_pos->eval_col,
		eval_pos->eval_row);
111 112
}

113 114
ErrorMessage *
error_message_new (void)
115
{
116
	ErrorMessage *error_message = g_new (ErrorMessage, 1);
117

118 119 120
	error_message->message = NULL;
	error_message->alloc = NULL;
	error_message->small [0] = '\0';
121

122
	return error_message;
123 124 125
}

void
126
error_message_set (ErrorMessage *error_message, const char *message)
127
{
128
	g_return_if_fail (error_message != NULL);
129

130 131 132 133 134 135 136 137 138 139 140 141
	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;
142 143 144
}

const char *
145
error_message_txt (ErrorMessage *error_message)
146
{
147 148 149
	g_return_val_if_fail (error_message != NULL, NULL);

	return error_message->message;
150 151 152 153
}

/* Can be turned into a #define for speed later */
gboolean
154
error_message_is_set (ErrorMessage *error_message)
155
{
156 157 158
	g_return_val_if_fail (error_message, FALSE);

	return error_message->message != NULL;
159 160 161
}

void
162
error_message_free (ErrorMessage *error_message)
163
{
164 165 166
	if (error_message->alloc)
		g_free (error_message->alloc);
	g_free (error_message);
167
}
168

169
ExprTree *
Morten Welinder's avatar
Morten Welinder committed
170
expr_tree_new_constant (Value *v)
171
{
172 173 174
	ExprTree *ans;

	ans = g_new (ExprTree, 1);
175 176 177 178 179 180 181
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_CONSTANT;
	ans->u.constant = v;

Morten Welinder's avatar
Morten Welinder committed
182 183 184 185 186 187 188 189 190
	return ans;
}

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

	ans = g_new (ExprTree, 1);
191 192 193 194 195 196 197
	if (!ans)
		return NULL;

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

Morten Welinder's avatar
Morten Welinder committed
198 199 200 201 202 203 204 205 206 207
	return ans;
}


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

	ans = g_new (ExprTree, 1);
208 209 210 211 212 213 214 215
	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
216 217 218 219 220 221 222 223 224 225
	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);
226 227 228 229 230 231 232 233
	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
234 235
	return ans;
}
236 237 238 239 240 241 242 243 244 245 246
       
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;
}

247 248 249 250 251 252 253 254 255 256 257
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;
}

258 259 260 261 262 263 264 265 266 267 268
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;
}

269
ExprTree *
270
expr_parse_string (const char *expr, const ParsePosition *pp,
271 272 273 274 275
		   const char **desired_format, char **error_msg)
{
	ExprTree *tree;
	g_return_val_if_fail (expr != NULL, NULL);

276
	switch (gnumeric_expr_parser (expr, pp, desired_format, &tree)) {
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	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
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
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;
}
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

/*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
327 328 329 330 331 332 333

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

	ans = g_new (ExprTree, 1);
334 335 336 337 338 339 340
	if (!ans)
		return NULL;
	
	ans->ref_count = 1;
	ans->oper = OPER_VAR;
	ans->u.ref = *cr;

341 342 343
	return ans;
}

Morten Welinder's avatar
Morten Welinder committed
344 345 346 347
ExprTree *
expr_tree_new_error (const char *txt)
{
	Symbol *func;
Morten Welinder's avatar
Morten Welinder committed
348 349 350 351 352 353 354 355 356
	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
357 358

	symbol_ref (func);
Morten Welinder's avatar
Morten Welinder committed
359
	return expr_tree_new_funcall (func, args);
Morten Welinder's avatar
Morten Welinder committed
360 361
}

362 363 364 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
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
399 400 401 402
/*
 * expr_tree_ref:
 * Increments the ref_count for part of a tree
 */
403 404 405 406 407 408
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
409
	tree->ref_count++;
Arturo Espinosa's avatar
Arturo Espinosa committed
410 411 412 413 414
}

static void
do_expr_tree_unref (ExprTree *tree)
{
Morten Welinder's avatar
Morten Welinder committed
415 416 417
	if (--tree->ref_count > 0)
		return;

Arturo Espinosa's avatar
Arturo Espinosa committed
418
	switch (tree->oper){
419
	case OPER_VAR:
Arturo Espinosa's avatar
Arturo Espinosa committed
420
		break;
421

422
	case OPER_CONSTANT:
Morten Welinder's avatar
Morten Welinder committed
423
		value_release (tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
424
		break;
425

Morten Welinder's avatar
Morten Welinder committed
426 427 428 429 430 431 432
	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
433
		break;
Morten Welinder's avatar
Morten Welinder committed
434
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
435

Michael Meeks's avatar
Michael Meeks committed
436 437 438
	case OPER_NAME:
		break;

439
	case OPER_ANY_BINARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
440 441 442 443
		do_expr_tree_unref (tree->u.binary.value_a);
		do_expr_tree_unref (tree->u.binary.value_b);
		break;

444
	case OPER_ANY_UNARY:
Arturo Espinosa's avatar
Arturo Espinosa committed
445 446
		do_expr_tree_unref (tree->u.value);
		break;
447 448 449 450
	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;
451 452 453
	default:
		g_warning ("do_expr_tree_unref error\n");
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
454
	}
455

Morten Welinder's avatar
Morten Welinder committed
456
	g_free (tree);
457 458
}

Morten Welinder's avatar
Morten Welinder committed
459 460 461 462 463 464
/*
 * 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.)
 */
465 466 467 468 469 470
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
471
	do_expr_tree_unref (tree);
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 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
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
525 526 527 528
/*
 * simplistic value rendering
 */
char *
Michael Meeks's avatar
Michael Meeks committed
529
value_get_as_string (const Value *value)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
530
{
531 532
	struct lconv *locinfo;
	
533
	char const * separator = ",";
534 535 536 537 538 539
	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
540 541 542 543 544
	switch (value->type){
	case VALUE_STRING:
		return g_strdup (value->v.str->str);

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

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

550 551 552 553 554
	case VALUE_ARRAY: {
		GString *str = g_string_new ("{");
		guint lpx, lpy;
		char *ans;

555 556 557 558
		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];

559 560 561 562 563
				g_return_val_if_fail (v->type == VALUE_STRING ||
						      v->type == VALUE_FLOAT ||
						      v->type == VALUE_INTEGER,
						      "Duff Array contents");
				if (lpx)
564
					g_string_sprintfa (str, separator);
565 566 567 568 569 570 571
				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));
			}
572
			if (lpy < value->v.array.y-1)
573 574 575 576 577 578 579
				g_string_sprintfa (str, ";");
		}
		g_string_sprintfa (str, "}");
		ans = str->str;
		g_string_free (str, FALSE);
		return ans;
	}
580

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

Arturo Espinosa's avatar
Arturo Espinosa committed
589
	return g_strdup ("Internal problem");
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
590 591
}

592 593 594
void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
595
	g_return_if_fail (value != NULL);
596

597 598 599 600 601 602 603
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
604
		break;
605

606 607 608 609
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

610
	case VALUE_ARRAY:{
611
		guint lpx, lpy;
612

613
		for (lpx = 0; lpx < value->v.array.x; lpx++){
614
			for (lpy = 0; lpy < value->v.array.y; lpy++)
615
				value_release (value->v.array.vals [lpx][lpy]);
616 617
			g_free (value->v.array.vals [lpx]);
		}
618

619
		g_free (value->v.array.vals);
Morten Welinder's avatar
Morten Welinder committed
620
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
621
	}
622

Arturo Espinosa's avatar
Arturo Espinosa committed
623 624
	case VALUE_CELLRANGE:
		break;
625

626 627 628
	default:
		g_warning ("value_release problem\n");
		break;
629
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
630
	g_free (value);
631
}
632

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

	dest->type = source->type;
643

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

Arturo Espinosa's avatar
Arturo Espinosa committed
671 672 673 674
/*
 * Makes a copy of a Value
 */
Value *
675
value_duplicate (const Value *value)
Arturo Espinosa's avatar
Arturo Espinosa committed
676 677 678 679 680 681
{
	Value *new_value;

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

Arturo Espinosa's avatar
Arturo Espinosa committed
683 684 685
	return new_value;
}

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

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

	return v;
}

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

718
Value *
Michael Meeks's avatar
Michael Meeks committed
719
value_new_string (const char *str)
720 721 722 723 724 725 726 727 728
{
	Value *v = g_new (Value, 1);

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

	return v;
}

729
Value *
Michael Meeks's avatar
Michael Meeks committed
730
value_new_cellrange (const CellRef *a, const CellRef *b)
731 732
{
	Value *v = g_new (Value, 1);
Morten Welinder's avatar
Morten Welinder committed
733

734
	v->type = VALUE_CELLRANGE;
Morten Welinder's avatar
Morten Welinder committed
735 736 737
	v->v.cell_range.cell_a = *a;
	v->v.cell_range.cell_b = *b;

738 739 740
	return v;
}

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

Arturo Espinosa's avatar
Arturo Espinosa committed
750 751 752 753
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

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

Arturo Espinosa's avatar
Arturo Espinosa committed
755 756 757
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
758
	value_release (v);
759

Arturo Espinosa's avatar
Arturo Espinosa committed
760 761 762
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
763
int
Michael Meeks's avatar
Michael Meeks committed
764
value_get_as_bool (const Value *v, int *err)
Arturo Espinosa's avatar
Arturo Espinosa committed
765 766 767
{
	*err = 0;

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

774
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
775 776
		*err = 1;
		return 0;
777 778
		
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
779 780
		return v->v.v_int != 0;

781
	case VALUE_FLOAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
782 783
		return v->v.v_float != 0.0;

784
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
785
		return 0;
786 787 788 789
	default:
		g_warning ("Unhandled value in value_get_boolean");
		break;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
790 791 792
	return 0;
}

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

801
	case VALUE_CELLRANGE:
Arturo Espinosa's avatar
Arturo Espinosa committed
802 803 804
		g_warning ("Getting range as a double: what to do?");
		return 0.0;

805
	case VALUE_INTEGER:
Arturo Espinosa's avatar
Arturo Espinosa committed
806
		return (float_t) v->v.v_int;
807 808
		
	case VALUE_ARRAY:
Arturo Espinosa's avatar
Arturo Espinosa committed
809
		return 0.0;
810

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

820
int
821
value_get_as_int (const Value *v)
822
{
823 824 825
	switch (v->type)
	{
	case VALUE_STRING:
826 827
		return atoi (v->v.str->str);

828
	case VALUE_CELLRANGE:
Morten Welinder's avatar
Morten Welinder committed
829
		g_warning ("Getting range as a int: what to do?");
830
		return 0;
831

832
	case VALUE_INTEGER:
833 834
		return v->v.v_int;

835
	case VALUE_ARRAY:
836
		return 0;
837

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

847 848 849
Value *
value_array_new (guint width, guint height)
{
850
	int x, y;
851 852 853 854 855

	Value *v = g_new (Value, 1);
	v->type = VALUE_ARRAY;
	v->v.array.x = width;
	v->v.array.y = height;
856
	v->v.array.vals = g_new (Value **, width);
857 858

	for (x = 0; x < width; x++){
859 860 861
		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);
862 863 864 865
	}
	return v;
}

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

876 877 878
	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
879 880
}

881 882 883
void
value_array_resize (Value *v, guint width, guint height)
{
884
	int x, y, xcpy, ycpy;
885
	Value *newval;
886
	Value ***tmp;
887

Michael Meeks's avatar
Michael Meeks committed
888
	g_warning ("Totally untested");
889 890 891 892 893
	g_return_if_fail (v);
	g_return_if_fail (v->type == VALUE_ARRAY);

	newval = value_array_new (width, height);

894 895
	xcpy = MIN (width, v->v.array.x);
	ycpy = MIN (height, v->v.array.y);
896

897
	for (x = 0; x < xcpy; x++)
898
		for (y = 0; y < ycpy; y++) {
899
			value_array_set (newval, x, y, v->v.array.vals[x][y]);
900 901
			v->v.array.vals[x][y] = NULL;
		}
902 903

	tmp = v->v.array.vals;
904
	v->v.array.vals = newval->v.array.vals;
905
	newval->v.array.vals = tmp;
906 907
	newval->v.array.x = v->v.array.x;
	newval->v.array.y = v->v.array.y;
908 909
	v->v.array.x = width;
	v->v.array.y = height;
910 911

	value_release (newval);
912 913 914 915 916
}

void
value_array_copy_to (Value *v, const Value *src)
{
917
	int x, y;
918 919 920 921 922

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

925 926 927 928
	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]);
929 930 931 932
	}
}

guint
933
value_area_get_width (const EvalPosition *ep, Value const *v)
934 935 936 937
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
938

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

guint
953
value_area_get_height (const EvalPosition *ep, Value const *v)
954 955 956 957
{
	g_return_val_if_fail (v, 0);
	g_return_val_if_fail (v->type == VALUE_ARRAY ||
			      v->type == VALUE_CELLRANGE, 1);
958

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

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

	if (v->type == VALUE_ARRAY){
984 985
		g_return_val_if_fail (x < v->v.array.x &&
				      y < v->v.array.y,
986
				      NULL);
987
		return v->v.array.vals [x][y];
988
	} else {
989 990 991 992 993 994
		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;
995
		Cell *cell;
Michael Meeks's avatar
Michael Meeks committed
996
		Sheet *sheet;
997

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
		/* 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 */
1009
		if (a_row > b_row) {
1010 1011 1012 1013
			int tmp = a_row;
			a_row = b_row;
			b_row = tmp;
		}
1014
		if (a_col > b_col) {
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
			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.
		 */
1028 1029
		g_return_val_if_fail (a_row<=b_row, NULL);
		g_return_val_if_fail (a_col<=b_col, NULL);
1030

Michael Meeks's avatar
Michael Meeks committed
1031
		sheet = a->sheet?a->sheet:ep->sheet;
1032
		g_return_val_if_fail (sheet != NULL, NULL);
1033 1034

		/* Speedup */
1035 1036 1037
		if (sheet->max_col_used < a_col ||
		    sheet->max_row_used < a_row)
			return NULL;
1038

1039
		cell = sheet_cell_get (sheet, a_col, a_row);
1040

1041 1042 1043
		if (cell && cell->value)
			return cell->value;
	}
Michael Meeks's avatar
Michael Meeks committed
1044
	
1045 1046 1047 1048 1049 1050 1051
	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);
1052
	static Value *value_zero = NULL;
1053 1054
	if (res)
		return res;
1055 1056 1057

	if (value_zero == NULL)
		value_zero = value_new_int (0);
1058
	return value_zero;
1059 1060
}

1061 1062 1063 1064 1065 1066
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)
1067
		cell_ref->col = eval_col + cell_ref->col;
1068 1069 1070

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1072 1073
	cell_ref->row_relative = 0;
	cell_ref->col_relative = 0;
1074 1075
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
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;
	}
}

1090 1091 1092 1093
static void
free_values (Value **values, int top)
{
	int i;
1094

1095
	for (i = 0; i < top; i++)
1096 1097
		if (values[i])
			value_release (values [i]);
1098 1099 1100
	g_free (values);
}

1101 1102 1103
static Value *
eval_expr_real (FunctionEvalInfo *s, ExprTree *tree);

Arturo Espinosa's avatar
Arturo Espinosa committed
1104
static Value *
1105
eval_funcall (FunctionEvalInfo *s, ExprTree *tree)
Arturo Espinosa's avatar
Arturo Espinosa committed
1106
{
1107
	const Symbol *sym;
Arturo Espinosa's avatar
Arturo Espinosa committed
1108 1109
	FunctionDefinition *fd;
	GList *l;
1110
	int argc, arg;
1111
	Value *v = NULL;
1112 1113 1114 1115 1116 1117
	Sheet *sheet;
	int eval_col;
	int eval_row;

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

1119
	sym = tree->u.function.symbol;
1120 1121 1122 1123 1124

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

Arturo Espinosa's avatar
Arturo Espinosa committed
1125 1126 1127
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

1128 1129 1130
	if (sym->type != SYMBOL_FUNCTION)
		return function_error (s, _("Internal error"));

1131
	fd = (FunctionDefinition *)sym->data;
Morten Welinder's avatar
Morten Welinder committed
1132
	s->func_def = fd;
1133

1134 1135
	if (fd->fn_type == FUNCTION_NODES) {
		/* Functions that deal with ExprNodes */		
1136
		v = fd->fn.fn_nodes (s, l);
1137
	} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
1138 1139
		/* Functions that take pre-computed Values */
		Value **values;
Arturo Espinosa's avatar
Arturo Espinosa committed
1140
		int fn_argc_min = 0, fn_argc_max = 0, var_len = 0;
1141 1142
		const char *arg_type = fd->args;
		const char *argptr = fd->args;
1143

1144
		/* Get variable limits */
Arturo Espinosa's avatar
Arturo Espinosa committed
1145 1146 1147 1148
		while (*argptr){
			if (*argptr++ == '|'){
				var_len = 1;
				continue;
1149 1150
			}
			if (!var_len)
Arturo Espinosa's avatar
Arturo Espinosa committed
1151 1152
				fn_argc_min++;
			fn_argc_max++;
1153
		}
1154

1155 1156
		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
1157 1158 1159
			return NULL;
		}

1160
		values = g_new (Value *, fn_argc_max);
1161

1162
		for (arg = 0; l; l = l->next, arg++, arg_type++){
Arturo Espinosa's avatar
Arturo Espinosa committed
1163
			ExprTree *t = (ExprTree *) l->data;
1164
			int type_mismatch = 0;
1165
			Value *v;
1166

Arturo Espinosa's avatar
Arturo Espinosa committed
1167 1168
			if (*arg_type=='|')
				arg_type++;
1169 1170 1171 1172

			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 */