Out: Friday, 21 March 2008
Due: 5 PM Friday, 28 March 2008
Submission: Individual
This assignment is to be done individually, not in pairs.
The purpose of this assignment is to explore the behavior of interesting control constructs.
LETREC
language
that evaluates to an absolute value function.
That is, it should be an expression that evaluates
to a procedure that behaves just like the
following Int → Int
function f
:
for all integers n >= 0, f(n) = n f(-n) = n
All remaining tasks on this homework will involve rewriting code written in a particular imperative language that offers interesting control constructs. You must choose whether to do your work in C or in Java.
If you choose to work in C, then your tasks will be modifications to the file mp7.c. If you choose to work in Java, then your tasks will be modifications to the file MP7.java.
Each task consists of changing the definition of a function
and its support routines
so that they do not use any of the following constructs:
{ setjmp
, longjmp
, goto
,
continue
, break
, throw
},
and so that every function has exactly one use of
the return
construct, at the end of the function
definition, e.g.:
int abs(int x) { z = x; if (x < 0) z = -x; return z; }
Your changes must preserve the interface and behavior of the function; if the provided function when given input X produces output Y, then your modified version of it must continue to have the same input/output behavior. However, you may change the interface and behavior of the function's support routines, or even get rid of the support routines entirely, if you desire.
roots
function
so that it has only one use of the return
construct,
rather than the three uses it currently exhibits.
break
construct
from the prod
function.
break
construct
from the find
function.
continue
construct
from the mystery
function.
mp7.c
, remove all uses of the goto
construct
from the gcd
function.
MP7.java
, remove all uses of the
try
, catch
, and throw
construct from
the gcd
function.
mp7.c
, remove all uses of the
setjmp
and longjmp
construct from
the fact_like
function and its support routine,
fact_core
.
MP7.java
, remove all uses of the
try
, catch
, and throw
construct from
the fact_like
function and its support routine,
fact_core
.
You are given the following interpreters:
You are also given the following source code files:
The easiest way to copy the interpreters into one of your own
directories is to use the cp
command on a CCIS Solaris
machine:
cp -r /course/csg111/.www/interps/letrec-lang .
To compile and run mp7.c
on a CCIS Solaris machine:
gcc -o mp7 mp7.c ./mp7
(Don't forget the -o
option, unless you would prefer to
name your executable a.out
.)
To compile and run MP7.java
on a CCIS Solaris machine:
javac MP7.java java MP7
mp7.txt
that contains
your answer for task 1.
mp7.c
,
or MP7.java
, depending on whether you choose to do
tasks 2 through 7 in C or in Java. You must submit either source
code file, but not both. You should make sure that your code
compiles and runs on a CCIS Solaris machine.
mp7-test-output.txt
.
Make sure you double-check that the submission is just plain text
and that it matches the output you see when you run your program.
Last modified: 22 March 2008