| 1 | ;;; Guile Emacs Lisp |
| 2 | |
| 3 | ;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. |
| 4 | ;;; |
| 5 | ;;; This library is free software; you can redistribute it and/or |
| 6 | ;;; modify it under the terms of the GNU Lesser General Public |
| 7 | ;;; License as published by the Free Software Foundation; either |
| 8 | ;;; version 3 of the License, or (at your option) any later version. |
| 9 | ;;; |
| 10 | ;;; This library is distributed in the hope that it will be useful, |
| 11 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | ;;; Lesser General Public License for more details. |
| 14 | ;;; |
| 15 | ;;; You should have received a copy of the GNU Lesser General Public |
| 16 | ;;; License along with this library; if not, write to the Free Software |
| 17 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | |
| 19 | ;;; Code: |
| 20 | |
| 21 | (define-module (language elisp runtime) |
| 22 | #:use-module (ice-9 format) |
| 23 | #:use-module ((system base compile) |
| 24 | #:select (compile)) |
| 25 | #:use-module (language tree-il eval) |
| 26 | #:export (nil-value |
| 27 | t-value |
| 28 | value-slot-module |
| 29 | function-slot-module |
| 30 | elisp-bool |
| 31 | ensure-dynamic! |
| 32 | symbol-name |
| 33 | symbol-value |
| 34 | set-symbol-value! |
| 35 | symbol-function |
| 36 | set-symbol-function! |
| 37 | symbol-plist |
| 38 | set-symbol-plist! |
| 39 | symbol-bound? |
| 40 | symbol-fbound? |
| 41 | symbol-default-bound? |
| 42 | symbol-default-value |
| 43 | set-symbol-default-value! |
| 44 | bind-symbol |
| 45 | makunbound! |
| 46 | fmakunbound! |
| 47 | symbol-desc |
| 48 | proclaim-special! |
| 49 | special? |
| 50 | emacs! |
| 51 | unbound |
| 52 | lexical-binding? |
| 53 | set-lexical-binding-mode |
| 54 | log! |
| 55 | eval-elisp |
| 56 | compile-elisp |
| 57 | local-eval-elisp |
| 58 | make-lisp-string |
| 59 | lisp-string?) |
| 60 | #:export-syntax (defspecial prim)) |
| 61 | |
| 62 | ;;; This module provides runtime support for the Elisp front-end. |
| 63 | |
| 64 | ;;; Values for t and nil. (FIXME remove this abstraction) |
| 65 | |
| 66 | (define nil-value #nil) |
| 67 | |
| 68 | (define t-value #t) |
| 69 | |
| 70 | (define make-lisp-string identity) |
| 71 | (define lisp-string? string?) |
| 72 | |
| 73 | ;;; Modules for the binding slots. |
| 74 | ;;; Note: Naming those value-slot and/or function-slot clashes with the |
| 75 | ;;; submodules of these names! |
| 76 | |
| 77 | (define value-slot-module (define-module* '(elisp-symbols) #:pure #t)) |
| 78 | |
| 79 | (define function-slot-module (define-module* '(elisp-functions) #:pure #t)) |
| 80 | |
| 81 | (define plist-slot-module (define-module* '(elisp-plists) #:pure #t)) |
| 82 | |
| 83 | (define nil_ 'nil) |
| 84 | (define t_ 't) |
| 85 | |
| 86 | ;;; Routines for access to elisp dynamically bound symbols. This is |
| 87 | ;;; used for runtime access using functions like symbol-value or set, |
| 88 | ;;; where the symbol accessed might not be known at compile-time. These |
| 89 | ;;; always access the dynamic binding and can not be used for the |
| 90 | ;;; lexical! |
| 91 | |
| 92 | (define lexical-binding #t) |
| 93 | |
| 94 | (define (lexical-binding?) |
| 95 | lexical-binding) |
| 96 | |
| 97 | (define (set-lexical-binding-mode x) |
| 98 | (set! lexical-binding x)) |
| 99 | |
| 100 | (define unbound (make-symbol "unbound")) |
| 101 | |
| 102 | (define dynamic? vector?) |
| 103 | (define (make-dynamic) |
| 104 | (vector #f 4 0 0 unbound)) |
| 105 | (define (dynamic-ref x) |
| 106 | (vector-ref x 4)) |
| 107 | (define (dynamic-set! x v) |
| 108 | (vector-set! x 4 v)) |
| 109 | (define (dynamic-unset! x) |
| 110 | (vector-set! x 4 unbound)) |
| 111 | (define (dynamic-bound? x) |
| 112 | (not (eq? (vector-ref x 4) unbound))) |
| 113 | (define (dynamic-bind x v thunk) |
| 114 | (let ((old (vector-ref x 4))) |
| 115 | (dynamic-wind |
| 116 | (lambda () (vector-set! x 4 v)) |
| 117 | thunk |
| 118 | (lambda () (vector-set! x 4 old))))) |
| 119 | |
| 120 | (define-inlinable (ensure-present! module sym thunk) |
| 121 | (or (module-local-variable module sym) |
| 122 | (let ((variable (make-variable (thunk)))) |
| 123 | (module-add! module sym variable) |
| 124 | variable))) |
| 125 | |
| 126 | (define-inlinable (ensure-desc! module sym) |
| 127 | (ensure-present! module |
| 128 | sym |
| 129 | (lambda () |
| 130 | (let ((x (make-dynamic))) |
| 131 | (vector-set! x 0 sym) |
| 132 | x)))) |
| 133 | |
| 134 | (define-inlinable (schemify symbol) |
| 135 | (case symbol |
| 136 | ((#nil) nil_) |
| 137 | ((#t) t_) |
| 138 | (else symbol))) |
| 139 | |
| 140 | (define (symbol-name symbol) |
| 141 | (symbol->string (schemify symbol))) |
| 142 | |
| 143 | (define (symbol-desc symbol) |
| 144 | (let ((symbol (schemify symbol))) |
| 145 | (let ((module value-slot-module)) |
| 146 | (variable-ref (ensure-desc! module symbol))))) |
| 147 | |
| 148 | (define (ensure-dynamic! sym) |
| 149 | (vector-set! (symbol-desc sym) 3 1)) |
| 150 | |
| 151 | (define (symbol-dynamic symbol) |
| 152 | (ensure-dynamic! symbol) |
| 153 | (symbol-desc symbol)) |
| 154 | |
| 155 | (define (symbol-value symbol) |
| 156 | (dynamic-ref (symbol-desc symbol))) |
| 157 | |
| 158 | (define (set-symbol-value! symbol value) |
| 159 | (dynamic-set! (symbol-desc symbol) value) |
| 160 | value) |
| 161 | |
| 162 | (define (symbol-function symbol) |
| 163 | (cond |
| 164 | ((module-variable function-slot-module (schemify symbol)) |
| 165 | => variable-ref) |
| 166 | (else #nil))) |
| 167 | |
| 168 | (define (set-symbol-function! symbol value) |
| 169 | (set! symbol (schemify symbol)) |
| 170 | (ensure-present! function-slot-module symbol (lambda () #nil)) |
| 171 | (let ((module function-slot-module)) |
| 172 | (module-define! module symbol value) |
| 173 | (module-export! module (list symbol))) |
| 174 | value) |
| 175 | |
| 176 | (define (symbol-plist symbol) |
| 177 | (set! symbol (schemify symbol)) |
| 178 | (ensure-present! plist-slot-module symbol (lambda () #nil)) |
| 179 | (let ((module plist-slot-module)) |
| 180 | (module-ref module symbol))) |
| 181 | |
| 182 | (define (set-symbol-plist! symbol value) |
| 183 | (set! symbol (schemify symbol)) |
| 184 | (ensure-present! plist-slot-module symbol (lambda () #nil)) |
| 185 | (let ((module plist-slot-module)) |
| 186 | (module-define! module symbol value) |
| 187 | (module-export! module (list symbol))) |
| 188 | value) |
| 189 | |
| 190 | (define (symbol-bound? symbol) |
| 191 | (set! symbol (schemify symbol)) |
| 192 | (and |
| 193 | (module-bound? value-slot-module symbol) |
| 194 | (let ((var (module-variable value-slot-module |
| 195 | symbol))) |
| 196 | (and (variable-bound? var) |
| 197 | (if (dynamic? (variable-ref var)) |
| 198 | (dynamic-bound? (variable-ref var)) |
| 199 | #t))))) |
| 200 | |
| 201 | (define symbol-default-bound? symbol-bound?) |
| 202 | |
| 203 | (define symbol-default-value symbol-value) |
| 204 | |
| 205 | (define set-symbol-default-value! set-symbol-value!) |
| 206 | |
| 207 | (define (symbol-fbound? symbol) |
| 208 | (set! symbol (schemify symbol)) |
| 209 | (and |
| 210 | (module-bound? function-slot-module symbol) |
| 211 | (variable-bound? |
| 212 | (module-variable function-slot-module symbol)) |
| 213 | (variable-ref (module-variable function-slot-module symbol)))) |
| 214 | |
| 215 | (define (bind-symbol symbol value thunk) |
| 216 | (dynamic-bind (symbol-desc symbol) value thunk)) |
| 217 | |
| 218 | (define (makunbound! symbol) |
| 219 | (if (module-bound? value-slot-module symbol) |
| 220 | (let ((var (module-variable value-slot-module |
| 221 | symbol))) |
| 222 | (if (and (variable-bound? var) (dynamic? (variable-ref var))) |
| 223 | (dynamic-unset! (variable-ref var)) |
| 224 | (variable-unset! var)))) |
| 225 | symbol) |
| 226 | |
| 227 | (define (fmakunbound! symbol) |
| 228 | (if (module-bound? function-slot-module symbol) |
| 229 | (variable-unset! (module-variable function-slot-module symbol))) |
| 230 | symbol) |
| 231 | |
| 232 | (define (special? sym) |
| 233 | (eqv? (vector-ref (symbol-desc sym) 3) 1)) |
| 234 | |
| 235 | (define (proclaim-special! sym) |
| 236 | (vector-set! (symbol-desc sym) 3 1) |
| 237 | #nil) |
| 238 | |
| 239 | (define (emacs! ref set boundp dref dset dboundp bind) |
| 240 | (set! symbol-value ref) |
| 241 | (set! set-symbol-value! set) |
| 242 | (set! symbol-bound? boundp) |
| 243 | (set! symbol-default-value dref) |
| 244 | (set! set-symbol-default-value! dset) |
| 245 | (set! symbol-default-bound? dboundp) |
| 246 | (set! bind-symbol bind) |
| 247 | (set! lexical-binding? (lambda () (symbol-value 'lexical-binding))) |
| 248 | (set! set-lexical-binding-mode (lambda (x) (set-symbol-value! 'lexical-binding x)))) |
| 249 | |
| 250 | (define (eval-elisp form) |
| 251 | (eval (compile form #:from 'elisp #:to 'tree-il) (current-module))) |
| 252 | |
| 253 | (define (compile-elisp form) |
| 254 | (compile (compile form #:from 'elisp #:to 'bytecode) |
| 255 | #:from 'bytecode #:to 'value)) |
| 256 | |
| 257 | (set-symbol-value! nil_ #nil) |
| 258 | (set-symbol-value! t_ #t) |
| 259 | |
| 260 | (define (make-string s) s) |
| 261 | |
| 262 | ;;; Define a predefined macro for use in the function-slot module. |
| 263 | |
| 264 | (define (make-id template-id . data) |
| 265 | (let ((append-symbols |
| 266 | (lambda (symbols) |
| 267 | (string->symbol |
| 268 | (apply string-append (map symbol->string symbols)))))) |
| 269 | (datum->syntax template-id |
| 270 | (append-symbols |
| 271 | (map (lambda (datum) |
| 272 | ((if (identifier? datum) |
| 273 | syntax->datum |
| 274 | identity) |
| 275 | datum)) |
| 276 | data))))) |
| 277 | |
| 278 | (define-syntax defspecial |
| 279 | (lambda (x) |
| 280 | (syntax-case x () |
| 281 | ((_ name args body ...) |
| 282 | (with-syntax ((scheme-name (make-id #'name 'compile- #'name))) |
| 283 | #'(begin |
| 284 | (define scheme-name |
| 285 | (cons 'special-operator (lambda args body ...))) |
| 286 | (set-symbol-function! 'name scheme-name))))))) |