e7c91888f15cec4380aabf2d6b65537a06ca200a
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')) {
90 VERBOSE (DEBUG
, PRINTOUT ("start processing coma\n"));
93 return parser (str
, &str
);
94 } else if (root
->func
!= Set
) {
95 new = newelement (Set
, MAX_OPERANDS
);
101 VERBOSE (DEBUG
, PRINTOUT ("end processing first coma\n"));
102 } else /* if (root->func == Set) */ {
103 new = parser (str
, &str
);
107 for (i
= 0; i
< root
->nbops
; i
++) {
108 if (root
->ops
[i
] == NULL
) {
116 VERBOSE (DEBUG
, PRINTOUT ("end processing other coma\n"));
121 /* check for parent */
124 VERBOSE (DEBUG
, PRINTOUT ("start processing bracket\n"));
125 new = parser (str
+ 1, &str
);
126 if (new == ERROR_OP
) {
130 for (i
= 0, j
= 0; i
< root
->nbops
; i
++) {
131 if (root
->ops
[i
] == NULL
) {
132 if (new->func
== Set
) {
133 root
->ops
[i
] = new->ops
[j
++];
134 if (new->ops
[j
] == NULL
) {
149 if (new->func
!= Set
) {
155 VERBOSE (DEBUG
, PRINTOUT ("stop processing bracket\n"));
156 //if (next != NULL) {
169 /* look for operators */
171 for (i
= 0; i
< NB_OPERATORS
; i
++) {
172 keyword_t
*operator = operators
+ i
;
173 if (codecmp (operator->keyword
, str
) == 0) {
174 VERBOSE (DEBUG
, PRINTOUT ("start processing operator\n"));
175 str
+= operator->offset
;
176 if ((root
) && (root
->func
!= Set
)) {
177 VERBOSE (INFO
, PRINTOUT ("Oper: %d\n", operator->func
));
178 new = newelement (operator->func
, operator->nbops
);
184 new = parser (str
, &str
);
185 if (new == ERROR_OP
) {
193 VERBOSE (DEBUG
, PRINTOUT ("stop processing operator\n"));
201 /* look for functions */
203 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
204 keyword_t
*function
= functions
+ i
;
205 if (codecmp (function
->keyword
, str
) == 0) {
206 VERBOSE (DEBUG
, PRINTOUT ("start processing function\n"));
208 VERBOSE (INFO
, PRINTOUT ("Func: %d\n", function
->func
));
209 new = newelement (function
->func
, function
->nbops
);
217 str
+= function
->offset
;
219 VERBOSE (DEBUG
, PRINTOUT ("stop processing function\n"));
227 /* last attend to detect addition and substraction */
229 if (((*str
== '-') || (*str
== '+')) &&
230 ((*(str
+ 1) >= '0') && (*(str
+ 1) <= '9')) &&
231 ((root
) && (root
->func
== Val
))) {
232 VERBOSE (INFO
, PRINTOUT ("Oper: %d\n", Add
));
233 new = newelement (Add
, 2);
241 /* look for number */
243 if (((*str
>= '0') && (*str
<= '9')) || (*str
== '.')) {
244 VERBOSE (DEBUG
, PRINTOUT ("start processing value\n"));
246 float value
= strtof (str
, &pt
);
247 VERBOSE (INFO
, PRINTOUT ("Value: %f\n", value
));
249 new = newelement (Val
, 1);
257 if (root
->func
== Val
) {
258 element_t
*set
= newelement (Set
, MAX_OPERANDS
);
265 for (i
= 0; i
< root
->nbops
; i
++) {
266 if (root
->ops
[i
] == NULL
) {
279 VERBOSE (DEBUG
, PRINTOUT ("stop processing value\n"));
296 /* print element tree */
298 void print_element (element_t
*root
, int level
)
303 if ((root
== NULL
) || (root
== ERROR_OP
)) {
307 for (i
= 0; i
< level
; i
++) {
311 switch (root
->func
) {
312 case Val
: func
= "Value"; break;
313 case Set
: func
= "Set"; break;
314 case Add
: func
= "Addition"; break;
315 case Sub
: func
= "Subtraction"; break;
316 case Mul
: func
= "Multiplication"; break;
317 case Div
: func
= "Division"; break;
318 case Pow
: func
= "Power"; break;
319 case Sqr
: func
= "Square Root"; break;
320 case Cos
: func
= "Cosine"; break;
321 case Sin
: func
= "Sine"; break;
322 case Atn
: func
= "Arc Tangent"; break;
323 case Log
: func
= "Logarithm"; break;
324 case Exp
: func
= "Exponantial"; break;
327 PRINTOUT ("Function: %s\n", func
);
329 if (root
->func
== Val
) {
330 for (i
= 0; i
< level
; i
++) {
333 PRINTOUT ("value: %f\n", root
->value
);
335 for (i
= 0; i
< root
->nbops
; i
++) {
336 print_element (root
->ops
[i
], level
+ 1);
341 /* vim: set ts=4 sw=4 et: */