CSC 530 Assignment 1 -- Operational Semantics of Lisp



ISSUED: Wednesday, 3 April 2002
DUE: Wednesday, 17 April 2002

In this assignment you define the operational semantics for the core of Lisp by writing a Lisp interpreter, in Lisp. As discussed in the Lecture notes and Lisp primer, the function name for the Lisp interpreter is "eval". To distinguish your version of eval from GCL's, yours is named "xeval".

Given below is a shell for the xeval interpreter. All of the functions prefixed with "x" should behave as their Common Lisp counterparts of the same name behave. To get a working interpreter, convert all of the comments into code. Details of how to test and hand in your completed interpreter are given in the file test-files/README, a copy of which is attached.

; Replace all comments with code.  Add a top-level comment here describing your
; general approach to the solution.  Also add explanatory comments above each
; function that you implement, describing your implementation.

(defmacro comment (&rest l) ())

; Evaluate the given sexpr in the context of the given alist.
(defun xeval (sexpr alist)
    (cond
        ( (eq sexpr ':dump) (pprint alist) (list nil alist) )
              ;-- :dump is for debugging purposes only
        ( (numberp sexpr)       (comment -- return numbers as is) )
        ( (stringp sexpr)       (comment -- ditto for strings) )
        ( (eq sexpr t)          (comment -- ditto for t) )
        ( (null sexpr)          (comment -- and ditto for nil) )
        ( (atom sexpr)
            (cond
                ( (comment -- check if atomic sexpr is on the alist)
                  (comment --  and if so use that value) )
                ( t (comment -- otherwise print an "Unbound Variable" error) )
            )
        )
        ( (atom (car sexpr))
            (cond
                ( (eq (car sexpr) 'xquote)
                  (comment -- return quotes as is) )

                ( (eq (car sexpr) 'xsetq)
                  (comment -- call eval-xsetq with appropriate args) )

                ( (eq (car sexpr) 'xcond)
                  (comment -- call eval-xcond with appropriate args) )

                ( (eq (car sexpr) 'xdefun)
                  (comment -- call eval-xdefun with appropriate args) )

                ( (comment -- if alist value of (car sexpr) is a function body)
                  (comment -- then call xapply to evaluate it) )

                ( t (comment -- call eval-with-gcl for the rest) )

            )
        )
        ( t (comment -- what if (car sexpr) is not an atom?) )
    )
)

(defun eval-xsetq (atom-name value alist)
    (comment -- bind name to value on alist))

(defun eval-xcond (sexpr alist)
    (comment -- evaluate a Lisp conditional))

(defun eval-xdefun (fn-name formals fn-body alist)
    (comment -- add fn-name to alist along with its formal parms and body))

(defun xapply (fn-body formals actuals alist)
    (comment -- bind args and xeval fn-body))

(defun xbind (formals actuals alist)
    (comment -- extend the alist with new bindings (called from xapply)))

(defun xeval-list (sexpr-list alist)
    (comment -- x-evaluate a list of sexprs returning the value of the last
        (called from eval-xcond and xapply))
)

(defun eval-with-gcl (sexpr alist)
    (comment -- if sexpr is a function xevaluate each arg and then use gcl
    to eval.  If sexpr is not a function send sexpr directly to gcl.)
)

(defun xeval-list-list (sexpr-list alist)
    (comment -- x-evaluate a list of sexprs returning a list of quoted values
        (called from eval-with-gcl))
)

(comment -- add additional auxiliary function you may need)


Description of Assignment 1 Testing Files

Here is a description of the files in the assignments/1/test-files directory:

File Description
xeval-outline.l This is the shell for the xeval function, exactly as it appears on the handout for assignment 1. Copy this file into a file named xeval.l and use it as the basis for your solution.
xeval-tests.xl This is the actual test data file that your completed version of xeval should read and evaluate. See below for further details on how to read it in.
read-xeval-print.l This is the utility file for testing your xeval function interactively. Use the function defined in this file to read interactive test data while you're debugging your solution. See the comments at the top of the file for further details.
readfile-xeval-print.l This is the utility for reading the test data file when you're ready to hand in the final result. See the comments at the top of the file for further details.
xeval-test-demo This is what your correct test output script should look like, except for the comments and the exact format of the alist dump. This file was generated by running a completed version of the assignment. Regarding the alist dump (the result of the :dump directive), the format of your alist may be different than the one that appears here, but the semantics must be the same, such that all of the other output results are the same as those in this demo file.