From: Laurent Mazet Date: Mon, 23 Jan 2023 14:38:23 +0000 (+0100) Subject: condition feature X-Git-Tag: v0.8~27 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=94b4e517849129fca159355365a7fb01db21d2da;p=calc.git condition feature --- diff --git a/calc.c b/calc.c index 67c4b2e..4ced168 100644 --- a/calc.c +++ b/calc.c @@ -283,5 +283,9 @@ int main (int argc, char *argv[]) // test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error // test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe 2>&1 | grep -c error | xargs test 3 = +// test: echo -e 'sto (2, pi)\ncond (rcl (2) > 2, log (64), exp (75))' | calc.exe | grep -q '=> 4\.15888' +// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04' +// test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond +// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 = /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index 53d6e6c..8007f86 100644 --- a/parser.c +++ b/parser.c @@ -87,7 +87,7 @@ keyword_t operators[NB_OPERATORS] = { { "|", Or, 2, 1, -2} }; -#define NB_FUNCTIONS 13 +#define NB_FUNCTIONS 14 keyword_t functions[NB_FUNCTIONS] = { { "sqrt", Sqr, 1, 4, 5}, { "pow", Pow, 2, 3, 5}, @@ -101,7 +101,8 @@ keyword_t functions[NB_FUNCTIONS] = { { "disp", Disp, 0, 4, 9}, { "quit", Quit, 0, 4, 9}, { "help", Help, 0, 4, 9}, - { "!", Not, 1, 1, 6} + { "!", Not, 1, 1, 6}, + { "cond", Cond, 3, 4, 5} }; #define NB_CONSTANTS 3 @@ -430,6 +431,7 @@ void print_element (element_t *root, int level) case And: func = "And"; break; case Or: func = "Or"; break; case Not: func = "Not"; break; + case Cond: func = "Condition"; break; } fprintf (stdout, "Function: %s\n", func); @@ -491,19 +493,21 @@ void quit (void) void help (void) { fprintf (stdout, "calc is a simple calculator\n\n"); - fprintf (stdout, "supported operators:\n"); - fprintf (stdout, " + - * / %% ^\n\n"); - fprintf (stdout, "camparison operators:\n"); - fprintf (stdout, " == != >= <= > <\n\n"); - fprintf (stdout, "logical operators:\n"); - fprintf (stdout, " & | !\n\n"); - fprintf (stdout, "supported functions:\n"); - fprintf (stdout, " pow sqrt cos sin atan log exp\n\n"); - fprintf (stdout, "storage functions:\n"); - fprintf (stdout, " sto rcl\n\n"); - fprintf (stdout, "miscellaneous functions:\n"); - fprintf (stdout, " quit help\n\n"); - fprintf (stdout, "supported constants:\n"); + fprintf (stdout, "supported operators:"); + fprintf (stdout, " + - * / %% ^\n"); + fprintf (stdout, "camparison operators:"); + fprintf (stdout, " == != >= <= > <\n"); + fprintf (stdout, "logical operators:"); + fprintf (stdout, " & | !\n"); + fprintf (stdout, "supported functions:"); + fprintf (stdout, " pow sqrt cos sin atan log exp\n"); + fprintf (stdout, "storage functions:"); + fprintf (stdout, " sto rcl\n"); + fprintf (stdout, "conditional functions:"); + fprintf (stdout, " cond\n"); + fprintf (stdout, "miscellaneous functions:"); + fprintf (stdout, " quit help\n"); + fprintf (stdout, "supported constants:"); fprintf (stdout, " e pi\n"); } @@ -577,6 +581,7 @@ double evaluate_element (element_t *root, char mask) case Exp: case Recall: case Not: + case Cond: if (root->ops[0]) { op0 = evaluate_element (root->ops[0], 0); } else { @@ -625,6 +630,14 @@ double evaluate_element (element_t *root, char mask) case And: return (op0 != 0) && (op1 != 0); case Or: return (op0 != 0) || (op1 != 0); case Not: return (op0 == 0); + case Cond: + if ((op0) && (root->ops[1])) { + return evaluate_element (root->ops[1], 0); + } else if ((!op0) && (root->ops[2])) { + return evaluate_element (root->ops[2], 0); + } else { + return 0; + } } return 0; diff --git a/parser.h b/parser.h index 80fbd98..409b4d4 100644 --- a/parser.h +++ b/parser.h @@ -18,7 +18,8 @@ typedef enum { Quit, Help, Ans, E, Pi, Equal, Diff, Ge, Le, Gt, Lt, - And, Or, Not + And, Or, Not, + Cond } func_t; /* keyword type */