changeset 692: |
f51b73f49946 |
parent: |
a0dfde3cb3c4
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Thu, 03 Oct 2024 17:56:11 -0400 |
permissions: |
-rw-r--r-- |
description: |
std/task and tests |
1 ;;; sym.lisp --- Symbol utils 3 ;; inspired by alexandria/symbols.lisp 10 ;; :include '(:with-unique-names :symbolicate :package-symbolicate :keywordicate :gensymify*)) 12 ;; On SBCL, `with-unique-names' is defined under 13 ;; src/code/primordial-extensions.lisp. We use that instead of 15 (eval-when (:compile-toplevel :load-toplevel :execute) 16 (setf (macro-function 'with-gensyms) (macro-function 'with-unique-names))) 18 (declaim (inline ensure-symbol)) 19 (defun ensure-symbol (name &optional (package *package*)) 20 "Returns a symbol with name designated by NAME, accessible in package 21 designated by PACKAGE. If symbol is not already accessible in PACKAGE, it is 22 interned there. Returns a secondary value reflecting the status of the symbol 23 in the package, which matches the secondary return value of INTERN. 27 (ensure-symbol :cons :cl) => cl:cons, :external 29 (intern (string name) package)) 31 (defun maybe-intern (name package) 34 (intern name (if (eq t package) *package* package)) 37 (declaim (inline format-symbol)) 38 (defun format-symbol (package control &rest arguments) 39 "Constructs a string by applying ARGUMENTS to string designator CONTROL as 40 if by FORMAT within WITH-STANDARD-IO-SYNTAX, and then creates a symbol named 43 If PACKAGE is NIL, returns an uninterned symbol, if package is T, returns a 44 symbol interned in the current package, and otherwise returns a symbol 45 interned in the package designated by PACKAGE." 46 (maybe-intern (with-standard-io-syntax 47 (apply #'format nil (string control) arguments)) 50 (defun make-keyword (name) 51 "Interns the string designated by NAME in the KEYWORD package." 52 (intern (string name) :keyword)) 54 (defmacro make-slot-name (name) 56 `(intern ,(string-upcase name) :keyword)) 58 (defun make-gensym (name) 59 "If NAME is a non-negative integer, calls GENSYM using it. Otherwise NAME 60 must be a string designator, in which case calls GENSYM using the designated 61 string as the argument." 62 (gensym (if (typep name '(integer 0)) 66 (defun mkstr (&rest args) 67 (with-output-to-string (s) 68 (dolist (a args) (princ a s)))) 70 (defun symb (&rest args) 71 (values (intern (apply #'mkstr args)))) 73 (sb-ext:with-unlocked-packages (:sb-int) 75 ((sb-kernel:redefinition-warning #'muffle-warning)) 76 (defun make-gensym-list (length &optional (x "G")) 77 "Returns a list of LENGTH gensyms, each generated as if with a call to 78 MAKE-GENSYM, using the second (optional, defaulting to \"G\") 79 argument. This function is implemented in SBCL 80 src/code/primordial-extensions.lisp but re-implemented here. The only 81 difference is that we also handle non-zero integers, which can be 82 passed as the first argument to `gensym'." 83 (let ((g (if (typep x '(integer 0)) x (string x)))) 85 collect (gensym g))))))