add init memory
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 19 Dec 2023 15:54:32 +0000 (16:54 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 19 Dec 2023 15:54:32 +0000 (16:54 +0100)
bf.c
debug.c

diff --git a/bf.c b/bf.c
index e687f94b1724f0a2721cb5c277df04a64241a893..a3f78b58bb6aa27322f92658e37e21220ca30f36 100644 (file)
--- a/bf.c
+++ b/bf.c
@@ -14,7 +14,7 @@
 /* macros */
 
 #define BUFSIZE 256
-#define MEMSIZE 10
+#define MEMSIZE 8
 
 /* type definition */
 
@@ -29,10 +29,10 @@ char mem[MEMSIZE] = {0};
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname);
+    fprintf (fd, "usage: %s [-i file] [-h] [-m memory] [-o file] [-v]\n", progname);
     fprintf (fd, " -i : input file\n");
     fprintf (fd, " -h : help message\n");
-    fprintf (fd, " -m : indent mode\n");
+    fprintf (fd, " -m : memory [0..0]\n");
     fprintf (fd, " -o : output file\n");
     fprintf (fd, " -v : verbose level (%d)\n", verbose);
 
@@ -46,6 +46,7 @@ int main (int argc, char *argv[])
     char *input = NULL;
     char *output = NULL;
     char *buffer = NULL;
+    int i;
     int n = 0;
     int size = 0;
     FILE *fid = NULL;
@@ -60,11 +61,23 @@ int main (int argc, char *argv[])
     }
 
     int c;
-    while ((c = getopt(argc, argv, "i:o:hv:")) != EOF) {
+    while ((c = getopt(argc, argv, "i:m:o:hv:")) != EOF) {
         switch (c) {
         case 'i':
             input = optarg;
             break;
+        case 'm':
+            for (i = 0; i < MEMSIZE; i++) {
+                if (optarg) {
+                    mem[i] = strtol (optarg, &optarg, 10);
+                    VERBOSE (DEBUG, fprintf (stderr, "%s: mem[%d] = %d\n", progname, i, mem[i]));
+                }
+            }
+            if (*optarg != 0) {
+                VERBOSE (WARNING, fprintf (stderr, "%s: too many memory values\n", progname));
+                return 1;
+            }
+            break;
         case 'o':
             output = optarg;
             break;
@@ -125,6 +138,7 @@ int main (int argc, char *argv[])
 
     /* main process */
     int rc = process (buffer, size, fid);
+    VERBOSE (INFO, fprintf (stdout, "\nmemory:"); int _i; for (_i = 0; _i < MEMSIZE; _i++) fprintf (stdout," %d", mem[_i]); fprintf (stdout,"\n"));
 
     /* close output file */
     fclose (fid);
@@ -215,8 +229,11 @@ int process (char *buffer, int nb, FILE *out) {
 // test: bf.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 // test: bf.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: bf.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-// test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe
-// test: echo '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++++++++++++++.>+++++++++++++++++.<<++.>+++++++++++++.>--.<<.>+++.+.--.>----.++++++.+.<++.>----.++.<<.>>+.-------.<<.>>++.<.>+++++.<<.>-.+.<.>---.>---.<-.++++++++.>----.<---.>+++++++.<---.++++++++.' | bf.exe
-// test: echo ',3>,2>>++++++++++++++++[-<+++<---<--->>>]<<[<[>>+>+<<<-]>>>[<<<+>>>-]<<-]>.' | bf.exe
+// test: echo '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.' | bf.exe -v1 | grep -q "Hello World!"
+// test: echo '++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++++++++++++++.>+++++++++++++++++.<<++.>+++++++++++++.>--.<<.>+++.+.--.>----.++++++.+.<++.>----.++.<<.>>+.-------.<<.>>++.<.>+++++.<<.>-.+.<.>---.>---.<-.++++++++.>----.<---.>+++++++.<---.++++++++.' | bf.exe -v1 | grep -q "Tu as decouvert un peu de brainfuck"
+// test: echo ',4>++++++[<-------->-],3[<+>-]<.' | bf.exe -v1 | grep -q 7
+
+// test: echo ',3>,2>>++++++++++++++++[-<+++<---<--->>>]<<[<[>>+>+<<<-]>>>[<<<+>>>-]<<-]>.' | bf.exe -v1 | grep -q 6
+// test: echo ',3>,2[->>+<<]+<[->>>[>>>>>+<<<<<->+<<]<[>]>>>>>[-<<->>>>>>>]<[>]<<<<<<]>[>>>>->]<<<<<<[-]>[-]>>[-]>.' | bf.exe -v1 | grep -q 3
 
 /* vim: set ts=4 sw=4 et: */
diff --git a/debug.c b/debug.c
index 151e615c45f6731cee7f2371f7877a59a03c6c8b..fbf493886bb727b86931c54b4568fece862cf477 100644 (file)
--- a/debug.c
+++ b/debug.c
@@ -1,5 +1,5 @@
 #include "debug.h"
 
-int verbose = 1;
+int verbose = INFO;
 
 /* vim: set ts=4 sw=4 et: */