MiniPig

From Esolang
Jump to: navigation, search

Minipig, or MP, is an esoteric programming language created by User:Challenger5. It is a minimalistic stack-based language that was pretty much designed in under two hours. Its only literal is 1, and its only operation is subtraction. However, it can still represent, albeit in an extremely verbose manner, any integer. Gotos in this language have two quirks:

  • They are relative, so this pseudocode:
flag
goto -1

is an infinite loop.

  • There are no conditional gotos. If you want to make decisions on the program flow, you preform subtraction on stack elements.

The former decision was made to save pain, and the latter was made to exponentially increase it.

Execution Model

The language has two stacks and one register, k. The stacks can hold unbounded integers.

Syntax

Every command has both a symbolic and letter form. They can be used interchangeably within a program.

Format:
a b  description

u ^  pop->k
d v  push k
s /  swap the first two elements
S ;  switch working stacks (all stack operations are preformed on the current working stack.)
l 1  push the number 1 onto the stack
m -  pop->a, pop->b, push b-a
i [  input a number
o ]  output a number
I {  input a character and push its ASCII value
O }  pop a number and output its corresponding ASCII character
r %  reverse the stack
f *  nothing (used as a flag)
g >  pop->n, do nothing if n==0, go back -n flag characters if n<0, go forward n flag characters if n>0

Examples

Useful Subprograms

duplicate a number, preserving the register value:

;v;^vv;^;

negate a number:

11-/-

add the two numbers on top of the stack:

11-/-/-11-/-

multiply a number by 2, preserving the register value:

;v;^vv;^;11-/-/-11-/-

pop and print a number, followed by a newline:

]11-1-1-1-1-1-1-1-1-1-1-11-/-}

rotate the stack forwards, preserving the register value:

;v;%^%v;^;

rotate the stack backwards, preserving the register value:

;v;^%v%;^;

Fibonacci Sequence

111-*^v;v;/^v;v11-/-/-11-/-^vv]
11-1-1-1-1-1-1-1-1-1-1-11-/-}
;^;^;v11-1->

Tips

  • Writing letters can get tricky. For example, this:
1
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
1
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
^vv11-/-/-11-/-
-1-1-1-1-1-1-1-1-1-1-1-
}

prints the letter e. It is recommended that you keep a copy of the numbers 65 (A) and 97 (a) at the bottom of the stack where you can dupe and use them.

  • Use the symbolic versions of instructions, not the letter versions. The symbolic versions better represent the actions they preform.

Computational Class

Assuming that the > instruction throws an exception and stops the program when given an invalid flag, it is not known if one can write a loop that stops after a specified, finite number of iterations, or after a condition is met. If such a loop can be written, it would be very difficult. The addition of an abs instruction would make it almost certainly Turing-complete, because (x+|x|)/2 is x iff x>=0, and 0 otherwise.

Implementation

An implementation can be found here. Note that it does not support the { instruction because it is written in Python, but an implementation in C should work.