Machine Problem 7: Control Constructs

Out: Friday, 21 March 2008

Due: 5 PM Friday, 28 March 2008

Submission: Individual

This assignment is to be done individually, not in pairs.

Purpose

The purpose of this assignment is to explore the behavior of interesting control constructs.

Tasks

  1. [3pts] Write an expression in the 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
    

    Hint: if while walking you come to a fork in the road and do not know whether to turn left or right, wouldn't it be nice if you could walk down both the left and the right paths simultaneously, and not worry about having to choose which way to go?

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.

  1. [2pts] Change the roots function so that it has only one use of the return construct, rather than the three uses it currently exhibits.
  2. [2pts] Remove all uses of the break construct from the prod function.
  3. [2pts] Remove all uses of the break construct from the find function.
  4. [2pts] Remove all uses of the continue construct from the mystery function.
  5. [2pts]
    • For mp7.c, remove all uses of the goto construct from the gcd function.
    • For MP7.java, remove all uses of the try, catch, and throw construct from the gcd function.
  6. [2pts]
    • For mp7.c, remove all uses of the setjmp and longjmp construct from the fact_like function and its support routine, fact_core.
    • For MP7.java, remove all uses of the try, catch, and throw construct from the fact_like function and its support routine, fact_core.

Infrastructure

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

Deliverables

  1. A text file mp7.txt that contains your answer for task 1.
  2. Exactly one of the files 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.
  3. The output obtained by running your second deliverable on a CCIS Solaris machine, in a file named 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.
  4. A Development Diary

Back to Machine Problems

Felix S Klock II

Last modified: 22 March 2008

Valid XHTML 1.0!