5333a9c4a06ee01163a972a544777eb2cde1977f
11 /* global variables */
15 #define STORAGE_SIZE 10
16 double storage
[STORAGE_SIZE
] = {0};
22 int codecmp (char *ref
, char *str
)
26 while (*ref
!= '\0') {
28 sig
= (*str
== '.') ? -1 : ((*str
>= '0') && (*str
<= '9'));
33 return (sig
> 0) ? 1 : -1;
42 /* allocate new element */
44 element_t
*newelement (func_t function
, int nbops
, int prio
)
46 element_t
*new = (element_t
*) calloc (1, sizeof (element_t
));
48 VERBOSE (ERROR
, fprintf (stderr
, "can't allocate memory\n"));
52 new->ops
= (element_t
**) calloc (nbops
, sizeof (element_t
*));
53 if (new->ops
== NULL
) {
54 VERBOSE (ERROR
, fprintf (stderr
, "can't allocate memory\n"));
65 /* desallocate element */
67 void delelement (element_t
*root
)
69 if ((root
!= NULL
) && (root
!= ERROR_OP
)) {
71 for (i
= 0; i
< root
->nbops
; i
++) {
72 if ((root
->ops
[i
] != NULL
) && (root
->ops
[i
] != ERROR_OP
)) {
73 delelement (root
->ops
[i
]);
83 /* duplicate element */
85 element_t
*dupelement (element_t
*root
)
87 element_t
*tmp
= NULL
;
90 if ((root
== NULL
) || (root
== ERROR_OP
)) {
93 tmp
= newelement (root
->func
, root
->nbops
, root
->prio
);
94 tmp
->value
= root
->value
;
95 for (i
= 0; i
< root
->nbops
; i
++) {
96 tmp
->ops
[i
] = dupelement (root
->ops
[i
]);
103 #define NB_OPERATORS 14
104 keyword_t operators
[NB_OPERATORS
] = {
105 { "+\t", Add
, 2, 1, 1},
106 { "-\t", Sub
, 2, 1, 1},
107 { "*", Mul
, 2, 1, 2},
108 { "/", Div
, 2, 1, 2},
109 { "%", Mod
, 2, 1, 3},
110 { "^", Pow
, 2, 1, 4},
111 { "==", Equal
, 2, 2, -1},
112 { "!=", Diff
, 2, 2, -1},
113 { ">=", Ge
, 2, 2, -1},
114 { "<=", Le
, 2, 2, -1},
115 { ">", Gt
, 2, 1, -1},
116 { "<", Lt
, 2, 1, -1},
117 { "&", And
, 2, 1, -2},
121 #define NB_FUNCTIONS 24
122 keyword_t functions
[NB_FUNCTIONS
] = {
123 { "sqrt", Sqr
, 1, 4, 5},
124 { "pow", Pow
, 2, 3, 5},
125 { "cos", Cos
, 1, 3, 5},
126 { "sin", Sin
, 1, 3, 5},
127 { "tan", Tan
, 1, 3, 5},
128 { "acos", Acos
, 1, 4, 5},
129 { "asin", Asin
, 1, 4, 5},
130 { "atan", Atan
, 1, 4, 5},
131 { "log", Log
, 1, 3, 5},
132 { "exp", Exp
, 1, 3, 5},
133 { "abs", Abs
, 1, 3, 5},
134 { "floor", Floor
, 1, 5, 5},
135 { "ceil", Ceil
, 1, 4, 5},
136 { "sto", Store
, 2, 3, 5},
137 { "rcl", Recall
, 1, 3, 5},
138 { "inc", Inc
, 1, 3, 5},
139 { "dec", Dec
, 1, 3, 5},
140 { "disp", Disp
, 0, 4, 9},
141 { "quit", Quit
, 0, 4, 9},
142 { "help", Help
, 0, 4, 9},
143 { "!", Not
, 1, 1, 6},
144 { "cond", Cond
, 3, 4, 5},
145 { "while", While
, 2, 5, 5},
146 { "print", Print
, 1, 5, 5}
149 #define NB_CONSTANTS 3
150 keyword_t constants
[NB_CONSTANTS
] = {
151 { "ans", Ans
, 0, 3, 5},
157 char *symbols
[NB_SYMBOLS
] = {
161 /* subparser function */
163 element_t
*subparser (element_t
**proot
, char **pstr
, func_t func
, int nbops
, int prio
)
165 element_t
*new = newelement (func
, nbops
, prio
);
166 new->ops
[0] = *proot
;
167 new->ops
[1] = parser (*pstr
, pstr
, new->prio
);
168 if ((new->ops
[1] == NULL
) || ((new->ops
[1] != ERROR_OP
) && (new->ops
[1]->prio
== 9))) {
169 delelement (new->ops
[1]);
170 new->ops
[1] = ERROR_OP
;
172 if (new->ops
[1] == ERROR_OP
) {
177 *proot
= newelement (Val
, 1, 5);
178 (*proot
)->ops
[0] = new;
183 /* parser function */
185 element_t
*parser (char *str
, char **next
, int prio
)
187 element_t
*root
= NULL
;
190 VERBOSE (DEBUG
, fprintf (stdout
, "Starting parsing\n"));
193 while (*str
!= '\0') {
195 element_t
*new = NULL
;
196 VERBOSE (INFO
, fprintf (stdout
, "Processing: %s\n", str
));
198 /* end without printing */
207 /* skip spaces and tabs */
209 if ((*str
== ' ') || (*str
== '\t')) {
214 /* check for open brace */
217 VERBOSE (DEBUG
, fprintf (stdout
, "start processing brace\n"));
222 element_t
**prog
= NULL
;
223 new = newelement (Prog
, 0, 5);
228 new = parser (str
+ 1, &str
, 0);
229 if ((new == NULL
) || ((new != ERROR_OP
) && (new->prio
== 9))) {
233 if (new == ERROR_OP
) {
237 element_t
*newprog
= newelement (Prog
, (*prog
)->nbops
+ 1, 5);
238 for (i
= 0; i
< (*prog
)->nbops
; i
++) {
239 newprog
->ops
[i
] = (*prog
)->ops
[i
];
240 (*prog
)->ops
[i
] = NULL
;
242 newprog
->ops
[(*prog
)->nbops
] = new;
245 } while (*str
== ',');
252 VERBOSE (DEBUG
, fprintf (stdout
, "stop processing brace\n"));
256 /* check for open bracket */
259 VERBOSE (DEBUG
, fprintf (stdout
, "start processing bracket\n"));
263 new = parser (str
+ 1, &str
, 0);
264 if ((new == NULL
) || ((new != ERROR_OP
) && (new->prio
== 9))) {
268 if ((new == NULL
) || (new == ERROR_OP
)) {
272 for (i
= 0; i
< root
->nbops
; i
++) {
273 if (root
->ops
[i
] == NULL
) {
284 } while (*str
== ',');
286 root
= newelement (Val
, 1, 5);
287 new = parser (str
+ 1, &str
, 0);
288 if ((new == NULL
) || ((new != ERROR_OP
) && (new->prio
== 9))) {
292 if ((new == NULL
) || (new == ERROR_OP
) || (*str
== ',')) {
304 VERBOSE (DEBUG
, fprintf (stdout
, "stop processing bracket\n"));
308 /* check for closing bracket, closing brace or koma */
310 if ((*str
== ')') || (*str
== '}') || (*str
== ',')) {
321 /* look for operators */
323 for (i
= 0; i
< NB_OPERATORS
; i
++) {
324 keyword_t
*operator = operators
+ i
;
325 if (codecmp (operator->keyword
, str
) == 0) {
326 VERBOSE (DEBUG
, fprintf (stdout
, "start processing operator\n"));
328 if ((prio
) && (prio
> operator->prio
)) {
329 VERBOSE (DEBUG
, fprintf (stdout
, "stop because operator priority\n"));
333 str
+= operator->offset
;
334 VERBOSE (INFO
, fprintf (stdout
, "Oper: %d\n", operator->func
));
335 if (subparser (&root
, &str
, operator->func
, operator->nbops
, operator->prio
) == ERROR_OP
) {
339 } else if (*str
== '-') {
340 root
= newelement (Sig
, 1, 9);
345 VERBOSE (DEBUG
, fprintf (stdout
, "stop processing operator\n"));
353 /* look for functions */
355 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
356 keyword_t
*function
= functions
+ i
;
357 if (codecmp (function
->keyword
, str
) == 0) {
358 VERBOSE (DEBUG
, fprintf (stdout
, "start processing function\n"));
360 VERBOSE (INFO
, fprintf (stdout
, "Func: %d\n", function
->func
));
361 root
= newelement (function
->func
, function
->nbops
, function
->prio
);
366 str
+= function
->offset
;
368 VERBOSE (DEBUG
, fprintf (stdout
, "stop processing function\n"));
376 /* look for constant */
378 for (i
= 0; i
< NB_CONSTANTS
; i
++) {
379 keyword_t
*constant
= constants
+ i
;
380 if (codecmp (constant
->keyword
, str
) == 0) {
381 VERBOSE (DEBUG
, fprintf (stdout
, "start processing constant\n"));
383 VERBOSE (INFO
, fprintf (stdout
, "Const: %d\n", constant
->func
));
384 root
= newelement (constant
->func
, constant
->nbops
, constant
->prio
);
389 str
+= constant
->offset
;
391 VERBOSE (DEBUG
, fprintf (stdout
, "stop processing constant\n"));
399 /* look for number */
401 if (((*str
>= '0') && (*str
<= '9')) ||
402 (*str
== '.') || (*str
== '+') || (*str
== '-')) {
403 VERBOSE (DEBUG
, fprintf (stdout
, "start processing value\n"));
405 double value
= strtod (str
, &pt
);
406 VERBOSE (INFO
, fprintf (stdout
, "Value: %f\n", value
));
408 if ((root
== NULL
) || (root
->prio
== 6)) {
409 new = newelement (Val
, 1, 5);
414 for (i
= 0; i
< root
->nbops
; i
++) {
415 if (root
->ops
[i
] == NULL
) {
428 } else if ((*str
== '+') || (*str
== '-')) {
429 if ((prio
) && (prio
> 1)) {
430 VERBOSE (DEBUG
, fprintf (stdout
, "stop because operator priority\n"));
434 if (subparser (&root
, &str
, Add
, 2, 1) == ERROR_OP
) {
444 VERBOSE (DEBUG
, fprintf (stdout
, "stop processing value\n"));
463 /* print element tree */
465 void print_element (element_t
*root
, int level
)
470 if ((root
== NULL
) || (root
== ERROR_OP
)) {
474 for (i
= 0; i
< level
; i
++) {
475 fprintf (stdout
, " ");
478 switch (root
->func
) {
479 case Val
: func
= "Value"; break;
480 case Sig
: func
= "Sign"; break;
481 case Add
: func
= "Addition"; break;
482 case Sub
: func
= "Subtraction"; break;
483 case Mul
: func
= "Multiplication"; break;
484 case Div
: func
= "Division"; break;
485 case Mod
: func
= "Modulo"; break;
486 case Pow
: func
= "Power"; break;
487 case Sqr
: func
= "Square Root"; break;
488 case Cos
: func
= "Cosine"; break;
489 case Sin
: func
= "Sine"; break;
490 case Tan
: func
= "Tangent"; break;
491 case Acos
: func
= "Arc Cosine"; break;
492 case Asin
: func
= "Arc Sine"; break;
493 case Atan
: func
= "Arc Tangent"; break;
494 case Log
: func
= "Logarithm"; break;
495 case Exp
: func
= "Exponantial"; break;
496 case Abs
: func
= "Absolute value"; break;
497 case Ceil
: func
= "Ceil value"; break;
498 case Floor
: func
= "Floor value"; break;
499 case Store
: func
= "Store"; break;
500 case Recall
: func
= "Recall"; break;
501 case Inc
: func
= "Increase"; break;
502 case Dec
: func
= "Decrease"; break;
503 case Disp
: func
= "Display"; break;
504 case Quit
: func
= "Quit"; break;
505 case Help
: func
= "Help"; break;
506 case Ans
: func
= "Ans"; break;
507 case Pi
: func
= "Pi"; break;
508 case E
: func
= "E"; break;
509 case Equal
: func
= "Equal"; break;
510 case Diff
: func
= "Different"; break;
511 case Ge
: func
= "Greater or equal"; break;
512 case Le
: func
= "Lesser or equal"; break;
513 case Gt
: func
= "Greater"; break;
514 case Lt
: func
= "Lesser"; break;
515 case And
: func
= "And"; break;
516 case Or
: func
= "Or"; break;
517 case Not
: func
= "Not"; break;
518 case Cond
: func
= "Condition"; break;
519 case While
: func
= "While"; break;
520 case Prog
: func
= "Program"; break;
521 case Print
: func
= "Print"; break;
524 fprintf (stdout
, "Function: %s\n", func
);
526 if ((root
->func
== Val
) && (root
->ops
[0] == NULL
)) {
527 for (i
= 0; i
< level
; i
++) {
528 fprintf (stdout
, " ");
530 fprintf (stdout
, "value: %f\n", root
->value
);
532 for (i
= 0; i
< root
->nbops
; i
++) {
533 print_element (root
->ops
[i
], level
+ 1);
538 /* storage functions */
540 double store (int index
, double value
)
542 if ((index
> 0) && (index
<= STORAGE_SIZE
)) {
543 storage
[index
- 1] = value
;
545 VERBOSE (WARNING
, fprintf (stdout
, "invalid index (%d) [1, %d]\n", index
, STORAGE_SIZE
));
550 double recall (int index
)
552 if ((index
> 0) && (index
<= STORAGE_SIZE
)) {
553 return storage
[index
- 1];
555 VERBOSE (WARNING
, fprintf (stdout
, "invalid index (%d) [1, %d]\n", index
, STORAGE_SIZE
));
560 double increase (int index
)
562 if ((index
> 0) && (index
<= STORAGE_SIZE
)) {
563 return storage
[index
- 1]++;
565 VERBOSE (WARNING
, fprintf (stdout
, "invalid index (%d) [1, %d]\n", index
, STORAGE_SIZE
));
570 double decrease (int index
)
572 if ((index
> 0) && (index
<= STORAGE_SIZE
)) {
573 return storage
[index
- 1]--;
575 VERBOSE (WARNING
, fprintf (stdout
, "invalid index (%d) [1, %d]\n", index
, STORAGE_SIZE
));
583 fprintf (stdout
, "storage:");
584 for (i
= 0; i
< STORAGE_SIZE
; i
++) {
585 fprintf (stdout
, " %g", storage
[i
]);
587 fprintf (stdout
, "\n");
590 /* While do function */
592 double while_do (element_t
*cond
, element_t
*action
)
595 element_t
*temp
= NULL
;
597 VERBOSE (DEBUG
, fprintf (stdout
, "starting while loop\n"));
599 VERBOSE (DEBUG
, fprintf (stdout
, "loop...\n"));
601 temp
= dupelement (cond
);
602 if (!evaluate_element (temp
, 0)) {
606 temp
= dupelement (action
);
607 ret
= evaluate_element (temp
, 0);
611 VERBOSE (DEBUG
, fprintf (stdout
, "ending while loop\n"));
616 /* program function */
618 double program_do (element_t
**prog
, int nbcalls
)
622 for (i
= 0; i
< nbcalls
; i
++) {
623 ret
= evaluate_element (prog
[i
], 0);
631 void set_format (char *prompt
, int precision
)
633 char buffer
[128] = {0};
634 sprintf (buffer
, "%s%%.%dg\n", prompt
, precision
);
636 format
= strdup (buffer
);
647 double print (double value
)
649 fprintf (stdout
, format
? format
: "=> %.6g\n", value
);
658 fprintf (stdout
, "bye\n");
666 fprintf (stdout
, "calc is a simple calculator\n\n");
667 fprintf (stdout
, "supported operators:");
668 fprintf (stdout
, " + - * / %% ^\n");
669 fprintf (stdout
, "camparison operators:");
670 fprintf (stdout
, " == != >= <= > <\n");
671 fprintf (stdout
, "logical operators:");
672 fprintf (stdout
, " & | !\n");
673 fprintf (stdout
, "mathematic functions:");
674 fprintf (stdout
, " pow sqrt log exp\n");
675 fprintf (stdout
, "trigonometric functions:");
676 fprintf (stdout
, " cos sin tan acos asin atan\n");
677 fprintf (stdout
, "supported functions:");
678 fprintf (stdout
, " abs ceil floor\n");
679 fprintf (stdout
, "storage functions:");
680 fprintf (stdout
, " sto rcl inc dec\n");
681 fprintf (stdout
, "conditional functions:");
682 fprintf (stdout
, " cond while print {} ;\n");
683 fprintf (stdout
, "miscellaneous functions:");
684 fprintf (stdout
, " quit help\n");
685 fprintf (stdout
, "supported constants:");
686 fprintf (stdout
, " e pi\n");
689 /* evaluate element tree */
694 double evaluate_element (element_t
*root
, char mask
)
696 double op0
= 0, op1
= 0;
697 char nextmask
= mask
;
699 if ((root
== NULL
) || (root
== ERROR_OP
)) {
700 VERBOSE (WARNING
, fprintf (stdout
, "error while evaluating\n"));
704 /* mask to manage sub operator sub and div */
705 switch (root
->func
) {
707 nextmask
&= ~MASK_SUB
;
708 nextmask
&= ~MASK_DIV
;
711 nextmask
|= MASK_SUB
;
712 nextmask
&= ~MASK_DIV
;
715 nextmask
&= ~MASK_DIV
;
718 nextmask
|= MASK_DIV
;
724 switch (root
->func
) {
727 op0
= (root
->ops
[0]) ? evaluate_element (root
->ops
[0], nextmask
) : root
->value
;
745 op1
= evaluate_element (root
->ops
[1], nextmask
);
746 } else if (root
->func
!= Store
) {
747 VERBOSE (WARNING
, fprintf (stdout
, "error while evaluating (op[1])\n"));
769 op0
= evaluate_element (root
->ops
[0], 0);
771 VERBOSE (WARNING
, fprintf (stdout
, "error while evaluating (op[0])\n"));
784 if (root
->ops
[0] == NULL
) {
785 VERBOSE (WARNING
, fprintf (stdout
, "error while evaluating (op[0])\n"));
790 op0
= (root
->ops
[0]) ? evaluate_element (root
->ops
[0], 0) : answer
;
794 switch (root
->func
) {
795 case Val
: return op0
;
796 case Sig
: return -op0
;
797 case Add
: return ((mask
& MASK_SUB
) == 0) ? op0
+ op1
: op0
- op1
;
798 case Sub
: return ((mask
& MASK_SUB
) == 0) ? op0
- op1
: op0
+ op1
;
799 case Mul
: return ((mask
& MASK_DIV
) == 0) ? op0
* op1
: op0
/ op1
;
800 case Div
: return ((mask
& MASK_DIV
) == 0) ? op0
/ op1
: op0
* op1
;
801 case Mod
: return fmod (op0
, op1
);
802 case Pow
: return pow (op0
, op1
);
803 case Sqr
: return sqrt (op0
);
804 case Cos
: return cos (op0
);
805 case Sin
: return sin (op0
);
806 case Tan
: return tan (op0
);
807 case Acos
: return acos (op0
);
808 case Asin
: return asin (op0
);
809 case Atan
: return atan (op0
);
810 case Log
: return log (op0
);
811 case Exp
: return exp (op0
);
812 case Abs
: return fabs (op0
);
813 case Ceil
: return ceil (op0
);
814 case Floor
: return floor (op0
);
815 case Store
: return store ((int)op0
, (op1
) ? op1
: answer
);
816 case Recall
: return recall ((int)op0
);
817 case Inc
: return increase ((int)op0
);
818 case Dec
: return decrease ((int)op0
);
819 case Disp
: display (); break;
820 case Quit
: quit (); break;
821 case Help
: help (); break;
822 case Ans
: return answer
;
823 case Pi
: return M_PI
;
825 case Equal
: return op0
== op1
;
826 case Diff
: return op0
!= op1
;
827 case Ge
: return op0
>= op1
;
828 case Le
: return op0
<= op1
;
829 case Gt
: return op0
> op1
;
830 case Lt
: return op0
< op1
;
831 case And
: return (op0
!= 0) && (op1
!= 0);
832 case Or
: return (op0
!= 0) || (op1
!= 0);
833 case Not
: return (op0
== 0);
835 if ((op0
) && (root
->ops
[1])) {
836 return evaluate_element (root
->ops
[1], 0);
837 } else if ((!op0
) && (root
->ops
[2])) {
838 return evaluate_element (root
->ops
[2], 0);
842 case While
: return while_do (root
->ops
[0], root
->ops
[1]);
843 case Prog
: return program_do (root
->ops
, root
->nbops
);
844 case Print
: return print (op0
);
850 char **generate_completion_list ()
853 char **list
= (char **) calloc (NB_OPERATORS
+ NB_FUNCTIONS
+ NB_CONSTANTS
+ NB_SYMBOLS
+ 1, sizeof (char *));
855 VERBOSE (ERROR
, fprintf (stderr
, "can't allocate memory\n"));
859 for (i
= 0; i
< NB_OPERATORS
; i
++) {
860 list
[l
] = strdup ((operators
+ i
)->keyword
);
861 for (j
= 0; j
< (int)strlen (list
[l
]); j
++) {
862 if (list
[i
][j
] == '\t') {
866 if (list
[l
] != NULL
) {
871 for (i
= 0; i
< NB_FUNCTIONS
; i
++) {
872 list
[l
] = strdup ((functions
+ i
)->keyword
);
873 if (list
[l
] != NULL
) {
878 for (i
= 0; i
< NB_CONSTANTS
; i
++) {
879 list
[l
] = strdup ((constants
+ i
)->keyword
);
880 if (list
[l
] != NULL
) {
885 for (i
= 0; i
< NB_SYMBOLS
; i
++) {
886 list
[l
] = strdup (symbols
[i
]);
887 if (list
[l
] != NULL
) {
895 void free_completion_list (char **list
)
900 for (i
= 0; i
< NB_FUNCTIONS
+ NB_CONSTANTS
; i
++) {
901 if (list
[i
] != NULL
) {
910 /* vim: set ts=4 sw=4 et: */