Smilefuck

From Esolang
Jump to navigation Jump to search

Smilefuck is an esoteric programming language similar to brainfuck. It was created on June 6th, 2014 by User:Sacchan.

Memory layout

The data structure manipulated by a Smilefuck program consists of two stacks, l and r, and a working register w. Each entry on the stacks or in the working register is of a boolean type; that is, it is either 0 or 1. When the program is started, the input is loaded onto stack l, and after termination, the output is read from stack r. Input and output are encoded with the top-most bits on the stack being the last ones of the input/output.

Syntax and Semantics

There are four elementary instructions that work on the data structure

  • ! inverts the working register
  • _ swaps the stacks
  • ^ pops the top value of stack l into w. If stack l is empty, this results in an error
  • v pushes the value of w on stack l

Furthermore, there are two loop constructs:

  • (...) repeats the code in the parentheses until the left stack is empty. In C-style, (P) translates to while(!empty(l)){P;}
  • [...] repeats the code in the brackets while the working register is 1. In C-style, [P] translates to while(w == 1){P;}

Intertwining different kinds of loops (e.g. (..[..)..] ) is not allowed.

Example Programs

Identity Function

 _

Reverse the input

 (^_v_)

Bitwise invert the input

 (^!_v_)_(^_v_)

Increment the input

 (^_v_)[!]v_(^_v_)_^[!_v_^]!_v_(^_v_)_^[v!](^_v_)

Explanation: First add a 0 as most significant bit.

 (^_v_)[!]v_(^_v_)_

Then invert all digits, as long as they are 1s, as well as the first encountered 0, starting from the lsb

 ^[!_v_^]!_v_

Simply move the rest of the bits over

 (^_v_)_

If the msb is a 0, clear it

 ^[v!]

Reverse the bit string

 (^_v_)

Computational Class

User:Sacchan believes Smilefuck to be Turing-complete, since it is easily possible to model the states of a BCT program with it, though without output. There is no formal proof on the subject as of yet.

External resources