Skip to content

Instantly share code, notes, and snippets.

@sogaiu
Forked from r-moeritz/pretty-literals.lisp
Created May 3, 2021 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sogaiu/6dc23be582693b938b936c15e8fbce04 to your computer and use it in GitHub Desktop.
Save sogaiu/6dc23be582693b938b936c15e8fbce04 to your computer and use it in GitHub Desktop.
pretty hash table & vector literal syntax for common lisp
;;;; pretty-literals.lisp - pretty hash table & vector literal syntax
;;;; inspired by and uses code from http://frank.kank.net/essays/hash.html
(in-package #:pretty-literals)
;; vector literal syntax using brackets
(set-macro-character #\[
(lambda (str char)
(declare (ignore char))
(let ((*readtable* (copy-readtable *readtable* nil))
(keep-going t))
(set-macro-character #\] (lambda (stream char)
(declare (ignore char) (ignore stream))
(setf keep-going nil)))
(let ((items (loop for value = (read str nil nil t)
while keep-going
collect value)))
(coerce items 'vector)))))
;; hash-table literal syntax using braces
(set-macro-character #\{
(lambda (str char)
(declare (ignore char))
(let ((*readtable* (copy-readtable *readtable* nil))
(keep-going t))
(set-macro-character #\} (lambda (stream char)
(declare (ignore char) (ignore stream))
(setf keep-going nil)))
(let ((pairs (loop for key = (read str nil nil t)
while keep-going
for value = (read str nil nil t)
collect (list key value)))
(retn (gensym)))
`(let ((,retn (make-hash-table :test #'equal)))
,@(mapcar
(lambda (pair)
`(setf (gethash ,(car pair) ,retn) ,(cadr pair)))
pairs)
,retn)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment