CS 450 Homework 10
Compilation!

Carl Offner
Spring 2022

Due Tuesday, May 17, 5:00 PM

In this assignment you will make some modifications to the compiler presented in Chapter 5 of the text to make it suitable for compiling a larger and more respectable subset of Scheme. In particular, you will be able to compile the metacircular evaluator of Chapter 4.

Start by copying from your hw9 directory into your new hw10 directory the following files:

     regsim.scm
     syntax.scm
     eceval-support.scm
(You don't need eceval.scm.) Then copy from ~offner/cs450/hw10  into your hw10 directory the following files:
     compiler.scm
     compiler-shell.scm
     machine-shell.scm

You may make modifications to any or all of these files. (Well actually, you won't need to make any changes at all to compiler-shell.scm, and I would be astonished if you needed to make any changes to machine-shell.scm. So you can pretty much forget about modifying those two files.) I will collect all of them, and in addition I will collect the usual file

 notes.txt 

In doing these problems, you will want to add more Scheme primitives to the ones already supported by the compiler. You add them simply in eceval-support.scm to primitive-procedures; each new primitive is a one-line addition to that list.

Just to be clear about this: the primitive procedures you add in this way are names of procedures that the compiler (which is, after all, compiling Scheme code) will recognize in the Scheme code it is compiling as being Scheme primitive procedures. So for instance, if you are compiling a Scheme program that includes the Scheme primitive not, then you would want to add not to the list of primitive-procedures in eceval-support.scm. And don't add them to primitive-procedures in s450.scm! s450.scm is just the program you will be compiling. Dont make any changes to it. The reason for this is that the primitive-procedures in s450.scm are what that Scheme interpreter will recognise as Scheme primitive procedures in what it (i.e., the Scheme interpreter s450.scm) is interpreting. This is a bit confusing at first, I know.

Also please note that there are some changes in the compiler that I have given you from the compiler in the book, principally in the lists of modified registers that are passed around.


  1. Add support in the compiler for let and or. You can implement these simply as syntax transformations: just add procedures let->appl and or->if to syntax.scm and then use these procedures in the dispatch procedure of the compiler. Do it as follows:

    This is entirely analogous to the way the compiler handles cond by transforming it into a set of nested if clauses using the syntax procedure cond->if. The main dispatch procedure of the compiler recognizes each cond expression and invokes cond->if, passing the result to compile. Of course you may also need some supporting functions for let->appl and or->if in syntax.scm. Add whatever you need, and put them in syntax.scm.

    And please look up the definitions of let and or in R5RS to make sure you have really implemented the Scheme definitions of these procedures. You will undoubtedly be surprised at what you find. If you don't do this, I can almost guarantee that your compiler will not pass my tests.

  2. Extend the compiler so you can compile the metacircular evaluator from Chapter 4. I have placed a very slightly modified version of that evaluator in ~offner/cs450/hw10/s450.scm  which you should link to. (Or copy it into your own directory. But I'm not going to collect it; I'll use my own copy. So don't even think of editing it.) This is the version I want you to be able to compile. The main difference between this version of s450.scm and the original one is that after the metacircular evaluator is loaded it starts executing without waiting to be invoked. This is necessary because otherwise machine-shell.scm exits immediately to the underlying Scheme.

    This is essentially Exercise 5.50 on page 610 of the textbook. It's fairly difficult, so let me give you some advice:

    When you finish this, you should be able to compile s450.scm into s450.cmp and execute that file using machine-shell.scm to get a running Scheme interpreter. Of course, as the book points out, because of the multiple levels of interpretation going on, this interpreter will run rather slowly, but it should run well, and you should experiment with it to see that it does.

  3. In notes.txt please describe clearly how you approached this work, any particular difficulties or decisions you had to make and how you resolved them. In addition, if you have any thoughts on additional things you would like to have done (if you had time, I know...) please put them in also.

    Here's something to think about: I wrote above that apply and map cannot be handled by syntax transformations. Can you explain why that is true? If you can, put your reasoning in notes.txt.