[Permalink]     code inactive other programming

About Obfuna

Obfuna is an esoteric programming language designed to minimise the instruction set, the length of code necessary to express a program, and the readability while still remaining vaguely useful.

To achieve these goals, all Obfuna operations are assigned one-chacracter names and take exactly one argument. In fact, the name Obfuna is derived from 'obfuscated' (Obfuna code is, at least initially, difficult to read) and 'unary' (All operations have exactly one argument). There are twenty six variables and one random-access unbounded array. All variables are dynamically typed as this cuts down on the code length.


Obfuna has 26 standard variables which are represented by the lower-case letters a to z.

There is one random-access array which is infinitely extendable (well, in theory anyway; in practice this will be limited by your machine's memory) which is represented by a number (indicating the position in the array - starting at 0) or a function returning a number surrounded by round brackets, i.e. ( and ). For example, the first element in the array is represented by (0). The size of the array increases as you assign to higher locations. The initial size of the array is 0.

There are two special 'variables' used to ascertain the current size of the array. $ holds the index number of the last element in the array and % holds the number of elements in the array. From above, it will be obvious that initially $ will be -1 and % will be 0. Assigning to these variables can grow or shrink (in which case you will lose data) the array to a specified size. These are of particular use as ($) can used to reference the last element in the array and (%) will reference one element beyond the end, allowing you to add a new element, treating the array like a stack.

In addition, there are three special 'variables' which act as I/O pipes. Passing a value to ?, prints the value to the standard output. Assigning a variable from ?, reads from the standard input. ! acts similarly except that in the case of output, it appends a newline (return) character to the value, and in the case of input, it preserves the newline from standard input. # works in exactly the same way as ? but inputs and outputs from and to the currently open file.

The syntax of Obfuna is extremely simple. There are only two types of instruction: function calls and assignments. Assigments take the form of <variable name><value> and function calls look like <function name><value> or <function name><function call>. Note you can nest functions in this way.

As an example, to assign the number 10 to the variable n, you simply write n10. There is no = operator.

Strings are quoted using the angle brackets, < and >. Anything between curly brackets, { and }, is considered a comment and will be ignored by Obfuna. These are particularly useful for naming variables more helpfully. Instead of just n10, you could write n{umber}10.

The only other syntactic element of Obfuna is the square brackets, [ and ]. Any code between a pair of square brackets will be considered a block to be processed as one instruction. This is of particular use when using flow control functions which skip or repeat only the next instruction.

Below is a list of all the functions using x as their argument for example purposes.

Character Name Comments
+x Plus Adds x to ($)
-x Subtract Subtracts x from ($)
*x Multiply Multiplies ($) by x
/x Divide Divides ($) by x
^x Raise Raises ($) to the power of x
Ax ASCII Converts x's numeric value to an ASCII character and returns the result
Cx Concatenate Appends x to the end of ($)
Dx Do Repeats the next instruction x times
Ex Evaluate Intereprets x as a list of obfuna instructions
Fx File Opens or re-opens x as a file. Not yet implemented.
Ix Integer Returns the integer portion of x.
Lx Length Returns the number of characters in x
Mx Mismatch Returns 1 if ($) is not equal to x, else returns 0.
Nx Numeric Returns the ASCII value of the first character of x
Ox Over Returns 1 if ($) is greater than x, else returns 0.
Qx Equal Returns 1 if ($) is equal to x, else returns 0.
Rx Random Returns a random integer from 0 to x
Ux Under Returns 1 if ($) is less than x, else returns 0.
Vx Invert Returns the inverse of x, i.e. 1/x
Wx While Repeats the next instruction while x is true (i.e. non-zero)
Xx Extract Returns the xth character of ($)


The best way to learn and get a feel for Obfuna is by example. A few very simple examples are shown below.

Hello world

This program simply prints "Hello, world!" and a newline:

!<Hello, world!>

Ask my name

A simple program to ask a question and print the input back out to the screen:

!<Hi, I'm Obfuna. What's your name?>n?
?<Hello, >!n

99 bottles

This runs through the 99 bottles of beer song:

WO1[?($)!< bottles of beer on the wall...>-1]
!<1 bottle of beer on the wall.>

Number guessing game

This simple program prompts the user to guess a random number between 0 and 50 and then guides hir along to the right answer by saying "too low" or "too high".

{assign r to a random number from 0 to 50}

{Repeat the block while ($) is equal to 0}
{Prompt for a guess and store it in g}
?<Make a guess from 0 to 50: >

{Put the original number on top of the array for comparison}

{Make the appropriate comment}
DOg[!<Too low.>]
DUg[!<Too high.>]
DQg[!<Just right!>]

{Put truth value of r=g on top of the array}

Compact version:

rR50(%)0WQ0[?<Make a guess from 0 to 50: >g?($)rDOg[!<Too low.>]DUg[!<Too high.>]DQg[!<Just right!>]($)Qg]

Reversing a string

The next program takes in a string from the user and outputs it backwards. Very useful of course.

{Prompt for and get a string from the user}
?<Enter a string to reverse: >

{Repeat the loop a number of times equal to the length of s}

{Put s on top of the array, chop out just the character we're on}

{Concatenate the result so far onto the end and store the new results as r}

{Increment the counter}

{Print the final result!}

Compact version:

?<Enter a string to reverse: >s?r<>n0(%)rDLs[($)s($)XnCrr($)($)n+1n($)]!r
  Markdown   blog   ploxy   code command line inactive server server   miniserv   code command line inactive server server   Gnowt   code inactive other web   dmenu-notify   code desktop inactive other   Violining a contemptible fellow   blog code git   Break In!   blog games   xmodmap Hints and Tips   blog linux   Black Jack - pick up seven!   blog games   My favourite spoonerisms   words   Good things that have happened in the past week   blog   Good times with git   blog git   It's all Geek to me   blog   Javascript Closures   blog code javascript   Waffle. Move along.   blog   Things I learned today   blog   Hey, at least I'm not rioting   blog   HTML5 and holidays   blog holiday   Blah, cold. Do not want.   blog   Worst.Landlord.Ever   blog   Wheeee   blog   First!!1!one!!eleven   blog