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.
31 (built-in-func eq (lambda (a b)
32 (elisp-bool (eq? a b))))
34 (built-in-func equal (lambda (a b)
35 (elisp-bool (equal? a b))))
37 ;;; Number predicates.
39 (built-in-func floatp (lambda (num)
40 (elisp-bool (and (real? num)
42 (prim not (integer? num)))))))
44 (built-in-func integerp (lambda (num)
45 (elisp-bool (and (exact? num)
48 (built-in-func numberp (lambda (num)
49 (elisp-bool (real? num))))
51 (built-in-func wholenump (lambda (num)
52 (elisp-bool (and (exact? num)
56 (built-in-func zerop (lambda (num)
57 (elisp-bool (prim = num 0))))
59 ;;; Number comparisons.
61 (built-in-func = (lambda (num1 num2)
62 (elisp-bool (prim = num1 num2))))
64 (built-in-func /= (lambda (num1 num2)
65 (elisp-bool (prim not (prim = num1 num2)))))
67 (built-in-func < (lambda (num1 num2)
68 (elisp-bool (prim < num1 num2))))
70 (built-in-func <= (lambda (num1 num2)
71 (elisp-bool (prim <= num1 num2))))
73 (built-in-func > (lambda (num1 num2)
74 (elisp-bool (prim > num1 num2))))
76 (built-in-func >= (lambda (num1 num2)
77 (elisp-bool (prim >= num1 num2))))
79 (built-in-func max (lambda (. nums)
80 (prim apply (@ (guile) max) nums)))
82 (built-in-func min (lambda (. nums)
83 (prim apply (@ (guile) min) nums)))
85 (built-in-func abs (@ (guile) abs))
87 ;;; Number conversion.
89 (built-in-func float (lambda (num)
94 ;;; TODO: truncate, floor, ceiling, round.
96 ;;; Arithmetic functions.
98 (built-in-func 1+ (@ (guile) 1+))
100 (built-in-func 1- (@ (guile) 1-))
102 (built-in-func + (@ (guile) +))
104 (built-in-func - (@ (guile) -))
106 (built-in-func * (@ (guile) *))
108 (built-in-func % (@ (guile) modulo))
110 ;;; TODO: / with correct integer/real behaviour, mod (for floating-piont
113 ;;; Floating-point rounding operations.
115 (built-in-func ffloor (@ (guile) floor))
117 (built-in-func fceiling (@ (guile) ceiling))
119 (built-in-func ftruncate (@ (guile) truncate))
121 (built-in-func fround (@ (guile) round))
127 (elisp-bool (pair? el))))
131 (elisp-bool (prim not (pair? el)))))
135 (elisp-bool (or (pair? el) (null? el)))))
137 (built-in-func nlistp
139 (elisp-bool (and (prim not (pair? el))
140 (prim not (null? el))))))
144 (elisp-bool (null? el))))
146 ;;; Accessing list elements.
160 (built-in-func car-safe
166 (built-in-func cdr-safe
179 ((null? tail) nil-value)
180 ((zero? i) (prim car tail))
181 (else (iterate (prim 1- i) (prim cdr tail))))))))
183 (built-in-func nthcdr
190 ((null? tail) nil-value)
192 (else (iterate (prim 1- i) (prim cdr tail))))))))
194 (built-in-func length (@ (guile) length))
198 (built-in-func cons (@ (guile) cons))
200 (built-in-func list (@ (guile) list))
202 (built-in-func make-list
204 (prim make-list len obj)))
206 (built-in-func append (@ (guile) append))
208 (built-in-func reverse (@ (guile) reverse))
210 (built-in-func copy-tree (@ (guile) copy-tree))
212 (built-in-func number-sequence
213 (lambda (from . rest)
214 (if (prim > (prim length rest) 2)
215 (runtime-error "too many arguments for number-sequence"
219 (let ((to (prim car rest))
220 (sep (if (or (null? (prim cdr rest))
221 (eq? nil-value (prim cadr rest)))
225 ((or (eq? nil-value to) (prim = to from)) `(,from))
226 ((and (zero? sep) (prim not (prim = from to)))
227 (runtime-error "infinite list in number-sequence"))
228 ((prim < (prim * to sep) (prim * from sep)) '())
230 (let iterate ((i (prim +
234 (prim abs (prim - to from))
239 (iterate (prim - i sep) (prim cons i result)))))))))))
243 (built-in-func setcar
245 (prim set-car! cell val)
248 (built-in-func setcdr
250 (prim set-cdr! cell val)
253 ;;; Accessing symbol bindings for symbols known only at runtime.
255 (built-in-func symbol-value
257 (reference-variable-with-check value-slot-module sym)))
259 (built-in-func symbol-function
261 (reference-variable-with-check function-slot-module sym)))
265 (set-variable! value-slot-module sym value)))
269 (set-variable! function-slot-module sym value)))
271 (built-in-func makunbound
273 (set-variable! value-slot-module sym void)
276 (built-in-func fmakunbound
278 (set-variable! function-slot-module sym void)
281 (built-in-func boundp
283 (elisp-bool (prim not
284 (eq? void (reference-variable value-slot-module sym))))))
286 (built-in-func fboundp
288 (elisp-bool (prim not
289 (eq? void (reference-variable function-slot-module sym))))))
291 ;;; Function calls. These must take care of special cases, like using
292 ;;; symbols or raw lambda-lists as functions!
295 (lambda (func . args)
296 (let ((real-func (cond
298 (reference-variable-with-check function-slot-module
301 (if (and (prim not (null? func))
302 (eq? (prim car func) 'lambda))
303 (compile func #:from 'elisp #:to 'value)
304 (runtime-error "list is not a function" func)))
306 (prim apply (@ (guile) apply) real-func args))))
308 (built-in-func funcall
309 (let ((myapply (fluid-ref apply)))
310 (lambda (func . args)
311 (myapply func args))))
313 ;;; Throw can be implemented as built-in function.
317 (prim throw 'elisp-exception tag value)))
323 (if x nil-value t-value)))
327 (compile form #:from 'elisp #:to 'value)))