1 /*
2  *	HT Editor
3  *	eval.h
4  *
5  *	Copyright (C) 1999, 2000, 2001 Stefan Weyergraf
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License version 2 as
9  *	published by the Free Software Foundation.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *	GNU General Public License for more details.
15  *
16  *	You should have received a copy of the GNU General Public License
17  *	along with this program; if not, write to the Free Software
18  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #ifndef __EVAL_H__
22 #define __EVAL_H__
23 
24 /*#define EVAL_DEBUG*/
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif /* __cplusplus */
29 
30 #include <math.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include "evaltype.h"
41 
42 #ifndef __cplusplus
43 #define bool int
44 #endif
45 
46 typedef bool (*eval_func_handler)(eval_scalar *result, char *name, eval_scalarlist *params);
47 typedef bool (*eval_symbol_handler)(eval_scalar *result, char *name);
48 
49 #include "lex.h"
50 #include "evalx.h"
51 
52 #ifdef EVAL_DEBUG
53 
54 int debug_dump_ident;
55 
56 #endif
57 
58 /*
59  *
60  */
61 
62 #define MAX_FUNCNAME_LEN		16
63 #define MAX_SYMBOLNAME_LEN	32
64 #define MAX_ERRSTR_LEN		128
65 
66 /*
67 int f2i(double f);
68 char *binstr2cstr(char *s, int len);
69 int bin2str(char *result, void *S, int len);
70 */
71 
72 /*
73  *	ERROR HANDLING
74  */
75 
76 void clear_eval_error();
77 int get_eval_error(const char **str, int *pos);
78 void set_eval_error(const char *format,...);
79 void set_eval_error_ex(int pos, const char *format, ...);
80 
81 /*
82  *
83  */
84 
85 
86 #ifdef EVAL_DEBUG
87 
88 void integer_dump(eval_int *i);
89 void float_dump(eval_float *f);
90 void string_dump(eval_str *s);
91 
92 #endif
93 
94 void string_destroy(eval_str *s);
95 
96 /*
97  *	SCALARLIST
98  */
99 
100 void scalarlist_set(eval_scalarlist *l, eval_scalar *s);
101 void scalarlist_concat(eval_scalarlist *l, eval_scalarlist *a, eval_scalarlist *b);
102 void scalarlist_destroy(eval_scalarlist *l);
103 void scalarlist_destroy_gentle(eval_scalarlist *l);
104 
105 #ifdef EVAL_DEBUG
106 void scalarlist_dump(eval_scalarlist *l);
107 #endif
108 
109 /*
110  *	SCALAR
111  */
112 
113 void scalar_setint(eval_scalar *s, eval_int *i);
114 void scalar_setstr(eval_scalar *s, eval_str *t);
115 
116 #ifdef EVAL_DEBUG
117 void scalar_dump(eval_scalar *s);
118 #endif
119 
120 void scalar_create_int(eval_scalar *result, const eval_int *t);
121 void scalar_create_int_c(eval_scalar *result, const int i);
122 void scalar_create_int_q(eval_scalar *result, const uint64 q);
123 void scalar_create_str(eval_scalar *result, const eval_str *t);
124 void scalar_create_str_c(eval_scalar *result, const char *cstr);
125 void scalar_create_float(eval_scalar *result, const eval_float *t);
126 void scalar_create_float_c(eval_scalar *result, const double f);
127 
128 void scalar_clone(eval_scalar *result, const eval_scalar *s);
129 
130 void scalar_context_str(const eval_scalar *s, eval_str *t);
131 void scalar_context_int(const eval_scalar *s, eval_int *t);
132 void scalar_context_float(const eval_scalar *s, eval_float *t);
133 void string_concat(eval_str *s, eval_str *a, eval_str *b);
134 void scalar_concat(eval_scalar *s, const eval_scalar *a, const eval_scalar *b);
135 void scalar_destroy(eval_scalar *s);
136 int string_compare(const eval_str *a, const eval_str *b);
137 int scalar_strop(eval_scalar *xr, const eval_scalar *xa, const eval_scalar *xb, int op);
138 int scalar_float_op(eval_scalar *xr, const eval_scalar *xa, const eval_scalar *xb, int op);
139 int scalar_int_op(eval_scalar *xr, const eval_scalar *xa, const eval_scalar *xb, int op);
140 int scalar_op(eval_scalar *xr, eval_scalar *xa, eval_scalar *xb, int op);
141 void scalar_negset(eval_scalar *xr, eval_scalar *xa);
142 void scalar_notset(eval_scalar *xr, eval_scalar *xa);
143 void scalar_lnotset(eval_scalar *xr, eval_scalar *xa);
144 void scalar_miniif(eval_scalar *xr, eval_scalar *xa, eval_scalar *xb, eval_scalar *xc);
145 void sprintf_puts(char **b, char *blimit, const char *buf);
146 int sprintf_percent(char **fmt, int *fmtl, char **b, char *blimit, eval_scalar *s);
147 int func_sprintf(eval_scalar *r, const eval_str *format, const eval_scalarlist *scalars);
148 
149 /*
150  *	FUNCTIONS
151  */
152 
153 int func_eval(eval_scalar *r, eval_str *p);
154 int func_error(eval_scalar *r, eval_str *s);
155 eval_protomatch match_evalfunc_proto(char *name, eval_scalarlist *params, eval_func *proto);
156 int exec_evalfunc(eval_scalar *r, eval_scalarlist *params, eval_func *proto);
157 int evalsymbol(eval_scalar *r, char *sname);
158 int std_eval_func_handler(eval_scalar *r, char *fname, eval_scalarlist *params, eval_func *protos);
159 int evalfunc(eval_scalar *r, char *fname, eval_scalarlist *params);
160 void *eval_get_context();
161 void eval_set_context(void *context);
162 void eval_set_func_handler(eval_func_handler func_handler);
163 void eval_set_symbol_handler(eval_symbol_handler symbol_handler);
164 
165 #ifdef __cplusplus
166 }
167 #endif /* __cplusplus */
168 
169 /*
170  *	Debugging
171  */
172 
173 #ifdef EVAL_DEBUG
174 
175 extern int debug_dump_ident;
176 
177 #define DEBUG_DUMP_INDENT {\
178 	int i;\
179 	for (i=0; i<debug_dump_ident; i++) printf("\t");\
180 }
181 
182 #define DEBUG_DUMP(text...) {\
183 	DEBUG_DUMP_INDENT;\
184 	printf(text);\
185 	printf("\n");\
186 }
187 
188 #define DEBUG_DUMP_SCALAR(scalarptr, text...) {\
189 	DEBUG_DUMP_INDENT;\
190 	printf(text);\
191 	scalar_dump(scalarptr);\
192 	printf("\n");\
193 }
194 
195 #define DEBUG_DUMP_SCALARLIST(scalarlistptr, text...) {\
196 	DEBUG_DUMP_INDENT;\
197 	printf(text);\
198 	scalarlist_dump(scalarlistptr);\
199 	printf("\n");\
200 }
201 
202 #define DEBUG_DUMP_INDENT_IN debug_dump_ident++
203 #define DEBUG_DUMP_INDENT_OUT debug_dump_ident--
204 
205 #else
206 
207 #define DEBUG_DUMP(text...)
208 #define DEBUG_DUMP_SCALAR(scalarptr, text...)
209 #define DEBUG_DUMP_SCALARLIST(scalarlistptr, text...)
210 #define DEBUG_DUMP_INDENT_IN
211 #define DEBUG_DUMP_INDENT_OUT
212 
213 #endif
214 
215 #endif /* __EVAL_H__ */
216 
217