| 1 |
;;; |
| 2 |
;;; libsym.scm - built-in symbol and keyword procedures |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(select-module gauche.internal) |
| 35 |
(inline-stub |
| 36 |
(.include "gauche/priv/configP.h" |
| 37 |
"gauche/vminsn.h" |
| 38 |
"gauche/priv/identifierP.h")) |
| 39 |
|
| 40 |
;;; |
| 41 |
;;; Symbols |
| 42 |
;;; |
| 43 |
|
| 44 |
(select-module scheme) |
| 45 |
(define-cproc symbol? (obj) ::<boolean> :fast-flonum :constant |
| 46 |
(inliner SYMBOLP) SCM_SYMBOLP) |
| 47 |
(define-cproc symbol->string (obj::<symbol>) :constant |
| 48 |
(return (SCM_OBJ (SCM_SYMBOL_NAME obj)))) |
| 49 |
(define-cproc string->symbol (obj::<string>) :constant Scm_Intern) |
| 50 |
|
| 51 |
(select-module gauche) |
| 52 |
(define-cproc gensym (:optional (prefix::<string>? #f)) Scm_Gensym) |
| 53 |
(define-cproc symbol-interned? (s::<symbol>) ::<boolean> SCM_SYMBOL_INTERNED) |
| 54 |
(define-cproc string->uninterned-symbol (name::<string>) |
| 55 |
(return (Scm_MakeSymbol name FALSE))) |
| 56 |
(define-cproc symbol-sans-prefix (s::<symbol> p::<symbol>) |
| 57 |
Scm_SymbolSansPrefix) |
| 58 |
|
| 59 |
;; Bigloo has symbol-append symbol ... -> symbol |
| 60 |
;; We enhance it a bit. |
| 61 |
(select-module gauche.internal) |
| 62 |
(define-in-module gauche symbol-append |
| 63 |
(letrec ([->string |
| 64 |
;; to make it work regardless of keyword-symbol integration |
| 65 |
(^x (cond [(keyword? x) #":~(keyword->string x)"] |
| 66 |
[(identifier? x) (symbol->string (identifier->symbol x))] |
| 67 |
[else (x->string x)]))] |
| 68 |
[do-append |
| 69 |
(^[objs interned?] |
| 70 |
((if interned? string->symbol string->uninterned-symbol) |
| 71 |
(apply string-append (map ->string objs))))]) |
| 72 |
(case-lambda |
| 73 |
[() '||] ; edge case |
| 74 |
[(maybe-flag . syms) |
| 75 |
(if (boolean? maybe-flag) |
| 76 |
(do-append syms maybe-flag) |
| 77 |
(do-append (cons maybe-flag syms) #t))]))) |
| 78 |
|
| 79 |
;; R7RS |
| 80 |
(select-module gauche) |
| 81 |
(define (symbol=? x y . rest) |
| 82 |
(if-let1 z (find ($ not $ symbol? $) (list* x y rest)) |
| 83 |
(error "symbol required, but got:" z)) |
| 84 |
(and (eq? x y) (or (null? rest) (every (cut eq? x <>) rest)))) |
| 85 |
|
| 86 |
|
| 87 |
;;; |
| 88 |
;;; Keywords |
| 89 |
;;; |
| 90 |
|
| 91 |
(select-module gauche) |
| 92 |
(define-cproc keyword? (obj) ::<boolean> :fast-flonum :constant SCM_KEYWORDP) |
| 93 |
|
| 94 |
(define-cproc make-keyword (name) |
| 95 |
(let* ([sname::ScmString* NULL]) |
| 96 |
(cond [(SCM_STRINGP name) (set! sname (SCM_STRING name))] |
| 97 |
[(SCM_SYMBOLP name) (set! sname (SCM_SYMBOL_NAME name))] |
| 98 |
[else (SCM_TYPE_ERROR name "string or symbol")]) |
| 99 |
(return (Scm_MakeKeyword sname)))) |
| 100 |
|
| 101 |
(define-cproc get-keyword (key list :optional fallback) :constant |
| 102 |
Scm_GetKeyword) |
| 103 |
|
| 104 |
(define-cproc delete-keyword (key list) Scm_DeleteKeyword) |
| 105 |
(define-cproc delete-keyword! (key list) Scm_DeleteKeywordX) |
| 106 |
|
| 107 |
(define-cproc keyword->string (key::<keyword>) Scm_KeywordToString) |
| 108 |
|
| 109 |
(define-in-module gauche (delete-keywords ks kvlist) |
| 110 |
(define (rec kvs) |
| 111 |
(cond [(null? kvs) '()] |
| 112 |
[(null? (cdr kvs)) (error "incomplete key list" kvlist)] |
| 113 |
[(memv (car kvs) ks) (rec (cddr kvs))] |
| 114 |
[else (list* (car kvs) (cadr kvs) (rec (cddr kvs)))])) |
| 115 |
(rec kvlist)) |
| 116 |
|
| 117 |
(define-in-module gauche (delete-keywords! ks kvlist) |
| 118 |
(define (head kvs) |
| 119 |
(cond [(null? kvs) '()] |
| 120 |
[(null? (cdr kvs)) (error "incomplete key list" kvlist)] |
| 121 |
[(memv (car kvs) ks) (head (cddr kvs))] |
| 122 |
[else (cut-tail! (cddr kvs) kvs) kvs])) |
| 123 |
(define (cut-tail! kvs prev) |
| 124 |
(cond [(null? kvs)] |
| 125 |
[(null? (cdr kvs)) (error "incomplete key list" kvlist)] |
| 126 |
[(memv (car kvs) ks) |
| 127 |
(set-cdr! (cdr prev) (cddr kvs)) |
| 128 |
(cut-tail! (cddr kvs) prev)] |
| 129 |
[else (cut-tail! (cddr kvs) kvs)])) |
| 130 |
(head kvlist)) |
| 131 |
|
| 132 |
;;; |
| 133 |
;;; Identifiers |
| 134 |
;;; |
| 135 |
|
| 136 |
;; NB: Identifiers are mainly used for hygienic macro. However, |
| 137 |
;; we found a disjoit type for it is sometimes annoying (especially, |
| 138 |
;; when you inspect the output of macro expander, you have to deal |
| 139 |
;; with both symbols and identifiers.) We have a vague plan to |
| 140 |
;; integrate it to a symbol---that is, we'll have a special subtype |
| 141 |
;; of symbols that can have auxiliary info needed for hygiene. |
| 142 |
|
| 143 |
(select-module gauche) |
| 144 |
(define-cproc wrapped-identifier? (obj) ::<boolean> :constant |
| 145 |
SCM_IDENTIFIERP) |
| 146 |
(define-cproc identifier? (obj) ::<boolean> :constant |
| 147 |
(inliner IDENTIFIERP) |
| 148 |
(return (or (SCM_SYMBOLP obj) (SCM_IDENTIFIERP obj)))) |
| 149 |
(define-cproc identifier->symbol (obj) :constant |
| 150 |
(cond [(SCM_SYMBOLP obj) (return obj)] |
| 151 |
[(SCM_IDENTIFIERP obj) |
| 152 |
(return (SCM_OBJ (Scm_UnwrapIdentifier (SCM_IDENTIFIER obj))))] |
| 153 |
[else (Scm_Error "identifier required, but got %S" obj)])) |
| 154 |
|
| 155 |
(select-module gauche.internal) |
| 156 |
(define-cproc make-identifier (name mod::<module> env::<list>) |
| 157 |
Scm_MakeIdentifier) |
| 158 |
(define-cproc identifier-module (id::<identifier>) |
| 159 |
(return (SCM_OBJ (-> id module)))) |
| 160 |
(define-cproc identifier-name (id::<identifier>) |
| 161 |
(return (SCM_OBJ (-> id name)))) |
| 162 |
(define-cproc identifier-env (id::<identifier>) |
| 163 |
(return (Scm_IdentifierEnv id))) |
| 164 |
|
| 165 |
(select-module gauche.internal) |
| 166 |
;; EXPERIMENTAL |
| 167 |
;; Concatenate symbols or identifiers. If any one of args is an identifier, |
| 168 |
;; the result is also an identifier with the same scope of the first |
| 169 |
;; identifier in the args. |
| 170 |
;; This sounds pretty kludgy. I suspect this won't be necessary once |
| 171 |
;; we use er-macro for everything low-level (rename procedure should take |
| 172 |
;; care of identifier marking). It is needed, for now, to add certain level |
| 173 |
;; of hygiene to define-macro. |
| 174 |
(define (identifier-append . args) |
| 175 |
(let1 r (apply symbol-append #t args) |
| 176 |
(if-let1 first-id (find wrapped-identifier? args) |
| 177 |
(make-identifier r (identifier-module first-id) (identifier-env first-id)) |
| 178 |
r))) |
| 179 |
|
| 180 |
(define-cproc identifier-toplevel? (id::<identifier>) ::<boolean> |
| 181 |
(return (SCM_NULLP (Scm_IdentifierEnv id)))) |