aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--brainfuck.c18
2 files changed, 10 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index d579511..d409a8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
1## Ignore Visual Studio temporary files, build results, and 1## Ignore Visual Studio temporary files, build results, and
2## files generated by popular Visual Studio add-ons. 2## files generated by popular Visual Studio add-ons.
3 3
4*.exe
4# User-specific files 5# User-specific files
5*.suo 6*.suo
6*.user 7*.user
diff --git a/brainfuck.c b/brainfuck.c
index cda6857..8e80d07 100644
--- a/brainfuck.c
+++ b/brainfuck.c
@@ -9,7 +9,6 @@ typedef struct brainfuck{
9 char command[MAX_DATA]; 9 char command[MAX_DATA];
10 char *ptr; 10 char *ptr;
11 int count; 11 int count;
12 int pos;
13}bf; 12}bf;
14 13
15bf one; 14bf one;
@@ -18,11 +17,10 @@ void init()
18{ 17{
19 char c; 18 char c;
20 memset(&one, 0, sizeof(one)); 19 memset(&one, 0, sizeof(one));
21 one.pos = 0;
22 one.count = 0; 20 one.count = 0;
23 one.ptr = one.data; 21 one.ptr = one.data;
24 22
25 while (c = getchar()) 23 while ((c = getchar()) != EOF)
26 { 24 {
27 if (c == '\n') 25 if (c == '\n')
28 break; 26 break;
@@ -31,6 +29,7 @@ void init()
31 one.command[++one.count] = c; 29 one.command[++one.count] = c;
32 } 30 }
33 } 31 }
32 one.command[0] = one.count;
34 one.command[one.count] = '\0'; 33 one.command[one.count] = '\0';
35} 34}
36 35
@@ -39,7 +38,8 @@ void run()
39 char c; 38 char c;
40 int i; 39 int i;
41 40
42 while (c = one.command[++one.pos]) 41 one.count = 0;
42 while (c = one.command[++one.count])
43 { 43 {
44 switch (c) 44 switch (c)
45 { 45 {
@@ -66,11 +66,11 @@ void run()
66 break; 66 break;
67 else 67 else
68 { 68 {
69 for (i = one.pos + 1; i <= one.count; i++) 69 for (i = one.count + 1; i <= one.command[0]; i++)
70 { 70 {
71 if (one.command[i] == ']') 71 if (one.command[i] == ']')
72 { 72 {
73 one.pos = i; 73 one.count = i;
74 break; 74 break;
75 } 75 }
76 } 76 }
@@ -81,11 +81,11 @@ void run()
81 break; 81 break;
82 else 82 else
83 { 83 {
84 for (i = one.pos - 1; i >= 1; i--) 84 for (i = one.count - 1; i >= 1; i--)
85 { 85 {
86 if (one.command[i] == '[') 86 if (one.command[i] == '[')
87 { 87 {
88 one.pos = i; 88 one.count = i;
89 break; 89 break;
90 } 90 }
91 } 91 }
Powered by cgit v1.2.3 (git 2.41.0)