Brainfuck interpreter written in C - Printable Version +- Forums - Open Redstone Engineers (https://forum.openredstone.org) +-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html) +--- Forum: Programming (https://forum.openredstone.org/forum-8.html) +--- Thread: Brainfuck interpreter written in C (/thread-69.html) |
Brainfuck interpreter written in C - mad1231999 - 04-22-2013 This is just a Brainfuck interpreter that I made in the programming language C. It's only 36 lines, and still very readable! Source code: Code: #include <stdio.h> How to compile: On windows, install Mingw, and run this command: Code: mingw32-gcc Brainfuck.c -o BF.exe On Mac or Linux, run this: Code: gcc Brainfuck.c -o BF Alternatively, you can just install Code::Blocks and compile with that too(should work on all platforms). RE: Brainfuck interpreter written in C - JeremyG - 04-22-2013 In my opinion, the number of 'lines' is a bad measurement. You could put everything on 1 line in most programming languages. RE: Brainfuck interpreter written in C - mad1231999 - 04-22-2013 (04-22-2013, 04:13 PM)JeremyG Wrote: In my opinion, the number of 'lines' is a bad measurement. You could put everything on 1 line in most programming languages. Yeah, I know. I just needed to beat Invalid ![]() RE: Brainfuck interpreter written in C - mort96 - 04-23-2013 I just made a BF interpreter one line. Beat that. Code: #include <stdio.h>; const int MAX_DEPTH = 100, MEM_SIZE = 30000; int main(int argc, char **argv){ if(argc <= 1) { printf("You have to specify a filename. Usage:\n<executable> <BF filename>\n"); return 0; }; char mem[MEM_SIZE]; int lindex[MAX_DEPTH]; int depth = 0, len = 0, ptr = 0; FILE *f = fopen(argv[1], "r"); if(f == 0) { printf("Invalid filename: %s\n", argv[1]); return 0; }; int curr; while((curr = fgetc(f)) != EOF){ switch((char) curr){ case '+': ++(mem[ptr]); break; case '-': --(mem[ptr]); break; case '<': if(ptr > 0) --ptr; break; case '>': if(ptr < MEM_SIZE) ++ptr; break; case '.': putchar(mem[ptr]); break; case ',': mem[ptr] = getchar();getchar(); break; case '[': if(mem[ptr] == 0) { int d = 0, i = ftell(f); char c = '\0'; while(!feof(f)) { ++i; c = fgetc(f); if(c == '[') { ++d; } else if(c == ']') { if(d == 0) { fseek(f, i, SEEK_SET); break; } else { --d; } } } } else { lindex[depth++] = ftell(f); break; case ']': if(depth > 0 && mem[ptr] == 0) {} else { fseek(f, lindex[depth - 1], SEEK_SET); } break; default: break; } if(feof(f)) break; } fclose(f); printf("\n");} (not tested it, but let's say it works for the sake of argument - simply modified mads' interpreter in notepad during a boring class at school ![]() RE: Brainfuck interpreter written in C - mad1231999 - 04-23-2013 (04-23-2013, 10:33 AM)mort96 Wrote: I just made a BF interpreter one line. Beat that. You can't have preprocessor stuff and code on the same line, neither can you have 2 preprocessor statements on one line ![]() RE: Brainfuck interpreter written in C - redstonewarrior - 04-23-2013 Use... use inline assembly for IO then... RE: Brainfuck interpreter written in C - JeremyG - 05-28-2013 I know I'm late, but here have this Code: s[99],*r=s,*d,c;main(a,b){char*v=1[d=b];for(;c=*v++%93;)for(b=c&2,b=c%7?a&&(c&17 RE: Brainfuck interpreter written in C - Xeomorpher - 05-30-2013 I like my cats warm and fuzzly. RE: Brainfuck interpreter written in C - David - 05-30-2013 (05-30-2013, 06:07 PM)Xeomorpher Wrote: I like my cats warm and fuzzly. I like my cats deep fried RE: Brainfuck interpreter written in C - VirtualPineapple - 05-31-2013 (05-30-2013, 08:31 PM)David Wrote:(05-30-2013, 06:07 PM)Xeomorpher Wrote: I like my cats warm and fuzzly. Faith in humanity is lost once again. RE: Brainfuck interpreter written in C - Xeomorpher - 05-31-2013 My cat would not be happy to read that. RE: Brainfuck interpreter written in C - Malcolmforde - 06-01-2013 How to use it? RE: Brainfuck interpreter written in C - VirtualPineapple - 06-01-2013 How to compile: On windows, install Mingw, and run this command: Code: mingw32-gcc Brainfuck.c -o BF.exe and run the executable from the commandline. Make a new file, input your BF code, save it. Then in the command line do BF (filename) RE: Brainfuck interpreter written in C - Malcolmforde - 06-02-2013 (06-01-2013, 11:57 PM)VirtualPineapple Wrote: How to compile: But... what type of file? :o RE: Brainfuck interpreter written in C - VirtualPineapple - 06-02-2013 Any file type... do like Testing.bf RE: Brainfuck interpreter written in C - AFtExploision - 01-17-2014 tid=69 RE: Brainfuck interpreter written in C - VirtualPineapple - 01-18-2014 fucking bumpers T_T, leave dead threads dead. Don't practice necrophelia |