LooPy is a terminal program for stepwise simulation of basic LOOP/WHILE programs found in theoretical computer science. It is written purely in python.
LooPy only requires Python 3.7+ and the Lark parser library
The program syntax LooPy uses is designed to closely mirror that of the formal notation for LOOP programs. It supports the basic operations of addition, subtraction, looping and concatenation, as well as convenience operations that can be simulated by this basic set.
x999 := 1; # x<n> is variable n
X0 := 2; # case insensitive
x1 := 5; # Assign Constant
x2 := x1; # Assign Variabe
x1 := x1 + 5; # Add Constant
x2 := x1 + x2; # Add Variable
x1 := x1 - 5; # Subtract Constant
x2 := x1 - x2; # Subtract Variable
x1 := x1 * 5; # Multiply by Constant
x2 := x1 * x2; # Multiply by Variable
# Analogue: Whole Number Division, Modulo
x1 := x1 / 2;
x1 := x1 % 2;
LOOP x1 DO x1 := x1 + 5 END; # Loop
LOOP x1 DO # Nested Loop
LooP x1 dO # case insensitive
x1
:= x1
* 2;
eNd
END
IF x1 = 0 DO x1 := 5 END # If conditional
# x1 != 0
# x1 < 1
# x1 <= 1
# x1 > 1
# x1 >= 1
WHILE x1 != 0 DO x1 := x1 - 1; x2 := x2 + 5 END; # While Loop
WHILE x7 <= 5 DO x7 := x7 + 1 END; # Different conditional
x0 := x2 # ! No trailing ';' for last statement
# Comment # Comment
If the input program is malformed, LooPy will output an error message pointing to where the parsing failed.
You can run loopy on a program with
python3 loopy.py [options] [program file] [variable assignments]
[program file]
is the text file in which you have saved your loop program.
[variable assignments]
are expressions of the form <int>=<int>, e.g. 1=12, where the first number is the variable index to be assigned, and the second number is the value to assign to it.
[options]
LooPy currently has the following options
--no_render
skip TUI, directly output value in x_0--all_vals
output all referenced variables as TSV instead of only x_0
On the left, there’s the register column which lists all variables which have been referenced, as well as their current values. Variables which were changed during the last instruction are highlighted in bold.
To the right of that is the program view, which shows the current program, with the currently executing statement highlighted.
At the top right, the number of steps taken and the current instruction type are shown. At the bottom right is the current page number. LooPy will automatically switch pages as the execution moves past the border of the current page.
You can control program execution using several keys:
Key | Behaviour | |
---|---|---|
N | Next | Step forward to next instruction |
S | to Step | Skip forward to n-th instruction (prompts for number) |
L | to Line | Skip forward to n-th line (prompts for number) |
E | to End | Skip forward to results screen |
As LooPy supports WHILE
loops, it is possible to write programs that do not terminate. Whilst this is correct and corresponds to ‘no result’, it does get boring watching a while loop iterate forever, or to not watch by trying to skip forward to the end of the program.
This is why LooPy has a limit to the number of times a while loop may execute. It is set in the variable MAX_ITERATIONS
and set to 1e6 by default, but this may be changed by users with really intense while loops, or much more patience than I have.
If a while loop reaches this limit, it’ll break and the program will return -1, and display ⊥ as the value of x0 in the result screen.
Once the program has finished executing, LooPy will display a results screen showing the final output and variable values.