Given the following LISP eval function - what is required to add defmacro?

前端 未结 2 1038
情深已故
情深已故 2020-12-12 13:50

Given the following definition of the LISP eval function - what is required to add the defmacro function? (Or even just evaluate a macro)

(defun         


        
相关标签:
2条回答
  • 2020-12-12 14:35

    This is also quite good: https://web.archive.org/web/20120702032624/http://jlongster.com/2012/02/18/its-not-about-macros-its-about-read.html

    "You can implement a macro system in 30 lines of Lisp. All you need is read, and it's easy." https://gist.github.com/1712455

    0 讨论(0)
  • 2020-12-12 14:46

    The representation of an anonymous macro is by convention a list of the form (macro lambda ...). Try evaling these in your favorite Lisp interpreter (tested in Emacs):

    > (defmacro triple (x) `(+ ,x ,x ,x))

    triple

    > (symbol-function 'triple)

    (macro lambda (x) (\` (+ (\, x) (\, x) (\, x))))

    Although things don't work that way in Emacs, the only thing left to do is to give the adequate semantics to such a form. That is, when eval. sees ((macro lambda (x) EXPR) FORM), it must

    1. Replace every occurence of x in FORM with EXPR without evaluating EXPR first (as opposed to what happens in a function call);
    2. eval. the result of above.

    You can achieve this by adding a clause to the outermost cond in eval. that deals with the ((macro lambda ...) ...) case. Here is a crude prototype:

    ((eq (caar e) 'macro)
         (cond
          ((eq (cadar e) 'lambda)
           (eval. (eval. (car (cffffdar e))
                         (cons (list. (car (caddar e)) (cadr e)) a))
                  a))))
    

    This code only works for single-argument macros. Fixing that involves writing an auxiliary function substlis. that works like evlis. but without looping to eval.; that is left as an exercise to the reader :-)

    To test, define cadr. as a macro thusly:

    (defmacro cadr. (x)
      (list. 'car (list. 'cdr x)))
    

    After this you would have

    > (symbol-function 'cadr.)

    (macro lambda (x) (list. (quote car) (list. (quote cdr) x)))

    You can construct a form that applies this (macro lambda ...) to an expression, and eval that construction within a context that contains a definition for list. (because it is not considered primitive by the eval. interpreter). For instance,

    (let ((e '((macro lambda (x) (list (quote car) (list (quote cdr) x)))
               (cons (quote x) (cons (quote y) nil))))
          (bindings `((list ,(symbol-function 'list.)))))
      (eval. e bindings))
    

    y

    Tada!

    0 讨论(0)
提交回复
热议问题