From: Laurent Mazet Date: Tue, 24 Jan 2023 18:25:13 +0000 (+0100) Subject: partial while feature X-Git-Tag: v0.8~24 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=2a68864231564fadaec71a6561c86b12f781196d;p=calc.git partial while feature --- diff --git a/calc.c b/calc.c index 611e155..41429cb 100644 --- a/calc.c +++ b/calc.c @@ -291,5 +291,6 @@ int main (int argc, char *argv[]) // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1' // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe 2>&1 | grep -c error | xargs test 4 = // test: echo -e 'inc (11)\ndec (0)' | calc.exe 2>&1 | grep -c invalid | xargs test 2 = +// test: echo -e 'whl (inc (1) < 3, sto (rcl (2) + 3))' | calc.exe | xargs test 16 = /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index c347287..8654fa8 100644 --- a/parser.c +++ b/parser.c @@ -67,6 +67,31 @@ void delelement (element_t *root) } } +/* duplicate element */ + +element_t *dupelement (element_t *root) +{ + element_t *tmp = NULL; + int i; + + if ((root == NULL) || (root == ERROR_OP)) { + return root; + } + tmp = newelement (root->func, root->nbops, root->prio); + if (tmp == NULL) { + return ERROR_OP; + } + tmp->value = root->value; + for (i = 0; i < root->nbops; i++) { + tmp->ops[i] = dupelement (root->ops[i]); + if (tmp->ops[i] == ERROR_OP) { + delelement (tmp); + return ERROR_OP; + } + } + return tmp; +} + /* functions */ #define NB_OPERATORS 14 @@ -87,7 +112,7 @@ keyword_t operators[NB_OPERATORS] = { { "|", Or, 2, 1, -2} }; -#define NB_FUNCTIONS 16 +#define NB_FUNCTIONS 17 keyword_t functions[NB_FUNCTIONS] = { { "sqrt", Sqr, 1, 4, 5}, { "pow", Pow, 2, 3, 5}, @@ -96,7 +121,7 @@ keyword_t functions[NB_FUNCTIONS] = { { "atan", Atan, 1, 4, 5}, { "exp", Exp, 1, 3, 5}, { "log", Log, 1, 3, 5}, - { "sto", Store, 2, 3, 9}, + { "sto", Store, 2, 3, 5}, { "rcl", Recall, 1, 3, 5}, { "inc", Inc, 1, 3, 5}, { "dec", Dec, 1, 3, 5}, @@ -104,7 +129,8 @@ keyword_t functions[NB_FUNCTIONS] = { { "quit", Quit, 0, 4, 9}, { "help", Help, 0, 4, 9}, { "!", Not, 1, 1, 6}, - { "cond", Cond, 3, 4, 5} + { "cond", Cond, 3, 4, 5}, + { "whl", While, 2, 3, 5} }; #define NB_CONSTANTS 3 @@ -436,6 +462,7 @@ void print_element (element_t *root, int level) case Or: func = "Or"; break; case Not: func = "Not"; break; case Cond: func = "Condition"; break; + case While: func = "While"; break; } fprintf (stdout, "Function: %s\n", func); @@ -504,6 +531,35 @@ void display (void) fprintf (stdout, "\n"); } +/* While do function */ + +double while_do (element_t *cond, element_t *action) +{ + double ret = 0; + element_t *temp = NULL; + + VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n")); + if (cond == NULL) { + return ret; + } + while (1) { + VERBOSE (DEBUG, fprintf (stdout, "loop...\n")); + + temp = dupelement (cond); + if (!evaluate_element (temp, 0)) { + break; + } + if (action) { + temp = dupelement (action); + ret = evaluate_element (temp, 0); + } + } + + VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n")); + + return ret; +} + /* quit function */ void quit (void) @@ -621,6 +677,7 @@ double evaluate_element (element_t *root, char mask) case Ans: case Pi: case E: + case While: break; } @@ -666,6 +723,7 @@ double evaluate_element (element_t *root, char mask) } else { return 0; } + case While: return while_do (root->ops[0], root->ops[1]); } return 0; diff --git a/parser.h b/parser.h index 540f1c8..49acc20 100644 --- a/parser.h +++ b/parser.h @@ -19,7 +19,7 @@ typedef enum { Ans, E, Pi, Equal, Diff, Ge, Le, Gt, Lt, And, Or, Not, - Cond + Cond, While } func_t; /* keyword type */