aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LICENSE13
-rw-r--r--README8
-rw-r--r--brainfuck.c67
3 files changed, 34 insertions, 54 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..456c488
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
diff --git a/README b/README
index 98c184a..4022180 100644
--- a/README
+++ b/README
@@ -1,14 +1,12 @@
./brainfuck: Brainfuck programming language interpreter
-See <http://en.wikipedia.org/wiki/Brainfuck> for more details
-
Usage: ./brainfuck [options] [file]
Size of each data cell is 4 byte(s)
All data cells are zeros initially
-Options (defaults are in brackets):
- -s num stack size (128)
- -d num data size (1024)
+Options:
+ -s num stack size (default 128)
+ -d num data size (default 1024)
-t trace execution for debugging
-O optimize code
-C translate into C (to stdout)
diff --git a/brainfuck.c b/brainfuck.c
index fb15282..ef056ae 100644
--- a/brainfuck.c
+++ b/brainfuck.c
@@ -1,4 +1,3 @@
-
/*
* gcc -ansi -pedantic -Wall -Wextra -Werror -o brainfuck brainfuck.c
*/
@@ -9,41 +8,27 @@
#include <string.h>
#include <getopt.h>
+#define xstr(a) str(a)
+#define str(a) #a
#define DATATYPE int
-#define DATATYPE_STR "int"
+#define DATATYPE_STR xstr(DATATYPE)
FILE *prog;
-
char *print_as = NULL;
-
char *code = NULL;
-
DATATYPE *data = NULL;
-
unsigned int *stack = NULL;
-
unsigned int data_size = 1024;
-
unsigned int stack_size = 128;
-
char fmt = 'u';
-
char format[9] = "%u";
-
int trace = 0;
-
int compile = 0;
-
int optimize = 0;
-
unsigned int cp = 0;
-
unsigned int dp = 0;
-
unsigned int max_dp = 0;
-
unsigned int sp = 0;
-
char commands[] = "+-<>,.[]cioux";
void
@@ -261,14 +246,14 @@ run_code ()
data[dp] = 0;
++cp;
break;
- case 'I': /* I23 -> add 22 to the current cell */
+ case 'I': /* I23 -> add 23 to the current cell */
++cp;
sscanf (&(code[cp]), "%u", &d);
(data[dp]) += (DATATYPE) d;
while (code[cp] >= '0' && code[cp] <= '9')
++cp; /* skip digits */
break;
- case 'D': /* D23 -> subtract 22 from the current cell */
+ case 'D': /* D23 -> subtract 23 from the current cell */
++cp;
sscanf (&(code[cp]), "%u", &d);
(data[dp]) -= (DATATYPE) d;
@@ -404,9 +389,9 @@ optimize_code ()
else if (d != 0)
{
if (d < 0)
- sprintf (tmp, "L%u", -d); /* move left by d */
+ sprintf (tmp, "L%u", -d); /* move left by d */
else
- sprintf (tmp, "R%u", d); /* move right by d */
+ sprintf (tmp, "R%u", d); /* move right by d */
k = 0;
while (tmp[k])
new_code[j++] = tmp[k++];
@@ -422,14 +407,10 @@ optimize_code ()
*/
substr = new_code;
while (NULL != (substr = strstr (substr, "[-]")))
- {
- memcpy (substr, "Z ", 3); /* [-] set current cell to 0 */
- }
+ memcpy (substr, "Z ", 3); /* [-] set current cell to 0 */
substr = new_code;
while (NULL != (substr = strstr (substr, "[+]")))
- {
- memcpy (substr, "Z ", 3); /* [-] set current cell to 0 */
- }
+ memcpy (substr, "Z ", 3); /* [-] set current cell to 0 */
free (code);
code = new_code;
@@ -463,14 +444,14 @@ bf2c ()
printf ("}\n");
++cp;
break;
- case 'I': /* I23 -> add 22 to the current cell */
+ case 'I': /* I23 -> add 23 to the current cell */
++cp;
sscanf (&(code[cp]), "%u", &d);
printf ("data[dp] += %u;\n", d);
while (code[cp] >= '0' && code[cp] <= '9')
++cp; /* skip digits */
break;
- case 'D': /* D23 -> subtract 22 from the current cell */
+ case 'D': /* D23 -> subtract 23 from the current cell */
++cp;
sscanf (&(code[cp]), "%u", &d);
printf ("data[dp] -= %u;\n", d);
@@ -619,17 +600,15 @@ void
usage (char *self)
{
printf ("%s: Brainfuck programming language interpreter\n", self);
- printf
- ("See <http://en.wikipedia.org/wiki/Brainfuck> for more details\n\n");
printf ("Usage: %s [options] [file]\n\n", self);
printf ("Size of each data cell is %lu byte(s)\n",
(long unsigned int) sizeof (DATATYPE));
printf ("All data cells are zeros initially\n\n");
- printf ("Options (defaults are in brackets):\n");
- printf (" -s num stack size (%u)\n", stack_size);
- printf (" -d num data size (%u)\n", data_size);
+ printf ("Options:\n");
+ printf (" -s num stack size (default %u)\n", stack_size);
+ printf (" -d num data size (default %u)\n", data_size);
printf (" -t trace execution for debugging\n");
printf (" -O optimize code\n");
printf (" -C translate into C (to stdout)\n");
@@ -673,9 +652,7 @@ int
main (int argc, char **argv)
{
char *filename;
-
char *self = argv[0];
-
int opt;
while (-1 != (opt = getopt (argc, argv, "p:OCts:d:h?iucox")))
@@ -724,9 +701,7 @@ main (int argc, char **argv)
}
}
else
- {
- prog = stdin;
- }
+ prog = stdin;
read_code ();
@@ -737,19 +712,13 @@ main (int argc, char **argv)
optimize_code ();
if (compile)
- {
- bf2c ();
- }
+ bf2c ();
else if (print_as != NULL)
{
if (strcmp (print_as, "cow") == 0)
- {
- bf2moo ();
- }
+ bf2moo ();
else
- {
- fprintf (stderr, "Unknown argument to -p: %s\n", print_as);
- }
+ fprintf (stderr, "Unknown argument to -p: %s\n", print_as);
}
else
{