aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'brainfuck.c')
-rw-r--r--brainfuck.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/brainfuck.c b/brainfuck.c
new file mode 100644
index 0000000..cda6857
--- /dev/null
+++ b/brainfuck.c
@@ -0,0 +1,106 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#define MAX_DATA 30000
6
7typedef struct brainfuck{
8 char data[MAX_DATA];
9 char command[MAX_DATA];
10 char *ptr;
11 int count;
12 int pos;
13}bf;
14
15bf one;
16
17void init()
18{
19 char c;
20 memset(&one, 0, sizeof(one));
21 one.pos = 0;
22 one.count = 0;
23 one.ptr = one.data;
24
25 while (c = getchar())
26 {
27 if (c == '\n')
28 break;
29 else
30 {
31 one.command[++one.count] = c;
32 }
33 }
34 one.command[one.count] = '\0';
35}
36
37void run()
38{
39 char c;
40 int i;
41
42 while (c = one.command[++one.pos])
43 {
44 switch (c)
45 {
46 case '+':
47 (*one.ptr)++;
48 break;
49 case '-':
50 (*one.ptr)--;
51 break;
52 case '>':
53 one.ptr++;
54 break;
55 case '<':
56 one.ptr--;
57 break;
58 case ',':
59 *one.ptr = getchar();
60 break;
61 case '.':
62 putchar(*one.ptr);
63 break;
64 case '[':
65 if (*one.ptr != 0)
66 break;
67 else
68 {
69 for (i = one.pos + 1; i <= one.count; i++)
70 {
71 if (one.command[i] == ']')
72 {
73 one.pos = i;
74 break;
75 }
76 }
77 }
78 break;
79 case ']':
80 if (*one.ptr == 0)
81 break;
82 else
83 {
84 for (i = one.pos - 1; i >= 1; i--)
85 {
86 if (one.command[i] == '[')
87 {
88 one.pos = i;
89 break;
90 }
91 }
92 }
93 break;
94 }
95 }
96}
97
98int main(int argc, char **argv)
99{
100 if (argc > 1) {
101 freopen(argv[1], "r", stdin);
102 }
103 init();
104 run();
105 return 0;
106} \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)