Battle Turing's Custom Language

Tags:

#battle turing #custom programming language

By: Aaron Jencks

Views: 441
Posted on:

March 17, 2023, 1:47 a.m.

0 👍 0 👎

For Battle Turing players need to be able to write scripts that describe the operation of their turing machines. This is done using a custom language that is compiled using a custom compiler written in Python.

Basic Operation

The language implements the basic operations for turing machines with a few additions to help with the ease of coding. The basic operations include

  • Moving to the left/right
  • Reading from the tape
  • Writing to the tape
  • Accepting the string
  • Rejecting the string
  • Halting

For the last three elements there are special caveats:

Accepting/Rejecting: Because player implemented turing machine take place on a single tape that lasts for more than a single string, this will be localized to a section of the tape that is defined by the code for the turing machine Halting: This is to indicate that the turing machine is finished running for the current battle, there isn't really a reason why this would be used.

Extra Operations

In addition to the basic operation for turing machines, the language also implements simple loops, conditionals, labels (kind of), and gotos. Comments are also supported, represented by #.

Syntax

The syntax for Battle Turing's custom language is as follows:

Moving

To move left and right you can either use << >> or left and right. These by default move a single space, but if followed immediately by a number then the machine will shift by that number of cells.

ie:

<<;      # Move left 1 cell
left 3;  # Move left 3 cells
>> 4;     # Move right 4 cells
right;  # Move right 1 cell

Writing

To write to the tape use the write command followed by a literal. For example write 'a' would write the literal a to the tape at the current position.

Reading

To read from the tape you can use one of two special operands ^ or read. These can only be used in conditional expressions and do not work on their own.

ie:

if(= ^ 'b') ...
if(!= read 'c') ...
while(!= read 'd') ...

Flow Control

There are a few different types of flow control, you can use if/else statements, or looping

If/Else

Simple if statement example

if(exp) {
  # 0 or more statements
}

Simple if else statement example

if(exp) {
  # 0 or more statements
}
else {
  # 0 or more statements
}

Looping

There are two types of loops

while loop:

while(exp) {
  # 0 or more statements
}

do-while loop

do {
  # 0 or more statements
} while(exp);

Expressions

You may have noticed that above for flow control the commands simply use the keyword exp in place of conditional expressions. This exp keyword is not actually a command, or keyword, it's short of expression, Battle Turing supports basic expressions for boolean evaluation. It's important to note that expressions in Battle Turing use prefix-notation, which means the syntax is op lhs rhs where op is the operator, lhs is the left argument, and rhs is the right argument.

Comparison

  • =: equals
  • <: less than
  • >: greater than
  • <=: less than or equals
  • >=: greater than or equals
  • !=: not equal

Logical Operators

  • &: logical AND
  • |: logical OR

Labels and Gotos

Similar to function calls you can implement labels and gotos in Battle Turing's custom language. When a label is called its statement block is executed and then execution returns to the position of the goto command, this is a bit different than normal labels. You cannot pass parameters or arguments to the labels, this is on purpose.

A label looks something like this:

myLabel: {
  # 0 or more statements
}

And then to call that function, you'd use:

goto myLabel;

A full example

print_name:
{
    write 'A';
    right 1;write 'a';
    right 1;write 'r';
    right 1;write 'o'; 
    right 1;write 'n';
    right 1;
}
goto print_name;
right 5;
left 20;
goto print_name;
while(= read '0') { write '1'; right 1; }
halt;

Comments

You are not logged in, please log in here to comment

Current Comments

aaron:

hewoo

Posted on:

Nov. 17, 2024, 8:55 p.m.

0 👍 0 👎