print function
authorLaurent Mazet <mazet@softndesign.org>
Thu, 26 Jan 2023 23:33:51 +0000 (00:33 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 26 Jan 2023 23:43:30 +0000 (00:43 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index eee39fe0b54cc34dcd1fb13eab0f0d85d94ef08a..8081c804963290490c166fef5eb5cf1d1ef2425c 100644 (file)
--- 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: */
index eef08464a87fa3855fa3d2abda00c9f52b4103f2..9b6d5bb2f3f101a2d49c664c404f85fa789b70f4 100644 (file)
--- 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;
index b48f6d004491d09494ecd158e2d7ca2db94a4333..9e4c3f608f18196905f8eca679c70fae004ce369 100644 (file)
--- 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 */