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

char *parser_expr;
6
ParseErr parser_error;
7 8 9
EvalNode *parser_result;

EvalNode *
Arturo Espinosa's avatar
Arturo Espinosa committed
10
eval_parse_string (char *expr, char **error_msg)
11 12
{
	parser_expr = expr;
13
	parser_error = PARSE_OK;
14
	
15
	yyparse ();
16 17 18
	switch (parser_error){
	case PARSE_OK:
		*error_msg = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
19
		return parser_result;
20

Arturo Espinosa's avatar
Arturo Espinosa committed
21 22 23 24
	case PARSE_ERR_SYNTAX:
		*error_msg = _("Syntax error");
		break;
			
25 26 27 28
	case PARSE_ERR_NO_QUOTE:
		*error_msg = _("Missing quote");
		break;
	}
29 30 31
	return NULL;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
32
void
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
eval_release_value (Value *value)
{
	switch (value->type){
	case VALUE_STRING:
		symbol_unref (value->v.str);
		break;

	case VALUE_INTEGER:
		mpz_clear (value->v.v_int);
		break;
		
	case VALUE_FLOAT:
		mpf_clear (value->v.v_float);
		break;

	default:
		g_warning ("Unknown value type passed to eval_release_value\n");
	}
}

53 54 55
void
eval_release_node (EvalNode *node)
{
56 57
	g_return_if_fail (node != NULL);
	
58
	switch (node->oper){
59 60 61
	case OP_VAR:
		break;
		
62
	case OP_CONSTANT:
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		eval_release_value (node->u.constant);
		break;
		
	case OP_FUNCALL:
		symbol_unref (node->u.function.symbol);
		break;

	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
	case OP_CONCAT:
		eval_release_node (node->u.binary.value_a);
		eval_release_node (node->u.binary.value_b);
		break;

	case OP_NEG:
		eval_release_node (node->u.value);
		break;
		
84
	default:
85
		g_warning ("Unknown ExprNode type passed to eval_release_node\n");
86
	}
87
	g_free (node);
88
}
89

Arturo Espinosa's avatar
Arturo Espinosa committed
90 91 92 93
/*
 * Casts a value to float if it is integer, and returns
 * a new Value * if required
 */
94
Value *
Arturo Espinosa's avatar
Arturo Espinosa committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
eval_cast_to_float (Value *v)
{
	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);
	eval_release_value (v);
	
	return newv;
}

static Value *
eval_cell_value (Sheet *sheet, Value *value)
114
{
Arturo Espinosa's avatar
Arturo Espinosa committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	Value *res;
	
	res = g_new (Value, 1);
	res->type = value->type;
	
	switch (res->type){
	case VALUE_STRING:
		res->v.str = value->v.str;
		symbol_ref (res->v.str);
		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;
		
	case VALUE_CELLRANGE:
		res->v.cell_range = value->v.cell_range;
		break;
	}
	return res;
}

Value *
eval_node_value (void *asheet, EvalNode *node, char **error_string)
{
	Value *a, *b, *res;
	Sheet *sheet = asheet;
	
149
	switch (node->oper){
Arturo Espinosa's avatar
Arturo Espinosa committed
150 151 152 153 154
	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
Arturo Espinosa's avatar
Arturo Espinosa committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 316 317 318 319 320
		a = eval_node_value (sheet, node->u.binary.value_a,
				     error_string);
		b = eval_node_value (sheet, node->u.binary.value_b,
				     error_string);

		if (!(a && b)){
			if (a)
				eval_release_value (a);
			if (b)
				eval_release_value (b);
			return NULL;
		}
		
		if (!VALUE_IS_NUMBER (a) || !VALUE_IS_NUMBER (b)){
			eval_release_value (a);
			eval_release_value (b);
			*error_string = _("Type mismatch");
			return NULL;
		}
		
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER && b->type == VALUE_INTEGER){
			res->type = VALUE_INTEGER;
			mpz_init (res->v.v_int);
			
			switch (node->oper){
			case OP_ADD:
				mpz_add (res->v.v_int, a->v.v_int, b->v.v_int);
				break;
				
			case OP_SUB:
				mpz_sub (res->v.v_int, a->v.v_int, b->v.v_int);
				break;
				
			case OP_MULT:
				mpz_mul (res->v.v_int, a->v.v_int, b->v.v_int);
				break;
				
			case OP_DIV:
				if (mpz_cmp_si (b->v.v_int, 0)){
					eval_release_value (a);
					eval_release_value (b);
					eval_release_value (res);
					*error_string = _("Division by zero");
					return NULL;
				}
					
				mpz_tdivq(res->v.v_int, a->v.v_int,b->v.v_int);
				break;
				
			case OP_EXP:
				mpz_set_si (res->v.v_int, 0);
				g_warning ("INT EXP not implemented yet\n");
				break;
			default:
			}
		} else {
			res->type = VALUE_FLOAT;
			mpf_init (res->v.v_float);
			a = eval_cast_to_float (a);
			b = eval_cast_to_float (b);
			
			switch (node->oper){
			case OP_ADD:
				mpf_add (res->v.v_float,
					 a->v.v_float, b->v.v_float);
				break;
				
			case OP_SUB:
				mpf_sub (res->v.v_float,
					 a->v.v_float, b->v.v_float);
				break;
				
			case OP_MULT:
				mpf_mul (res->v.v_float,
					 a->v.v_float, b->v.v_float);
				break;
				
			case OP_DIV:
				if (mpf_cmp_si (b->v.v_int, 0)){
					eval_release_value (a);
					eval_release_value (b);
					eval_release_value (res);
					*error_string = _("Division by zero");
					return NULL;
				}
					
				mpf_div (res->v.v_float,
					 a->v.v_float, b->v.v_float);
				break;
				
			case OP_EXP:
				mpz_set_si (res->v.v_int, 0);
				g_warning ("FLOAT EXP not implemented yet\n");
				break;
			default:
			}
		}
		eval_release_value (a);
		eval_release_value (b);
		return res;
		
	case OP_CONCAT:
		g_warning ("Concat not implemented yet\n");
		*error_string = _("OOPS");
		return NULL;

	case OP_FUNCALL:
		g_warning ("Function call not implemented yet\n");
		*error_string = _("OOPS");
		return NULL;

	case OP_CONSTANT:
		return eval_cell_value (sheet, node->u.constant);

	case OP_VAR:{
		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;
		}

		col = node->u.constant->v.cell.col;
		row = node->u.constant->v.cell.row;

		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:
		a = eval_node_value (sheet, node->u.value,
				     error_string);
		if (!a)
			return NULL;
		if (!VALUE_IS_NUMBER (a)){
			*error_string = _("Type mismatch");
			eval_release_value (a);
			return NULL;
		}
		res = g_new (Value, 1);
		if (a->type == VALUE_INTEGER){
			mpz_init_set (res->v.v_int, a->v.v_int);
			mpz_neg (res->v.v_int, a->v.v_int);
		} else {
			mpf_init_set (res->v.v_int, a->v.v_int);
			mpf_neg (res->v.v_float, a->v.v_float);
		}
		eval_release_value (a);
		return res;
321
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
322 323 324
	
	*error_string = _("Unknown evaluation error");
	return NULL;
325
}