2190178ecba23fc5898a4cac2a2dc22db343f982
11 int codecmp (char *ref
, char *str
)
15 while (*ref
!= '\0') {
16 sig
= *str
++ - *ref
++;
18 return (sig
> 0) ? 1 : -1;
25 /* allocate new element */
27 element_t
*newelement (func_t function
, int nbops
)
29 element_t
*new = (element_t
*) calloc (1, sizeof (element_t
));
31 VERBOSE (ERROR
, fdprintf (stdfderr
, "can't allocate memory\n"));
42 #define NB_OPERATORS 7
44 keyword_t operators
[NB_OPERATORS
] = {
54 #define NB_FUNCTIONS 7
55 keyword_t functions
[NB_FUNCTIONS
] = {
56 { "sqrt", Sqr
, 1, 4 },
60 { "atan", Atn
, 1, 4 },
67 element_t
*parser (char *str
, char **next
)
69 element_t
*root
= NULL
;
73 while (*str
!= '\0') {
75 element_t
*new = NULL
;
76 VERBOSE (DEBUG
, PRINTOUT ("Processing: %s\n", str
));
78 /* skip spaces and tabs */
80 if ((*str
== ' ') || (*str
== '\t')) {
90 } else if (root
->func
!= Set
) {
91 new = newelement (Set
, MAX_OPERANDS
);
99 /* check for parent */
108 new = parser (str
+ 1, &str
);
109 if (new == (void *)(-1)) {
113 for (i
= 0, j
= 0; i
< root
->nbops
; i
++) {
114 if (root
->ops
[i
] == NULL
) {
115 if (new->func
== Set
) {
116 root
->ops
[i
] = new->ops
[j
++];
117 if (new->ops
[j
] == NULL
) {
136 /* look for operators */
138 for (i
= 0; i
< NB_OPERATORS
; i
++) {
139 keyword_t
*operator = operators
+ i
;
140 if (codecmp (operator->keyword
, str
) == 0) {
141 if ((root
) && (root
->func
== Val
)) {
142 VERBOSE (DEBUG
, PRINTOUT ("Oper: %d\n", operator->func
));
143 new = newelement (operator->func
, operator->nbops
);
152 str
+= operator->offset
;
158 VERBOSE (DEBUG
, PRINTOUT ("stop processing operator\n"));
162 /* look for functions */
164 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
165 keyword_t
*function
= functions
+ i
;
166 if (codecmp (function
->keyword
, str
) == 0) {
168 VERBOSE (DEBUG
, PRINTOUT ("Func: %d\n", function
->func
));
169 new = newelement (function
->func
, function
->nbops
);
177 str
+= function
->offset
;
183 VERBOSE (DEBUG
, PRINTOUT ("stop processing function\n"));
187 /* last attend to detect addition and substraction */
189 if (((*str
== '-') || (*str
== '+')) &&
190 ((*(str
+ 1) >= '0') && (*(str
+ 1) <= '9')) &&
191 ((root
) && (root
->func
== Val
))) {
192 VERBOSE (DEBUG
, PRINTOUT ("Oper: %d\n", Add
));
193 new = newelement (Add
, 2);
201 /* look for number */
203 if (((*str
>= '0') && (*str
<= '9')) ||
204 (*str
== '.') || (*str
== '-') ||(*str
== '+')) {
206 float value
= strtof (str
, &pt
);
207 VERBOSE (DEBUG
, PRINTOUT ("Value: %f\n", value
));
209 new = newelement (Val
, 1);
217 if (root
->func
== Val
) {
218 element_t
*set
= newelement (Set
, MAX_OPERANDS
);
225 for (i
= 0; i
< root
->nbops
; i
++) {
226 if (root
->ops
[i
] == NULL
) {
252 /* print element tree */
254 void print_element (element_t
*root
, int level
)
262 for (i
= 0; i
< level
; i
++) {
266 switch (root
->func
) {
267 case Val
: func
= "Value"; break;
268 case Set
: func
= "Set"; break;
269 case Add
: func
= "Addition"; break;
270 case Sub
: func
= "Subtraction"; break;
271 case Mul
: func
= "Multiplication"; break;
272 case Div
: func
= "Division"; break;
273 case Pow
: func
= "Power"; break;
274 case Sqr
: func
= "Square Root"; break;
275 case Cos
: func
= "Cosine"; break;
276 case Sin
: func
= "Sine"; break;
277 case Atn
: func
= "Arc Tangent"; break;
278 case Log
: func
= "Logarithm"; break;
279 case Exp
: func
= "Exponantial"; break;
282 PRINTOUT ("Function: %s\n", func
);
284 if (root
->func
== Val
) {
285 for (i
= 0; i
< level
; i
++) {
288 PRINTOUT ("value: %f\n", root
->value
);
290 for (i
= 0; i
< root
->nbops
; i
++) {
291 print_element (root
->ops
[i
], level
+ 1);
296 /* vim: set ts=4 sw=4 et: */