Macro and array crossing

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2011-01-16T16:27:48Z Indexed on 2011/01/16 16:53 UTC
Read the original article Hit count: 226

Filed under:
|
|

I am having a problem with a lisp macro. I would like to create a macro which generate a switch case according to an array.

Here is the code to generate the switch-case:

(defun split-elem(val)
    `(,(car val) ',(cdr val)))

(defmacro generate-switch-case (var opts)
  `(case ,var
     ,(mapcar #'split-elem opts)))

I can use it with a code like this:

(generate-switch-case onevar ((a . A) (b . B)))

But when I try to do something like this:

(defparameter *operators* '((+ . OPERATOR-PLUS) (- . OPERATOR-MINUS)
                                                (/ . OPERATOR-DIVIDE)
                                                (= . OPERATOR-EQUAL)
                                                (* . OPERATOR-MULT)))
(defmacro tokenize (data ops)
  (let ((sym (string->list data)))
        (mapcan (lambda (x) (generate-switch-case x ops)) sym)))


(tokenize data *operators*)

I got this error: *** - MAPCAR: A proper list must not end with OPS. But I don't understand why.

When I print the type of ops I get SYMBOL I was expecting CONS, is it related?

Also, for my function tokenize how many times the lambda is evaluated (or the macro expanded)?

© Stack Overflow or respective owner

Related posts about macros

Related posts about common-lisp