From 715580ff81a00316fb8033d4ca2ceae4a2cafd94 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 27 Jan 2023 00:33:51 +0100 Subject: [PATCH] print function --- calc.c | 6 ++++-- parser.c | 14 +++++++++++--- parser.h | 4 +++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/calc.c b/calc.c index eee39fe..8081c80 100644 --- a/calc.c +++ b/calc.c @@ -133,7 +133,6 @@ int main (int argc, char *argv[]) } /* format */ - char format[9] = "=> %..g\n"; format[5] = '0' + precision; /* completion list*/ @@ -336,7 +335,10 @@ int main (int argc, char *argv[]) // test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 = // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1' +// Gauss sequence +// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) < 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})}' | calc.exe | grep -q '=> 5050' + // Fibonacci sequence -// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), sto (2, rcl (3))})}' | calc.exe | grep '=> 144' +// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})}' | calc.exe | grep '=> 144' /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index eef0846..9b6d5bb 100644 --- a/parser.c +++ b/parser.c @@ -15,6 +15,8 @@ double answer = 0; #define STORAGE_SIZE 10 double storage[STORAGE_SIZE] = {0}; +char format[9] = "=> %..g\n"; + /* compare codes */ int codecmp (char *ref, char *str) @@ -116,7 +118,7 @@ keyword_t operators[NB_OPERATORS] = { { "|", Or, 2, 1, -2} }; -#define NB_FUNCTIONS 23 +#define NB_FUNCTIONS 24 keyword_t functions[NB_FUNCTIONS] = { { "sqrt", Sqr, 1, 4, 5}, { "pow", Pow, 2, 3, 5}, @@ -140,7 +142,8 @@ keyword_t functions[NB_FUNCTIONS] = { { "help", Help, 0, 4, 9}, { "!", Not, 1, 1, 6}, { "cond", Cond, 3, 4, 5}, - { "while", While, 2, 5, 5} + { "while", While, 2, 5, 5}, + { "print", Print, 1, 5, 5} }; #define NB_CONSTANTS 3 @@ -506,6 +509,7 @@ void print_element (element_t *root, int level) case Cond: func = "Condition"; break; case While: func = "While"; break; case Prog: func = "Program"; break; + case Print: func = "Print"; break; } fprintf (stdout, "Function: %s\n", func); @@ -641,7 +645,7 @@ void help (void) fprintf (stdout, "storage functions:"); fprintf (stdout, " sto rcl inc dec\n"); fprintf (stdout, "conditional functions:"); - fprintf (stdout, " cond while\n"); + fprintf (stdout, " cond while print {}\n"); fprintf (stdout, "miscellaneous functions:"); fprintf (stdout, " quit help\n"); fprintf (stdout, "supported constants:"); @@ -748,6 +752,9 @@ double evaluate_element (element_t *root, char mask) return 0; } break; + case Print: + op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer; + break; } switch (root->func) { @@ -800,6 +807,7 @@ double evaluate_element (element_t *root, char mask) } case While: return while_do (root->ops[0], root->ops[1]); case Prog: return program_do (root->ops, root->nbops); + case Print: printf (format, op0); return op0; } return 0; diff --git a/parser.h b/parser.h index b48f6d0..9e4c3f6 100644 --- a/parser.h +++ b/parser.h @@ -5,6 +5,8 @@ extern double answer; +extern char format[9]; + /* function type */ typedef enum { @@ -20,7 +22,7 @@ typedef enum { Ans, E, Pi, Equal, Diff, Ge, Le, Gt, Lt, And, Or, Not, - Cond, While, Prog + Cond, While, Prog, Print } func_t; /* keyword type */ -- 2.30.2