2 This file is part of the software library CADLIB written by Conrad Ziesler
3 Copyright 2003, Conrad Ziesler, all rights reserved.
5 *************************
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* eqn.h, equation parsing and evaluation
27 /* defining equations eliminates expression parsing
28 and roughly halves memory needed for storage of numerics
29 however you can no longer use parameters.
32 /* #define NO_EQUATIONS */
55 OPeol_const
, /* special terminator, for constant (reduced memory usage) */
56 OPeol_valp
, /* special terminator, for indirection (float *) */
57 OPeol_qty
, /* for simplified decoding of eol */
77 #define OPsyms { "OPeolist", "OPeol_const", "OPeol_valp", "OPeol_qty", \
78 "OPlit", "OPlitm", "OPlitv", "OPlitvm", \
79 "OPval", "OPvalm", "OPadd", "OPsub", \
80 "OPmul", "OPdiv", "OPexp","OPopen", \
84 #define OPprecidence { 0, 0, 0, 0, \
92 #define OPtype { 0, 1, 1, 0, \
99 #define OP_ISEV(a) (((a)>OPeolist) && ((a) < OPeol_qty) )
100 #define OP_ISV(a) (((a)>=OPlit) && ((a) <= OPvalm))
101 #define OP_ISEND(a) ( ((a)<=OPeol_qty) )
102 #define OP_ISANYV(a) (OP_ISV(a) || OP_ISEV(a))
105 typedef int eqn_litref_t
;
106 #define eqn_litref_INIT -1
108 typedef struct eqntokenlit_st
116 typedef struct eqntokenval_st
121 typedef struct eqntokenpval_st
126 typedef struct eqntoken_st
138 #define EQN_DELIM '\''
140 #define MAXEQNSTACK 512
141 typedef struct eqntop_st
146 eqntoken_t nodes
[MAXEQNSTACK
];
155 typedef struct plookup_st
157 eqn_litref_t (*lookup
)(struct plookup_st
*user
, char *str
);
162 typedef struct eqneval_st
164 float stack
[MAXSTACK
];
171 typedef struct eqn_st
182 eqn_t
eqn_const(float val
);
183 void debug_eqn(void *dbg_fp
, char *str
, eqn_t
*eqn
);
184 eqn_t
eqn_undefined(void);
185 eqn_t
eqn_valp(float *valp
);
186 float eqn_setvalp(eqn_t
*eqn
, float *valp
);
187 int eqn_is_undefined(eqn_t e
);
189 eqntoken_t
*eqntok_parse(char *str
);
191 eqntoken_t
*eqntoken_next(eqntoken_t
*p
);
192 int eqntok_depend(eqntoken_t
*list
, plookup_t
*lookup
);
193 int eqntok_eval(float *res
, eqntoken_t
*list
);
194 float parse_float(unsigned char *val
);
195 eqntoken_t
*eqntok_copy(eqntoken_t
*p
);
196 float eqn_setval(eqn_t
*eqn
, float val
);
197 float eqn_getval_(eqn_t
*eqn
);
198 #define eqn_getval(a) ___FIXME___
199 eqn_t
eqn_copy(eqn_t e
);
200 eqn_t
eqn_copy_m(eqn_t e
, float m
);
201 eqn_t
eqn_empty(void);
202 eqn_t
eqn_parse(unsigned char *val
);
204 void *eqn_mem_new(void);
205 void eqn_mem_push(void *p
);
206 void *eqn_mem_pop(void);
207 void eqn_mem_free(void *p
);
210 /** dependency lists ***/
213 typedef struct eqnlist_st
220 void eqnl_depend(eqnlist_t
*nl
, eqn_t e
);
221 void eqnl_free(eqnlist_t
*nl
);
222 void eqnl_init(eqnlist_t
*nl
);
223 void eqnl_evaldep(eqnlist_t
*nl
);
224 float eqnl_eval(eqnlist_t
*nl
, eqn_t e
);
225 int eqnl_add(eqnlist_t
*nl
, eqn_t e
, char *str
);
226 eqn_t
eqnl_eqn(eqnlist_t
*nl
, int index
);
227 int eqnl_find(eqnlist_t
*nl
, char *str
);
228 int eqnl_define(eqnlist_t
*nl
, int index
, float *valp
);
229 int eqnl_is_undefined(eqnlist_t
*nl
, int index
);
230 int eqnl_qty(eqnlist_t
*nl
);
231 char * eqnl_lookup(eqnlist_t
*nl
, int index
);
232 void eqnl_autodepend(eqnlist_t
*nl
);