From 2d0cd54c457714ca3eb97cfad3cc892612248353 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 27 Jan 2023 16:48:19 +0100 Subject: [PATCH] add hidden option --- calc.c | 37 ++++++++++++++++++++++--------------- parser.c | 11 ++++++++++- parser.h | 1 + 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/calc.c b/calc.c index 873e310..1a8dd67 100644 --- a/calc.c +++ b/calc.c @@ -176,8 +176,6 @@ int main (int argc, char *argv[]) } else if (strcmp (buffer, ".") == 0) { break; } - line[0] = buffer; - nb = 1; /* add line into history */ add_history (buffer); @@ -190,21 +188,27 @@ int main (int argc, char *argv[]) } } } else { + printf ("%s", iprompt); if (read (STDIN_FILENO, buffer, BUFFER_SIZE) == 0) { break; } - nb = 0; - char *pt = line[nb++] = buffer; - while (*pt != '\0') { - if (*pt == '\n') { - *pt = '\0'; - line[nb++] = pt + 1; - } - pt++; - } VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); } + /* pre-process buffer */ + nb = 0; + char *pt = line[nb++] = buffer; + while (*pt != '\0') { + switch (*pt) { + case '\n': + *pt = '\0'; + // fallthrough + case ';': + line[nb++] = pt + 1; + } + pt++; + } + /* look for end of line */ for (i = 0; i < nb; i++) { if (*line[i] == '\0') { @@ -217,7 +221,9 @@ int main (int argc, char *argv[]) } else if (element != NULL) { VERBOSE (INFO, print_element (element, 0)); answer = evaluate_element (element, 0); - print (answer); + if (!element->hidden) { + print (answer); + } delelement (element); ret = 0; } @@ -360,14 +366,15 @@ 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 'print (1)' | calc.exe -v 3 | grep -q Print // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1' +// test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2 // 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' +// 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)), print (sto (2, rcl (3)))})}' | calc.exe | grep -q '=> 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 -q '=> 144' // Gold number -// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})}' | calc.exe | grep -q '=> 1.61803' +// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})};' | calc.exe | grep -q '=> 1.61803' /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index 4da6b6b..5333a9c 100644 --- a/parser.c +++ b/parser.c @@ -195,6 +195,15 @@ element_t *parser (char *str, char **next, int prio) element_t *new = NULL; VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str)); + /* end without printing */ + + if (*str == ';') { + if (root) { + root->hidden = 1; + } + break; + } + /* skip spaces and tabs */ if ((*str == ' ') || (*str == '\t')) { @@ -670,7 +679,7 @@ void help (void) fprintf (stdout, "storage functions:"); fprintf (stdout, " sto rcl inc dec\n"); fprintf (stdout, "conditional functions:"); - fprintf (stdout, " cond while print {}\n"); + fprintf (stdout, " cond while print {} ;\n"); fprintf (stdout, "miscellaneous functions:"); fprintf (stdout, " quit help\n"); fprintf (stdout, "supported constants:"); diff --git a/parser.h b/parser.h index 522adba..eed45f6 100644 --- a/parser.h +++ b/parser.h @@ -41,6 +41,7 @@ typedef struct _element_t { struct _element_t **ops; double value; int prio; + int hidden; } element_t; #define ERROR_OP ((element_t *)(-1)) -- 2.30.2