-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfpp.cpp
140 lines (123 loc) · 3.56 KB
/
bfpp.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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#include <exception>
const std::string VALID_INSTRUCTIONS = "+-<>[],.";
const int TAPE_SIZE = 30000;
std::vector<char> load_program(std::string filename) {
std::ifstream inputFile;
inputFile.open(filename);
if (!inputFile.is_open() || !inputFile.good()) {
throw std::runtime_error("File opening failed!");
}
std::vector<char> programVector;
char c = inputFile.get();
if (VALID_INSTRUCTIONS.find(c) != std::string::npos)
programVector.push_back(c);
while (inputFile.good()) {
c = inputFile.get();
if (VALID_INSTRUCTIONS.find(c) != std::string::npos)
programVector.push_back(c);
}
inputFile.close();
return programVector;
}
void execute(const std::vector<char>& programVector) {
int currentPosition = 0;
int tapeIndex = 0;
int programCounter = 0;
int openBracket = 0;
std::array<unsigned int, TAPE_SIZE> tape = {0};
std::unordered_map<int, int> startToEnd;
std::unordered_map<int, int> endToStart;
while (programCounter < programVector.size()) {
switch (programVector[programCounter]) {
case '+':
tape[tapeIndex] += 1;
programCounter++;
break;
case '-':
tape[tapeIndex] -= 1;
programCounter++;
break;
case '>':
tapeIndex = (tapeIndex + 1 % TAPE_SIZE);
programCounter++;
break;
case '<':
tapeIndex = (tapeIndex - 1 % TAPE_SIZE);
programCounter++;
break;
case ',':
tape[tapeIndex] = std::getchar();
programCounter++;
break;
case '.':
std::cout << (char) tape[tapeIndex];
programCounter++;
break;
case '[':
if (tape[tapeIndex] == 0) {
if (startToEnd.count(programCounter) == 1) {
programCounter = startToEnd[programCounter];
} else {
currentPosition = programCounter;
openBracket = 1;
while (openBracket >= 1) {
currentPosition += 1;
if (programVector[currentPosition] == '[') {
openBracket += 1;
} else if (programVector[currentPosition] == ']') {
openBracket -= 1;
}
}
startToEnd[programCounter] = currentPosition;
programCounter = currentPosition;
}
}
programCounter += 1;
break;
case ']':
if (tape[tapeIndex] != 0) {
if (endToStart.count(programCounter) == 1) {
programCounter = endToStart[programCounter];
} else {
currentPosition = programCounter - 1;
openBracket = 1;
while (openBracket >= 1) {
currentPosition -= 1;
if (programVector[currentPosition] == '[') {
openBracket -= 1;
} else if (programVector[currentPosition] == ']') {
openBracket += 1;
}
}
endToStart[programCounter] = currentPosition;
programCounter = currentPosition;
}
} else {
programCounter += 1;
}
break;
}
}
std::cout << std::flush;
}
int main(int argc, char** argv) {
if (argc <= 1) {
std::cout << "No filename given." << std::endl;
return -1;
}
std::vector<char> programVector;
try {
programVector = load_program(argv[1]);
} catch (std::runtime_error& e) {
std::cout << e.what() << std::endl;
}
execute(programVector);
}