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

char *parser_expr;
7
ParseErr parser_error;
8 9
ExprTree *parser_result;
int parser_col, parser_row;
10

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

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

37 38 39 40 41 42 43
/*
 * 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
44
void
45
eval_expr_release (ExprTree *tree)
46
{
47
	g_return_if_fail (tree != NULL);
48
	
49
	switch (tree->oper){
50 51 52
	case OP_VAR:
		break;
		
53
	case OP_CONSTANT:
54
		value_release (tree->u.constant);
55 56 57
		break;
		
	case OP_FUNCALL:
58
		symbol_unref (tree->u.function.symbol);
59 60 61 62 63 64 65 66
		break;

	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
	case OP_CONCAT:
67 68
		eval_expr_release (tree->u.binary.value_a);
		eval_expr_release (tree->u.binary.value_b);
69 70 71
		break;

	case OP_NEG:
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		eval_expr_release (tree->u.value);
		break;
		
	default:
		g_warning ("Unknown ExprTree type passed to eval_expr_release\n");
	}
	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
104 105
	g_return_if_fail (value != NULL);
	
106 107 108 109 110 111 112
	switch (value->type){
	case VALUE_STRING:
		string_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
113 114
		break;
		
115 116 117 118
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

Arturo Espinosa's avatar
Arturo Espinosa committed
119 120 121 122 123 124 125 126 127 128 129
	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;
		
130
	default:
131
		g_warning ("Unknown value type passed to value_release\n");
132
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
133
	g_free (value);
134
}
135

Arturo Espinosa's avatar
Arturo Espinosa committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*
 * 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
166 167 168 169
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
170
Value *
171
value_cast_to_float (Value *v)
Arturo Espinosa's avatar
Arturo Espinosa committed
172 173 174 175 176 177 178 179 180 181 182
{
	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);
183
	value_release (v);
Arturo Espinosa's avatar
Arturo Espinosa committed
184 185 186 187
	
	return newv;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
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
230 231 232
	if (v->type == VALUE_ARRAY)
		return 0.0;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
233
	return (float_t) v->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
234 235
}

Arturo Espinosa's avatar
Arturo Espinosa committed
236 237
static Value *
eval_cell_value (Sheet *sheet, Value *value)
238
{
Arturo Espinosa's avatar
Arturo Espinosa committed
239 240 241 242 243 244 245 246
	Value *res;
	
	res = g_new (Value, 1);
	res->type = value->type;
	
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
247
		string_ref (res->v.str);
Arturo Espinosa's avatar
Arturo Espinosa committed
248 249 250 251 252 253 254 255 256 257 258
		break;
		
	case VALUE_INTEGER:
		mpz_init (res->v.v_int);
		mpz_set  (res->v.v_int, value->v.v_int);
		break;
		
	case VALUE_FLOAT:
		mpf_init (res->v.v_float);
		mpf_set (res->v.v_float, value->v.v_float);
		break;
Arturo Espinosa's avatar
Arturo Espinosa committed
259 260 261 262 263 264 265

	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
266 267 268 269 270 271 272
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
273 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
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;
		}
316
		v = fd->fn (values, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
317 318 319 320 321 322 323 324 325 326

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

Arturo Espinosa's avatar
Arturo Espinosa committed
327 328 329 330 331 332 333 334 335 336 337 338 339
enum {
	IS_EQUAL,
	IS_LESS,
	IS_BIGGER,
};

static int
compare (Value *a, Value *b)
{
	g_warning ("Value comparission is not yet implemented\n");
	return IS_EQUAL;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
340
Value *
341
eval_expr (void *asheet, ExprTree *tree, int eval_col, int eval_row, char **error_string)
Arturo Espinosa's avatar
Arturo Espinosa committed
342 343 344
{
	Value *a, *b, *res;
	Sheet *sheet = asheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
345 346 347 348 349

	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
350
	
351
	switch (tree->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
352 353 354 355 356 357 358 359 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 399 400 401 402 403 404 405 406 407 408
	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);

		switch (tree->oper){
		case OP_EQUAL:
			res->v.v_int = comp == IS_EQUAL;
			break;

		case OP_GT:
			res->v.v_int = comp == IS_BIGGER;
			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:
			res->v.v_int = (comp == IS_EQUAL || comp == IS_BIGGER);
			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
409 410 411 412 413
	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
414 415 416 417
		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
418 419 420

		if (!(a && b)){
			if (a)
421
				value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
422
			if (b)
423
				value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
424 425 426 427
			return NULL;
		}
		
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
428 429
			value_release (a);
			value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
430 431 432 433 434 435 436 437
			*error_string = _("Type mismatch");
			return NULL;
		}
		
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
			res->type = VALUE_INTEGER;
			
438
			switch (tree->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
439
			case OP_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
440
				res->v.v_int = a->v.v_int + b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
441 442 443
				break;
				
			case OP_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
444
				res->v.v_int = a->v.v_int - b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
445 446 447
				break;
				
			case OP_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
448
				res->v.v_int = a->v.v_int * b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
449
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
450

Arturo Espinosa's avatar
Arturo Espinosa committed
451
			case OP_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
452
				if (b->v.v_int == 0){
453 454 455
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
456 457 458 459
					*error_string = _("Division by zero");
					return NULL;
				}
					
Arturo Espinosa's avatar
Arturo Espinosa committed
460
				res->v.v_int =  a->v.v_int / b->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
461 462
				break;
				
Arturo Espinosa's avatar
Arturo Espinosa committed
463 464
			case OP_EXP: 
				res->v.v_int = pow (a->v.v_int, b->v.v_int);
Arturo Espinosa's avatar
Arturo Espinosa committed
465 466 467 468 469
				break;
			default:
			}
		} else {
			res->type = VALUE_FLOAT;
Arturo Espinosa's avatar
Arturo Espinosa committed
470
			res->v.v_float = 0.0;
471 472
			a = value_cast_to_float (a);
			b = value_cast_to_float (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
473
			
474
			switch (tree->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
475
			case OP_ADD:
Arturo Espinosa's avatar
Arturo Espinosa committed
476
				res->v.v_float = a->v.v_float + b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
477 478 479
				break;
				
			case OP_SUB:
Arturo Espinosa's avatar
Arturo Espinosa committed
480
				res->v.v_float = a->v.v_float - b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
481 482 483
				break;
				
			case OP_MULT:
Arturo Espinosa's avatar
Arturo Espinosa committed
484
				res->v.v_float = a->v.v_float * b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
485 486 487
				break;
				
			case OP_DIV:
Arturo Espinosa's avatar
Arturo Espinosa committed
488
				if (b->v.v_float == 0.0){
489 490 491
					value_release (a);
					value_release (b);
					value_release (res);
Arturo Espinosa's avatar
Arturo Espinosa committed
492 493 494
					*error_string = _("Division by zero");
					return NULL;
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
495 496

				res->v.v_float = a->v.v_float / b->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
497 498 499
				break;
				
			case OP_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
500
				res->v.v_float = pow (a->v.v_float, b->v.v_float);
Arturo Espinosa's avatar
Arturo Espinosa committed
501 502 503 504
				break;
			default:
			}
		}
505 506
		value_release (a);
		value_release (b);
Arturo Espinosa's avatar
Arturo Espinosa committed
507 508 509 510 511 512 513 514
		return res;
		
	case OP_CONCAT:
		g_warning ("Concat not implemented yet\n");
		*error_string = _("OOPS");
		return NULL;

	case OP_FUNCALL:
Arturo Espinosa's avatar
Arturo Espinosa committed
515
		return eval_funcall (sheet, tree, eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
516 517

	case OP_CONSTANT:
518
		return eval_cell_value (sheet, tree->u.constant);
Arturo Espinosa's avatar
Arturo Espinosa committed
519 520

	case OP_VAR:{
521
		CellRef *ref;
Arturo Espinosa's avatar
Arturo Espinosa committed
522 523 524 525 526 527 528 529 530 531 532 533 534
		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;
		}

535
		ref = &tree->u.constant->v.cell;
Arturo Espinosa's avatar
Arturo Espinosa committed
536
		cell_get_abs_col_row (&tree->u.constant->v.cell, eval_col, eval_row, &col, &row);
537
		
Arturo Espinosa's avatar
Arturo Espinosa committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
		cell = sheet_cell_get (sheet, col, row);
		if (!cell)
			cell = sheet_cell_new (sheet, col, row);
		
		if (!cell->value){
			res = g_new (Value, 1);
			
			res->type = VALUE_INTEGER;
			res->v.v_int = 0;
		} else {
			return eval_cell_value (sheet, cell->value);
		}
		return res;
	}
	case OP_NEG:
553 554
		a = eval_expr (sheet, tree->u.value,
			       eval_col, eval_row, error_string);
Arturo Espinosa's avatar
Arturo Espinosa committed
555 556 557 558
		if (!a)
			return NULL;
		if (!VALUE_IS_NUMBER (a)){
			*error_string = _("Type mismatch");
559
			value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
560 561 562 563
			return NULL;
		}
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER){
Arturo Espinosa's avatar
Arturo Espinosa committed
564
			res->v.v_int = -a->v.v_int;
Arturo Espinosa's avatar
Arturo Espinosa committed
565
		} else {
Arturo Espinosa's avatar
Arturo Espinosa committed
566
			res->v.v_float = -a->v.v_float;
Arturo Espinosa's avatar
Arturo Espinosa committed
567
		}
568
		value_release (a);
Arturo Espinosa's avatar
Arturo Espinosa committed
569
		return res;
570
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
571 572 573
	
	*error_string = _("Unknown evaluation error");
	return NULL;
574
}
Arturo Espinosa's avatar
Arturo Espinosa committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

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;
}