fix bracket evaluation
[calc.git] / element.c
1 #include <malloc.h>
2
3 #include "alloc.h"
4
5 #include "element.h"
6
7 /* global variables */
8
9 #define MAX_ARGS 100
10
11 keyword_t operators[NB_OPERATORS] = {
12 { "+\t", Add, 2, 1, 1},
13 { "-\t", Sub, 2, 1, 1},
14 { "*", Mul, 2, 1, 2},
15 { "/", Div, 2, 1, 2},
16 { "%", Mod, 2, 1, 3},
17 { "^", Pow, 2, 1, 4},
18 { "==", Equal, 2, 2, -1},
19 { "!=", Diff, 2, 2, -1},
20 { ">=", Ge, 2, 2, -1},
21 { "<=", Le, 2, 2, -1},
22 { ">", Gt, 2, 1, -1},
23 { "<", Lt, 2, 1, -1},
24 { "&", And, 2, 1, -2},
25 { "|", Or, 2, 1, -2}
26 };
27
28 keyword_t functions[NB_FUNCTIONS] = {
29 { "sqrt", Sqr, 1, 4, 5},
30 { "pow", Pow, 2, 3, 5},
31 { "cos", Cos, 1, 3, 5},
32 { "sin", Sin, 1, 3, 5},
33 { "tan", Tan, 1, 3, 5},
34 { "acos", Acos, 1, 4, 5},
35 { "asin", Asin, 1, 4, 5},
36 { "atan", Atan, 1, 4, 5},
37 { "ln", Ln, 1, 2, 5},
38 { "log", Log, 1, 3, 5},
39 { "exp", Exp, 1, 3, 5},
40 { "erfc", Erfc, 1, 4, 5},
41 { "erf", Erf, 1, 3, 5},
42 { "abs", Abs, 1, 3, 5},
43 { "floor", Floor, 1, 5, 5},
44 { "ceil", Ceil, 1, 4, 5},
45 { "sto", Store, 2, 3, 5},
46 { "rcl", Recall, 1, 3, 5},
47 { "inc", Inc, 1, 3, 5},
48 { "dec", Dec, 1, 3, 5},
49 { "disp", Disp, 0, 4, 9},
50 { "mem", Memory, 1, 3, 5},
51 { "clr", Clear, 0, 3, 9},
52 { "quit", Quit, 0, 4, 9},
53 { "help", Help, 0, 4, 9},
54 { "hist", History, 0, 4, 9},
55 { "!", Not, 1, 1, 6},
56 { "cond", Cond, 3, 4, 5},
57 { "while", While, 2, 5, 5},
58 { "print", Print, 1, 5, 5},
59 { "prog", Prog, 2, 4, 9},
60 { "arg", Arg, 1, 3, 5},
61 { "call", Call, MAX_ARGS, 4, 5},
62 { "ls", List, 0, 2, 9},
63 { "edit", Edit, 1, 4, 9},
64 { "del", Del, 1, 3, 9},
65 { "get", Get, 1, 3, 5},
66 { "len", Length, 0, 3, 5},
67 { "pop", Pop, 0, 3, 5},
68 { "push", Push, 1, 4, 5},
69 { "put", Put, 2, 3, 5},
70 { "set", Set, MAX_ARGS, 3, 5},
71 { "show", Show, 0, 4, 5},
72 { "max", Max, 2, 3, 5},
73 { "mean", Mean, 2, 4, 5},
74 { "med", Median, 0, 3, 5},
75 { "min", Min, 2, 3, 5},
76 { "ord", Order, 0, 3, 5},
77 { "prod", Prod, 0, 4, 5},
78 { "sum", Sum, 0, 3, 5},
79 { "var", Variance, 2, 3, 5},
80 { "format", Precision, 1, 6, 9},
81 { "base", Base, 2, 4, 9},
82 { "deg", Deg, 0, 3, 9},
83 { "grad", Grad, 0, 4, 9},
84 { "rad", Rad, 0, 3, 9}
85 };
86
87 keyword_t constants[NB_CONSTANTS] = {
88 { "ans", Ans, 0, 3, 5},
89 { "e", E, 0, 1, 5},
90 { "pi", Pi, 0, 2, 5}
91 };
92
93 char *symbols[NB_SYMBOLS] = {
94 "(", ")", "{", "}"
95 };
96
97 /* allocate new element */
98
99 element_t *newelement (func_t function, int nbops, int prio)
100 {
101 element_t *new = (element_t *) callocordie (1, sizeof (element_t));
102 if (nbops) {
103 new->ops = (element_t **) callocordie (nbops, sizeof (element_t *));
104 }
105 new->func = function;
106 new->nbops = nbops;
107 new->prio = prio;
108
109 return new;
110 }
111
112 /* desallocate element */
113
114 void delelement (element_t *root)
115 {
116 if ((root != NULL) && (root != ERROR_OP)) {
117 int i;
118 for (i = 0; i < root->nbops; i++) {
119 delelement (root->ops[i]);
120 }
121 if (root->nbops) {
122 free (root->ops);
123 }
124 free (root);
125 }
126 }
127
128 /* duplicate element */
129
130 element_t *dupelement (element_t *root)
131 {
132 element_t *tmp = NULL;
133 int i;
134
135 if ((root == NULL) || (root == ERROR_OP)) {
136 return root;
137 }
138 tmp = newelement (root->func, root->nbops, root->prio);
139 tmp->value = root->value;
140 for (i = 0; i < root->nbops; i++) {
141 tmp->ops[i] = dupelement (root->ops[i]);
142 }
143 return tmp;
144 }
145
146 /* vim: set ts=4 sw=4 et: */