0caa750c65020c7f490c3da51ed64b7a82e8a0b5
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
, int prio
)
35 element_t
*new = (element_t
*) calloc (1, sizeof (element_t
));
37 VERBOSE (ERROR
, fdprintf (stdfderr
, "can't allocate memory\n"));
49 #define NB_OPERATORS 5
51 keyword_t operators
[NB_OPERATORS
] = {
52 { "+\t", Add
, 2, 1, 1},
53 { "-\t", Sub
, 2, 1, 1},
59 #define NB_FUNCTIONS 7
60 keyword_t functions
[NB_FUNCTIONS
] = {
61 { "sqrt", Sqr
, 1, 4, 4},
62 { "pow", Pow
, 2, 3, 4},
63 { "cos", Cos
, 1, 3, 4},
64 { "sin", Sin
, 1, 3, 4},
65 { "atan", Atn
, 1, 4, 4},
66 { "exp", Exp
, 1, 3, 4},
67 { "log", Log
, 1, 3, 4}
72 element_t
*parser (char *str
, char **next
, int prio
)
74 element_t
*root
= NULL
;
77 VERBOSE (DEBUG
, PRINTOUT ("Starting parsing\n"));
80 while (*str
!= '\0') {
82 element_t
*new = NULL
;
83 VERBOSE (INFO
, PRINTOUT ("Processing: %s\n", str
));
85 /* skip spaces and tabs */
87 if ((*str
== ' ') || (*str
== '\t')) {
92 /* check for open bracket */
95 VERBOSE (DEBUG
, PRINTOUT ("start processing bracket\n"));
99 new = parser (str
+ 1, &str
, 0);
100 if (new == ERROR_OP
) {
103 for (i
= 0; i
< root
->nbops
; i
++) {
104 if (root
->ops
[i
] == NULL
) {
113 } while (*str
== ',');
115 root
= newelement (Val
, 1, 4);
119 new = parser (str
+ 1, &str
, 0);
120 if ((new == ERROR_OP
) || (*str
== ',')) {
126 VERBOSE (DEBUG
, PRINTOUT ("stop processing bracket\n"));
130 /* check for closing bracket or koma */
132 if ((*str
== ')') || (*str
== ',')) {
139 /* look for operators */
141 for (i
= 0; i
< NB_OPERATORS
; i
++) {
142 keyword_t
*operator = operators
+ i
;
143 if (codecmp (operator->keyword
, str
) == 0) {
144 VERBOSE (DEBUG
, PRINTOUT ("start processing operator\n"));
146 if ((prio
) && (prio
> operator->prio
)) {
147 VERBOSE (DEBUG
, PRINTOUT ("stop processing operator because operator priority\n"));
151 str
+= operator->offset
;
152 VERBOSE (INFO
, PRINTOUT ("Oper: %d\n", operator->func
));
153 new = newelement (operator->func
, operator->nbops
, operator->prio
);
158 new->ops
[1] = parser (str
, &str
, new->prio
);
159 if (new->ops
[1] == ERROR_OP
) {
162 root
= newelement (Val
, 1, 4);
163 if (root
== ERROR_OP
) {
171 VERBOSE (DEBUG
, PRINTOUT ("stop processing operator\n"));
179 /* look for functions */
181 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
182 keyword_t
*function
= functions
+ i
;
183 if (codecmp (function
->keyword
, str
) == 0) {
184 VERBOSE (DEBUG
, PRINTOUT ("start processing function\n"));
186 VERBOSE (INFO
, PRINTOUT ("Func: %d\n", function
->func
));
187 new = newelement (function
->func
, function
->nbops
, function
->prio
);
195 str
+= function
->offset
;
197 VERBOSE (DEBUG
, PRINTOUT ("stop processing function\n"));
205 /* look for number */
207 if (((*str
>= '0') && (*str
<= '9')) ||
208 (*str
== '.') || (*str
== '+') || (*str
== '-')) {
209 VERBOSE (DEBUG
, PRINTOUT ("start processing value\n"));
211 float value
= strtof (str
, &pt
);
212 VERBOSE (INFO
, PRINTOUT ("Value: %f\n", value
));
214 new = newelement (Val
, 1, 4);
221 } else if (root
->func
== Val
) {
222 if ((*str
== '+') || (*str
== '-')) {
223 element_t
*add
= newelement (Add
, 2, 1);
239 VERBOSE (DEBUG
, PRINTOUT ("stop processing value\n"));
256 /* print element tree */
258 void print_element (element_t
*root
, int level
)
263 if ((root
== NULL
) || (root
== ERROR_OP
)) {
267 for (i
= 0; i
< level
; i
++) {
271 switch (root
->func
) {
272 case Val
: func
= "Value"; break;
273 case Set
: func
= "Set"; break;
274 case Add
: func
= "Addition"; break;
275 case Sub
: func
= "Subtraction"; break;
276 case Mul
: func
= "Multiplication"; break;
277 case Div
: func
= "Division"; break;
278 case Pow
: func
= "Power"; break;
279 case Sqr
: func
= "Square Root"; break;
280 case Cos
: func
= "Cosine"; break;
281 case Sin
: func
= "Sine"; break;
282 case Atn
: func
= "Arc Tangent"; break;
283 case Log
: func
= "Logarithm"; break;
284 case Exp
: func
= "Exponantial"; break;
287 PRINTOUT ("Function: %s\n", func
);
289 if ((root
->func
== Val
) && (root
->ops
[0] == NULL
)) {
290 for (i
= 0; i
< level
; i
++) {
293 PRINTOUT ("value: %f\n", root
->value
);
295 for (i
= 0; i
< root
->nbops
; i
++) {
296 print_element (root
->ops
[i
], level
+ 1);
301 /* vim: set ts=4 sw=4 et: */