Take the 2-minute tour ×
Theoretical Computer Science Stack Exchange is a question and answer site for theoretical computer scientists and researchers in related fields. It's 100% free, no registration required.

In this paper McCarthy sets out the following primitives: QUOTE, CAR, CDR, CONS, EQUAL, ATOM, COND, LAMBDA and LABEL.

He then sets up the following abbreviations: T, NIL, DEFUN, NULL, CADR, LIST, AND, ALT, and SUBST.

He rounds it up with an incredible definition of the EVAL function used in the reader.

Given the above set of LISP primitives, is it possible to extend the EVAL function to evaluate defmacro?

share|improve this question
1  
The book: Lisp in Small Pieces by Christian Queinnec presents an implementation of macros in Scheme that might be useful. –  Dave Clarke Dec 7 '12 at 13:04
add comment

2 Answers 2

up vote 1 down vote accepted

Yes, in fact it's quite similar to evaluating DEFUN. Of course, in the paper EVAL has no rule for evaluating DEFUN, since it's treated as an abbreviation. From the definition of DEFMACRO in emacs LISP (link):

Special Form: defmacro name argument-list body-forms...

defmacro defines the symbol name as a macro that looks like this:

(macro lambda argument-list . body-forms)

It's easier if we ignore the name of the macro and treat that as an abbreviation. Then we have simply to create the list (macro lambda argument-list . body-forms) and set name to be shorthand for that list, the same way a (defun name (arguments) (body)) sets name as a shorthand for (lambda (arguments) (body)). As for evaluating a macro, that is also easy --- just pass the function inside the macro your arguments, but WITHOUT evaluating them first.

share|improve this answer
    
Are you saying that defmacro would need to be implemented by both a modification to eval and a new primitve? (Being similar to defun) –  hawkeye Sep 7 '10 at 11:05
    
The name binding part would need a new primitive, or however it's actually handled in eval --- since the paper treats the name-binding part of defun as an abbreviation. AFAIK, an interpreter keeps a lookup table with variable bindings, and defun etc. will add an entry to that table saying "this variable is now bound to this function". So, if you ignore the name-binding part, you need only modify eval. –  Evgenij Thorstensen Sep 7 '10 at 11:31
add comment

This shows macro expansion as a 'code template' to solve this problem.

share|improve this answer
    
link is not a good answer. Could you please expand this? Thanks. –  Kaveh Dec 6 '12 at 7:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.