Soft'N'Design Software
/
projects
/
calc.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
add log10 function
[calc.git]
/
parser.c
diff --git
a/parser.c
b/parser.c
index 0359234f352fb4669805cd3162d7aa957784d0c4..e20427cb0135215912ae9498ee4a5bb8e7f0c18d 100644
(file)
--- a/
parser.c
+++ b/
parser.c
@@
-83,9
+83,7
@@
void delelement (element_t *root)
if ((root != NULL) && (root != ERROR_OP)) {
int i;
for (i = 0; i < root->nbops; i++) {
if ((root != NULL) && (root != ERROR_OP)) {
int i;
for (i = 0; i < root->nbops; i++) {
- if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
- delelement (root->ops[i]);
- }
+ delelement (root->ops[i]);
}
if (root->nbops) {
free (root->ops);
}
if (root->nbops) {
free (root->ops);
@@
-132,7
+130,7
@@
keyword_t operators[NB_OPERATORS] = {
{ "|", Or, 2, 1, -2}
};
{ "|", Or, 2, 1, -2}
};
-#define NB_FUNCTIONS 3
1
+#define NB_FUNCTIONS 3
2
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
@@
-142,6
+140,7
@@
keyword_t functions[NB_FUNCTIONS] = {
{ "acos", Acos, 1, 4, 5},
{ "asin", Asin, 1, 4, 5},
{ "atan", Atan, 1, 4, 5},
{ "acos", Acos, 1, 4, 5},
{ "asin", Asin, 1, 4, 5},
{ "atan", Atan, 1, 4, 5},
+ { "ln", Ln, 1, 2, 5},
{ "log", Log, 1, 3, 5},
{ "exp", Exp, 1, 3, 5},
{ "abs", Abs, 1, 3, 5},
{ "log", Log, 1, 3, 5},
{ "exp", Exp, 1, 3, 5},
{ "abs", Abs, 1, 3, 5},
@@
-241,10
+240,7
@@
element_t *parser (char *str, char **next, int prio)
delelement (root);
return ERROR_OP;
}
delelement (root);
return ERROR_OP;
}
- element_t **prog = NULL;
- new = newelement (Code, 0, 5);
- root = new;
- prog = &root;
+ root = newelement (Code, 0, 5);
do {
new = parser (str + 1, &str, 0);
do {
new = parser (str + 1, &str, 0);
@@
-256,14
+252,14
@@
element_t *parser (char *str, char **next, int prio)
delelement (root);
return ERROR_OP;
}
delelement (root);
return ERROR_OP;
}
- element_t *newprog = newelement (Code,
(*prog)
->nbops + 1, 5);
- for (i = 0; i <
(*prog)
->nbops; i++) {
- newprog->ops[i] =
(*prog)
->ops[i];
-
(*prog)
->ops[i] = NULL;
+ element_t *newprog = newelement (Code,
root
->nbops + 1, 5);
+ for (i = 0; i <
root
->nbops; i++) {
+ newprog->ops[i] =
root
->ops[i];
+
root
->ops[i] = NULL;
}
}
- newprog->ops[
(*prog)
->nbops] = new;
- delelement (
*prog
);
-
(*prog)
= newprog;
+ newprog->ops[
root
->nbops] = new;
+ delelement (
root
);
+
root
= newprog;
} while (*str == ',');
if (*str != '}') {
} while (*str == ',');
if (*str != '}') {
@@
-518,7
+514,8
@@
void print_element (element_t *root, int level)
case Acos: func = "Arc Cosine"; break;
case Asin: func = "Arc Sine"; break;
case Atan: func = "Arc Tangent"; break;
case Acos: func = "Arc Cosine"; break;
case Asin: func = "Arc Sine"; break;
case Atan: func = "Arc Tangent"; break;
- case Log: func = "Logarithm"; break;
+ case Ln: func = "Logarithm (e base)"; break;
+ case Log: func = "Logarithm (10 base)"; break;
case Exp: func = "Exponantial"; break;
case Abs: func = "Absolute value"; break;
case Ceil: func = "Ceil value"; break;
case Exp: func = "Exponantial"; break;
case Abs: func = "Absolute value"; break;
case Ceil: func = "Ceil value"; break;
@@
-700,7
+697,6
@@
double execute_code (element_t **prog, int nbcalls)
int i;
for (i = 0; i < nbcalls; i++) {
ret = evaluate_element (prog[i], 0);
int i;
for (i = 0; i < nbcalls; i++) {
ret = evaluate_element (prog[i], 0);
- prog[i] = NULL;
}
return ret;
}
}
return ret;
}
@@
-832,8
+828,8
@@
double call (int id, int nbops, element_t **ops)
double *tmp = (double *) callocordie (nbops, sizeof (double));
memcpy (tmp, storage, storage_size * sizeof (double));
free (storage);
double *tmp = (double *) callocordie (nbops, sizeof (double));
memcpy (tmp, storage, storage_size * sizeof (double));
free (storage);
- storage = tmp;
- storage_size = nbops;
+
(programs + n)->storage =
storage = tmp;
+
(programs + n)->storage_size =
storage_size = nbops;
}
for (i = 0; i < nbops; i++) {
double val = evaluate_element (ops[i], 0);
}
for (i = 0; i < nbops; i++) {
double val = evaluate_element (ops[i], 0);
@@
-968,19
+964,21
@@
void help (void)
fprintf (stdout, "logical operators:");
fprintf (stdout, " & | !\n");
fprintf (stdout, "mathematic functions:");
fprintf (stdout, "logical operators:");
fprintf (stdout, " & | !\n");
fprintf (stdout, "mathematic functions:");
- fprintf (stdout, "
pow sqrt log exp
\n");
+ fprintf (stdout, "
exp ln log pow sqrt
\n");
fprintf (stdout, "trig. func.:");
fprintf (stdout, "trig. func.:");
- fprintf (stdout, "
cos sin tan acos asin a
tan\n");
+ fprintf (stdout, "
acos asin atan cos sin
tan\n");
fprintf (stdout, "supported functions:");
fprintf (stdout, " abs ceil floor\n");
fprintf (stdout, "supported functions:");
fprintf (stdout, " abs ceil floor\n");
- fprintf (stdout, "storage functions:");
- fprintf (stdout, " mem sto rcl inc dec disp\n");
+ fprintf (stdout, "stor. functions:");
+ fprintf (stdout, " clear dec disp inc mem rcl sto\n");
+ fprintf (stdout, "cond. functions:");
+ fprintf (stdout, " cond print while {} ;\n");
fprintf (stdout, "prog. functions:");
fprintf (stdout, "prog. functions:");
- fprintf (stdout, " c
ond while print {} ;
\n");
+ fprintf (stdout, " c
all del edit ls prog
\n");
fprintf (stdout, "misc. functions:");
fprintf (stdout, "misc. functions:");
- fprintf (stdout, "
quit help
\n");
+ fprintf (stdout, "
help quit
\n");
fprintf (stdout, "supported constants:");
fprintf (stdout, "supported constants:");
- fprintf (stdout, " e pi\n");
+ fprintf (stdout, "
ans
e pi\n");
}
/* evaluate element tree */
}
/* evaluate element tree */
@@
-1054,6
+1052,7
@@
double evaluate_element (element_t *root, char mask)
case Acos:
case Asin:
case Atan:
case Acos:
case Asin:
case Atan:
+ case Ln:
case Log:
case Exp:
case Abs:
case Log:
case Exp:
case Abs:
@@
-1112,7
+1111,8
@@
double evaluate_element (element_t *root, char mask)
case Acos: return acos (op0);
case Asin: return asin (op0);
case Atan: return atan (op0);
case Acos: return acos (op0);
case Asin: return asin (op0);
case Atan: return atan (op0);
- case Log: return log (op0);
+ case Ln: return log (op0);
+ case Log: return log10 (op0);
case Exp: return exp (op0);
case Abs: return fabs (op0);
case Ceil: return ceil (op0);
case Exp: return exp (op0);
case Abs: return fabs (op0);
case Ceil: return ceil (op0);