Macro-macro.

For the Compleat Fan
Locked
kinghajj
Posts: 30
Joined: Sun Jul 15, 2007 2:37 pm

Macro-macro.

Post by kinghajj »

Code: Select all

; 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:

Code: Select all

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

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

Locked