4Head

From Esolang
Jump to navigation Jump to search

4Head (pronounced "fore•head") is a brainfuck-inspired programming language. The additions on top of brainfuck are: there is now a register called "hold", which holds a cell value and is the only value that can be directly operated upon. Memory is operated upon by loading a cell from memory into the hold register, doing an operation, and putting the value back into memory. In the source code, if a decimal number 'N' comes right before a +/- instruction or a </> instruction, then that instruction is done 'N' times. This is just a shortcut for compact programs. Also, the main memory is layed out as 2D (but the program text is still a 1D sequence of characters). The memory cells could be of any size, but 16-bit 2's complement integers are good. Non-command characters should be ignored and not cause errors.

Correspondence with brainfuck

Any brainfuck program can be rewritten into an equivalent 4Head program. One straightforward way of doing this is by doing the following for each +/-/./, instruction:

  1. Insert a @ before it
  2. Insert a ! after it

This means that 4Head is a superset of brainfuck, thus it is Turing-complete.

Instructions

Instruction table
Instruction character Program effect
> move right in memory
< move left in memory
^ move up in memory
v move down in memory
+ increment hold
- decrement hold
@ "fetch" - write current value in memory into hold
! "store" - write current value in hold into memory
% swap hold value with currently selected memory value
? read input into hold
. write output from hold
[ loop begin using hold
] loop end using hold

Hello world program

Hello, world!
@8+![#>@4+![>@2+!>@3+!>@3+!>@+!4<@-!@]>@+!>@+!>@-!2>@+![<@]<@-!@]2>@.>@3-!@.@7+!@.@.@3+!@.2>@.<@-!@. 
<@.@3+!@.@6-!.@8-!@.2>@+!@.>@2+!@.

Implemenations