I Wrote a BrainF*** Interpreter and REPL in TypeScript!

Andrew (he/him) - May 29 '20 - - Dev Community

Photo by Robina Weermeijer on Unsplash


This was my first time ever coding in TypeScript, my first npm package, and more. Let me know if you have any comments or suggestions!

Try out the sample code on the GitHub repo!

GitHub logo awwsmm / BrainScript

A BrainF*** interpreter written in TypeScript

BrainScript

A BrainF*** (BF) interpreter and REPL, written in TypeScript.

Installation

Install from npm with

$ npm i brainscript

Or try it online at npm.runkit.com

var lib = require("brainscript")

lib.bf("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")
Enter fullscreen mode Exit fullscreen mode

Use

BrainScript provides a BF interpreter bf which can be used in interactive or batch mode, as well as a BF REPL brain.

bf can be used to batch process BF code, returning any resulting output as a string

const output: string = bf("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")

console.log(output) // Hello World!\n
Enter fullscreen mode Exit fullscreen mode

...but it also provides basic interactive capabilities for programs which require user input

// input.ts
console.log(bf(",."))
Enter fullscreen mode Exit fullscreen mode
$ npx ts-node input.ts

    Please provide a single character for ',' input:
    : !

!
Enter fullscreen mode Exit fullscreen mode

brain is an interactive REPL which accepts single- or…

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .