Write a "desk calculator" that evaluates expressions over scalars and 3D vectors. The calculator reads statements, one per line. Each statement is either an assignment or an expression.
set v = <expression>. It evaluates the expression and stores the result in variable v, overwriting any previous value. It produces no output.Statements are processed in the order in which they appear.
Kinds of value. There are exactly two kinds of value: scalars (a single integer) and vectors (three integers). Every vector is three-dimensional and its components are integers. All integer values and intermediate results fit in 32-bit signed integers.
Variables. A variable name is a single lower-case letter (a–z).
Literals. Integer literals are always non-negative; use the unary minus operator to obtain a negative value.
Operators.
* — multiplication: a scalar times a scalar, a scalar times a vector, or a vector times a scalar (a vector is scaled component by component).+ — addition of two scalars or two vectors.- — binary subtraction of two scalars or two vectors.- — unary minus, negating a scalar or a vector.. — dot product of two vectors, producing a scalar.X — cross product of two vectors, producing a vector; this is the upper-case letter X.The unary minus operator may not appear immediately after another operator. For example, 2 + - 3 is not allowed; it must be written as 2 - 3.
Precedence and associativity.
*, ., X) have equal precedence and associate left to right.+, binary -, and unary -) have equal precedence and associate left to right.Parentheses and vectors. Three bracket styles are available: ( ), [ ], and { }. A bracketed group either controls the order of evaluation (as in ordinary arithmetic) or, when it contains exactly three comma-separated expressions, denotes a vector. For example, {1, 2, 3} is a vector, whereas (2 + 3) is a parenthesised scalar.
Normally each opening bracket is closed by a closing bracket of the same style. There is one exception: a closing bracket closes every still-open group back to, and including, the nearest opening bracket of its own style.
For example,
( 2 + ( a + [ b + [ c + [ d + e ) )
is a valid expression and is equivalent to
( 2 + ( a + [ b + [ c + [ d + e ] ] ] ) )
because the first ) matches back to the ( just before a, implicitly closing the three open [ groups.
The input is a series of lines, each holding one statement (an assignment or an expression). Within a line, tokens may be preceded, separated, or followed by any number of spaces (including none). No line is longer than 500 characters. The input ends with a line containing only the word done. Every input line holds a valid statement.
For each expression statement (not for assignments) print its value on its own line. A scalar result is printed as the integer alone, with no surrounding spaces. A vector result is printed as (x,y,z): three integers separated by commas, with no spaces.