在 Brainfuck 中实现控制结构

发布于 2024-09-13 15:46:03 字数 1516 浏览 1 评论 0原文

对于外行来说,Brainfuck 是一种图灵完备的语言,只有 8 个命令,所有命令都有C 中的字面等价物:

bf    c
----------------------
>     ++ptr;
<     --ptr;
+     ++*ptr;
-     --*ptr;
.     putchar(*ptr);
,     *ptr=getchar();
[     while (*ptr) {
]     }

在任何具有程序包管理器的 Linux 发行版上,您应该能够找到并安装包 beef,这是一个 Brainfuck 解释器,这样您就可以在家玩了。

正如您在上面所看到的,Brainfuck 只有一个控制结构,[…] 翻译成 C 为:

while (*ptr) { … }

它为您提供了 IF VAR = 0 的所有控制然后从 BASIC 转到 10。以下代码将调用 getchar() 直到返回 0

,    # *ptr = getchar();
[    # while (*ptr) {
  >,    # *(++ptr) = getchar();
]    # }

但是如果我只想读取换行符 \n 该怎么办?在我绞尽脑汁思考如何将其调整为简单的 if 后,我想出了以下方法:(

,            # *ptr = getchar();      /* store input                               */
----------   # *ptr -= 10;            /* test for \n by subtracting 10 before loop */
[            # while (*ptr) {         /* if *ptr == 0, last char read was \n       */
  ++++++++++   # *ptr += 10;          /*   wasn't \n, add 10 back to val under ptr */
  >,           # *(++ptr) = getchar();
  ----------   # *ptr -= 10;
]            # }

如果有人有更好的方法,请告诉我)

现在假设除了 \n 之外,我还想在 \r 上测试跳出该循环。鉴于我只有一次机会跳出循环,我该如何测试其中任何一个?我的目标是能够模拟 switch、嵌套 ifif/else if

For the uninitiated, Brainfuck is a Turing-complete language with only 8 commands, all of which have literal equivalents in C:

bf    c
----------------------
>     ++ptr;
<     --ptr;
+     ++*ptr;
-     --*ptr;
.     putchar(*ptr);
,     *ptr=getchar();
[     while (*ptr) {
]     }

On any linux distro that has a package manager, you ought to be able to find and install the package beef, a Brainfuck interpreter so you can play along at home.

As you can see above, Brainfuck has but one control structure, […] which translates to C as:

while (*ptr) { … }

Which gives you all the control of IF VAR = 0 THEN GOTO 10 from BASIC. The following will call getchar() until it returns 0:

,    # *ptr = getchar();
[    # while (*ptr) {
  >,    # *(++ptr) = getchar();
]    # }

But what if I only want to read to a newline char \n? After having some difficulty wrapping my brain around how this could be adapted to work as a simple if I came up with the following:

,            # *ptr = getchar();      /* store input                               */
----------   # *ptr -= 10;            /* test for \n by subtracting 10 before loop */
[            # while (*ptr) {         /* if *ptr == 0, last char read was \n       */
  ++++++++++   # *ptr += 10;          /*   wasn't \n, add 10 back to val under ptr */
  >,           # *(++ptr) = getchar();
  ----------   # *ptr -= 10;
]            # }

(If anyone has a better way of doing this, please let me know)

Now lets say I want to test break out of that loop on \r in addition to \n. How can I test for either, given that I only have one opportunity to break out of the loop? My goal is to be able to emulate switch, nested ifs or if/else ifs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以心动 2024-09-20 15:46:03

这里是为 Brain F*ck 执行基本逻辑但复杂操作的算法:)

Here are algorithms to perform basic LOGICAL but complex operation for Brain F*ck :)

南七夏 2024-09-20 15:46:03

我还没有看链接,但我想 AShelly 的答案 可能更普遍有用...但是,之前不敢让我的手指输入一些 Brainfuck,我决定现在是最好的时机,并且能够得到以下内容,我< em>认为有效。

后半部分仅用于打印第一部分捕获的内容。另外,我不会说谎,在尝试解决这个问题后,我的大脑确实感觉彻底“操蛋”了……一种命名非常恰当的语言,呵呵。

+
[
    >,
    ----------
    [
        ---
        [
            +++++++++++++
            >>
        ]
    ]

    <
    [
      >>
    ]
    <
]

++++++++++++
[
    <
]

>
[
    .>
]

I didn't take a look at the link yet, but I imagine that AShelly's answer is probably more universally useful...however, having not dared to let my fingers type out some Brainfuck before, I decided that now was as good of a time as any and was able to get the following, which I think works.

The second half is just for printing out what was captured by the first part. Also, I won't lie, my brain does indeed feel thoroughly "fucked" after having tried to work this out...A very aptly-named language, heh.

+
[
    >,
    ----------
    [
        ---
        [
            +++++++++++++
            >>
        ]
    ]

    <
    [
      >>
    ]
    <
]

++++++++++++
[
    <
]

>
[
    .>
]
农村范ル 2024-09-20 15:46:03

有关如何从单个 BF 控制结构开始并定义一系列的说明,请参阅此页面代表更复杂操作的日益复杂的序列。

See this page for a description of how to start with the single BF control structure and define a series of increasingly complex sequences representing more sophisticated operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文