# Hyperfunge

**Hyperfunge** is a Befunge-related language that stores its program in a 5:4 pentagonal hyperbolic grid. Each program instruction is contained in a pentagon, with 4 pentagons around each grid corner. This causes the grid to expand exponentially as you get further away from the starting point.

Because of this space expansion, Hyperfunge programs must be stored in a tree form. To do this, they are constructed around a middle line: the 2 vertical lines in the middle of the program are represented literally and do not have any space expansion. From there, as you move left or right from this middle line, space expands.

For pentagons on the right side of the middle line, each pentagon to the right leads to 3 pentagons (the top one on the side, then the middle one from the corner, then the bottom one on the side), unless it was reached from a corner, in which case it leads to 5 pentagons (top on the side, mid-top on the corner, middle on the side, mid-bottom on the corner, bottom on the side). Pentagons on the left side of the middle line behave similarly (but branch on the left, of course).

## Formatting

In text form, the line to the right of the middle is uniquely indicated by the ";" character, and other successive cells to the right are ordered by layers:

; 1abcXYZdefghUVW

- The "1" cell is located just to the right of the middle line. It has 3 cells to its right: "a", "b", "c".
- The "a" cell is reached from the side, so it has 3 to its right: "X", "Y", "Z".
- The "b" cell is reached from a corner, so it has 5 cells to its right: "d", "e", "f", "g", "h".
- The "c" cell is reached from the side and has 3 cells to its right: "U", "V", "W".
- The cells "X", "Z", "d", "f", "h", "U", "W" are reached from sides so they have 3 cells to their right
- The cells "Y", "e", "g", "V" are reached from corners so they have 5 cells to their right.
- For left side cells, this representation is flipped:

; WVUhgfedZYXcba1

## Commands

Command | Description |
---|---|

`;` |
This must be on first line and indicates which column of the text file is the one to the right of the middle line. (NOP if executed) Also, execution starts here and proceeds downwards. The data pointer also starts here. |

`v` |
Go downwards (as defined on the column right of the middle line). |

`^` |
Go upwards (reverse from going downwards). |

`>` |
Go right-upwards to the top cell. |

`<` |
Go leftwards (reverse from going right-upwards). |

`]` |
Go right-downwards to the bottom cell. |

`?` |
Random direction (pick one of the 5 directions). |

`_` |
Horizontal if: pop number, go right-upwards if zero, leftwards if nonzero. |

`|` |
Vertical if: pop number, go down if zero, up if nonzero. |

`#` |
Bridge: jump over next command. |

`0123456789` |
Push numbers 0..9 onto the stack. |

`"` |
Toggle string mode - push following characters onto stack until next ". |

`+` |
Add the two top numbers on the stack. |

`-` |
Subtract the two top numbers on the stack. |

`*` |
Multiply the two top numbers on the stack. |

`/` |
Divide the two top numbers on the stack. |

`%` |
Modulo the two top numbers on the stack. |

`!` |
Logical not: replace the top number on the stack with 1 if it was 0, or with 0 otherwise. |

``` |
Greater than: pop two numbers, push 1 if b>a, otherwise 0. |

`:` |
Duplicate the top number on the stack. |

`$` |
Discard the top number on the stack. |

`\` |
Swap the top 2 numbers on the stack. |

`~` |
Input character, push it on the stack. |

`,` |
Pop number from the stack and output corresponding ASCII character. |

`&` |
Input formatted number from, push it on the stack. |

`.` |
Pop number from the stack and output as formatted number. |

`@` |
End program. |

`m` |
Move data pointer: pop 2 numbers x y. Move data pointer x spaces right-upwards, then y spaces downwards. |

`g` |
Get character from program at the position where the data pointer is, push onto stack. |

`p` |
Pop a number from the stack and put the corresponding character in the program where the data pointer is. |