-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFIDE.cpp
227 lines (212 loc) · 4.87 KB
/
BFIDE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <string>
#include <locale>
#include "stdio.h"
using namespace std;
void duffCopy(char* to, int size, char* from)
{
int n = (size + 7) / 8;
switch (size % 8)
{
case 0: do {
*to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}
void testDuffCopy()
{
int size = 100;
char* from = new char[size];
for (int i = 0; i < size; i++)
{
int c = 97 + i % 26;
from[i] = c;
}
char* to = new char[size];
duffCopy(to, size, from);
for (int i = 0; i < size; i++)
{
printf("%c\r\n", to[i]);
}
}
char *ptr = new char[1000];
int index = 0;
int maxSize = 0;
int running = 1;
int startLoopCount = 0;
void parseSingleInput(char in)
{
switch (in)
{
case 62:
++index;
break;
case 60:
if (index < 1)
{
printf("Runtime error - index moved out of bounds\n");
}
else
--index;
break;
case 43:
++ptr[index];
break;
case 45:
--ptr[index];
break;
case 46:
putchar(ptr[index]);
break;
case 44:
//input is handled separately
//ptr[index]=getchar();
break;
case 91:
//loop endpoints are handled separately
break;
case 93:
//loop endpoints are handled separately
break;
default:
printf("Syntax error - invalid BF symbol: '%c'\n", in);
break;
}
if (index >= maxSize)
maxSize = index + 1;
}
void printState()
{
printf("\n\n");
printf("=============PROGRAM STATE=============\n");
printf("VALUES:\t");
for (int i = 0; i < maxSize; i++)
printf("%d\t", (int)(ptr[i]));
printf("\nindex:\t");
for (int i = 0; i < maxSize; i++)
printf("%d\t", i);
printf("\nPTR:\t");
for (int i = 0; i < index; i++)
printf("\t");
printf("^\n");
}
void printHelp()
{
printf("Valid BF syntax: \n");
printf("\t>\tIncrement data pointer\n");
printf("\t<\tDecrement data pointer\n");
printf("\t+\tIncrement data pointee\n");
printf("\t-\tDecrement data pointee\n");
printf("\t.\tOutput data pointee byte\n");
printf("\t,\tStore input as data pointee byte\n");
printf("\t[\tLoop opener\n");
printf("\t]\tIf data pointee == 0, jump to '['\n\n");
printf("\"clear\" to clear program state\n");
printf("\"exit\" to exit\n\n");
printState();
}
void init()
{
for (int i = 0; i < 1000; i++)
ptr[i] = 0;
index = 0;
maxSize = 1;
running = 1;
printf("\n=============STATE CLEARED=============\n");
printState();
}
int findLoopEnd(string line, int start)
{
int openCount = 0;
for (int i = start + 1; i < line.length(); i++)
{
if (line[i] == '[')
openCount++;
if (line[i] == ']')
{
if (openCount)
openCount--;
else
return i;
}
}
return -1;
}
void queryInput()
{
printf("input: ");
cin >> ptr[index];
}
int parseLine(string line)
{
if (line.compare("help") == 0)
printHelp();
else if (line.compare("exit") == 0)
running = 0;
else if (line.compare("clear") == 0)
init();
else
{
//CAN'T DO IT THIS WAY
//DOESN'T SUPPORT MULTIPLE LOOPS
for (int i = 0; i < (int)(line.length()); i++)
{
if (line[i] == '[')
{
int loopEnd = findLoopEnd(line, i);
if (i > loopEnd)
printf("Syntax error - loop end before loop start: ']' at position %d; '[' at position %d\n", loopEnd, i);
string insideLoop = line.substr(i + 1, loopEnd - i - 1);
parseLine(insideLoop);
while (ptr[index])
parseLine(insideLoop);
i = loopEnd;
}
else
{
if (line[i] == ',')
queryInput();
else
parseSingleInput(line[i]);
//printState();
}
}
return 1;
}
return 0;
}
string getLine()
{
string input;
getline(cin, input);
return input;
}
void BFIDEMain()
{
init();
printf("Enter BF code or type \"help\" for help.\n");
while (running)
{
string input = getLine();
if (parseLine(input))
printState();
}
printf("Goodbye :)\n");
}
int main(void)
{
BFIDEMain();
// don't let the terminal window go away until we want it to
/*
char in;
std::cin >> in;
*/
return 0;
}