help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Compiling a recursive macro


From: Jakub Jankiewicz
Subject: Re: Compiling a recursive macro
Date: Fri, 12 Jun 2020 02:03:14 +0200


On Thu, 11 Jun 2020 16:15:05 -0400
Douglas Lewan <d.lewan2000@gmail.com> wrote:

> I'm not an expert on macros whatsoever. I apologize if this is idiotic.
> 
> I've been doing battle trying to define a recursive macro, and I'd like 
> it to be byte compiled. However, in the info (elisp) Compiling Macros, I 
> find the following:
> 
>         In order for compilation of macro calls to work, the macros must
>     already be defined in Lisp when the calls to them are compiled.
> 
> That suggests to me that you can't compile any recursive macro. Do I 
> understand it correctly? If I do, then I'm curious if there's a common 
> style for getting around this.


Here is example of recursive macro:

(defmacro case_ (val &rest list)
  "(case value
        ((<items>) result1)
        ((<items>) result2)
        [else result3])

   Macro for switch case statement. It test if value is any of the item. If
   item match the value it will return coresponding result expression value.
   If no value match and there is else it will return that result."
  (if (listp list)
      (let* ((item (car list))
             (first (car item))
             (result (cadr item))
             (rest (cdr list))
             (value (gensym)))
        `(let ((,value ,val))
           (if (member ,value ',first)
               ,result
               ,(if (and (listp rest)
                         (eq (caar rest) 'else))
                    'xxx
                    (if (not (null rest))
                        `(case_ ,value ,@rest))))))
      nil))

(case_ (+ 1 2)
  ((1 2) 'foo)
  ((4 3) 'bar))


it's converted from Scheme code for my LIPS interpteter.

--
Jakub Jankiewicz, Web Developer
https://jcubic.pl/me

Attachment: pgpnhBdePE024.pgp
Description: Podpis cyfrowy OpenPGP


reply via email to

[Prev in Thread] Current Thread [Next in Thread]