#P3695. CYaRon! Language Interpreter

    ID: 16946 Type: Default 1000ms 256MiB

CYaRon! Language Interpreter

CYaRon! Language Interpreter

Your task is to implement an interpreter for the CYaRon! language. The language is a toy imperative language with a few special commands. Each variable is initially \(0\) and variable names only contain English letters. The language supports:

  • :yosoro which outputs the evaluated value of an expression followed by a space. For example, :yosoro 2 outputs 2 .
  • :set which assigns a value to a variable or an array element. Only the plus and minus operators are allowed in expressions (e.g. :set chika, 1 or :set ruby[i], i+1).
  • A declaration block using { vars where variables (of type int) and one-dimensional arrays (declared as array[int, L..R]) are defined. For arrays the indices range from L to R (inclusive) and are automatically initialized to 0.
  • Conditional execution using the ihu block. The header is in the format { ihu op, expr1, expr2 where op is one of \(eq\) (\(==\)), \(neq\) (\(!=\)), \(lt\) (\(\)), \(le\) (\(=\)). The block executes only if the condition holds.
  • For-loops using the hor block. Its header is like { hor i, start, end. The loop variable i takes all integer values from the evaluated start to the evaluated end (inclusive), and the block is executed once for each value.
  • While-loops using the while block with a header similar to { while op, expr1, expr2 which continues looping as long as the condition (evaluated with the given operator) holds.

An expression in this language may be an integer literal, a variable name, an array element (using the format a[i]) or a combination using addition and subtraction. Note that if an expression contains an array access, its index expression will be a simple expression (e.g. i or i+2) and will not be nested inside another array access.

Your interpreter should read a CYaRon! program from the standard input, execute it, and output the result exactly as described (make sure there is a trailing newline in the output).

inputFormat

The input consists of several lines containing a valid CYaRon! program. Lines starting with # are comments and should be ignored. The program may include declaration blocks, output commands, variable assignments, and control structures (ihu, hor, and while blocks). The last line of the program is always a newline.

outputFormat

Output the result generated by executing the given CYaRon! code. Each :yosoro statement prints a number followed by a space. The final output should match exactly, including a trailing newline at the end.

sample

{ vars
    chika:int
    you:int
    ruby:array[int, 1..2]
    i:int
}
:yosoro 2
:set chika, 1
:set you, 2
:yosoro chika + you
{ ihu eq, chika, 1
    :set you, 3
    :yosoro 1
}
{ hor i, 1, you
    :yosoro i
}
:set i, 1
{ while le, i, 2
    :yosoro i
    :set ruby[i], i+1
    :yosoro ruby[i]
    :set i, i+1
}
2 3 1 1 2 3 1 2 2 3

</p>