Run small integer programs with arithmetic, comparisons, if/else branches, while loops, and print statements.
Medium6SimulationImplementationStringStackNo attempts yetTime limit1sMemory limit256 MBWrite an interpreter for the small programming language described below.
The language has a single data type, a 32-bit signed integer.
Operators and statements that read a value as a boolean (!, &&, ||, if, while) treat 0 as false and every other value as true.
Operators that produce a boolean (!, <, <=, >, >=, ==, !=, &&, ||) return 0 for false and 1 for true.
A variable name is a single lowercase letter from a to z. Every variable is initialized to 0 at the start of each program.
Each simple statement (set or print) and each part of a compound statement (if, else, end if, while, end while) sits on its own line. There are no blank lines.
Any amount of whitespace (a space or a tab) may appear before or after a token (an operator, a variable name, a keyword, a constant), but never inside a token. Whitespace is guaranteed only between two adjacent alphanumeric tokens.
A statement is one of the following.
If with else
if expression
statements
else
statements
end if
If expression is true (non-zero), run the statements in the first block. Otherwise run the statements in the second block. A block may be empty.
If without else
if expression
statements
end if
If expression is true (non-zero), run the statements in the block. Otherwise skip the block.
While
while expression
statements
end while
Evaluate expression. If it is true (non-zero), run the statements in the block and go back to the start of the while statement to evaluate expression again. Otherwise skip the block.
Assignment
set name = expression
Set the variable named name to the result of expression.
Output
print expression
Write the result of expression to standard output on its own line.
An expression is a variable name, an integer constant (a sequence of digits whose value is between 0 and 231−1 inclusive), or one of the operations in the table below.
Some operators bind more tightly than others. 1+2*3 is the same as 1+(2*3), while 1*2+3 is the same as (1*2)+3.
Inside one precedence level, binary operators group left to right and unary operators group right to left. 1+2-3 is the same as (1+2)-3, and !-x is the same as !(-x).
| Precedence | Operator | Description |
|---|---|---|
| 7 | () | grouping |
| 6 | - | unary minus (additive inverse) |
! | logical negation (not) | |
| 5 | * | multiplication |
/ | integer division | |
% | modulo (remainder) | |
| 4 | + | addition |
- | subtraction | |
| 3 | < | less than |
<= | less than or equal | |
> | greater than | |
>= | greater than or equal | |
| 2 | == | equal |
!= | not equal | |
| 1 | && | logical and |
| 0 | || | logical or |
Division / truncates the quotient toward zero, and the remainder % takes the sign of the left operand. So -7/2 is -3 and -7%2 is -1.
Every final and intermediate value fits in a 32-bit signed integer, so no overflow check is needed. Division by 0 never happens, for either / or %.
The input holds one or more programs. Each program begins with a line holding the number of lines N (1≤N≤50) in that program, and the next N lines hold the program itself. No line is longer than 100 characters. A line holding 0 ends the input.
Print only the values produced by the print statements of the programs, one per line, in the order the statements run.