This page is still under construction.

Parts of this page are still being built. What you see may change.

Interpreter

Time limit1sMemory limit256 MB

Summary
Run small integer programs with arithmetic, comparisons, if/else branches, while loops, and print statements.
Level

Medium6 of 10

Topics
Simulation, Implementation, String, Stack
Solved
No attempts yet

Problem

Write an interpreter for the small programming language described below.

Data types

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.

Variables

A variable name is a single lowercase letter from a to z. Every variable is initialized to 0 at the start of each program.

Whitespace

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.

Statements

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.

Expressions

An expression is a variable name, an integer constant (a sequence of digits whose value is between 0 and 231−12^{31}-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).

PrecedenceOperatorDescription
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 %.

Input

The input holds one or more programs. Each program begins with a line holding the number of lines NN (1≤N≤501 \le N \le 50) in that program, and the next NN lines hold the program itself. No line is longer than 100 characters. A line holding 0 ends the input.

Output

Print only the values produced by the print statements of the programs, one per line, in the order the statements run.

Examples2

  1. Example 1

    Input
    11
        set n = 22
        set s = 1
        while n > 1
            if n % 2 == 0
                set n = n / 2
            else
                set n = 3 * n + 1
            end if
            set s = s + 1
        end while
        print s
    1
        print ((2*4-6/3)*(3*5+8/4))-(2+3)+1/2*0
    0
    
    Expected output
    16
    97
    
  2. Example 2

    Input
    6
    print -7/2
    print 7/-2
    print -7%2
    print 7%-2
    print -7/-2
    print -8/2*3
    0
    
    Expected output
    -3
    -3
    -1
    1
    3
    -12