HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Implementing a Smallfuck Interpreter in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
interpreterimplementingjavascriptsmallfuck

Problem

As I've mentioned before, I've been training on CodeWars quite a bit lately. I've been solving some fairly involved katas, especially ones related to <dfn title="Short for esoteric programming language; a computer programming language designed to experiment with unconventional ideas, be difficult to program in, or serve as a joke, rather than for practical use.">Esolang</dfn> interpreters. One of the most interesting ones I've come across is the Smallfuck interpreter Kata.
> [!IMPORTANT]
>
> I'll be solving the Kata in this article, so if you're interested in solving it yourself, I suggest you do so before reading on. Please note that cheating on CodeWars is not permitted. That being said, you may read my solution if you feel stuck or need a hint, but please don't copy it, as it may have consequences on your account.
The Smallfuck language is a minimalist, <dfn title="A Turing-complete system can solve any computational problem given enough time and memory">Turing-complete</dfn> esolang. It operates on an array of bits, the tape, provided as input. The language has only six commands:

Solution

const boundCheck = length => n => n < 0 || n >= length;
const codeCompleteCheck = length => n => n == length;


>
> I'll be solving the Kata in this article, so if you're interested in solving it yourself, I suggest you do so before reading on. Please note that cheating on CodeWars is not permitted. That being said, you may read my solution if you feel stuck or need a hint, but please don't copy it, as it may have consequences on your account.
The Smallfuck language is a minimalist, <dfn title="A Turing-complete system can solve any computational problem given enough time and memory">Turing-complete</dfn> esolang. It operates on an array of bits, the tape, provided as input. The language has only six commands:
| Command | Description |
| ------- | ----------- |
| > | Move pointer to the right (by 1 cell) |

Code Snippets

const boundCheck = length => n => n < 0 || n >= length;
const codeCompleteCheck = length => n => n == length;
const parseTape = tape => tape.split('');
const parseCode = code => code.replace(/[^><*\[\]]/g, '');
const interpretBitFlip = (symbol, pointer, tape) => {
  if (symbol === '*') tape[pointer] = tape[pointer] === '1' ? '0' : '1';
};

Context

From 30-seconds-of-code: smallfuck-interpreter

Revisions (0)

No revisions yet.