gpt4 book ai didi

c++ - 有人可以解释 brainfuck 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:16:14 25 4
gpt4 key购买 nike

我正在尝试编写一个 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/

我真的不明白这是在做什么。我看过其他视频,但这没有任何意义。有人可以解释一下目标吗?

如果给函数一个要翻译的数组,那么 30000 字节的数组有什么意义?

编辑:我应该编写 C++ 代码,将字符转换为 brainfuck 命令的字符,并且它们应该在一些 30000 字节的数组上执行相应的命令,以及这意味着什么。

编辑:提供说明

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 name interpret. 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 in bf.h and bf.cpp. The function signature is std::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_;
}

您应该能够在不实例化任何东西的情况下调用解释,所以我不知道还有另一种方法可以将它们组合在一起。我还没有实现跳转功能。

最佳答案

What is the point of the 30000 byte array if the function is given an array to translate?

假设您得到两个数字 - 2 和 5 - 并希望您的 Brainfuck 程序打印它们的和。

当您所能做的就是操纵当前单元格中的值并“选择”当前单元格时,您是怎么做的?当然,您有时会需要一些临时内存。

为了在两个单独的单元格中有两个值,为加法做准备,您可以这样做:

,>,

如果用户输入 23 (十进制,不是 ascii)那么 Brainfuck 程序存储器或磁带的前两个字节将如下所示:

[2, 3, 0, 0, ...]
// ^ This is where our tape pointer is now. We incremented it with `>`

很好,现在加法呢?一种解决方案是使用 Brainfuck 循环。

让我们将两个自然整数相加,除了递增和递减值外什么都不做:

int a = 2, b = 3

while (a != 0)
{
b += 1
a -= 1
}

基本上,我们正在递减第一个值直到它达到零,随着它的减少,我们正在递增第二个值。随着时间的推移,这将显示:

a = 2, b = 3
a = 1, b = 4
a = 0, b = 5
a == 0, break

因此,我们确实得到了 2 + 3 = 5 .这很简单,可以在 brainfuck 中实现。

,  Get value for first cell (a)
>, Get value for second cell (b)
< Move back to a
[ Loop until a == 0
>+ Decrement b
<- Increment a
We are still at a at this point, so everything is alright
] Loop again if a != 0
>. Print the final result (sum of a plus b)

...所有这些都是为了演示如何使用 Brainfuck 的磁带存储器来做实际的事情。

我相信很多 Brainfuck 程序都将磁带作为堆栈进行操作。有趣的是,您实际上并不需要了解 Brainfuck 程序采用的以可用方式临时存储值的技术。

让我们看看 Brainfuck 解释器如何粗略地与上述程序一起工作,一个指令一个指令。

*在堆栈中的值之后意味着它是堆栈指针现在指向的位置。

我会将其中一些分组,以免让这段时间太长。

  1. , - tape[index] = input() ,堆栈现在是 [2*, 0, 0, ...]
  2. > - index += 1 ,堆栈现在是 [2, 0*, 0, ...]
  3. , - tape[index] = input() ,堆栈现在是 [2, 3*, 0, ...]
  4. < - index -= 1 ,堆栈现在是 [2*, 3, 0, ...]
  5. [ - if (tape[index] == 0) { skipToLoopEnd(); } ,不跳转(2 == 0 为假),堆栈保持不变
  6. >+ - 堆栈现在是 [2, 4*, 0, ...]
  7. <- - 堆栈现在是 [1*, 4, 0, ...]
  8. ] - if (tape[index] != 0) { skipToLoopBegin(); } , 跳转( 1 != 0 为真), 栈左边不变
  9. >+ - 堆栈现在是 [1, 5*, 0, ...]
  10. <- - 堆栈现在是 [0*, 5, 0, ...]
  11. ] - if (tape[index] != 0) { skipToLoopBegin(); } , 不跳转(0 != 0 为假),堆栈保持不变
  12. > - index += 1 ,堆栈现在是 [0, 5*, 0, ...]
  13. . - print(tape[index]) , 打印 5 !

不用说,我没有意识到这个问题来自 2015 年! (耶!)至少有人会发现这在未来很有用...... :^)

关于c++ - 有人可以解释 brainfuck 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com