expr.c 12.8 KB
Newer Older
1
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
2
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
3
#include <math.h>
4
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
5
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
6
#include "expr.h"
7 8

char *parser_expr;
9
ParseErr parser_error;
10 11
ExprTree *parser_result;
int parser_col, parser_row;
12

13 14
ExprTree *
expr_parse_string (char *expr, int col, int row, char **error_msg)
15 16
{
	parser_expr = expr;
17
	parser_error = PARSE_OK;
18 19 20
	parser_col = col;
	parser_row = row;
		
21
	yyparse ();
22 23 24
	switch (parser_error){
	case PARSE_OK:
		*error_msg = NULL;
25
		parser_result->ref_count = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
26
		return parser_result;
27

Arturo Espinosa's avatar
Arturo Espinosa committed
28 29 30 31
	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;
			
32 33 34 35
	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
	}
36 37 38
	return NULL;
}

39 40 41 42 43 44 45
/*
 * eval_expr_release:
 * @ExprTree:  The tree to be released
 *
 * This releases all of the resources used by a tree.  
 * It is only used internally by eval_expr_unref
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
46
void
47
eval_expr_release (ExprTree *tree)
48
{
49
	g_return_if_fail (tree != NULL);
50
	
51
	switch (tree->oper){
52 53 54
	case OP_VAR:
		break;
		
55
	case OP_CONSTANT:
56
		value_release (tree->u.constant);
57 58 59
		break;
		
	case OP_FUNCALL:
60
		symbol_unref (tree->u.function.symbol);
61 62
		break;

63 64 65 66 67 68
	case OP_EQUAL:
	case OP_GT:
	case OP_LT:
	case OP_GTE:
	case OP_LTE:
	case OP_NOT_EQUAL:
69 70 71 72 73 74
	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
	case OP_CONCAT:
75 76
		eval_expr_release (tree->u.binary.value_a);
		eval_expr_release (tree->u.binary.value_b);
77 78 79
		break;

	case OP_NEG:
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		eval_expr_release (tree->u.value);
		break;
	}
	g_free (tree);
}

void
expr_tree_ref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

	tree->ref_count++;
}

void
expr_tree_unref (ExprTree *tree)
{
	g_return_if_fail (tree != NULL);
	g_return_if_fail (tree->ref_count > 0);

	tree->ref_count--;
	if (tree->ref_count == 0)
		eval_expr_release (tree);
}

void
value_release (Value *value)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
109 110
	g_return_if_fail (value != NULL);
	
111 112 113 114 115 116 117
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
118 119
		break;
		
120 121 122 123
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

Arturo Espinosa's avatar
Arturo Espinosa committed
124 125 126 127 128 129 130 131 132 133 134
	case VALUE_ARRAY: {
		GList *l;

		for (l = value->v.array; l; l = l->next)
			value_release (l->data);
		g_list_free (l);
	}
	
	case VALUE_CELLRANGE:
		break;
		
135
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
136
	g_free (value);
137
}
138

Arturo Espinosa's avatar
Arturo Espinosa committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/*
 * Copies a Value.
 */
void
value_copy_to (Value *dest, Value *source)
{
	g_return_if_fail (dest != NULL);
	g_return_if_fail (source != NULL);

	dest->type = source->type;
	
	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;

	default:
		g_warning ("value_copy_to: VALUE type not yet supported\n");
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
169 170 171 172
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
173
Value *
174
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
175 176 177 178 179 180 181 182 183 184 185
{
	Value *newv;
	
	g_return_val_if_fail (VALUE_IS_NUMBER (v), NULL);

	if (v->type == VALUE_FLOAT)
		return v;
	
	newv = g_new (Value, 1);
	newv->type = VALUE_FLOAT;
	mpf_set_z (newv->v.v_float, v->v.v_int);
186
	value_release (v);
Arturo Espinosa's avatar
Arturo Espinosa committed
187 188 189 190
	
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
int
value_get_bool (Value *v, int *err)
{
	*err = 0;

	if (v->type == VALUE_STRING)
		return atoi (v->v.str->str);

	if (v->type == VALUE_CELLRANGE){
		*err = 1;
		return 0;
	}

	if (v->type == VALUE_INTEGER)
		return v->v.v_int != 0;

	if (v->type == VALUE_FLOAT)
		return v->v.v_float != 0.0;

	if (v->type == VALUE_ARRAY)
		return 0;
	
	g_warning ("Unhandled value in value_get_boolean");

	return 0;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
float_t
value_get_as_double (Value *v)
{
	if (v->type == VALUE_STRING){
		return atof (v->v.str->str);
	}

	if (v->type == VALUE_CELLRANGE){
		g_warning ("Getting range as a double: what to do?");
		return 0.0;
	}

	if (v->type == VALUE_INTEGER)
		return (float_t) v->v.v_int;

Arturo Espinosa's avatar
Arturo Espinosa committed
233 234 235
	if (v->type == VALUE_ARRAY)
		return 0.0;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
236
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
237 238
}

Arturo Espinosa's avatar
Arturo Espinosa committed
239 240
static Value *
eval_cell_value (Sheet *sheet, Value *value)
241
{
Arturo Espinosa's avatar
Arturo Espinosa committed
242
	Value *res;
243

Arturo Espinosa's avatar
Arturo Espinosa committed
244 245 246 247 248 249
	res = g_new (Value, 1);
	res->type = value->type;
	
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
250
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
251 252 253
		break;
		
	case VALUE_INTEGER:
254
		res->v.v_int = value->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
255 256 257
		break;
		
	case VALUE_FLOAT:
258
		res->v.v_float = value->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
259
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
260 261 262 263 264 265 266

	case VALUE_ARRAY:
		g_warning ("VALUE_ARRAY not handled in eval_cell_value\n");
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
		break;
			
Arturo Espinosa's avatar
Arturo Espinosa committed
267 268 269 270 271 272 273
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
static Value *
eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
{
	FunctionDefinition *fd;
	GList *l;
	int argc, arg, i;
	Value *v;
	
	fd = (FunctionDefinition *) tree->u.function.symbol->data;
	
	l = tree->u.function.arg_list;
	argc = g_list_length (l);

	if (fd->expr_fn)
	{
		/* Functions that deal with ExprNodes */
		v = fd->expr_fn (sheet, l, eval_col, eval_row, error_string);
	}
	else
	{
		/* Functions that take pre-computed Values */
		Value **values;
		int fn_argc;
		char *arg_type = fd->args;
		
		fn_argc = strlen (fd->args);
		
		if (fn_argc != argc){
			*error_string = _("Invalid number of arguments");
			return NULL;
		}

		values = g_new (Value *, argc);
		
		for (arg = 0; l; l = l->next, arg++, arg_type++){
			ExprTree *t = (ExprTree *) l->data;
			
			v = eval_expr (sheet, t, eval_col, eval_row, error_string);
			if (v == NULL)
				goto free_list;
			
			values [arg] = v;
		}
317
		v = fd->fn (values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
318 319 320 321 322 323 324 325 326 327

	free_list:
		for (i = 0; i < arg; i++)
			value_release (values [i]);
		g_free (values);
		return v;
	}
	return v;
}

328
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
329 330
	IS_EQUAL,
	IS_LESS,
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	IS_GREATER,
	TYPE_ERROR
} compare_t;

static compare_t
compare_int_int (int a, int b)
{
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}

static compare_t
compare_float_float (float_t a, float_t b)
{
	if (a == b)
		return IS_EQUAL;
	else if (a < b)
		return IS_LESS;
	else
		return IS_GREATER;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
356

357 358 359 360
/*
 * Compares two (Value *) and returns one of compare_t
 */
static compare_t
Arturo Espinosa's avatar
Arturo Espinosa committed
361 362
compare (Value *a, Value *b)
{
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	if (a->type == VALUE_INTEGER){
		int f;
		
		switch (b->type){
		case VALUE_INTEGER:
			return compare_int_int (a->v.v_int, b->v.v_int);

		case VALUE_FLOAT:
			return compare_float_float (a->v.v_int, b->v.v_float);

		case VALUE_STRING:
			f = value_get_as_double (b);
			return compare_float_float (a->v.v_int, f);

		default:
			return TYPE_ERROR;
		}
	}
	
	if (a->type == VALUE_FLOAT){
		float_t f;

		switch (b->type){
		case VALUE_INTEGER:
			return compare_float_float (a->v.v_float, b->v.v_int);

		case VALUE_FLOAT:
			return compare_float_float (a->v.v_float, b->v.v_float);

		case VALUE_STRING:
			f = value_get_as_double (b);
			return compare_float_float (a->v.v_float, f);

		default:
			return TYPE_ERROR;
		}
	}

	if (a->type == VALUE_STRING && b->type == VALUE_STRING){
		int t;

		t = strcasecmp (a->v.str->str, b->v.str->str);
		if (t == 0)
			return IS_EQUAL;
		else if (t > 0)
			return IS_GREATER;
		else
			return IS_LESS;
	}

	return TYPE_ERROR;
Arturo Espinosa's avatar
Arturo Espinosa committed
414 415
}

Arturo Espinosa's avatar
Arturo Espinosa committed
416
Value *
417
eval_expr (void *asheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
418 419 420
{
	Value *a, *b, *res;
	Sheet *sheet = asheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
421 422 423 424 425

	g_return_val_if_fail (tree != NULL, NULL);
	g_return_val_if_fail (asheet != NULL, NULL);
	g_return_val_if_fail (error_string != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (asheet), NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
426
	
427
	switch (tree->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	case OP_EQUAL:
	case OP_NOT_EQUAL:
	case OP_GT:
	case OP_GTE:
	case OP_LT:
	case OP_LTE: {
		int comp;
		
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
		if (!(a && b)){
			if (a)
				value_release (a);
			if (b)
				value_release (b);
			return NULL;
		}
		res = g_new (Value, 1);
		res->type = VALUE_INTEGER;

		comp = compare (a, b);

452 453 454 455 456 457 458
		if (comp == TYPE_ERROR){
			value_release (a);
			value_release (b);
			*error_string = _("Type error");
			return NULL;
		}
		
Arturo Espinosa's avatar
Arturo Espinosa committed
459 460 461 462 463 464
		switch (tree->oper){
		case OP_EQUAL:
			res->v.v_int = comp == IS_EQUAL;
			break;

		case OP_GT:
465
			res->v.v_int = comp == IS_GREATER;
Arturo Espinosa's avatar
Arturo Espinosa committed
466 467 468 469 470 471 472 473 474 475 476
			break;

		case OP_LT:
			res->v.v_int = comp == IS_LESS;
			break;

		case OP_LTE:
			res->v.v_int = (comp == IS_EQUAL || comp == IS_LESS);
			break;

		case OP_GTE:
477
			res->v.v_int = (comp == IS_EQUAL || comp == IS_GREATER);
Arturo Espinosa's avatar
Arturo Espinosa committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491
			break;

		case OP_NOT_EQUAL:
			res->v.v_int = comp != IS_EQUAL;
			break;
			
		default:
			g_warning ("This should never be reached: comparission ops\n");
		}
		value_release (a);
		value_release (b);
		return res;
	}
	
Arturo Espinosa's avatar
Arturo Espinosa committed
492 493 494 495 496
	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
497 498 499 500
		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
501 502 503

		if (!(a && b)){
			if (a)
504
				value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
505
			if (b)
506
				value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
507 508 509 510
			return NULL;
		}
		
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
511 512
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
513 514 515 516 517 518 519 520
			*error_string = _("Type mismatch");
			return NULL;
		}
		
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
			res->type = VALUE_INTEGER;
			
521
			switch (tree->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
522
			case OP_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
523
				res->v.v_int = a->v.v_int + b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
524 525 526
				break;
				
			case OP_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
527
				res->v.v_int = a->v.v_int - b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
528 529 530
				break;
				
			case OP_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
531
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
532
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
533

Arturo Espinosa's avatar
Arturo Espinosa committed
534
			case OP_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
535
				if (b->v.v_int == 0){
536 537 538
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
539 540 541 542
					*error_string = _("Division by zero");
					return NULL;
				}
					
Arturo Espinosa's avatar
Arturo Espinosa committed
543
				res->v.v_int =  a->v.v_int / b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
544 545
				break;
				
Arturo Espinosa's avatar
Arturo Espinosa committed
546 547
			case OP_EXP: 
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
548 549 550 551 552
				break;
			default:
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
553
			res->v.v_float = 0.0;
554 555
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
556
			
557
			switch (tree->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
558
			case OP_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
559
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
560 561 562
				break;
				
			case OP_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
563
				res->v.v_float = a->v.v_float - b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
564 565 566
				break;
				
			case OP_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
567
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
568 569 570
				break;
				
			case OP_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
571
				if (b->v.v_float == 0.0){
572 573 574
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
575 576 577
					*error_string = _("Division by zero");
					return NULL;
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
578 579

				res->v.v_float = a->v.v_float / b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
580 581 582
				break;
				
			case OP_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
583
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
584 585 586 587
				break;
			default:
			}
		}
588 589
		value_release (a);
		value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
590 591
		return res;
		
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	case OP_CONCAT: {
		char *sa, *sb, *tmp;

		a = eval_expr (sheet, tree->u.binary.value_a,
			       eval_col, eval_row, error_string);
		if (!a)
			return NULL;
		b = eval_expr (sheet, tree->u.binary.value_b,
			       eval_col, eval_row, error_string);
		if (!b){
			value_release (a);
			return NULL;
		}

		res = g_new (Value, 1);
		res->type = VALUE_STRING;
		sa = value_string (a);
		sb = value_string (b);
		
		tmp = g_copy_strings (sa, sb, NULL);
		res->v.str = string_get (tmp);
		g_free (sa);
		g_free (sb);
		g_free (tmp);

		value_release (a);
		value_release (b);
		return res;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
621 622

	case OP_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
623
		return eval_funcall (sheet, tree, eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
624 625

	case OP_CONSTANT:
626
		return eval_cell_value (sheet, tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
627 628

	case OP_VAR:{
629
		CellRef *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
630 631 632 633 634 635 636 637 638 639 640 641 642
		Cell *cell;
		int col, row;
		
		if (sheet == NULL){
			/* Only the test program requests this */
			res = g_new (Value, 1);

			res->type = VALUE_FLOAT;
			res->v.v_float = 3.14;
			
			return res;
		}

643
		ref = &tree->u.constant->v.cell;
Arturo Espinosa's avatar
Arturo Espinosa committed
644
		cell_get_abs_col_row (&tree->u.constant->v.cell, eval_col, eval_row, &col, &row);
645
		
Arturo Espinosa's avatar
Arturo Espinosa committed
646
		cell = sheet_cell_get (sheet, col, row);
647 648 649 650 651 652

		if (cell && cell->value){
			if (cell->parsed_node && cell->generation != sheet->workbook->generation){
				cell->generation = sheet->workbook->generation;
				cell_eval (cell);
			} 
Arturo Espinosa's avatar
Arturo Espinosa committed
653
			
654 655
			if (cell->value)
				return eval_cell_value (sheet, cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
656
		}
657 658 659 660
		res = g_new (Value, 1);
			
		res->type = VALUE_INTEGER;
		res->v.v_int = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
661 662 663
		return res;
	}
	case OP_NEG:
664 665
		a = eval_expr (sheet, tree->u.value,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
666 667 668 669
		if (!a)
			return NULL;
		if (!VALUE_IS_NUMBER (a)){
			*error_string = _("Type mismatch");
670
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
671 672 673
			return NULL;
		}
		res = g_new (Value, 1);
674
		res->type = a->type;
Arturo Espinosa's avatar
Arturo Espinosa committed
675
		if (a->type == VALUE_INTEGER){
Arturo Espinosa's avatar
Arturo Espinosa committed
676
			res->v.v_int = -a->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
677
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
678
			res->v.v_float = -a->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
679
		}
680
		value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
681
		return res;
682
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
683 684 685
	
	*error_string = _("Unknown evaluation error");
	return NULL;
686
}
Arturo Espinosa's avatar
Arturo Espinosa committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

void
cell_get_abs_col_row (CellRef *cell_ref, int eval_col, int eval_row, int *col, int *row)
{
	g_return_if_fail (cell_ref != NULL);

	if (cell_ref->col_relative)
		*col = eval_col + cell_ref->col;
	else
		*col = cell_ref->col;

	if (cell_ref->row_relative)
		*row = eval_row + cell_ref->row;
	else
		*row = cell_ref->row;
}