# Headache

Jump to: navigation, search

WORK IN PROGRESS

## Commands

Headache is a double stack based language with only a few commands. It has no other way to store data than its two stacks. Both stacks start empty. You can only work with one stack at a time.

It is called headache because of the amount of switching, restacking, moving, and shuffling needed to get anything done.

A peek from an empty stack produces 1. A pop from an empty stack produces 0.

```<    Pushes a pop from Stack B onto Stack A (top value of b is moved to a)
>    Pushes a peek from Stack A onto Stack B (copies top of a onto b)
^    Discards the top value of Stack A. (pop if it matters)
v    Swaps the top values of Stack A and Stack B. (pop both)
+    a = a + b (adds top of b to the top of a) (peek only)
-    a = a - b (subtracts top of b from the top of a) (peek only)
.    Pop value from Stack A to output
,    Push value from input to Stack A

@    Switch Stacks (a is now b, and b is now a)

{    if a == b, Jump to after corresponding } (peek)
}    if a != b, Jump to after corresponding { (peek)

LAME COMMANDS:
!    Places a 1 on the top of stack A.
#    Places a number with the size of each stack on top of the corresponding stack.
```

The stacks usually hold 32 bit integers. If there is no corresponding { or }, ignore it.

Adding from an empty stack, produces a 1. Since it is peeking. If say, top of Stack A is 4, and Stack B is empty, a + will produce 5. If both are empty, a + will push 1 to Stack A.

#### Hello World (Branchless)

There is likely a more optimal way

```+>+>+>+@^@+>@^^@v<@+++++++v<++>@<.+++v@<v@<@-><><.+++++++>>><<<..+++><.<>+<
v@<@+++><.>++<v@<@-<<@v<v<@+.><.+++..-.@^^+.
```

#### GNU Cat

```+vv{,.}
```

#### Multiply Program

Takes the numbers on the top of both the stacks and multiplies them. Places the multiplied number on top of Stack A. Consumes both numbers.

```!>-><<@<<<{@<<@v<v<@-<<<+@<<v@<v@<<}^@^<@^^@
```

## Useful snippets (Branchless)

These all work on Stack A, unless otherwise specified.

Swap top 2 values

```Shorter >^v<
Faster  @<v@<
```

Copy the top values of both stacks

```><@><@
```

#### Invert N

Invert top 3 values

```@<v<v@<v<
```

Invert top 4 values

```@<v<v<v@<v<v@<v@<<
```

Invert top 5 values

```@<v<v<v<v@<v<v<v@<v<v@<v<<
```

Invert top 6 values

```@<v<v<v<v<v@<v<v<v<v@<v<v<v@<v<v@<v@<<<
```

#### Bring to Top

These are usually shorter than the inversions

Bring 3rd from top value to top (top 2 )

```@<<@v<v<
```

Bring 4th from top value to top (top 2 3 )

```@<<<@v<v<v<
```

Bring 5th from top value to top (top 2 3 4 )

```@<<<<@v<v<v<v<
```

Bring 6th from top value to top (top 2 3 4 5 )

```@<<<<<@v<v<v<v<v<
```

## Conversion from brainfuck

This simple conversion of BF to headache demonstrates that this subset of the language is Turing complete.

```Brainfuck  Headache
Init       <
>          <
<          >^
+          !>^+<^
-          !>^-<^
[          !>-<^>^{<^
]          !>-<^>^}<^
.          ><.
,          ^,
```

## Links

See also - Another Headache Language