Science Fair Project Encyclopedia
THRAT
THRAT is an esoteric programming language written by Matthew Varga (2004). THRAT is based on Brainfuck's instruction set and uses only two tokens to access entries in a table of 10 opcodes. The programmer can only move the table pointer forward and select entries within the table.
| Contents |
Technical description
THRAT uses two language instructions to reference and select entries in an op-table. The op-table is basically a list of Brainfuck instructions with two additional instructions, discussed later. The table pointer is controlled by incrementing the pointer's position and then selecting instructions from the table. There is no instruction to traverse backwards in the table so the only way to get to the beginning of the table is to traverse all the way to the end to where it wraps around to the first index. The semi-colon (;) is used to increment the pointer's position and the colon (:) is used the select the instruction being pointed to by the instruction pointer. Each instruction read from file is referenced with the instruction table and then the selected instruction is placed into an execution vector. Once all the instructions have been read in, referenced and pushed, the vector is executed much the same way Brainfuck's instructions are executed. When the program is first executed, the instruction pointer is set the beginning of the table.
Like Brainfuck, you are given a memory block in which you can do whatever you please. The default memory block size in THRAT is 4096 bytes, and the set minimum is 1024 bytes. More memory can be allocated by using a interpreter/compiler flag. You are given a single implicit pointer set the beginning of a memory block which has been initialized to zero. Moving out of the bounds of the block has an undefined effect on the program's execution
Instruction table
| ID
| Instruction | Brainfuck
|
| 0
| Halt Program Execution | Not Defined
|
| 1
| Increase Pointer Value | +
|
| 2
| Decrease Pointer Value | -
|
| 3
| Increase Pointer Position | >
|
| 4
| Decrease Pointer Position | <
|
| 5
| Begin While Loop | [
|
| 6
| End While Loop | ]
|
| 7
| Output pointer's value as integer | Not Defined
|
| 8
| Output pointer's value as ASCII | .
|
| 9
| Input a single byte and store at pointer's position | ,
|
- Increase instruction pointer's position: ;
- Select instruction: :
Note: The table wraps around. So when the IP (instruction pointer) reaches the end it will jump back to element zero.
Sample code
Here is a sample program that prints "Hi".
Prints 'Hi' In THRAT --------------------- ;::::::::::::::::::: ::::::::::::::::::::: ::::::::::::::::::::: :::::::::::;;;;;;;:;; ;;;:;;;;;;;;::::::::: ::::::::::::::::::::: ::::::::::::::::::::: ::::::::::::::::::::: ::::::::::::::::::::: ::::::::::::;;;;;;;:
Converting Brainfuck to THRAT
Here is a simple Perl script to convert Brainfuck files to THRAT:
#!/usr/bin/perl
%c=('+'=>1,'-'=>2,'>'=>3,'<'=>4,'['=>5,']'=>6,'.'=>8,','=>9);$n=10;$p=0;
while(<>){for(split ''){if(exists $c{$_}){$o=$c{$_};$d=$o-$p;if($d<0)
{$d+=$n;}print';'x$d;print':';$p=$o;}}}
Usage is very simple and unixy:
./bf2thrat.pl foo.bf > foo.thr
External links
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details


