以下是 BrainFuck 的编译器(使用C++实现):
#include <bits/stdc++.h>
using namespace std;
class BrainfuckCompiler {
vector<unsigned char> memory;
size_t ptr;
stack<size_t> loops;
public:
BrainfuckCompiler() : ptr(0) {
memory.resize(30000, 0);// 内存大小
}
void executeLine(const string& line, ofstream& out, ifstream& inputFile) {
reset();
size_t pc = 0;
int steps = 0;
while (pc < line.length() && steps < 1000000) {
steps++;
char cmd = line[pc];
switch (cmd) {
case '>': ptr = (ptr + 1) % memory.size(); break;
case '<': ptr = ptr ? ptr - 1 : memory.size() - 1; break;
case '+': memory[ptr]++; break;
case '-': memory[ptr]--; break;
case '.': out << memory[ptr]; break;
case ',': {
char c;
bool em = 0;
if (inputFile.is_open() && inputFile.get(c)) {
while (!('0' <= c && c <= '9') && inputFile.get(c))
if ('0' <= c && c <= '9') {
unsigned int num = 0;
while ('0' <= c && c <= '9') {
num = num * 10 + (c - '0');
char next_char = inputFile.peek();
if (next_char == ' ' || next_char == '\n' || next_char == '\t' ||
next_char == EOF || !('0' <= next_char && next_char <= '9')) {
break;
}
inputFile.get(c);
}
memory[ptr] = (unsigned char)num;
} else {
em = 1;
memory[ptr] = 0;
}
} else {
em = 1;
memory[ptr] = 0;
}
if (em) {
cerr << "警告: 输入无效,使用默认值 0" << endl;
}
} break;
case '[':
if (memory[ptr] == 0) {
int depth = 1;
while (depth && ++pc < line.length()) {
if (line[pc] == '[') depth++;
if (line[pc] == ']') depth--;
}
} else {
loops.push(pc);
}
break;
case ']':
if (memory[ptr] != 0) {
pc = loops.top();
} else {
loops.pop();
}
break;
}
pc++;
}
}
bool checkLineBrackets(const string& line) {
stack<int> s;
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == '[') s.push(i);
else if (line[i] == ']') {
if (s.empty()) return false;
s.pop();
}
}
return s.empty();
}
private:
void reset() {
ptr = 0;
fill(memory.begin(), memory.end(), 0);
while (!loops.empty()) loops.pop();
}
};
int main() {
string bfFilename;
cout << "======= BF_Compiler v26.01.1 =======" << endl;
cout << "请输入BF文件名 (.bf 或 .txt): ";
getline(cin, bfFilename);
if (bfFilename.empty()) {
cout << "错误: 未输入文件名" << endl;
return 1;
}
ifstream bfFile(bfFilename);
if (!bfFile.is_open()) {
cerr << "错误: 无法打开BF文件 '" << bfFilename << "'" << endl;
return 1;
}
ifstream inputFile("input.txt");
bool hasInputFile = inputFile.is_open();
if (!hasInputFile) {
cout << "提示: 未找到input.txt,将使用默认输入 0" << endl;
}
ofstream out("output.txt");
if (!out.is_open()) {
cerr << "错误: 无法创建输出文件" << endl;
return 1;
}// error
BrainfuckCompiler compiler;
auto start_time = chrono::high_resolution_clock::now();
cout << "\n编译运行中..." << endl;
string line;
int totalInstructions = 0;
while (getline(bfFile, line)) {
string bfCode;
for (char c : line) {
if (strchr("><+-.,[]", c) != nullptr) {
bfCode += c;
}
}
if (bfCode.empty()) continue;
if (!compiler.checkLineBrackets(bfCode)) {
cerr << "错误: 括号不匹配,跳过此行" << endl;
continue;
}
totalInstructions += bfCode.length();
compiler.executeLine(bfCode, out, inputFile);
}
auto end_time = chrono::high_resolution_clock::now();
auto runtime = chrono::duration_cast<chrono::milliseconds>(end_time - start_time);
bfFile.close();
inputFile.close();
out.close();
cout << "\n======= 运行结果 =======" << endl;
cout << "有效指令数: " << totalInstructions << endl;
cout << "用时: " << runtime.count() << " ms" << endl;
cout << "========================" << endl;
this_thread::sleep_for(chrono::seconds(5));
return 0;
}使用说明
- 目前此编译器仅支持读取后缀为
.txt/.bf的文件。 - 此编译器默认内存为30000个字节(可自己更改)。
- 此编译器目前仅能从输入文件中读取数字。
使用方法
- 创建一个文件夹,将此代码移至文件夹中。
- 创建
input.txt文件,用于读取输入内容。 - 编写一个 BF 程序,后缀名为
.txt或.bf。 - 运行编译器代码,输入程序名(例:Example.bf)。
- 回车,等待运行。
- 如有报错则检查代码或输入内容。
- 文件夹中会出现一个名为
output.txt的文件,内部为程序输出。
若此编译器有Bug,请私信我,有能力的大佬可以自己解决。 此编译器由 Deepseek 改进升级。