{"id":20367318,"url":"https://github.com/nst/brainfuck","last_synced_at":"2025-04-12T05:13:09.853Z","repository":{"id":66697929,"uuid":"89956413","full_name":"nst/Brainfuck","owner":"nst","description":"A flexible Brainfuck / Brainloller / Braincopter interpreter in Swift 3.1.","archived":false,"fork":false,"pushed_at":"2023-11-29T18:23:27.000Z","size":50,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T05:13:05.203Z","etag":null,"topics":["braincopter","brainfuck","brainfuck-interpreter","brainloller","esolang","esoteric-language","esoteric-programming-language","swift","swift-3","swift3"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-01T19:39:12.000Z","updated_at":"2023-04-12T11:30:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"7a656508-b767-4378-aa3b-9104a573c60d","html_url":"https://github.com/nst/Brainfuck","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nst%2FBrainfuck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nst%2FBrainfuck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nst%2FBrainfuck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nst%2FBrainfuck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nst","download_url":"https://codeload.github.com/nst/Brainfuck/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248519556,"owners_count":21117761,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["braincopter","brainfuck","brainfuck-interpreter","brainloller","esolang","esoteric-language","esoteric-programming-language","swift","swift-3","swift3"],"created_at":"2024-11-15T00:30:51.724Z","updated_at":"2025-04-12T05:13:09.827Z","avatar_url":"https://github.com/nst.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Brainfuck\n\n`Brainfuck.swift` is a flexible [Brainfuck](https://esolangs.org/wiki/Brainfuck) interpreter in Swift 3.1.\n\nIt comes with unit tests and tracing / debuging functions.\n\nIt also comes with [Brainloller](https://esolangs.org/wiki/Brainloller) and [Braincopter](https://esolangs.org/wiki/Braincopter) readers.\n\nBrainfuck commands:\n\n```\n\u003e\tMove pointer right\n\u003c\tMove pointer left\n+\tIncrement memory under pointer\n-\tDecrement memory under pointer\n.\tOutput value under pointer\n,\tInput value and store it under pointer\n[\tJump past matching ] if value under pointer is 0\n]\tJump back to matching [ if value under pointer is nonzero\n```\n\nHere are several ways to use Brainfuck.swift, given the following program:\n\n```let helloWorld = \"++++++++++[\u003e+++++++\u003e++++++++++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e++.\u003e+.+++++++..+++.\u003e++.\u003c\u003c+++++++++++++++.\u003e.+++.------.--------.\u003e+.\u003e.\"```\n\n## 1. Simple call\n\n```Swift\nlet b = try! Brainfuck(helloWorld)\nlet result = try! b.run()\n```\n\nResult: `Hello World!\\n`\n\n## 2. Use optional parameters\n\n```Swift\nlet b = try! Brainfuck(helloWorld, userInput: \"\", dataSize: 32)\nlet result = try! b.run()\n```\n\nResult: `Hello World!\\n`\n\n## 3. Call step by step\n\n```Swift\ndo {\n    let b = try Brainfuck(helloWorld)\n    \n    while b.canRun() {\n        if let byte = try b.step() {\n            print(byte)\n        }\n    }\n} catch let e {\n    print(e)\n}\n```\n\nOutput:\n\n```\n72\n101\n108\n108\n111\n32\n87\n111\n114\n108\n100\n33\n10\n```\n\n## 4. Print state at each step\n\n```Swift\ndo {\n    let b = try Brainfuck(\"++++++[\u003e++++++\u003c-]\u003e.\") // 6x6 == 0x24 == '$'\n    \n    while b.canRun() {\n        print(\"-------------------------------------------------------------\")\n        b.printStep()\n        b.printInstructions()\n        b.printData(upToIndex: 10)\n        \n        if let putByte = try b.step() {\n            print(\" PUT: \" + String(format: \"%02X\", putByte))\n        }\n    }\n    \n    print(\"-------------------------------------------------------------\")\n    b.printExecutionSummary()\n    \n} catch let e {\n    print(e)\n}\n```\n\nOutput:\n\n```\n...\n-------------------------------------------------------------\nSTEP: 73\nPROG: ++++++[\u003e++++++\u003c-]\u003e.\n                       ^ 17\nDATA: 00 24 00 00 00 00 00 00 00 00 00\n      ^^ 0\n-------------------------------------------------------------\nSTEP: 74\nPROG: ++++++[\u003e++++++\u003c-]\u003e.\n                        ^ 18\nDATA: 00 24 00 00 00 00 00 00 00 00 00\n         ^^ 1\n PUT: 24\n-------------------------------------------------------------\nSUMMARY: program stopped after 75 step(s) with output:\n    HEX: 24\n    STR: $\n```\n\n## 5. Brainloller\n\n[Brainloller](https://esolangs.org/wiki/Brainloller) stores Brainfuck commands in image pixels.\n\nExample with this code:\n\n```Swift\nlet bl = try! Brainloller(imagePath: \"/tmp/fibonacci.png\")\nlet s1 = bl.brainfuck()\nprint(s1)\n\nlet bf = try! Brainfuck(s1)\nlet s2 = try! bf.run()\nprint(s2)\n```\n\nInput (magnified 10x):\n\n\u003cIMG SRC=\"img/fibonacci_x10.png\" BORDER=1\u003e\n\nBrainloller commands:\n\n```\nred        (255,0,0)\t\u003e\ndarkred    (128,0,0)\t\u003c\ngreen      (0,255,0)\t+\ndarkgreen  (0,128,0)\t-\nblue       (0,0,255)\t.\ndarkblue   (0,0,128)\t,\nyellow     (255,255,0)\t[\ndarkyellow (128,128,0)\t]\ncyan       (0,255,255)\trotates the IP 90° clockwise\ndarkcyan   (0,128,128)\trotates the IP 90° counter-clockwise\nothers     n/a          nop\n```\n\nOutput (s1) is Brainfuck instructions:\n\n```\n++++++++++++++++++++++++++++++++++++++++++++\u003e++++++++++++++++++++++++++++++++\u003e++++++++++++++++\u003e\u003e+\u003c\u003c[\u003e\u003e\u003e\u003e++++++++++\u003c\u003c[-\u003e+\u003e-[\u003e+\u003e\u003e]\u003e[+[-\u003c+\u003e]\u003e+\u003e\u003e]\u003c\u003c\u003c\u003c\u003c\u003c]\u003e[\u003c+\u003e-]\u003e[-]\u003e\u003e\u003e++++++++++\u003c[-\u003e-[\u003e+\u003e\u003e]\u003e[+[-\u003c+\u003e]\u003e+\u003e\u003e]\u003c\u003c\u003c\u003c\u003c]\u003e[-]\u003e\u003e[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]\u003c[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]\u003c\u003c\u003c++++++++++++++++++++++++++++++++++++++++++++++++.[-]\u003c\u003c\u003c\u003c\u003c\u003c\u003c.\u003e.\u003e\u003e[\u003e\u003e+\u003c\u003c-]\u003e[\u003e+\u003c\u003c+\u003e-]\u003e[\u003c+\u003e-]\u003c\u003c\u003c-]\u003c\u003c++...\n```\n\nOutput (s2) is Brainfuck code exection output:\n\n`1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 121, 98, 219, ...`\n\n## 6. Braincopter\n\n[Braincopter](https://esolangs.org/wiki/Braincopter) is a variant of Brainloller in which commands are stored in pixels of any color.\n\nThe commands are retrieved from pixels this way:\n\n```\ncommand = (65536 * R + 256 * G + B) % 11\n\n0\t\u003e\n1\t\u003c\n2\t+\n3\t-\n4\t.\n5\t,\n6\t[\n7\t]\n8\trotate IP to the right\n9\trotate IP to the left\n10\tnop\n```\n\nExample with this code:\n\n```Swift\n    let path = \"/tmp/braincopter2.png\"\n    let bl = try! Braincopter(imagePath: path)\n    let (_, s1) = bl.brainfuck()\n    print(s1)\n    \n    let bf = try! Brainfuck(s1)\n    let s2 = try! bf.run()\n    print(s2)\n```\n\nInput (magnified 10x):\n\n\u003cIMG SRC=\"img/braincopter2_x10.png\" BORDER=1\u003e\n\nOutput (s1) is Brainfuck instructions:\n\n```\n\u003e++++++++++[\u003c++++++++++\u003e-]\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e++++[\u003e++++\u003c-]\u003e[\u003c\u003c\u003c\u003c\u003c\u003c\u003c++\u003e+++\u003e++++\u003e++++++\u003e+++++++\u003e+++++++\u003e++++\u003e-]\u003c++\u003c+++++\u003c++++++++++\u003c+++++++++\u003c++++++\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c[\u003e+\u003e+\u003e[-]\u003e\u003e\u003e\u003e[-]\u003e[-]\u003c\u003c\u003c\u003c\u003c\u003c\u003c[\u003e\u003e\u003e\u003e\u003e\u003e+\u003e+\u003c\u003c\u003c\u003c\u003c\u003c\u003c-]\u003e\u003e\u003e\u003e\u003e\u003e[\u003c\u003c\u003c\u003c\u003c\u003c+\u003e\u003e\u003e\u003e\u003e\u003e-]+\u003e---[\u003c-\u003e[-]]\u003c[\u003e\u003e\u003e\u003e\u003e\u003e.\u003e.\u003e..\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c+\u003c\u003c[-]\u003e\u003e\u003e\u003e\u003e\u003e-]\u003c\u003c\u003c\u003c\u003c[\u003e\u003e\u003e\u003e\u003e+\u003e+\u003c\u003c\u003c\u003c\u003c\u003c-]\u003e\u003e\u003e\u003e\u003e[\u003c\u003c\u003c\u003c\u003c+\u003e\u003e\u003e\u003e\u003e-]+\u003e-----[\u003c-\u003e[-]]\u003c[\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e.\u003c.\u003c..\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c+\u003c[-]\u003e\u003e\u003e\u003e\u003e-]\u003c+\u003e[-]\u003e[-]\u003e[-]\u003c\u003c\u003c[\u003e+\u003e+\u003e+\u003c\u003c\u003c-]\u003e[\u003c+\u003e-]+\u003e----------[\u003c-\u003e[-]]\u003c[\u003c\u003c+\u003e[-]\u003e-]\u003e[-]\u003e[-]\u003c\u003c\u003c\u003c[\u003e\u003e+\u003e+\u003e+\u003c\u003c\u003c\u003c-]\u003e\u003e[\u003c\u003c+\u003e\u003e-]+\u003e----------[\u003c-\u003e[-]]\u003c[\u003c\u003c\u003c+\u003e[-]\u003e\u003e-][-]\u003e[-]\u003c\u003c\u003c\u003c\u003c[\u003e\u003e\u003e\u003e+\u003e+\u003c\u003c\u003c\u003c\u003c-]\u003e\u003e\u003e\u003e[\u003c\u003c\u003c\u003c+\u003e\u003e\u003e\u003e-]+\u003e[\u003c-\u003e[-]]\u003c[[-]\u003e[-]\u003c\u003c\u003c\u003c[\u003e\u003e\u003e+\u003e+\u003c\u003c\u003c\u003c-]\u003e\u003e\u003e\u003e[\u003c\u003c\u003c\u003c+\u003e\u003e\u003e\u003e-]\u003c[\u003e++++++++[\u003c++++++\u003e-]\u003c.-.[-]][-]\u003e[-]\u003c\u003c\u003c[\u003e\u003e+\u003e+\u003c\u003c\u003c-]\u003e\u003e\u003e[\u003c\u003c\u003c+\u003e\u003e\u003e-]\u003c[\u003e++++++++[\u003c++++++\u003e-]\u003c.[-]][-]\u003e[-]\u003c\u003c[\u003e+\u003e+\u003c\u003c-]\u003e\u003e[\u003c\u003c+\u003e\u003e-]++++++++[\u003c++++++\u003e-]\u003c.[-]]\u003e\u003e\u003e\u003e.\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c-]\n```\n\nOutput (s2) is Brainfuck code exection output:\n\n```\n1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buz\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnst%2Fbrainfuck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnst%2Fbrainfuck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnst%2Fbrainfuck/lists"}