expr.h 6.44 KB
Newer Older
1 2
#ifndef GNUMERIC_EXPR_H
#define GNUMERIC_EXPR_H
3

4 5 6 7 8 9 10 11 12
/* Forward references for structures.  */
typedef struct _Value Value;
typedef struct _ExprTree ExprTree;
typedef struct _CellRef CellRef;

#include "sheet.h"
#include "symbol.h"
#include "numbers.h"
#include "str.h"
13

Morten Welinder's avatar
Morten Welinder committed
14 15
/* Some utility constants to make sure we all spell correctly */
/* TODO : These should really be const, but can't until error_string changes */
16 17 18 19 20 21 22
extern char *gnumeric_err_NULL;
extern char *gnumeric_err_DIV0;
extern char *gnumeric_err_VALUE;
extern char *gnumeric_err_REF;
extern char *gnumeric_err_NAME;
extern char *gnumeric_err_NUM;
extern char *gnumeric_err_NA;
Morten Welinder's avatar
Morten Welinder committed
23

24
typedef enum {
25 26 27 28 29 30
	OPER_EQUAL,		/* Compare value equal */
	OPER_GT,		/* Compare value greather than  */
	OPER_LT,		/* Compare value less than */
	OPER_GTE,		/* Compare value greather or equal than */
	OPER_LTE,		/* Compare value less or equal than */
	OPER_NOT_EQUAL,		/* Compare for non equivalence */
Arturo Espinosa's avatar
Arturo Espinosa committed
31
	
32
	OPER_ADD,		/* Add  */
33
	OPER_SUB,		/* Subtract */
34 35 36 37 38 39 40 41 42 43
	OPER_MULT,		/* Multiply */
	OPER_DIV,		/* Divide */
	OPER_EXP,		/* Exponentiate */
	OPER_CONCAT,		/* String concatenation */

	OPER_FUNCALL,		/* Function call invocation */

        OPER_CONSTANT,		/* Constant value */
	OPER_VAR,		/* Cell content lookup (variable) */
	OPER_NEG,		/* Sign inversion */
44 45
} Operation;

46 47 48 49 50 51 52
/* Shorthands for case statements.  Easy to read, easy to maintain.  */
#define OPER_ANY_BINARY OPER_EQUAL: case OPER_GT: case OPER_LT: case OPER_GTE: \
	case OPER_LTE: case OPER_NOT_EQUAL: \
	case OPER_ADD: case OPER_SUB: case OPER_MULT: case OPER_DIV: \
	case OPER_EXP: case OPER_CONCAT
#define OPER_ANY_UNARY OPER_NEG

53
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
54
	VALUE_STRING,
55 56
	VALUE_INTEGER,
	VALUE_FLOAT,
Arturo Espinosa's avatar
Arturo Espinosa committed
57
	VALUE_CELLRANGE,
Arturo Espinosa's avatar
Arturo Espinosa committed
58
	VALUE_ARRAY,
59 60
} ValueType;

61 62
struct _CellRef {
	Sheet *sheet;
63
	int   col, row;
Arturo Espinosa's avatar
Arturo Espinosa committed
64

Morten Welinder's avatar
Morten Welinder committed
65 66
	unsigned char col_relative;
	unsigned char row_relative;
67
};
68

69
struct _Value {
70 71
	ValueType type;
	union {
72
		CellRef cell;
73
		struct {
74 75 76
			CellRef cell_a;
			CellRef cell_b;
		} cell_range;
77

78 79
		struct {
			int x, y ;
80
			Value ***vals;  /* Array [x][y] */
81
		} array ;
82
		String *str;
83 84
		float_t v_float;	/* floating point */
		int_t   v_int;
85
	} v;
86
};
87

Arturo Espinosa's avatar
Arturo Espinosa committed
88 89 90
#define VALUE_IS_NUMBER(x) (((x)->type == VALUE_INTEGER) || \
			    ((x)->type == VALUE_FLOAT))

91
struct _ExprTree {
92
	Operation oper;
93 94

	int       ref_count;
95 96 97 98 99 100 101 102 103
	union {
		Value  *constant;

		struct {
			Symbol *symbol;
			GList  *arg_list;
		} function;
		
		struct {
104 105
			ExprTree *value_a;
			ExprTree *value_b;
106 107
		} binary;

108
		ExprTree *value;
109 110

		CellRef ref;
111 112 113
	} u;
};

114 115
typedef enum {
	PARSE_OK,
Arturo Espinosa's avatar
Arturo Espinosa committed
116 117
	PARSE_ERR_NO_QUOTE,
	PARSE_ERR_SYNTAX
118 119
} ParseErr;

120 121 122
/*
 * Functions come in two fashions:  Those that only deal with
 * very specific data types and a constant number of arguments,
123
 * and those who don't.
124 125 126 127 128 129 130 131
 *
 * The former kind of functions receives a precomputed array of
 * Value pointers.
 *
 * The latter sort of functions receives the plain ExprNodes and
 * it is up to that routine to do the value computations and range
 * processing.
 */
132 133

struct FunctionDefinition {
134 135 136
	/* The function name */
	char  *name;

137
	/**
138
	 *  The types accepted: see writing-functions.smgl ( bottom )
139 140 141
	 * f for float
	 * s for string
	 * b for boolean
142 143 144
	 * r for cell range
	 * a for cell array
	 * A for 'area': either range or array
145
	 * ? for any kind
146 147 148
	 *  For optional arguments do:
	 * "ff|ss" where the strings are optional
	 **/
149
	char  *args;
Arturo Espinosa's avatar
Arturo Espinosa committed
150
	char  *named_arguments;
151
	char  **help;
152
	Value *(*expr_fn)(Sheet *sheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string);
153
	
154 155 156 157
	Value *(*fn)(struct FunctionDefinition *func_def, Value *argv [], char **error_string);
};

typedef struct FunctionDefinition FunctionDefinition;
158

159
void        cell_get_abs_col_row (const CellRef *cell_ref,
160 161
				  int eval_col, int eval_row,
				  int *col, int *row);
Arturo Espinosa's avatar
Arturo Espinosa committed
162

163
ExprTree   *expr_parse_string    (const char *expr, Sheet *sheet, int col, int row,
164 165
				  const char **desired_format, char **error_msg);
/* In parser.y  */
166
ParseErr    gnumeric_expr_parser (const char *expr, Sheet *sheet,
167 168 169
				  int col, int row, const char **desired_format,
				  ExprTree **tree);

170
ExprTree   *expr_tree_duplicate  (ExprTree *expr);
171
char       *expr_decode_tree     (ExprTree *tree, Sheet *sheet,
172
				  int col, int row);
173

Morten Welinder's avatar
Morten Welinder committed
174 175 176 177 178 179 180
ExprTree   *expr_tree_new_constant(Value *v);
ExprTree   *expr_tree_new_unary  (Operation op, ExprTree *e);
ExprTree   *expr_tree_new_binary (ExprTree *l, Operation op, ExprTree *r);
ExprTree   *expr_tree_new_funcall(Symbol *sym, GList *args);
ExprTree   *expr_tree_new_var    (const CellRef *cr);
ExprTree   *expr_tree_new_error  (const char *txt);

181 182
void        expr_tree_ref        (ExprTree *tree);
void        expr_tree_unref      (ExprTree *tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
183

184 185
ExprTree   *expr_tree_invalidate_references (ExprTree *src, Sheet *src_sheet,
					     int src_col, int src_row, Sheet *sheet, int col, int row,
186 187
					     int colcount, int rowcount);

188 189
ExprTree   *expr_tree_fixup_references (ExprTree *src, Sheet *src_sheet,
					int src_col, int src_row, Sheet *sheet, int col, int row,
190 191
					int coldelta, int rowdelta);

192
Value      *eval_expr            (Sheet *sheet, ExprTree *tree,
193 194
				  int  col, int row,
				  char **error_string);
195

196
extern Value *value_zero;
197 198
Value       *value_new_float       (float_t f);
Value       *value_new_int         (int i);
199
Value       *value_new_bool        (gboolean b);
200 201 202
Value       *value_new_string      (const char *str);
Value       *value_new_cellrange   (const CellRef *a, const CellRef *b);

Michael Meeks's avatar
Michael Meeks committed
203
void         value_release         (Value *value);
204 205
Value       *value_duplicate       (const Value *value);
void         value_copy_to         (Value *dest, const Value *source);
Michael Meeks's avatar
Michael Meeks committed
206
Value       *value_cast_to_float   (Value *v);
207 208 209

int          value_get_as_bool     (const Value *v, int *err);
float_t      value_get_as_float    (const Value *v);
Michael Meeks's avatar
Michael Meeks committed
210
int          value_get_as_int      (const Value *v);
211 212
char        *value_get_as_string   (const Value *value);

213
void         value_dump            (const Value *value);
214 215 216 217 218 219 220

/* Area functions ( works on VALUE_RANGE or VALUE_ARRAY */
guint        value_area_get_width  (Value *v);
guint        value_area_get_height (Value *v);
const Value *value_area_get_at_x_y (Value *v, guint x, guint y);

Value       *value_array_new       (guint width, guint height);
Michael Meeks's avatar
Michael Meeks committed
221
void         value_array_set       (Value *array, guint col, guint row, Value *v);
222 223
void         value_array_resize    (Value *v, guint width, guint height);
void         value_array_copy_to   (Value *dest, const Value *src);
224

225
/* Setup of the symbol table */
Michael Meeks's avatar
Michael Meeks committed
226 227
void         functions_init        (void);
void         constants_init        (void);
228

229
#endif /* GNUMERIC_EXPR_H */