aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2011-09-14 01:19:32 +0400
committerIgor Pashev <pashev.igor@gmail.com>2011-09-14 01:19:32 +0400
commit6047b3835a1b88932ab2140570e75baf7ffbfa3e (patch)
treec89b3f1c5fdc3c779872a57ce18c7896c89b5059
parenta9b032de4614f6725cd8dd8bc1e0fad9a720e999 (diff)
downloadbrainfuck-6047b3835a1b88932ab2140570e75baf7ffbfa3e.tar.gz
Added translator to Cow :-)
-rw-r--r--README1
-rw-r--r--brainfuck.c75
2 files changed, 71 insertions, 5 deletions
diff --git a/README b/README
index 8e5fd76..98c184a 100644
--- a/README
+++ b/README
@@ -12,6 +12,7 @@ Options (defaults are in brackets):
-t trace execution for debugging
-O optimize code
-C translate into C (to stdout)
+ -p cow translate into Cow (to stdout)
Formats for operators '.' and ',' (output and input):
-c, -i, -u, -o, -x char, signed int, unsigned int, octal, hexadecimal
diff --git a/brainfuck.c b/brainfuck.c
index 9036965..dc36340 100644
--- a/brainfuck.c
+++ b/brainfuck.c
@@ -14,6 +14,8 @@
FILE *prog;
+char *print_as = NULL;
+
char *code = NULL;
DATATYPE *data = NULL;
@@ -415,16 +417,18 @@ optimize_code()
}
}
- /* FIXME valid while we use finite integers */
+ /*
+ * FIXME valid while we use finite integers
+ */
substr = new_code;
while (NULL != (substr = strstr(substr, "[-]")))
{
- strncpy(substr, "Z ", 3); /* [-] set current cell to 0 */
+ strncpy(substr, "Z ", 3); /* [-] set current cell to 0 */
}
substr = new_code;
while (NULL != (substr = strstr(substr, "[+]")))
{
- strncpy(substr, "Z ", 3); /* [-] set current cell to 0 */
+ strncpy(substr, "Z ", 3); /* [-] set current cell to 0 */
}
free(code);
@@ -552,6 +556,56 @@ bf2c()
}
void
+bf2moo()
+{
+ char cmd;
+
+ while ('\0' != (cmd = code[cp]))
+ {
+ switch (cmd)
+ {
+ case '+':
+ printf("MoO");
+ break;
+ case '-':
+ printf("MOo");
+ break;
+ case '>':
+ printf("moO");
+ break;
+ case '<':
+ printf("mOo");
+ break;
+ case '[':
+ if (strncmp(code + cp, "[-]", 3) == 0)
+ {
+ printf("OOO");
+ cp += 2;
+ }
+ else
+ {
+ printf("moo");
+ }
+ break;
+ case ']':
+ printf("MOO");
+ break;
+ case '.':
+ printf("OOM");
+ break;
+ case ',':
+ printf("oom");
+ break;
+ default:
+ fprintf(stderr, "unknown character %c at %u\n", cmd, cp);
+ break;
+ }
+ ++cp;
+ }
+ printf("\n");
+}
+
+void
free_all()
{
if (code != NULL)
@@ -570,7 +624,8 @@ usage(char *self)
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("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);
@@ -578,6 +633,7 @@ usage(char *self)
printf(" -t trace execution for debugging\n");
printf(" -O optimize code\n");
printf(" -C translate into C (to stdout)\n");
+ printf(" -p cow translate into Cow (to stdout)\n");
printf("\n");
printf("Formats for operators '.' and ',' (output and input):\n");
printf
@@ -622,10 +678,13 @@ main(int argc, char **argv)
int opt;
- while (-1 != (opt = getopt(argc, argv, "OCts:d:h?iucox")))
+ while (-1 != (opt = getopt(argc, argv, "p:OCts:d:h?iucox")))
{
switch (opt)
{
+ case 'p':
+ print_as = optarg;
+ break;
case 's':
sscanf(optarg, "%u", &stack_size);
break;
@@ -665,7 +724,9 @@ main(int argc, char **argv)
}
}
else
+ {
prog = stdin;
+ }
read_code();
@@ -679,6 +740,10 @@ main(int argc, char **argv)
{
bf2c();
}
+ else if (strcmp(print_as, "cow") == 0)
+ {
+ bf2moo();
+ }
else
{
init_data();