我正在尝试编写一个brainfuck解释器,但是我似乎缺少一些上下文或其他东西。负责处理"+><>
"等转换的函数应该被调用:
std::vector<int> Interpreter::interpret(const std::string &src_,
const std::vector<int> & input_)
程序测试如下:
int main()
{
std::vector<int> res;
// output: 1
res = interpret("+.");
for (auto i : res)
std::cout << i << " ";
2
// output: 2
res = interpret(",.", {2});
for (auto i : res)
std::cout << i << " ";
return 0;
}
http://www.muppetlabs.com/~breadbox/bf/
这个链接是关于Brainfuck语言的,它是一种最小化的编程语言,使用指针和移动数据指针来实现计算。通过这种方式,程序员可以使用最少的代码量来实现复杂的操作。 关于视频中的问题,理解可能有困难,但是您可以参考提供的代码和说明,逐步学习并掌握相关知识。另外,30000字节数组的作用是存储编程所需的数据,函数将其转换成对应的Brainfuck命令并执行。希望能对您有所帮助。Abstract Write a simple interpreter for Brainfk. 1 Introduction
A Brainfk program has an implicit byte pointer, called the pointer, which is free to move around within an array of 30000 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array. The Brainfuck programming language consists of eight commands, each of which is represented as a single character.
•
>
Increment the pointer.
•<
Decrement the pointer.
•+
Increment the byte at the pointer.
•-
Decrement the byte at the pointer.
•.
A dot, output the byte at the pointer.
•,
A comma, input a byte and store it in the byte at the pointer.
•[
Jump forward past the matching ] IF the byte at the pointer is zero.
•]
Jump backward to the matching [ UNLESS the byte at the pointer is zero.For example, one version of the "Hello, World!" program in Brainfk is
++++++++++[>+++++++>++++++++++>+++>+<<<<-] >++.>+.+++++++..+++.>++.<<+++++++++++++++.>. +++.------.--------.>+.>.
2 Requirement
2.1 Test Program
I will use program to test and grade your code in batch. So please double check your function signature. Failure to run properly may impact your project grade. The entry function will all have the nameinterpret
. And you may implement as many other helper functions as you want. The following sections elaborate on the specifications.2.1.1 C++ I would use C++11 (
g++ -std=c++11 ...
) to test your program. So feel free to employ some of the recent goodies added to C++, e.g., lambda function, array initialization, etc. For convenience, please separate your declaration and implementation code inbf.h
andbf.cpp
. The function signature isstd::vector<int> interpret(const std::string &src, const std::vector<int> &input = {});
My test program would look like
int main() { std::vector<int> res; // output: 1 res = interpret("+."); for (auto i : res) std::cout << i << " "; // output: 2 res = interpret(",.", {2}); for (auto i : res) std::cout << i << " "; return 0; }
编辑:目前为止,我所拥有的是:
BFK.h
#pragma once
#include <vector>
#include <iostream>
using namespace std;
char arr[30000];
char* p = arr;
void incPtr();
void decPtr();
void incByte();
void decByte();
void printByte();
void setByte();
void jumpF();
void jumpB();
std::vector<int> interpret(const std::string &src,
const std::vector<int> & input = {});
BFK.cpp
#include "BFK.h"
void incPtr() {
p++;
}
void decPtr() {
p--;
}
void incByte() {
(*p)++;
}
void decByte() {
(*p)--;
}
void printByte() {
std::cout << *p;
}
void setByte() {
std::cin >> *p;
}
void jumpF() {
if (*p == 0) {
}
}
void jumpB() {
}
std::vector<int> interpret(const std::string &src_,
const std::vector<int> & input_){
int i = 0;
int max = src_.size();
while (i < max) {
switch (src_[i]) {
case '>':
incPtr();
break;
case '<':
decPtr();
break;
case '+':
incByte();
break;
case '-':
decByte();
break;
case '.':
printByte();
break;
case ',':
setByte();
break;
case '[':
jumpF();
break;
case ']':
jumpB();
break;
}
}
return input_;
}
你应该可以在不实例化任何东西的情况下调用解释器,所以我不知道其他组合方法。我还没有实现跳转功能。
char
值,而不是int
),对其进行任何更改都会修改原始数据。你需要向我们展示实际输出和预期输出,也许还有你的“interpret”函数。 - paddyinterpret()
函数体。你已经写了些什么了吗? - CinCoutvector<int>
参数是可用输入,因此当,
需要读取值时,它来自该向量。类比地,返回的vector<int>
是.
操作的输出。因此测试程序循环返回的向量以打印值。 - Jonathan Lefflersrc
字符串是BF程序代码,将作用于内存;input
向量是输入值的供应;返回的向量是输出值的集合。从规范中我不清楚如果你将当前位置增加到内存中的最后一个字节之后会发生什么,或者在第一个字节之前将其减少,或者尝试在没有值可读取时读取值会发生什么。这些细节可能已经在某个地方详细说明了。 - Jonathan Leffler