2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「esolang 」に今来た私が試したこと(ChatGPT先生と共に)

Posted at

はじめに

この記事は、esolang Advent Calendar 2024 の1日目の記事となります。

アドベントカレンダのリストを見ていて、今、esolangに来た者の記録です。

基本事項

esolang ってなに? というわけでChatGPT先生に聞いてみる。
スクリーンショット 2024-12-15 18.38.49.png

スクリーンショット 2024-12-15 18.39.29.png

ほんとにChatGPT先生はなんでも知ってるな。本当かな・・でっちあげじゃないか試そう。

ChatGPTに処理系を作ってもらう

スクリーンショット 2024-12-15 18.41.12.png

esolang.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Brainfuck Runner</title>
</head>
<body>
<h1>Brainfuck Runner</h1>
<textarea id="code" rows="5" cols="50">++++++++[>+++++++++<-]>++.+.</textarea><br><br>
<button id="runButton">Run</button>
<pre id="output"></pre>

<script>
// Simple Brainfuck interpreter
function runBrainfuck(code) {
    const tape = new Uint8Array(30000);
    let ptr = 0;
    let out = "";
    let pc = 0;

    // Precompute loop jumps
    let stack = [];
    let jumpMap = {};
    for (let i = 0; i < code.length; i++) {
        if (code[i] === '[') {
            stack.push(i);
        } else if (code[i] === ']') {
            let start = stack.pop();
            jumpMap[start] = i;
            jumpMap[i] = start;
        }
    }

    while (pc < code.length) {
        let cmd = code[pc];
        switch (cmd) {
            case '>': ptr++; break;
            case '<': ptr--; break;
            case '+': tape[ptr]++; break;
            case '-': tape[ptr]--; break;
            case '.': out += String.fromCharCode(tape[ptr]); break;
            case ',': /* no input */ break;
            case '[':
                if (tape[ptr] === 0) {
                    pc = jumpMap[pc];
                }
                break;
            case ']':
                if (tape[ptr] !== 0) {
                    pc = jumpMap[pc];
                }
                break;
        }
        pc++;
    }

    return out;
}

document.getElementById('runButton').addEventListener('click', function() {
    const code = document.getElementById('code').value;
    const output = runBrainfuck(code);
    document.getElementById('output').textContent = output;
});
</script>
</body>
</html>

なんか出てきた。ブラウザで読み込んでみる。

スクリーンショット 2024-12-15 18.46.06.png

正しいのか分からない。Wikiを見てみる。

スクリーンショット 2024-12-15 18.49.28.png

HelloWorldがあった。これを入れてみよう。

hello.txt
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

実行結果

スクリーンショット 2024-12-15 18.52.49.png

おお! 素晴らしい! 
全部ちゃんと動くのかは分からないけど。これは正しいようだ。

私には見えないものがChatGPT先生には見えていた。

以上、簡単ですが、「esolang」に今来た私が試したこと(ChatGPT先生と共に)、でした。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?