Stackfuck is an esoteric programming language, largely based on Brainfuck; it retains six of the original eight instructions.

The original Brainfuck was created by Urban Mueller in 1993, mainly as a pet project and to try to make the world's simplest (and smallest) programming language. Many people took fast to his quaint little language, and it developed quite the following among many programming hobbyists. While it has no practical uses, it's been demonstrated to be Turing-complete, and it's even been able to decrypt the DeCSS algorithm.

This spinoff of Brainfuck was merely intended to bring something new to this genre of programming, while keeping it's original audience satisfied.

The instructions are as follows:

  • '.' (Out) - Show the character value of memory
  • ',' (In) - Read integer value of keystroke into memory
  • '$' (Push) - Push current value of memory to top of stack
  • '&' (Pop) - Pop top of stack into memory
  • '+' (Inc) - Increment value of memory by one
  • '-' (Dec) - Decrement value of memory by one
  • '[' (While) - Loop while memory is not zero
  • ']' (Wend) - Return to corresponding loop
If you're already familiar with Brainfuck, as I'm assuming you are, you'll notice the removal of the > and < insrtructions. They were removed because there are no more memory cells, but now data is managed on the stack (aha! the reasoning behind the name Stackfuck is revealed).

For those of you not familiar with the concept of a stack, it's basically just that, a stack of data. Stacks are most often manipulated in one of two ways - pushing or popping. When data is pushed onto a stack, a new entity of data is placed on top of the other entities of data in the stack. When a stack is popped, the topmost entity of the stack is discarded and it's value stored somewhere. This form of stack is called Last-In, First Out (LIFO). The idea of a stack is quite common when working with low-level programming, such as assembly.

This is a very simple implementation of Stackfuck in C, and lacks proper error checking, so if your Stackfuck programs crash the program, it's most likely the fault of a misplaced bracket or a bad pop instruction. Without further ado, here's the interpreter:

#include <stdio.h>
#include <stdlib.h>

#define STACKSIZE 1024
#define LOOPNEST 255

char *s = NULL;
int p, S[STACKSIZE], SP = 0, L[LOOPNEST], LP = 0, M = 0;

void sf_interpret()
{
    while(s[p])
    {
        switch(s[p])
        {
            case '.': printf("%c",M); break;
            case ',': M = (int)getch(); break;
            case '$': if (SP < STACKSIZE) { S[++SP] = M; } break;
            case '&': if (SP > 0) { M = S[SP--]; } break;
            case '+': M++; break;
            case '-': M--; break;
            case '[': if (LP < LOOPNEST) { L[++LP] = p; } break;
            case ']': if (M != 0 && LP > 0) { p = L[LP]; } else { LP--; } break;
            default: break;
        }
        p++;
    }
}

int main(int argc, char* argv[])
{
    FILE *fp;

    if (fp = fopen(argv[1],"r"))
    {
        fseek(fp,SEEK_CUR,SEEK_END);
        s = (char*)malloc(ftell(fp));
        if (s != NULL)
        {
            rewind(fp);
            while (!feof(fp))
            {
                s[p++] = fgetc(fp);
            }
            fclose(fp);
            p = 0;
            sf_interpret();
        }
    }
}

The code was slightly changed, due to an unnoticed bug in the & instruction.

If you're interested, you should check out the idea of a Turing Tarpit, or even other Esoteric Programming Languages. There are many, many different kinds, including Ook, INTERCAL, HQ9+, and of course, Brainfuck.

Log in or register to write something here or to contact authors.