mylisp.lisp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (defun print-tag (name alst closingp)
  2. (princ #\<)
  3. (when closingp
  4. (princ #\/))
  5. (princ (string-downcase name))
  6. (mapc (lambda (att)
  7. (format t " ~a=\"~a\"" (string-downcase (car att)) (cdr att)))
  8. alst)
  9. (princ #\>))
  10. (defmacro let1 (var val &body body)
  11. `(let ((,var ,val))
  12. ,@body))
  13. (defmacro split (val yes no)
  14. (let1 g (gensym)
  15. `(let1 ,g ,val
  16. (if ,g
  17. (let ((head (car ,g))
  18. (tail (cdr ,g)))
  19. ,yes)
  20. ,no))))
  21. (defun pairs (lst)
  22. (labels ((f (lst acc)
  23. (split lst
  24. (if tail
  25. (f (cdr tail) (cons (cons head (car tail)) acc))
  26. (reverse acc))
  27. (reverse acc))))
  28. (f lst nil)))
  29. (defmacro tag (name atts &body body)
  30. `(progn (print-tag ',name
  31. (list ,@(mapcar (lambda (x)
  32. `(cons ',(car x) ,(cdr x)))
  33. (pairs atts)))
  34. nil)
  35. ,@body
  36. (print-tag ',name nil t)))
  37. (defmacro svg (width height &body body)
  38. `(tag svg (xmlns "http://www.w3.org/2000/svg"
  39. "xmlns:xlink" "http://www.w3.org/1999/xlink"
  40. height ,height
  41. width ,width)
  42. ,@body))