Macro-macro.

Started by kinghajj, July 16, 2007, 05:16:24 PM

Previous topic - Next topic

kinghajj


; This macro provides a classic defun.
; If I remember CL correctly, if an argument is prefixed with &, then it is not
; evaluated; that is hom CL makes macros. This macro checks if the argument name
; has a & at the start, and if it does it does not evaluate it. This makes it
; easy if you want to write a macro that needs no evaluate some arguments.
(define-macro (defun _name _args)
(let (_body (args))
; go through to arguments
(dolist (_arg _args)
; evaluate argument unless prefixed with &
(unless (= (first (string _arg)) "&")
(push (list 'eval _arg) _body)))

; create macro function
(set _name (append (lambda-macro) (list _args) _body))))


Example:

(defun test (v1 &v2)
(println "Got " v1 " and " &v2))

(test 37 (+ 40 2))
; => "Got 37 and (+ 40 2)"