PL/Cool
Time Limit: 10 Seconds Memory Limit: 32768 KB
The new IMB compiler "Unvisual Age for PL/Cool" is planned for release next month. Your task in this problem is to write the interpreter for the PL/Cool language so that the compiler programmers could test their product.
Program on PL/Cool can contain expressions. Each expression can contain integer constants and variables. The following operations are allowed: + (add), - (substract), * (multiply), / (divide), % (modulo), and ^ (raise). All operations have their usual meaning and priority ([^] > [*] = [/] = [%] > [+] = [-]), all operations except ^ are evaluated from left to right, ^ is evaluated from right to left. Parenthesis can be used to change the order of evaluation, as usually. Unary minus and plus are allowed, their priority in this case is the highest.
Divide operation acts like integer division: first both operands are replaced with their absolute values, next integer division is performed, remainder is dropped, and finally the sign of the quotient is set equal to the sign of the true quotient of the operands. Taking modulo is perfomed the same way, except that the quotient is dropped and remainder is kept. For example, (80 + 4 * 75)=(56 - 2^2^3) = -1.
Program may contain variables. Variable name starts with a letter, which may be followed by letters and digits. Variable names are case insensitive. Maximal name length is 10 characters.
PL/Cool has two operators: print and define. Program is the sequence of the operators, one on a line.
Operator print has the following syntax: print
Operator define has the following syntax:
Here
the value of 2 + 2 is 8.
Note that substitution is performed recursively, until some undefined constant is met, for example the following sequence of operators prints 8 and 10:
define 3 2
print 3+3
define 4 5
print 3+3
An attempt to redefine some variable or constant is ignored. An attempt to make a definition that leads to circular dependence is also ignored. For example, the following sequence prints 4 and 4, because the last two definitions are ignored.
define 3 2
define 2 5
print 3
define 4 2
print 3
If some identifier is used in the expression that is not yet defined to be substituted by some integer constant, its value is set to zero. For example, the following operator prints 3:
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
Input file contains the program on PL/Cool. All numbers in the expressions, including numbers that occur during expression evaluation, do not exceed 109 by their absolute value. The total number of define operators does not exceed 30000, the total number of print operators does not exceed 2000, the length of each line does not exceed 200 characters.
In case of division denominator is never zero, zero is not raised to zero power and power exponent is always non-negative.
Output
Output file must contain the output of all print operators in the order they are executed, one on a line.
Sample Input
1 print (80+4*75)/(56-2^2^3) print 30/-1 print 31%-5 print 0^3 print 2+2 define 2 3 print 2+2 define 3 x print 2+2 define x 2 print 2+2 define 2 5 print 2+2 define x 7 print 2+2
Sample Output
-1 -30 -1 0 4 6 0 0 0 14Submit
Source: Andrew Stankevich's Contest #3