Skip to content

Commit 4738341

Browse files
committed
previous one was a mess, remaking it
1 parent 80e475c commit 4738341

File tree

6 files changed

+21
-68
lines changed

6 files changed

+21
-68
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
brainfuck
2+
.vscode
13
*.out

headers/brainfutils.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

main.c

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
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

157
int 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+
}

makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
COMPILER=cc
22
CONF=-o brainfuck
3-
COMPILE=$(COMPILER) $(CONF)
43

54
compile:
6-
$(COMPILE) main.c
5+
$(COMPILER) $(CONF) main.c
76

87
test: compile
98
./brainfuck src/thanks.bf

test/69.bf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
CELLS:5
21
+++[>++>+++<<-]>.>.

test/add.bf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
<<<<<<< HEAD
2-
CELLS:2/ASCII:n
3-
=======
4-
CELLS:2 ASCII:n
5-
>>>>>>> ee1db58502c951478db2ec7507fd0a17ac2de8d6
61
,>,[-<+>]<.

0 commit comments

Comments
 (0)