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

typedef enum {
5
6
7
8
9
10
	OPER_EQUAL,
	OPER_GT,
	OPER_LT,
	OPER_GTE,
	OPER_LTE,
	OPER_NOT_EQUAL,
Arturo Espinosa's avatar
Arturo Espinosa committed
11
	
12
13
14
15
16
17
	OPER_ADD,
	OPER_SUB,
	OPER_MULT,
	OPER_DIV,
	OPER_EXP,
	OPER_CONCAT,
18

19
	OPER_FUNCALL,
20

21
22
        OPER_CONSTANT,
	OPER_VAR,
23
	OPER_NEG,
24
25
26
} Operation;

typedef enum {
Arturo Espinosa's avatar
Arturo Espinosa committed
27
	VALUE_STRING,
28
29
	VALUE_INTEGER,
	VALUE_FLOAT,
Arturo Espinosa's avatar
Arturo Espinosa committed
30
	VALUE_CELLRANGE,
Arturo Espinosa's avatar
Arturo Espinosa committed
31
	VALUE_ARRAY,
32
33
34
} ValueType;

typedef struct {
35
36
	void *sheet;
	int   col, row;
Arturo Espinosa's avatar
Arturo Espinosa committed
37

38
39
	unsigned int col_relative:1;
	unsigned int row_relative:1;
40
41
42
43
44
} CellRef;

typedef struct {
	ValueType type;
	union {
45
		CellRef cell;
46
		struct {
47
48
49
			CellRef cell_a;
			CellRef cell_b;
		} cell_range;
50

Arturo Espinosa's avatar
Arturo Espinosa committed
51
		GList  *array;	        /* a list of Values */
52
53
		String *str;
		Symbol *sym;
54
55
		float_t v_float;	/* floating point */
		int_t   v_int;
56
57
58
	} v;
} Value;

Arturo Espinosa's avatar
Arturo Espinosa committed
59
60
61
#define VALUE_IS_NUMBER(x) (((x)->type == VALUE_INTEGER) || \
			    ((x)->type == VALUE_FLOAT))

62
struct ExprTree {
63
	Operation oper;
64
65

	int       ref_count;
66
67
68
69
70
71
72
73
74
	union {
		Value  *constant;

		struct {
			Symbol *symbol;
			GList  *arg_list;
		} function;
		
		struct {
75
76
			struct ExprTree *value_a;
			struct ExprTree *value_b;
77
78
		} binary;

79
		struct ExprTree *value;
80
81
82
	} u;
};

83
typedef struct ExprTree ExprTree;
84

85
86
typedef enum {
	PARSE_OK,
Arturo Espinosa's avatar
Arturo Espinosa committed
87
88
	PARSE_ERR_NO_QUOTE,
	PARSE_ERR_SYNTAX
89
90
} ParseErr;

Arturo Espinosa's avatar
Arturo Espinosa committed
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Functions come in two fashions:  Those that only deal with
 * very specific data types and a constant number of arguments,
 * and those who dont.
 *
 * 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.
 */
103
104

struct FunctionDefinition {
Arturo Espinosa's avatar
Arturo Espinosa committed
105
106
107
108
109
110
111
	/* The function name */
	char  *name;

	/* The types accepted:
	 * f for float
	 * s for string
	 * b for boolean
Arturo Espinosa's avatar
Arturo Espinosa committed
112
	 * ? for any kind
Arturo Espinosa's avatar
Arturo Espinosa committed
113
114
	 */
	char  *args;
Arturo Espinosa's avatar
Arturo Espinosa committed
115
	char  *named_arguments;
Arturo Espinosa's avatar
Arturo Espinosa committed
116
	char  **help;
Arturo Espinosa's avatar
Arturo Espinosa committed
117
118
	Value *(*expr_fn)(void *sheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string);
	
119
120
121
122
	Value *(*fn)(struct FunctionDefinition *func_def, Value *argv [], char **error_string);
};

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

124
/* For communication with yyparse */
125
extern char     *parser_expr;
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
126
extern char     *parser_desired_format;
Arturo Espinosa's avatar
Arturo Espinosa committed
127
extern ParseErr  parser_error;
128
extern ExprTree *parser_result;
129
extern void     *parser_sheet;
130
131
extern int       parser_col, parser_row;

132
133
134
void        cell_get_abs_col_row (CellRef *cell_ref,
				  int eval_col, int eval_row,
				  int *col, int *row);
Arturo Espinosa's avatar
Arturo Espinosa committed
135

136
ExprTree   *expr_parse_string    (char *expr, void *sheet, int col, int row,
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
137
138
				  char **desired_format, char **error_msg);

139
140
char       *expr_decode_tree     (ExprTree *tree, void *sheet,
				  int col, int row);
141

Arturo Espinosa's avatar
Arturo Espinosa committed
142
143
void        expr_tree_ref        (ExprTree *tree);
void        expr_tree_unref      (ExprTree *tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
144

Arturo Espinosa's avatar
Arturo Espinosa committed
145
146
147
Value      *eval_expr            (void *asheet, ExprTree *tree,
				  int  col, int row,
				  char **error_string);
148

Arturo Espinosa's avatar
Arturo Espinosa committed
149
150
151
152
void        value_release        (Value *value);
Value      *value_cast_to_float  (Value *v);
int         value_get_bool       (Value *v, int *err);
float_t     value_get_as_double  (Value *v);
153
int         value_get_as_int     (Value *v);
Arturo Espinosa's avatar
Arturo Espinosa committed
154
155
156
157
void        value_copy_to        (Value *dest, Value *source);
				 
void        value_dump           (Value *value);
char       *value_string         (Value *value);
Arturo Espinosa's avatar
Arturo Espinosa committed
158
159
Value      *value_duplicate      (Value *value);

160
161
162
Value      *value_float          (float_t f);
Value      *value_int            (int i);

Arturo Espinosa's avatar
Arturo Espinosa committed
163
164
165
166
167
int         yyparse              (void);

/* Setup of the symbol table */
void        functions_init       (void);
void        constants_init       (void);
168

169
#endif /* GNUMERIC_EXPR_H */