/*- * Copyright (c) 2004 Matthias Schmidt * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Simple Brainfuck Interpreter -- written @ 21c3 Berlin 2004 */ #include #include #include #include #include #include int main(int argc, char **argv) { int memory[32768], space[32768]; int fd, len, buf, spointer, kl = 0, mpointer; if (argc <= 1) { printf("Usage: %s \n", argv[0]); exit(1); } /* Open brainfuck file */ if ((fd = open(argv[1], O_RDONLY)) < 0) exit(1); /* Read the file byte by byte */ len = spointer = 1; while (len > 0) { len = read(fd, &buf, 1); space[spointer] = buf; spointer++; } close(fd); for (mpointer = 0; mpointer < 32768; mpointer++) memory[mpointer] = 0; len = spointer; spointer = mpointer = 0; for (spointer = 0; spointer < len; spointer++) { switch(space[spointer]) { /* Increment pointer value */ case '+': memory[mpointer]++; break; /* Decrement pointer value */ case '-': memory[mpointer]--; break; /* Increment pointer */ case '>': mpointer++; break; /* Decrement pointer */ case '<': mpointer--; break; /* Print current pointer value */ case '.': putchar(memory[mpointer]); break; /* Read value and store in current pointer */ case ',': memory[mpointer] = getchar(); break; /* Start loop */ case '[': if (memory[mpointer] == 0) { /* Find matching ] */ spointer++; /* If kl == 0 and space[pointer] == ']' we found * the matching brace */ while (kl > 0 || space[spointer] != ']') { if (space[spointer] == '[') kl++; if (space[spointer] == ']') kl--; /* Go in right direction */ spointer++; } } break; /* End loop */ case ']': if (memory[mpointer] != 0) { /* Find matching [ */ spointer--; while (kl > 0 || space[spointer] != '[') { if (space[spointer] == '[') kl--; if (space[spointer] == ']') kl++; /* Go left */ spointer--; } spointer--; } break; } } putchar('\n'); return (0); }