A Brainf*ck interpreter built in the TypeScript type system.

Related tags

Learning resource bf
Overview

Brainf*ck Interpreter in the TypeScript type system

Just another thing that no one asked for.

Why?

I love messing with the TypeScript type system. Somehow building crazy things with technologies that were definitely not supposed to be built with is exciting for me. And, building a Brainf*ck interpreter in the TypeScript type system is basically doing exactly that but twice at the same time.

How?

I 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:

type TupleOfNSize<N extends number, T, $Draft extends T[] = []> =
  // 'lil spacer for better formatting
  $Draft["length"] extends N ? $Draft : TupleOfNSize<N, T, [...$Draft, T]>;

You 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.

This basic recursive approach combined with TypeScript's inferring within conditional types allowed me to create the Brainf*ck interpreter.

Example

Let's run a hello world Brainf*ck program using our interpreter.

// import type { BF } from "./mod.ts";

type Output = BF<{
  program: "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
  outputType: "ascii";
}>;

Now, when you hover over the Output type, you will see that its type is "Hello, world!\n". Note how the BF type simply returns the output. See the API section for how you can pass input and change the output encoding.

Limitations

Due to TypeScript's type complexity cap, it is impossible to run certain complex programs as we breach the recursive type limit when running our recursive types.

API

BF

This is the only exported type from mod.ts and it is the type used to run programs. It takes in a single object type with the options for how to run your program within it. The pointers are Unsigned 8-bit integers and the default memory size is 256 slots. Here are the options you should know about:

  • program

This is your Brainf*ck program.

  • input

The input that the Brainf*ck program can ready byte-by-byte via the , command. By default this is just an empty string.

  • memory

The initial memory for the program. This is 256 slots byte default initialized with 0.

  • outputType

The type of the output when you use the . command. This determines the type of the output representation when the BF type finishes and returns your output. By default, this will be "buffer" which means that it will return all of the bytes stored in a tuple with each of the unsigned 8-bit integers within. However, you can specify "ascii" in which the API will return a string with the output characters encoded in the "ascii" format in a single string.

License

MIT

You might also like...

Type predicate functions for checking if a value is of a specific type or asserting that it is.

As-Is Description As-Is contains two modules. Is - Type predicates for checking values are of certain types. As - Asserting values are of a certain ty

Feb 10, 2022

🧬 A type builder for pagination with prisma and type-graphql.

🧬 Prisma TypeGraphql Pagination Prisma TypeGraphql Pagination builds prisma pagination types for type-graphql. import { ... } from 'type-graphql'

Apr 21, 2022

100% type-safe query builder for node-postgres :: Generated types, call any function, tree-shakable, implicit type casts, and more

⚠️ This library is currently in alpha. Contributors wanted! tusken Postgres client from a galaxy far, far away. your database is the source-of-truth f

Dec 29, 2022

RepoProvas API, a system for sharing tests between students, built with Typescript, Node.js, Express, Prisma and Postgres.

Repoprovas Built With 📋 Description RepoProvas API, a system for sharing tests between students, built with Typescript, Node.js, Express, Prisma and

Dec 13, 2022

Denail of service system for the Dentistimo system. Used to improve the tolerance and testing fail safe functionality.

 Denail of service system for the Dentistimo system. Used to improve the tolerance and testing fail safe functionality.

Distributed Systems - Dos Testing DoS (Denail of Service) System for Testing and Practical demonstration of systems capability to handle a basic DDoS

Nov 8, 2022

Simple, lightweight at-runtime type checking functions, with full TypeScript support

pheno Simple, lightweight at-runtime type checking functions, with full TypeScript support Features Full TypeScript integration: TypeScript understand

Sep 5, 2022

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

Dec 7, 2022

CLI tool to add @ts-expect-errors to typescript type errors

suppress-ts-errors Cli tool to add comments to suppress typescript type errors. Add @ts-expect-error or @ts-ignore comments to all locations where err

Dec 8, 2022
Owner
Carter Snook
I maintain and contribute to open-source. Rust + TypeScript all the way 🚀 he/him 17
Carter Snook
A Typescript assembly(ish) interpreter with a nice web UI

Assembly-Online Example Fibonacci Program MOV R0, #1 MOV R1, #1 fib: MOV R2, R0 ADD R0, R0, R1 MOV R1, R2 B print CMP R0, #40 BGT exit B fib exit: \ I

null 3 May 23, 2022
The Frontend of Escobar's Inventory Management System, Employee Management System, Ordering System, and Income & Expense System

Usage Create an App # with npx $ npx create-nextron-app my-app --example with-javascript # with yarn $ yarn create nextron-app my-app --example with-

Viver Bungag 4 Jan 2, 2023
An interpreter for College Board's specified pseudocode for the AP Computer Science Principles Exam.

College Board Pseudocode Interpreter A playground for this interpreter is up on my website. This project is a mostly-functioning interpreter for Colle

Daniel 7 Nov 16, 2022
Detect the executable python interpreter cmd in $PATH.

detect-python-interpreter Detect the executable python interpreter cmd in $PATH. Installation $ npm install --save detect-python-interpreter Usage con

Khaidi Chu 2 Apr 12, 2022
A browser-based Piet editor/interpreter

A browser-based Piet editor/interpreter Features An interpreter that fully conforms to the Piet specification Code editor with a palette with command

null 8 Nov 22, 2022
A simple tap test runner that can be used by any javascript interpreter.

just-tap A simple tap test runner that can be used in any client/server javascript app. Installation npm install --save-dev just-tap Usage import cre

Mark Wylde 58 Nov 7, 2022
Combine type and value imports using Typescript 4.5 type modifier syntax

type-import-codemod Combines your type and value imports together into a single statement, using Typescript 4.5's type modifier syntax. Before: import

Ian VanSchooten 4 Sep 29, 2022
A type programming language which compiles to and interops with type-level TypeScript

Prakaar Prakaar (hindi for "type") is a type programming language which compiles to and interops with type-level TypeScript. Prakaar itself is also a

Devansh Jethmalani 17 Sep 21, 2022
A transpiler from golang's type to typescript's type for collaboration between frontend & backend.

go2type go2type.vercel.app (backup site) A typescript transpiler that convert golang's type to typescript's type. Help front-end developer to work fas

Oanakiaja 8 Sep 26, 2022
Implementing various sorting algorithms in Typescript's type system

Sorta Cool I was on a 10 hour flight with no WiFi, and, bored out of my mind, I thought it would be fun to implement some sorting algorithms in the Ty

Neil Ramaswamy 7 Nov 10, 2022