Skip to content

Instantly share code, notes, and snippets.

@lagagain
Last active December 11, 2018 12:50
Show Gist options
  • Save lagagain/e1d8bdb977792f50f0a567c2502f0569 to your computer and use it in GitHub Desktop.
Save lagagain/e1d8bdb977792f50f0a567c2502f0569 to your computer and use it in GitHub Desktop.
自制@裝飾器
(defun Decorators (stream ch1)
(declare (ignore ch1))
(let ((Decorator (read stream))
(the-func-form (read stream)))
(unless (eq (first the-func-form) 'defun)
(error "need 'defun"))
(let ((the-func-name (second the-func-form))
(the-func-args (third the-func-form))
(the-func (cdddr the-func-form)))
`(flet ((old-func ,the-func-args ,@the-func))
(defun ,the-func-name ,the-func-args
(funcall (,Decorator #'old-func) ,@the-func-args))))))
(defun print-hello (func)
(print 'hello)
func)
(defun hello-name (name)
(format t "~&Hello, ~A~%" name))
(hello-name "john")
(funcall (print-hello #'hello-name) "john")
(set-macro-character #\@
#'Decorators)
@print-hello
(defun hello-world ()
(format t "~&Hello, world~%"))
#| output the result
(print (with-input-from-string (s "print-hello
(defun hello-world ()
(format t \"~&Hello, world~%\"))")
(Decorators s #\@)))
|#
(hello-world)
@print-hello
(defun show-num (num)
(print num))
(show-num 10)
#|
(print (with-input-from-string (s "print-hello
(defun show-num (num)
(print num))")
(Decorators s #\@)))
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment