Program for a Program
Every programmer, at some point, feels the urge to write a compiler for a programming language. If you're one of them, here's your chance to create an interpreter for a language we'll call **Syu**. It's a dynamically typed, C-like language with the following features:
1) The program consists of a set of functions. Functions can be called before they are defined, so function prototypes are included. A function is defined as:
"' name(argument1, argument2, ...) block "'
Function prototype:
"' name(argument1, argument2, ...); "'
The program execution begins with the 'main' function. If 'main' is absent and there's only one function, that function is executed.
2) Block:
"' statement1; statement2; ... "'
A block returns the value of its last statement.
3) Statements can be:
a) Function call:
"' functionName(param1, param2, ...) "'
b) Assignment:
"' var1 = var2 = ... = varN = expression; "'
c) Expression: Follows standard mathematical rules and may include ternary operators.
d) Ternary operator:
"' expression1 ? expression2 : expression3 "'
If 'expression1' is true, the result is 'expression2'; otherwise, it's 'expression3'.
e) Conditional operator:
"' if (expression1) block1 else block2 "'
Similar to a ternary operator. The 'else' part may be omitted.
f) Loops:
I) 'for' loop:
"' for (init; condition; increment) block "'
Executes 'init' first. While 'condition' is true, 'block' is executed, followed by 'increment'.
II) 'while' loop:
"' while (condition) block "'
Executes 'block' as long as 'condition' is true.
III) 'do-while' loop:
"' do block while (condition) "'
Executes 'block' first, then checks 'condition'.
g) Return statement:
"' return expression; "'
Returns the value of 'expression' and ends the function execution.
h) Break statement:
"' break; "'
Exits a loop or conditional block. It may return a value.
i) String:
"' "characters" "'
j) Character:
"' 'character' "'
k) Comparison operators: '>', '<', '>=', '<=', '!=', '==', '||' (logical OR), '' (logical AND).
l) Assignment operators: '+=', '-=', '*=', '/=', 'm) Name: A sequence of Latin letters, digits, and underscores. The first character cannot be a digit.
**Note:**
1) The program is syntactically correct and performs no more than 1,000,000 iterations.
2) If logical errors occur, the following messages should be displayed:
a) If there's no 'main' function and more than one function exists, display "Ambiguity in function calling."
b) If a variable is accessed before it's defined, display "No such variable."
c) If a function is called before it's defined, display "No such function."
d) If mathematical operations can't be performed with the given values, display "Incompatible types." All operations are numeric, except '+' and '*' for strings, which perform concatenation. Concatenating a number and a string results in the number being converted to a string. Other operations are not allowed.
e) If division by zero occurs, display "Division by zero."
3) All operations, except '=', are executed left-to-right (with equal priorities).
Input
The input file contains a program written in the described language.
Output
The output file contains the result of the program's execution (specifically, what the 'print' functions output) or the first error encountered, if the program fails to execute.