Skip to content

Instantly share code, notes, and snippets.

@lagagain

lagagain/Decorators.lisp

Last active Dec 11, 2018
Embed
What would you like to do?
自制@裝飾器
(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
You can’t perform that action at this time.