expr.h 3.6 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 35 36
} ValueType;

typedef struct {
	int col;
	int row;
37 38 39
} CellPos;

typedef struct {
40
	int col, row;
Arturo Espinosa's avatar
Arturo Espinosa committed
41

42 43
	unsigned int col_relative:1;
	unsigned int row_relative:1;
44 45 46 47 48
} CellRef;

typedef struct {
	ValueType type;
	union {
49
		CellRef cell;
50
		struct {
51 52 53
			CellRef cell_a;
			CellRef cell_b;
		} cell_range;
54

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

Arturo Espinosa's avatar
Arturo Espinosa committed
63 64 65
#define VALUE_IS_NUMBER(x) (((x)->type == VALUE_INTEGER) || \
			    ((x)->type == VALUE_FLOAT))

66
struct ExprTree {
67
	Operation oper;
68 69

	int       ref_count;
70 71 72 73 74 75 76 77 78
	union {
		Value  *constant;

		struct {
			Symbol *symbol;
			GList  *arg_list;
		} function;
		
		struct {
79 80
			struct ExprTree *value_a;
			struct ExprTree *value_b;
81 82
		} binary;

83
		struct ExprTree *value;
84 85 86 87 88

		struct {
			void    *sheet;
			CellRef cell;
		} extref;
89 90 91
	} u;
};

92
typedef struct ExprTree ExprTree;
93

94 95
typedef enum {
	PARSE_OK,
Arturo Espinosa's avatar
Arturo Espinosa committed
96 97
	PARSE_ERR_NO_QUOTE,
	PARSE_ERR_SYNTAX
98 99
} ParseErr;

Arturo Espinosa's avatar
Arturo Espinosa committed
100 101 102 103 104 105 106 107 108 109 110 111
/*
 * 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.
 */
112 113

struct FunctionDefinition {
Arturo Espinosa's avatar
Arturo Espinosa committed
114 115 116 117 118 119 120
	/* The function name */
	char  *name;

	/* The types accepted:
	 * f for float
	 * s for string
	 * b for boolean
Arturo Espinosa's avatar
Arturo Espinosa committed
121
	 * ? for any kind
Arturo Espinosa's avatar
Arturo Espinosa committed
122 123
	 */
	char  *args;
Arturo Espinosa's avatar
Arturo Espinosa committed
124
	char  *named_arguments;
Arturo Espinosa's avatar
Arturo Espinosa committed
125
	char  **help;
Arturo Espinosa's avatar
Arturo Espinosa committed
126 127
	Value *(*expr_fn)(void *sheet, GList *expr_node_list, int eval_col, int eval_row, char **error_string);
	
128 129 130 131
	Value *(*fn)(struct FunctionDefinition *func_def, Value *argv [], char **error_string);
};

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

133
/* For communication with yyparse */
134
extern char     *parser_expr;
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
135
extern char     *parser_desired_format;
Arturo Espinosa's avatar
Arturo Espinosa committed
136
extern ParseErr  parser_error;
137 138 139
extern ExprTree *parser_result;
extern int       parser_col, parser_row;

Arturo Espinosa's avatar
Arturo Espinosa committed
140
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
141

Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
142 143 144
ExprTree   *expr_parse_string    (char *expr, int col, int row,
				  char **desired_format, char **error_msg);

145 146
char       *expr_decode_tree     (ExprTree *tree, int col, int row);

Arturo Espinosa's avatar
Arturo Espinosa committed
147 148
void        expr_tree_ref        (ExprTree *tree);
void        expr_tree_unref      (ExprTree *tree);
Arturo Espinosa's avatar
Arturo Espinosa committed
149

Arturo Espinosa's avatar
Arturo Espinosa committed
150 151 152
Value      *eval_expr            (void *asheet, ExprTree *tree,
				  int  col, int row,
				  char **error_string);
153

Arturo Espinosa's avatar
Arturo Espinosa committed
154 155 156 157
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);
158
int         value_get_as_int     (Value *v);
Arturo Espinosa's avatar
Arturo Espinosa committed
159 160 161 162
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
163 164
Value      *value_duplicate      (Value *value);

165 166 167
Value      *value_float          (float_t f);
Value      *value_int            (int i);

Arturo Espinosa's avatar
Arturo Espinosa committed
168 169 170 171 172
int         yyparse              (void);

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

174
#endif /* GNUMERIC_EXPR_H */