11 int codecmp (char *ref
, char *str
)
15 while (*ref
!= '\0') {
17 sig
= (*str
== '.') ? -1 : ((*str
>= '0') && (*str
<= '9'));
22 return (sig
> 0) ? 1 : -1;
31 /* allocate new element */
33 element_t
*newelement (func_t function
, int nbops
)
35 element_t
*new = (element_t
*) calloc (1, sizeof (element_t
));
37 VERBOSE (ERROR
, fdprintf (stdfderr
, "can't allocate memory\n"));
48 #define NB_OPERATORS 5
50 keyword_t operators
[NB_OPERATORS
] = {
58 #define NB_FUNCTIONS 7
59 keyword_t functions
[NB_FUNCTIONS
] = {
60 { "sqrt", Sqr
, 1, 4 },
64 { "atan", Atn
, 1, 4 },
71 element_t
*parser (char *str
, char **next
)
73 element_t
*root
= NULL
;
76 VERBOSE (DEBUG
, PRINTOUT ("Starting parsing\n"));
79 while (*str
!= '\0') {
81 element_t
*new = NULL
;
82 VERBOSE (INFO
, PRINTOUT ("Processing: %s\n", str
));
84 /* skip spaces and tabs */
86 if ((*str
== ' ') || (*str
== '\t')) {
91 /* check for open bracket */
94 VERBOSE (DEBUG
, PRINTOUT ("start processing bracket\n"));
98 new = parser (str
+ 1, &str
);
99 if (new == ERROR_OP
) {
102 for (i
= 0; i
< root
->nbops
; i
++) {
103 if (root
->ops
[i
] == NULL
) {
112 } while (*str
== ',');
114 root
= newelement (Val
, 1);
118 new = parser (str
+ 1, &str
);
119 if ((new == ERROR_OP
) || (*str
== ',')) {
125 VERBOSE (DEBUG
, PRINTOUT ("stop processing bracket\n"));
129 /* check for closing bracket or koma */
131 if ((*str
== ')') || (*str
== ',')) {
138 /* look for operators */
140 for (i
= 0; i
< NB_OPERATORS
; i
++) {
141 keyword_t
*operator = operators
+ i
;
142 if (codecmp (operator->keyword
, str
) == 0) {
143 VERBOSE (DEBUG
, PRINTOUT ("start processing operator\n"));
144 str
+= operator->offset
;
145 if ((root
) && (root
->func
!= Set
)) {
146 VERBOSE (INFO
, PRINTOUT ("Oper: %d\n", operator->func
));
147 new = newelement (operator->func
, operator->nbops
);
153 new = parser (str
, &str
);
154 if (new == ERROR_OP
) {
162 VERBOSE (DEBUG
, PRINTOUT ("stop processing operator\n"));
170 /* look for functions */
172 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
173 keyword_t
*function
= functions
+ i
;
174 if (codecmp (function
->keyword
, str
) == 0) {
175 VERBOSE (DEBUG
, PRINTOUT ("start processing function\n"));
177 VERBOSE (INFO
, PRINTOUT ("Func: %d\n", function
->func
));
178 new = newelement (function
->func
, function
->nbops
);
186 str
+= function
->offset
;
188 VERBOSE (DEBUG
, PRINTOUT ("stop processing function\n"));
196 /* look for number */
198 if (((*str
>= '0') && (*str
<= '9')) ||
199 (*str
== '.') || (*str
== '+') || (*str
== '-')) {
200 VERBOSE (DEBUG
, PRINTOUT ("start processing value\n"));
202 float value
= strtof (str
, &pt
);
203 VERBOSE (INFO
, PRINTOUT ("Value: %f\n", value
));
205 new = newelement (Val
, 1);
212 } else if (root
->func
== Val
) {
213 if ((*str
== '+') || (*str
== '-')) {
214 element_t
*add
= newelement (Add
, 2);
230 VERBOSE (DEBUG
, PRINTOUT ("stop processing value\n"));
247 /* print element tree */
249 void print_element (element_t
*root
, int level
)
254 if ((root
== NULL
) || (root
== ERROR_OP
)) {
258 for (i
= 0; i
< level
; i
++) {
262 switch (root
->func
) {
263 case Val
: func
= "Value"; break;
264 case Set
: func
= "Set"; break;
265 case Add
: func
= "Addition"; break;
266 case Sub
: func
= "Subtraction"; break;
267 case Mul
: func
= "Multiplication"; break;
268 case Div
: func
= "Division"; break;
269 case Pow
: func
= "Power"; break;
270 case Sqr
: func
= "Square Root"; break;
271 case Cos
: func
= "Cosine"; break;
272 case Sin
: func
= "Sine"; break;
273 case Atn
: func
= "Arc Tangent"; break;
274 case Log
: func
= "Logarithm"; break;
275 case Exp
: func
= "Exponantial"; break;
278 PRINTOUT ("Function: %s\n", func
);
280 if ((root
->func
== Val
) && (root
->ops
[0] == NULL
)) {
281 for (i
= 0; i
< level
; i
++) {
284 PRINTOUT ("value: %f\n", root
->value
);
286 for (i
= 0; i
< root
->nbops
; i
++) {
287 print_element (root
->ops
[i
], level
+ 1);
292 /* vim: set ts=4 sw=4 et: */