changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/std/sym.lisp

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
2 
3 ;; inspired by alexandria/symbols.lisp
4 
5 ;;; Code:
6 (in-package :std/sym)
7 
8 ;;(std::reexport-from
9 ;; :sb-int
10 ;; :include '(:with-unique-names :symbolicate :package-symbolicate :keywordicate :gensymify*))
11 
12 ;; On SBCL, `with-unique-names' is defined under
13 ;; src/code/primordial-extensions.lisp. We use that instead of
14 ;; defining our own.
15 (eval-when (:compile-toplevel :load-toplevel :execute)
16  (setf (macro-function 'with-gensyms) (macro-function 'with-unique-names)))
17 
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.
24 
25 Example:
26 
27  (ensure-symbol :cons :cl) => cl:cons, :external
28 "
29  (intern (string name) package))
30 
31 (defun maybe-intern (name package)
32  (values
33  (if package
34  (intern name (if (eq t package) *package* package))
35  (make-symbol name))))
36 
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
41 by that string.
42 
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))
48  package))
49 
50 (defun make-keyword (name)
51  "Interns the string designated by NAME in the KEYWORD package."
52  (intern (string name) :keyword))
53 
54 (defmacro make-slot-name (name)
55  "make slot-name"
56  `(intern ,(string-upcase name) :keyword))
57 
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))
63  name
64  (string name))))
65 
66 (defun mkstr (&rest args)
67  (with-output-to-string (s)
68  (dolist (a args) (princ a s))))
69 
70 (defun symb (&rest args)
71  (values (intern (apply #'mkstr args))))
72 
73 (sb-ext:with-unlocked-packages (:sb-int)
74  (handler-bind
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))))
84  (loop repeat length
85  collect (gensym g))))))