{"id":22731331,"url":"https://github.com/sno2/bf","last_synced_at":"2025-04-13T05:13:32.308Z","repository":{"id":112568917,"uuid":"449075955","full_name":"sno2/bf","owner":"sno2","description":"A Brainheck interpreter built in the TypeScript type system.","archived":false,"fork":false,"pushed_at":"2024-11-15T17:50:20.000Z","size":16,"stargazers_count":73,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T05:13:22.507Z","etag":null,"topics":["interpreter","programming-language","typescript","typescript-types"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/sno2.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":"2022-01-17T23:13:53.000Z","updated_at":"2025-03-04T11:33:52.000Z","dependencies_parsed_at":"2024-12-10T19:40:14.033Z","dependency_job_id":null,"html_url":"https://github.com/sno2/bf","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/sno2%2Fbf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fbf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fbf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sno2%2Fbf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sno2","download_url":"https://codeload.github.com/sno2/bf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665746,"owners_count":21142123,"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":["interpreter","programming-language","typescript","typescript-types"],"created_at":"2024-12-10T19:21:33.895Z","updated_at":"2025-04-13T05:13:32.284Z","avatar_url":"https://github.com/sno2.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Brainheck Interpreter in the TypeScript type system\n\n## Why?\n\nI love messing with the TypeScript type system and playing around with this [hidden Turing-complete language](https://github.com/microsoft/TypeScript/issues/14833#issuecomment-1018872710) within a Turing-complete language.\n\n## How?\n\nI built the interpreter using only TypeScript types. I was also using Deno for easy TypeScript integration but the code in this repository will run on anywhere that can run a version of TypeScript that supports template literal string types and conditional inferration. It greatly depends on using recursive types. Here's a small example of a recursive type to build an array of `N` size with each item of type `T`:\n\n```ts\ntype TupleOfNSize\u003cN extends number, T, $Draft extends T[] = []\u003e =\n  // 'lil spacer for better formatting\n  $Draft[\"length\"] extends N ? $Draft : TupleOfNSize\u003cN, T, [...$Draft, T]\u003e;\n```\n\nYou can see, we basically just build an array using a recursive type that hold the accumulator array, `$Draft`. I prefer to prefix private generics within types with a dollar sign but that's just my personal preference.\n\nThis basic recursive approach combined with [TypeScript's inferring within conditional types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#inferring-within-conditional-types) allowed me to create the Brainf\\*ck interpreter.\n\n## Example\n\nLet's run a hello world Brainheck program using our interpreter.\n\n```ts\n// import type { BF } from \"./mod.ts\";\n\ntype Output = BF\u003c{\n  program: \"++++++++[\u003e++++[\u003e++\u003e+++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e+\u003e+\u003e-\u003e\u003e+[\u003c]\u003c-]\u003e\u003e.\u003e---.+++++++..+++.\u003e\u003e.\u003c-.\u003c.+++.------.--------.\u003e\u003e+.\u003e++.\";\n  outputType: \"ascii\";\n}\u003e;\n```\n\nNow, when you hover over the `Output` type, you will see that its type is\n`\"Hello, world!\\n\"`. Note how the `BF` type simply returns the output. See the\n[API](#API) section for how you can pass input and change the output encoding.\n\n## Limitations\n\nDue to TypeScript's type complexity cap, it is impossible to run certain complex\nprograms as we breach the recursive type limit when running our recursive types.\n\n## API\n\n### `BF`\n\nThis is the only exported type from `mod.ts` and it is the type used to run\nprograms. It takes in a single object type with the options for how to run your\nprogram within it. The pointers are Unsigned 8-bit integers and the default\nmemory size is 256 slots. Here are the options you should know about:\n\n- `program`\n\nThis is your Brainheck program.\n\n- `input`\n\nThe input that the Brainheck program can ready byte-by-byte via the `,`\ncommand. By default this is just an empty string.\n\n- `memory`\n\nThe initial memory for the program. This is 256 slots byte default initialized\nwith `0`.\n\n- `outputType`\n\nThe type of the output when you use the `.` command. This determines the type of\nthe output representation when the `BF` type finishes and returns your output.\nBy default, this will be `\"buffer\"` which means that it will return all of the\nbytes stored in a tuple with each of the unsigned 8-bit integers within.\nHowever, you can specify `\"ascii\"` in which the API will return a string with\nthe output characters encoded in the `\"ascii\"` format in a single string.\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsno2%2Fbf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsno2%2Fbf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsno2%2Fbf/lists"}