Macro and array crossing
- by Thomas
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)?