Skip to content

Integer Arithmetics Language

Implement an interpreter, preferably in Java (Python is also acceptable), for a simple XML-based programming language for integer arithmetics. The language supports three types of statements: var to define a new variable, add to add integers, and print to print to the standard output stream (i.e. System.out in Java). An example program written in the language appears below.

<program>
  <var name="a" value="5"/>
  <var name="b" value="6"/>
  <var name="c"/>
  <add n1="a" n2="b" to="c"/>
  <print n="c"/>
</program>

Executing this program should print 11 to the standard output stream. You can make any assumptions you see fit for the language above.

Scope

Please note that your program should not only be able to execute the program above; it should work with any program that contains such commands. For example, executing the program below should print 14. Your solution should also include error handling behaviour to address cases where e.g. a variable is defined multiple times, an undefined variable is used in an add/print statement.

<program>
  <var name="a" value="5" />
  <var name="b" value="6" />
  <var name="c" />
  <var name="d" />
  <add n1="a" n2="b" to="c" />
  <add n1="c" n2="3" to="d" />
  <print n="d" />
</program>

Libraries

You are allowed (and strongly encouraged) to use existing built-in/open-source libraries to parse XML documents instead of developing your own XML parser.