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