REVERSE The programming language

Most programming languages execute statements in a top-to-bottom fashion. REVERSE takes program flow literally. REVERSE executes statements from top to bottom and from bottom to top. There are no IFs, FORs or any other kind of loop or selection statement. The only flow of control statement is REVERSE, which reverses program flow.


Statements in general

Statements in REVERSE contain no whitespace. The statements in a REVERSE program are delimited by whitespace. Spaces, tabs and returns can all be used to delimit REVERSE statements. For example:
statement1 statement2 statement3 statement4
statement5 statement6 statement7 statement8

is equivalent to:
statement1
statement2
statement3
statement4
statement5
statement6
statement7
statement8


Variables and Modifiers

There are three types of variables in REVERSE. There are integer variables (type V), floating point variables (type W) and character variables (type X). A variable's name must be a string of at least two alphabetic characters (A-Z,a-z) with the first letter V, W, or X. The first letter in the variable's name identifies its type. A variable's type may not be changed. Integer variables and floating point variables are signed. Character variables are integers ranging from 0 to 127, mapping to the ASCII codes.

There are none of the common operators in REVERSE. Mathematics in REVERSE is performed by the use of variable modifiers. Modifiers are similar to the assignment operators (+=, *=, etc) in Java. There are six modifiers in REVERSE: add on (+), subtract (-), multiply (*), divide (/), exponent(^), remainder(%). Modifiers take two arguments, the variable to modify and the modify quantity. The modify quantity can be any variable, including the variable to modify, a constant, or even another modifier statement. If the modify quantity is another modifier statement, then the statements are executed from right to left. Examples:
If VA=15, VB=3 and VC=2
VA+VB would modify the value of VA to 18
VA-VB would modify the value of VA to 12.
VA/VB would modify the value of VA to 5.
VB*VB would modify the value of VB to 9.
VC^VB would modify the value of VC to 8.
VB%VC would modify the value of VB to 1.
VA+VB*VC would first modify the value of VB to 6 and then would modify the value of VA to 21.
VB+VB+VB would first modify the value of VB to 6 and then modify it to 12, not 9.

If a variable is undefined prior to its use in a statement, then the variable has a value of zero. Example:
If VA=3 and VB was not defined yet, then VA*VB would modify VA to 0.

Modifier statements with variables of different types are perfectly valid. The value is cast into the type of variable which is being modified. If a floating point value is cast into a integer, truncation is performed. If a regular integer is cast into a character, modular arithmetic is performed. Examples:
If VA=10, WB=3.14, XC=65 (the character A) and VD=300:
VA+WB would modify the value of VA to 13
WB+VA would modify the value of WB to 13.14
VA+XC would modify the value of VA to 75
XC+VA would modify the value of XC to 75 (the character K)
WB+XC would modify the value of WB to 68.14
XC+WB would modify the value of XC to 68 (the character D)
VD+XC would modify the value of VD to 365
XC+VD would modify the value of XC to 109 (the character m)

A note on remainder(%): If a floating point variable is involved, the result is zero.


Input and Output

REVERSE has one input command: GET and one output command: PUT. The type of data inputted/outputted is the same type of data as the variable argument of the command. The syntax for the I/O commands is the command name followed by the variable name (no space). Example:
GETVA
VA*4
PUTVA

This program segment accepts an integer input from the user and prints out four times the integer's value.
Note: Numeric values (integers and floats) are prepended with a space.


Program Flow

A REVERSE program which is running top to bottom is said to be running south. A REVERSE program which is running bottom to top is said to be running north. REVERSE programs always starts from the top and run south. A REVERSE program runs until it reaches either end of the program. If the program is running south and the bottom is reached, the program stops, similarly if the program is running north and the top is reached, the program stops.

The REVERSE command reverses the flow of the program when it is encountered. If the program is running south before, then it will run north upon execution of the REVERSE command. Example:
PUTVA
VA+5
VB+2
VA-VB+1
REVERSE
PUTVB

After the fourth statement is executed, VA=2 and VB=3. When the REVERSE command is executed in statement 5, program flow reverses (now runs north) and the next line to be executed is the fourth statement. When finished, VA=3 and VB=6 and the program prints out '6'. In this short program, the sixth statement is never reached. Also note, the fourth line is executed immediately before AND after the REVERSE command.

The SKIP command skips the next statement to be executed. The above program printed '0' when it started. This bug can be fixed with a SKIP as follows:
SKIP
PUTVA
VA+5
VB+2
VA-VB+1
REVERSE
PUTVB

The first statement executed after the SKIP is VA+5. When going north (after the REVERSE) in the program, there is no statement after SKIP, so the program ends. If there was just one statement after the skip, that statement would not get executed and the program would stop. Example:
GETVA
SKIP
PUTVA
VA*2
REVERSE

This program segment accepts an integer input from the user and prints out four times the integer's value just like the program in the Input and Output section. When the SKIP is reached for the second time, the GETVA command is skipped, and since there are no other statements left to execute, the program ends.


Making Decisions

The command REVERSE has another form which acts as the only conditional command in REVERSE. In this form, REVERSE is appended by one of six comparators followed by a variable. If the result of the comparator is true, then program flow is reversed, otherwise the program moves on to the next statement. The comparators are as follows:
< result is true if the variable is greater than 0
!< result is true if the variable is not greater than 0
> result is true if the variable is less than 0
!> result is true if the variable is not less than 0
= result is true if the variable is equal to 0
!= result is true if the variable is not equal to 0

The program below accepts a floating point value from the user and prints the absolute value.
GETWA
SKIP
REVERSE
VB+0
SKIP
WA*-1
REVERSE>WA
PUTWA

The fifth statement checks to see if WA is positive or negative. If WA is positive, the program keeps flowing south, if not then the flow is reversed so that WA can be multiplied by -1. Notice the statement VB+0. The statement is a filler so that the SKIP statement can be used to multiply WA by -1 exactly once. Without the SKIP and filler, WA would be multiplied by -1 twice.

The following program accepts an integer input from the user and counts down to 0, and when finished, it prints the sum of the numbers it printed. If zero or a negative input is submitted, then 0 is printed.
GETVA
SKIP
REVERSE
VC+0
SKIP
VA-VA
REVERSE>VA
VB+VA
SKIP
REVERSE
VB+VA
SKIP
VA-1
VC+0
SKIP
PUTVA
REVERSE<VA
VB/2
PUTVB


Questions? Email me at brianscsmith AT yahoo DOT com. 1