48fb98009b9ace46f0df19152c1a0ed8a4957a51
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
;
72 VERBOSE (DEBUG
, PRINTOUT ("Starting parsing\n"));
75 while (*str
!= '\0') {
77 element_t
*new = NULL
;
78 VERBOSE (INFO
, PRINTOUT ("Processing: %s\n", str
));
80 /* skip spaces and tabs */
82 if ((*str
== ' ') || (*str
== '\t')) {
92 } else if (root
->func
!= Set
) {
93 new = newelement (Set
, MAX_OPERANDS
);
101 /* check for parent */
104 VERBOSE (DEBUG
, PRINTOUT ("start processing bracket\n"));
105 new = parser (str
+ 1, &str
);
106 if (new == ERROR_OP
) {
110 for (i
= 0, j
= 0; i
< root
->nbops
; i
++) {
111 if (root
->ops
[i
] == NULL
) {
112 if (new->func
== Set
) {
113 root
->ops
[i
] = new->ops
[j
++];
114 if (new->ops
[j
] == NULL
) {
129 VERBOSE (DEBUG
, PRINTOUT ("stop processing bracket\n"));
142 /* look for operators */
144 for (i
= 0; i
< NB_OPERATORS
; i
++) {
145 keyword_t
*operator = operators
+ i
;
146 if (codecmp (operator->keyword
, str
) == 0) {
147 VERBOSE (DEBUG
, PRINTOUT ("start processing operator\n"));
148 str
+= operator->offset
;
149 if ((root
) && (root
->func
!= Set
)) {
150 VERBOSE (INFO
, PRINTOUT ("Oper: %d\n", operator->func
));
151 new = newelement (operator->func
, operator->nbops
);
157 new = parser (str
, &str
);
158 if (new == ERROR_OP
) {
166 VERBOSE (DEBUG
, PRINTOUT ("stop processing operator\n"));
174 /* look for functions */
176 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
177 keyword_t
*function
= functions
+ i
;
178 if (codecmp (function
->keyword
, str
) == 0) {
179 VERBOSE (DEBUG
, PRINTOUT ("start processing function\n"));
181 VERBOSE (INFO
, PRINTOUT ("Func: %d\n", function
->func
));
182 new = newelement (function
->func
, function
->nbops
);
190 str
+= function
->offset
;
192 VERBOSE (DEBUG
, PRINTOUT ("stop processing function\n"));
200 /* last attend to detect addition and substraction */
202 if (((*str
== '-') || (*str
== '+')) &&
203 ((*(str
+ 1) >= '0') && (*(str
+ 1) <= '9')) &&
204 ((root
) && (root
->func
== Val
))) {
205 VERBOSE (INFO
, PRINTOUT ("Oper: %d\n", Add
));
206 new = newelement (Add
, 2);
214 /* look for number */
216 if (((*str
>= '0') && (*str
<= '9')) || (*str
== '.')) {
217 VERBOSE (DEBUG
, PRINTOUT ("start processing value\n"));
219 float value
= strtof (str
, &pt
);
220 VERBOSE (INFO
, PRINTOUT ("Value: %f\n", value
));
222 new = newelement (Val
, 1);
230 if (root
->func
== Val
) {
231 element_t
*set
= newelement (Set
, MAX_OPERANDS
);
238 for (i
= 0; i
< root
->nbops
; i
++) {
239 if (root
->ops
[i
] == NULL
) {
252 VERBOSE (DEBUG
, PRINTOUT ("stop processing value\n"));
269 /* print element tree */
271 void print_element (element_t
*root
, int level
)
276 if ((root
== NULL
) || (root
== ERROR_OP
)) {
280 for (i
= 0; i
< level
; i
++) {
284 switch (root
->func
) {
285 case Val
: func
= "Value"; break;
286 case Set
: func
= "Set"; break;
287 case Add
: func
= "Addition"; break;
288 case Sub
: func
= "Subtraction"; break;
289 case Mul
: func
= "Multiplication"; break;
290 case Div
: func
= "Division"; break;
291 case Pow
: func
= "Power"; break;
292 case Sqr
: func
= "Square Root"; break;
293 case Cos
: func
= "Cosine"; break;
294 case Sin
: func
= "Sine"; break;
295 case Atn
: func
= "Arc Tangent"; break;
296 case Log
: func
= "Logarithm"; break;
297 case Exp
: func
= "Exponantial"; break;
300 PRINTOUT ("Function: %s\n", func
);
302 if (root
->func
== Val
) {
303 for (i
= 0; i
< level
; i
++) {
306 PRINTOUT ("value: %f\n", root
->value
);
308 for (i
= 0; i
< root
->nbops
; i
++) {
309 print_element (root
->ops
[i
], level
+ 1);
314 /* vim: set ts=4 sw=4 et: */