Compiler Construction 2015
Computer Laboratory
University of Cambridge
Timothy G. Griffin (tgg22@cam.ac.uk)
Practical Exercises, Set 1. (For slang1_interpret.)
There are many possible extensions or modifications
possible for the Slang.1 that be fun programming
(nested comments, better verbose output, etc).
However, I'll list only those exercises that I feel
reinforce the main points of the lectures.
They are listed from easiest to hardest.
) In this simple language, do we really need the
return types of functions? Could we always infer them?
) Add division to the arithmetic operations. How will
you handle division by zero?
) We have blurred the distinction between the defining
language (OCaml) and the defined language (Slang.1)
in several ways. For example, Slang.1 integers are
the ints supplied by OCaml on your machine. Now
suppose that Slang.1 only allows 16-bit integers,
where overflow or underflow should raise a
run-time error. Can you modify the implementation
to match this? Are there additional build-in functions
that you might want to add to such a language?
) Add mutual recursion to Slang1. Such as
let g(x : int) : int = ... f(e) ...
and f(z :int) : int = .... g(e') ...
in ... end
This requires careful treatment of environments!
) Consider adding references to Slang.1. We could do
this in several ways, from simplest to most complex:
--- Allow references to be used only locally.
--- Allow refs to be passed to functions and
then updated by the called function.
--- Allow refs to be returned by functions
Can interp_0.ml be extended to handle each of these?