For this homework, you will add exceptions and exception handling to
Cool. An instance of class Throwable or a subclass can be
thrown by using the syntax
throw
, an expression that does not return (and has any
type, that is ``native'' type). An expression evaluation
can be
protected with
handlers
using the syntax:
try {
} catch {
}.
Each handler has the same syntax as a
case branch. As with a match expression, the branch that most
closely matches the thrown object will be executed. As is usual in
languages with exception handling, the handlers are dynamically
scoped.
If no exception is thrown during the evaluation of
, the result of
the protected evaluation is the same as the evaluation of
,
otherwise if there is an exception thrown which is caught by a
handler, the result of the handler is the result of the protected
evaluation.
Errors that previously would terminate execution (such as dispatch on void, string index bad, etc) should now throw exceptions. Throwing null should cause a null pointer exception to be thrown instead. Furthermore, an uncaught exception should be printed to stdout and the program terminates. For more information on this requirement, see below.
The throw keyword should have lower precedence than any
operator except the semicolon (;) operator.
This extension mainly affects the code generation and runtime system, but also has effects on the front end. You will first need to formalize the syntax and static semantics of the extension before you implement it. Write the grammar changes and new type axioms and turn them in for a milestone by November 4th.
Normally one would also need to change the intermediate representation. This task has already been done for you, there are two new ``constructors'' for the new constructs:
constructor handle(body : Expression; cases: Cases) : Expression; constructor raise(exception : Expression) : Expression;The semantic analyzer and code generator will need to define routines for these new classes of nodes.
Obviously, the code generator will need to be changed, but
the run-time system will need to be changed as well to raise exceptions upon
errors rather than aborting, and
to catch and print uncaught
exceptions (and abort execution). A string should be printed
by calling the dynamic dispatch of toString on the exception object.
Get copies of the required files from the homework's source directory:
weise.cs: make -f /afs/cs.uwm.edu/users/classes/cs854/src/homework5/MakefileAs well as the source code (which is editable), you also receive several links to shared files:
Here are the sizes of the diffs (diff oldX X | wc), counting both deleted and added lines (changed lines are counted twice) plus one for every deleted or added block, with changed blocks counting twice again:
The file frag.s in the $CLASSHOME/src/homework5
directory contains assembly code to help you generate exception
strings. Part of the code is alternate code for Int.toString.