11#include <stdio.h>
2+ #include <stdlib.h>
23#include <string.h>
34
4- /*
5- * How does brainfuck work?
6- * + -> changes the value of the cell by 1
7- * - -> changes the value of the cell by -1
8- * > -> changes the cell by 1
9- * < -> changes the cell by -1
10- * . -> prints the value of the cell
11- * , -> accepts input and adds it to the cell
12- * [] -> loops if the value of the cell it ends on isn't 0
13- */
5+ #define LENGTH 9999
146
157int main (int argc , char * argv []){
16- if (argc != 2 ){
17- printf ("Usage: %s [brainfuck file]\n" , argv [0 ]);
18- return 1 ;
19- }
20- int pos = 0 , progress = 0 ;
21- char script [strlen (argv [2 ])]; strcpy (script , argv [2 ]), item ;
22- char cells [100 ], error [200 ] = "Don't know what you did, but hope you are happy.\n" ;
23- printf ("Script: %s\n\n" , script );
24- do {
25-
26- // When it ends reading the script, it kills itself
27- if (progress > strlen (script )){
28- printf ("\n\n\t***\tEND\t***\n\n" );
29- return 0 ;
30- }
8+
9+ // Check if all the needed arguments are provided
10+ if (argc != 2 ){
11+ fputs ("usage: ./brainfuck [path/to/file.bf]\n" , stderr );
12+ return 1 ;
13+ }
3114
32- // A fancier error handler
33- if (pos < 0 ){
34- strcpy (error , "Out of cells(left)" );
35- break ;
36- }
37- if (pos > strlen (cells )){
38- strcpy (error , "Out of cells(right)" );
39- break ;
40- }
15+ // Get the source code from the file provided
16+ char source [LENGTH ];
17+ strcpy (source , "" );
18+ FILE * file ;
19+ file = fopen (argv [1 ], "r" );
20+ fgets (source , LENGTH , file );
21+ fclose (file );
4122
42- // Read the character and perform an action based on it
43- item = script [progress ]
44- #include "../headers/brainfutils.h"
45- progress ++ ;
46- }while (1 );
47- printf ("Error: %s" , error );
48- return 0 ;
49- }
23+
24+ printf ("Source code: %s\n" , source );
25+ }
0 commit comments