This page describes a bonus implementation exercise of ICL-2025 lab
2. The goal is to implement the extensions to the While
language described in the pen-and-paper exercises
sheet. In particular, we want to extend the the big-step interpreter
to cope with repeat..until loop and pairs. This exercise
also serves as a warm up for next week lab, where you have to implement
an interpreter for a more serious programming language.
To help you building this extensions, we provide the basic structure
(as a set of Java files together with Makefile), to be
downloaded here: while_java.tar.gz.
Once uncompressed (with tar zxvf while_java.tar.gz), you
get a directory while_java/. This project contains a
package whileLang with the following files:
| *.java |
set of files encoding the abstract syntax of While language
(complete)
|
| Interp.java | interpreter, implementing the Visitor pattern (to be completed) |
| Main.java | main file (complete) |
| Makefile | to automate the building process (complete) |
The code compiles, using make all, but it is incomplete.
You have to complete Interp.java.
To run your solution do make test.
This exercises proposes you to add the cases of pair construction and
projections to the interpreter, as described in the interface
Visitor.java. In particular, you shall provide
implementation for the methods
Value interp (Epair e);
Value interp (Efst e);
Value interp (Esnd e);contained in the Interp.java. Recall that the
implementation of the interpreter visitor provides a method for each
construction of the While language abstract syntax and
stores the environment as an instance variable. In particular, the three
above methods return the value that corresponds to the result of fully
evaluating such an expression.
Complete the Value interp (Epair e) method of the
Interp.java class. An object e of class
Epair stands for a pair with components e1 and
e2, not yet fully-evaluated expressions.
Complete the Value interp (Efst e) method of the
Interp.java class. An object e of class
Efst stands for a call to the fst projection,
returning the first component of expression e, which should
thus evaluate into a Vpair.
Complete the Value interp (Esnd e) method of the
Interp.java class. An object e of class
Esnd stands for a call to the snd projection,
returning the second component of expression e, which
should thus evaluate into a Vpair.
This exercises proposes you to add the case of evaluation of the
repeat..until construction to the interpreter, as described
in the interface Visitor.java. In particular, you shall
provide implementation for the method
void interp (Erepeat s);contained in the Interp.java. This method returns
void, meaning it locally changes the state of the
environment store in an Interp instance. Since the
environment is represented as mutable data structure (an hash map),
there is no need to return its value. One just needs to access the
environment instance variable (which in reality stores a
pointer).
Complete the void interp (Srepeat s) method of the
Interp.java class. This method stands for the use of a
repeat s until e construction, meaning that one should
execute first statement s and then repeat its execution
until e evaluates down to the Vbool(true)
value.
[Hint]: as noted in the pen-and-paper exercises sheet, the
repeat..until construction does not really extend the
expressiveness of the While language. Hence, you are free
to define the interpretation of such a loop by translating it into a
previously existing statement. To do so, it might come in handy to use
the unary negation to get the opposite of a Boolean value. This
operation is already included in the language:
Unop.java contains the negation unary
operation;Eunop (in file Eunop.java) extends
class Expr and stands for a call of a unary operation on an
pure expression;