diff options
-rw-r--r-- | README.md | 20 | ||||
-rw-r--r-- | add.bf | 1 | ||||
-rw-r--r-- | brainfuck.c | 12 |
3 files changed, 5 insertions, 28 deletions
@@ -4,23 +4,5 @@ brainfuck | |||
4 | ##Usage: | 4 | ##Usage: |
5 | ```bash | 5 | ```bash |
6 | gcc brainfuck.c -o brainfuck | 6 | gcc brainfuck.c -o brainfuck |
7 | ./brainfuck HelloWorld.bf | 7 | ./brainfuck |
8 | ``` | 8 | ``` |
9 | |||
10 | ##What's brainfuck | ||
11 | Brainfuck is represented by an array with 30,000 cells initialized to zero | ||
12 | and a data pointer pointing at the current cell. | ||
13 | |||
14 | There are eight commands: | ||
15 | ``` | ||
16 | + : Increments the value at the current cell by one. | ||
17 | - : Decrements the value at the current cell by one. | ||
18 | > : Moves the data pointer to the next cell (cell on the right). | ||
19 | < : Moves the data pointer to the previous cell (cell on the left). | ||
20 | . : Prints the ASCII value at the current cell (i.e. 65 = 'A'). | ||
21 | , : Reads a single input character into the current cell. | ||
22 | [ : If the value at the current cell is zero, skips to the corresponding ] . | ||
23 | Otherwise, move to the next instruction. | ||
24 | ] : If the value at the current cell is zero, move to the next instruction. | ||
25 | Otherwise, move backwards in the instructions to the corresponding [ . | ||
26 | ``` \ No newline at end of file | ||
@@ -0,0 +1 @@ | |||
,>++++++[<-------->-],,[<+>-],<.>. \ No newline at end of file | |||
diff --git a/brainfuck.c b/brainfuck.c index 8e80d07..ceb52c4 100644 --- a/brainfuck.c +++ b/brainfuck.c | |||
@@ -1,5 +1,4 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | ||
3 | #include <string.h> | 2 | #include <string.h> |
4 | 3 | ||
5 | #define MAX_DATA 30000 | 4 | #define MAX_DATA 30000 |
@@ -19,15 +18,12 @@ void init() | |||
19 | memset(&one, 0, sizeof(one)); | 18 | memset(&one, 0, sizeof(one)); |
20 | one.count = 0; | 19 | one.count = 0; |
21 | one.ptr = one.data; | 20 | one.ptr = one.data; |
22 | 21 | ||
23 | while ((c = getchar()) != EOF) | 22 | while ((c = getchar()) != EOF) |
24 | { | 23 | { |
25 | if (c == '\n') | 24 | if (c == '\n') |
26 | break; | 25 | break; |
27 | else | 26 | one.command[++one.count] = c; |
28 | { | ||
29 | one.command[++one.count] = c; | ||
30 | } | ||
31 | } | 27 | } |
32 | one.command[0] = one.count; | 28 | one.command[0] = one.count; |
33 | one.command[one.count] = '\0'; | 29 | one.command[one.count] = '\0'; |
@@ -97,9 +93,7 @@ void run() | |||
97 | 93 | ||
98 | int main(int argc, char **argv) | 94 | int main(int argc, char **argv) |
99 | { | 95 | { |
100 | if (argc > 1) { | 96 | printf("Input the brainfuck expression below\n"); |
101 | freopen(argv[1], "r", stdin); | ||
102 | } | ||
103 | init(); | 97 | init(); |
104 | run(); | 98 | run(); |
105 | return 0; | 99 | return 0; |