expr.h 5.66 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

14
typedef enum {
15
16
17
18
19
20
	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
21
	
22
	OPER_ADD,		/* Add  */
23
	OPER_SUB,		/* Subtract */
24
25
26
27
28
29
30
31
32
33
	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 */
34
35
} Operation;

36
37
38
39
40
41
42
/* 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

43
typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
44
	VALUE_STRING,
45
46
	VALUE_INTEGER,
	VALUE_FLOAT,
Arturo Espinosa's avatar
Arturo Espinosa committed
47
	VALUE_CELLRANGE,
Arturo Espinosa's avatar
Arturo Espinosa committed
48
	VALUE_ARRAY,
49
50
} ValueType;

51
52
struct _CellRef {
	Sheet *sheet;
53
	int   col, row;
Arturo Espinosa's avatar
Arturo Espinosa committed
54

55
56
	unsigned int col_relative:1;
	unsigned int row_relative:1;
57
};
58

59
struct _Value {
60
61
	ValueType type;
	union {
62
		CellRef cell;
63
		struct {
64
65
66
			CellRef cell_a;
			CellRef cell_b;
		} cell_range;
67

68
69
		struct {
			int x, y ;
70
			Value **vals;   /* Array [x][y] */
71
		} array ;
72
73
		String *str;
		Symbol *sym;
74
75
		float_t v_float;	/* floating point */
		int_t   v_int;
76
	} v;
77
};
78

Arturo Espinosa's avatar
Arturo Espinosa committed
79
80
81
#define VALUE_IS_NUMBER(x) (((x)->type == VALUE_INTEGER) || \
			    ((x)->type == VALUE_FLOAT))

82
struct _ExprTree {
83
	Operation oper;
84
85

	int       ref_count;
86
87
88
89
90
91
92
93
94
	union {
		Value  *constant;

		struct {
			Symbol *symbol;
			GList  *arg_list;
		} function;
		
		struct {
95
96
			ExprTree *value_a;
			ExprTree *value_b;
97
98
		} binary;

99
		ExprTree *value;
100
101

		CellRef ref;
102
103
104
	} u;
};

105
106
typedef enum {
	PARSE_OK,
Arturo Espinosa's avatar
Arturo Espinosa committed
107
108
	PARSE_ERR_NO_QUOTE,
	PARSE_ERR_SYNTAX
109
110
} ParseErr;

Arturo Espinosa's avatar
Arturo Espinosa committed
111
112
113
/*
 * Functions come in two fashions:  Those that only deal with
 * very specific data types and a constant number of arguments,
114
 * and those who don't.
Arturo Espinosa's avatar
Arturo Espinosa committed
115
116
117
118
119
120
121
122
 *
 * 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.
 */
123
124

struct FunctionDefinition {
Arturo Espinosa's avatar
Arturo Espinosa committed
125
126
127
	/* The function name */
	char  *name;

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

typedef struct FunctionDefinition FunctionDefinition;
Arturo Espinosa's avatar
Arturo Espinosa committed
149

150
void        cell_get_abs_col_row (const CellRef *cell_ref,
151
152
				  int eval_col, int eval_row,
				  int *col, int *row);
Arturo Espinosa's avatar
Arturo Espinosa committed
153

154
ExprTree   *expr_parse_string    (const char *expr, Sheet *sheet, int col, int row,
155
156
				  const char **desired_format, char **error_msg);
/* In parser.y  */
157
ParseErr    gnumeric_expr_parser (const char *expr, Sheet *sheet,
158
159
160
				  int col, int row, const char **desired_format,
				  ExprTree **tree);

161
162
ExprTree   *expr_tree_duplicate  (ExprTree *expr);
ExprTree   *expr_tree_relocate   (ExprTree *expr, int col_diff, int row_diff);
163
char       *expr_decode_tree     (ExprTree *tree, Sheet *sheet,
164
				  int col, int row);
165

Morten Welinder's avatar
Morten Welinder committed
166
ExprTree   *expr_tree_new        (void);
Arturo Espinosa's avatar
Arturo Espinosa committed
167
168
void        expr_tree_ref        (ExprTree *tree);
void        expr_tree_unref      (ExprTree *tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
169

170
ExprTree   *expr_tree_invalidate_references (ExprTree *src, Sheet *sheet,
171
					     int src_col, int src_row, int col, int row,
172
173
					     int colcount, int rowcount);

174
ExprTree   *expr_tree_fixup_references (ExprTree *src, Sheet *sheet,
175
					int src_col, int src_row, int col, int row,
176
177
					int coldelta, int rowdelta);

178
Value      *eval_expr            (Sheet *sheet, ExprTree *tree,
Arturo Espinosa's avatar
Arturo Espinosa committed
179
180
				  int  col, int row,
				  char **error_string);
181

Arturo Espinosa's avatar
Arturo Espinosa committed
182
183
void        value_release        (Value *value);
Value      *value_cast_to_float  (Value *v);
184
185
186
187
188
189
190
191
192
193
194
195
196
int         value_get_bool       (const Value *v, int *err);
float_t     value_get_as_double  (const Value *v);
int         value_get_as_int     (const Value *v);
void        value_copy_to        (Value *dest, const Value *source);

/* 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);
void         value_array_resize    (Value *v, guint width, guint height);
void         value_array_copy_to   (Value *dest, const Value *src);
197

Morten Welinder's avatar
Morten Welinder committed
198
Value       *value_cellrange       (const CellRef *a, const CellRef *b);
199
			 
Arturo Espinosa's avatar
Arturo Espinosa committed
200
void        value_dump           (Value *value);
201
202
char       *value_string         (const Value *value);
Value      *value_duplicate      (const Value *value);
Arturo Espinosa's avatar
Arturo Espinosa committed
203

204
205
Value      *value_float          (float_t f);
Value      *value_int            (int i);
206
Value      *value_str            (const char *str);
207
	
Arturo Espinosa's avatar
Arturo Espinosa committed
208
209
210
/* Setup of the symbol table */
void        functions_init       (void);
void        constants_init       (void);
211

212
#endif /* GNUMERIC_EXPR_H */