3 ;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
21 (define-module (language elisp runtime function-slot)
22 #:use-module (language elisp runtime)
23 #:use-module (system base compile))
25 ;;; This module contains the function-slots of elisp symbols. Elisp
26 ;;; built-in functions are implemented as predefined function bindings
29 ;;; Equivalence and equalness predicates.
33 (elisp-bool (eq? a b))))
37 (elisp-bool (equal? a b))))
39 ;;; Number predicates.
43 (elisp-bool (and (real? num)
45 (prim not (integer? num)))))))
47 (built-in-func integerp
49 (elisp-bool (and (exact? num)
52 (built-in-func numberp
54 (elisp-bool (real? num))))
56 (built-in-func wholenump
58 (elisp-bool (and (exact? num)
64 (elisp-bool (prim = num 0))))
66 ;;; Number comparisons.
70 (elisp-bool (prim = num1 num2))))
74 (elisp-bool (prim not (prim = num1 num2)))))
78 (elisp-bool (prim < num1 num2))))
82 (elisp-bool (prim <= num1 num2))))
86 (elisp-bool (prim > num1 num2))))
90 (elisp-bool (prim >= num1 num2))))
94 (prim apply (@ (guile) max) nums)))
98 (prim apply (@ (guile) min) nums)))
103 ;;; Number conversion.
111 ;;; TODO: truncate, floor, ceiling, round.
113 ;;; Arithmetic functions.
115 (built-in-func 1+ (@ (guile) 1+))
117 (built-in-func 1- (@ (guile) 1-))
119 (built-in-func + (@ (guile) +))
121 (built-in-func - (@ (guile) -))
123 (built-in-func * (@ (guile) *))
125 (built-in-func % (@ (guile) modulo))
127 ;;; TODO: / with correct integer/real behaviour, mod (for floating-piont
130 ;;; Floating-point rounding operations.
132 (built-in-func ffloor (@ (guile) floor))
134 (built-in-func fceiling (@ (guile) ceiling))
136 (built-in-func ftruncate (@ (guile) truncate))
138 (built-in-func fround (@ (guile) round))
144 (elisp-bool (pair? el))))
148 (elisp-bool (prim not (pair? el)))))
152 (elisp-bool (or (pair? el) (null? el)))))
154 (built-in-func nlistp
156 (elisp-bool (and (prim not (pair? el))
157 (prim not (null? el))))))
161 (elisp-bool (null? el))))
163 ;;; Accessing list elements.
177 (built-in-func car-safe
183 (built-in-func cdr-safe
196 ((null? tail) nil-value)
197 ((zero? i) (prim car tail))
198 (else (iterate (prim 1- i) (prim cdr tail))))))))
200 (built-in-func nthcdr
207 ((null? tail) nil-value)
209 (else (iterate (prim 1- i) (prim cdr tail))))))))
211 (built-in-func length (@ (guile) length))
215 (built-in-func cons (@ (guile) cons))
217 (built-in-func list (@ (guile) list))
219 (built-in-func make-list
221 (prim make-list len obj)))
223 (built-in-func append (@ (guile) append))
225 (built-in-func reverse (@ (guile) reverse))
227 (built-in-func copy-tree (@ (guile) copy-tree))
229 (built-in-func number-sequence
230 (lambda (from . rest)
231 (if (prim > (prim length rest) 2)
232 (runtime-error "too many arguments for number-sequence"
236 (let ((to (prim car rest))
237 (sep (if (or (null? (prim cdr rest))
238 (eq? nil-value (prim cadr rest)))
242 ((or (eq? nil-value to) (prim = to from)) `(,from))
243 ((and (zero? sep) (prim not (prim = from to)))
244 (runtime-error "infinite list in number-sequence"))
245 ((prim < (prim * to sep) (prim * from sep)) '())
247 (let iterate ((i (prim +
260 (iterate (prim - i sep)
261 (prim cons i result)))))))))))
265 (built-in-func setcar
267 (prim set-car! cell val)
270 (built-in-func setcdr
272 (prim set-cdr! cell val)
275 ;;; Accessing symbol bindings for symbols known only at runtime.
277 (built-in-func symbol-value
279 (reference-variable-with-check value-slot-module sym)))
281 (built-in-func symbol-function
283 (reference-variable-with-check function-slot-module sym)))
287 (set-variable! value-slot-module sym value)))
291 (set-variable! function-slot-module sym value)))
293 (built-in-func makunbound
295 (set-variable! value-slot-module sym void)
298 (built-in-func fmakunbound
300 (set-variable! function-slot-module sym void)
303 (built-in-func boundp
305 (elisp-bool (prim not
307 (reference-variable value-slot-module
310 (built-in-func fboundp
312 (elisp-bool (prim not
314 (reference-variable function-slot-module
317 ;;; Function calls. These must take care of special cases, like using
318 ;;; symbols or raw lambda-lists as functions!
321 (lambda (func . args)
322 (let ((real-func (cond
324 (reference-variable-with-check
328 (if (and (prim not (null? func))
329 (eq? (prim car func) 'lambda))
330 (compile func #:from 'elisp #:to 'value)
331 (runtime-error "list is not a function"
334 (prim apply (@ (guile) apply) real-func args))))
336 (built-in-func funcall
337 (let ((myapply (fluid-ref apply)))
338 (lambda (func . args)
339 (myapply func args))))
341 ;;; Throw can be implemented as built-in function.
345 (prim throw 'elisp-exception tag value)))
351 (if x nil-value t-value)))
355 (compile form #:from 'elisp #:to 'value)))