Corrected a long-standing error in which ending text with a literal
[xcircuit.git] / spiceparser / eqn.h
blobe52fca43d4e9e74228656f9cd47ff6d7cf7d6a61
1 /********************
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
20 ******************/
21 /* eqn.h, equation parsing and evaluation
22 Conrad Ziesler
25 #define __EQN_H__
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 */
34 /*#ifndef __FILE_H__
35 #include "file.h"
36 #endif
39 #ifndef __LIST_H__
40 #include "list.h"
41 #endif
43 #ifndef __NAMES_H__
44 #include "names.h"
45 #endif
47 #ifndef __HASH_H__
48 #include "hash.h"
49 #endif
52 typedef enum itemop_e
54 OPeolist,
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 */
59 OPlit,
60 OPlitm,
61 OPlitv,
62 OPlitvm,
64 OPval,
65 OPvalm,
66 OPadd,
67 OPsub,
69 OPmul,
70 OPdiv,
71 OPexp,
72 OPopen,
74 OPclose
75 }itemop_t;
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", \
81 "OPclose" \
84 #define OPprecidence { 0, 0, 0, 0, \
85 0, 0, 0, 0, \
86 0, 0, 1, 1, \
87 2, 2, 3, 4, \
88 4 \
92 #define OPtype { 0, 1, 1, 0, \
93 1, -1, 1, -1, \
94 1, -1, 2, 2, \
95 2, 2, 2, 4, \
96 4 \
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
110 char *lit;
111 eqn_litref_t ref;
112 float cache;
113 }eqntokenlit_t;
116 typedef struct eqntokenval_st
118 float x;
119 }eqntokenval_t;
121 typedef struct eqntokenpval_st
123 float *xp;
124 } eqntokenpval_t;
126 typedef struct eqntoken_st
128 itemop_t op;
129 union lit_val_un
131 eqntokenlit_t lit;
132 eqntokenval_t val;
133 eqntokenpval_t pval;
135 }eqntoken_t;
138 #define EQN_DELIM '\''
140 #define MAXEQNSTACK 512
141 typedef struct eqntop_st
143 char *str;
144 char last;
146 eqntoken_t nodes[MAXEQNSTACK];
147 int nodep;
149 int litc;
150 int opc;
151 }eqntop_t;
155 typedef struct plookup_st
157 eqn_litref_t (*lookup)(struct plookup_st *user, char *str);
158 void *user;
159 }plookup_t;
161 #define MAXSTACK 512
162 typedef struct eqneval_st
164 float stack[MAXSTACK];
165 int stackp;
166 eqntoken_t *list;
168 }eqneval_t;
171 typedef struct eqn_st
173 #ifdef NO_EQUATIONS
174 float val;
175 #else
176 eqntoken_t *eqn;
177 #endif
178 }eqn_t;
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
215 names_t *params;
216 list_t eqnlist;
217 list_t cache;
218 }eqnlist_t;
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);