Functional Programming Counts

No attempts yetTime limit1sMemory limit128 MB

Problem

You must implement an interpreter for a very small functional language. The interpreter reads expressions, assignments, and function definitions one line at a time from the input. Function definitions are remembered and can be reused in later expressions; an expression is evaluated as soon as it is read, and its result is printed. The only data type in this language is the integer.

Input

The input is a series of lines, each holding one action. No line has more than 100 characters. The kinds of actions are:

  1. <expression> — Evaluate the expression to an integer and print the result.
  2. set <identifier> = <expression> — An assignment. <identifier> names a variable. If the name has not been seen before, create a new variable with the value of the expression; otherwise update the existing variable.
  3. def <identifier> ( <parameter> ) = <expression> — Define a function <identifier> with a single integer parameter. <parameter> is a <number> or an <identifier>. A number defines the function for that particular argument value; an identifier is the general definition for every value. The order of definition lines matters. All definitions of a function are kept, and a call scans them in input order for the first parameter match (a numeric definition matches when the argument equals its value; an identifier definition always matches). As a result, a definition's behaviour can never be changed once set.
  4. profile — For each defined function, report the number of calls charged to each definition line since the last profile (or since the start of the program). See the output format below.
  5. exit — The end of input.

Process one line at a time, performing each action immediately, until the exit line is reached. The only data type is the integer, and every value is always in the range -1,000,000 to 1,000,000 (inclusive).

The expression grammar is as follows, with the usual operator precedence (evaluate factors first, then terms, and finally expressions):

  • An <expression> is a sum (+) or difference (-) of <term>s, evaluated left to right (e.g. a + b - 3 + 4 - c).
  • A <term> is a product (*), quotient (/), or remainder (%) of <factor>s, evaluated left to right (e.g. 3 * 4 / 5 % x). Division is integer division, and remainder is never used with negative numbers.
  • A <factor> is a <number>; an <identifier> that is a variable or the parameter of the current function; a function call of the form <identifier>(<expression>); or a parenthesised (<expression>).
  • A <number> is a decimal value from 0 to 1,000,000 (inclusive).
  • An <identifier> is a string of upper- and lower-case letters and is case-sensitive (e.g. A and a are different variables). The reserved words def, set, profile, and exit may not be used as identifiers.

Whitespace is ignored except where it separates words. You may assume that all input is syntactically correct, that every function call terminates, and that every expression produces a result within the allowed range. The total number of calls charged to any single definition line of a function never exceeds 1,000,000.

Output

Print each expression result on its own line, prefixed by >> (two greater-than signs and a single space).

Print the profile result as one line per function, in the form <identifier> calls: n1 n2 n3 ... => nt, where ni is the number of calls on the i-th definition line (in input order) and nt is the total number of calls to that function (all separated by single spaces). Functions are printed in the order they were first defined.