Lab 01

Part 0: Lab Setup

Start by setting up your environment as described. Familiarize yourself with the Tool Programming Language and the Tool Compiler Project.

Part 1: Your first Tool programs

Write two example Tool programs each (4 per group of 2) and make sure you can compile them using the Tool Reference Compiler. Please be creative when writing your programs. We don't need 5 versions of a program computing the Fibonacci sequence. The examples at the end of the Tool page should convince you that you can write interesting programs.

Remember that you will use these programs in the remaining of the semester to test your compiler, so don't make them too trivial! Try to test many features of the language.

Part 2: An Interpreter for Tool

Complete the various methods of the Evaluator class so that it “runs” the program passed to it. Print the output straight to the console. Add as many helper methods and fields as you need in the same file. Keep the following in mind:

  • You can assume the program is correct:
    1. no undefined variables/fields being accessed
    2. the program type-checks
    3. referenced methods and classes are correctly defined
    4. The main method does not define/access variables, and it does not access this.

Your interpreter is free to crash with runtime errors in case any of the above is violated. It should not crash on valid programs.

Note: For now, you will work with compiled versions of Trees. You can find their API here:$

Implementation skeleton

For this lab, we provide you with a compiled version of the Trees, as well as a functioning parser. You have to complete the implementation of your Evaluator.

If you have followed Labs Setup, you should have a working Eclipse project with a stub implementation, containing the following files:

  • src/main/scala/toolc/eval/Evaluator.scala contains a partially implemented interpreter/evaluator
  • src/main/scala/toolc/Main.scala contains the main method which runs the interpreter on a given input file
  • The programs directory contains some example programs on which you can try your implementation.
  • lib/toolc-parser_2.11-2.0.jar contains a compiled lexer and parser, as well as all the trees.

Notice that all classes are in a toolc package. toolc.Main is the entry point.

You will have to complete the implementation of two methods: evalExpr and evalStmt. Whenever the program contains ???, you should replace it with a proper implementation. A few examples are already implemented to help you get started.


Please replace your findMethod defined as:

  def findMethod(cd: ClassDecl, name: String): MethodDecl = {
    cd.methods.find( == name).orElse(
      cd.parent.flatMap(p => findClass(p.value).methods.find( == name))
    ).getOrElse(fatal("Unknown method ""."+name))

to this version:

  def findMethod(cd: ClassDecl, name: String): MethodDecl = {
    cd.methods.find( == name).orElse( => findMethod(findClass(p.value), name))
    ).getOrElse(fatal("Unknown method ""."+name))


On programs/Pi.tool for instance, your interpreter should produce the following output:

  First method
  0/1 ~= 0.0000000000
  47/15 ~= 3.1333333333
  102913/32760 ~= 3.1414224664
  5454389/1739700 ~= 3.1352468816
  Second method


You are given 2 weeks for this assignment.

Deadline: Tuesday, Sep. 30, 23.59pm.

You must use the interface on as follows:

  1. Click on the “Deliverables” tab
  2. Click “Deliver this version” on the commit corresponding to the version of your code you want to submit
  • End of Chapter 1 in the Tiger Book presents a similar problem for another mini-language. A comparison of the implementation of ASTs in Java (as shown in the book) and Scala is instructive.