-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf_interpreter.cpp
119 lines (108 loc) · 2.31 KB
/
bf_interpreter.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
#include <iostream>
#include <cstring>
#include <fstream>
#include "termcolor.hpp"
using namespace std;
using namespace termcolor;
char tape[30000] = {0};
char *ptr = tape;
int link[50][2];
string ch;
void interpret(const char *str, char *in)
{
int x = 0, y = 0, z = 0, loop;
int temp;
bool checked = false;
ifstream ff(in);
for (int i = 0; i <= strlen(str); i++)
{
if (str[i] == '[')
{
x = i;
loop = 1;
while (loop > 0)
{
if (str[++x] == '[')
{
loop++;
}
else if (str[x] == ']')
{
loop--;
}
}
link[z][0] = i;
link[z][1] = x;
z++;
}
}
for (int i = 0; i <= strlen(str); i++)
{
switch (str[i])
{
case '>':
++ptr;
break;
case '<':
--ptr;
break;
case '+':
++*ptr;
break;
case '-':
--*ptr;
break;
case '[':
if (*ptr == 0)
{
for (int j = 0; j <= z; j++)
{
if (link[j][0] == i)
{
i = link[j][1];
}
}
}
break;
case ']':
if (*ptr != 0)
{
for (int j = 0; j <= z; j++)
{
if (link[j][1] == i)
{
i = link[j][0];
break;
}
}
}
break;
case ',':
ff.get(*ptr);
break;
case '.':
putchar(*ptr);
break;
default:
break;
}
}
}
int main(int argc, char *argv[])
{
char *in;
in = argv[2];
if (argc == 1)
cout << red << "Fatal error: No input .bf file" << reset << endl;
else if (argc >= 2)
{
if (argc == 2)
{
cout << blue << "No input in file" << reset << endl;
}
ifstream fin(argv[1]);
getline(fin, ch, '\0');
interpret(ch.c_str(), in);
}
return 0;
}