3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program 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
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 (define-module (language elisp runtime function-slot)
23 #:use-module (language elisp runtime)
24 #:use-module (system base compile))
26 ; This module contains the function-slots of elisp symbols. Elisp built-in
27 ; functions are implemented as predefined function bindings here.
30 ; Equivalence and equalness predicates.
32 (built-in-func eq (lambda (a b)
33 (elisp-bool (eq? a b))))
35 (built-in-func equal (lambda (a b)
36 (elisp-bool (equal? a b))))
41 (built-in-func floatp (lambda (num)
42 (elisp-bool (and (real? num)
44 (prim not (integer? num)))))))
46 (built-in-func integerp (lambda (num)
47 (elisp-bool (and (exact? num)
50 (built-in-func numberp (lambda (num)
51 (elisp-bool (real? num))))
53 (built-in-func wholenump (lambda (num)
54 (elisp-bool (and (exact? num)
58 (built-in-func zerop (lambda (num)
59 (elisp-bool (prim = num 0))))
64 (built-in-func = (lambda (num1 num2)
65 (elisp-bool (prim = num1 num2))))
66 (built-in-func /= (lambda (num1 num2)
67 (elisp-bool (prim not (prim = num1 num2)))))
69 (built-in-func < (lambda (num1 num2)
70 (elisp-bool (prim < num1 num2))))
71 (built-in-func <= (lambda (num1 num2)
72 (elisp-bool (prim <= num1 num2))))
73 (built-in-func > (lambda (num1 num2)
74 (elisp-bool (prim > num1 num2))))
75 (built-in-func >= (lambda (num1 num2)
76 (elisp-bool (prim >= num1 num2))))
78 (built-in-func max (lambda (. nums)
79 (prim apply (@ (guile) max) nums)))
80 (built-in-func min (lambda (. nums)
81 (prim apply (@ (guile) min) nums)))
83 (built-in-func abs (@ (guile) abs))
88 (built-in-func float (lambda (num)
93 ; TODO: truncate, floor, ceiling, round.
96 ; Arithmetic functions.
98 (built-in-func 1+ (@ (guile) 1+))
99 (built-in-func 1- (@ (guile) 1-))
100 (built-in-func + (@ (guile) +))
101 (built-in-func - (@ (guile) -))
102 (built-in-func * (@ (guile) *))
103 (built-in-func % (@ (guile) modulo))
105 ; TODO: / with correct integer/real behaviour, mod (for floating-piont values).
108 ; Floating-point rounding operations.
110 (built-in-func ffloor (@ (guile) floor))
111 (built-in-func fceiling (@ (guile) ceiling))
112 (built-in-func ftruncate (@ (guile) truncate))
113 (built-in-func fround (@ (guile) round))
120 (elisp-bool (pair? el))))
123 (elisp-bool (prim not (pair? el)))))
127 (elisp-bool (or (pair? el) (null? el)))))
128 (built-in-func nlistp
130 (elisp-bool (and (prim not (pair? el))
131 (prim not (null? el))))))
135 (elisp-bool (null? el))))
138 ; Accessing list elements.
151 (built-in-func car-safe
156 (built-in-func cdr-safe
169 ((null? tail) nil-value)
170 ((zero? i) (prim car tail))
171 (else (iterate (prim 1- i) (prim cdr tail))))))))
172 (built-in-func nthcdr
179 ((null? tail) nil-value)
181 (else (iterate (prim 1- i) (prim cdr tail))))))))
183 (built-in-func length (@ (guile) length))
188 (built-in-func cons (@ (guile) cons))
189 (built-in-func list (@ (guile) list))
190 (built-in-func make-list
192 (prim make-list len obj)))
194 (built-in-func append (@ (guile) append))
195 (built-in-func reverse (@ (guile) reverse))
196 (built-in-func copy-tree (@ (guile) copy-tree))
198 (built-in-func number-sequence
199 (lambda (from . rest)
200 (if (prim > (prim length rest) 2)
201 (runtime-error "too many arguments for number-sequence"
205 (let ((to (prim car rest))
206 (sep (if (or (null? (prim cdr rest))
207 (eq? nil-value (prim cadr rest)))
211 ((or (eq? nil-value to) (prim = to from)) `(,from))
212 ((and (zero? sep) (prim not (prim = from to)))
213 (runtime-error "infinite list in number-sequence"))
214 ((prim < (prim * to sep) (prim * from sep)) '())
216 (let iterate ((i (prim +
220 (prim abs (prim - to from))
225 (iterate (prim - i sep) (prim cons i result)))))))))))
230 (built-in-func setcar
232 (prim set-car! cell val)
235 (built-in-func setcdr
237 (prim set-cdr! cell val)
241 ; Accessing symbol bindings for symbols known only at runtime.
243 (built-in-func symbol-value
245 (reference-variable-with-check value-slot-module sym)))
246 (built-in-func symbol-function
248 (reference-variable-with-check function-slot-module sym)))
252 (set-variable! value-slot-module sym value)))
255 (set-variable! function-slot-module sym value)))
257 (built-in-func makunbound
259 (set-variable! value-slot-module sym void)
261 (built-in-func fmakunbound
263 (set-variable! function-slot-module sym void)
266 (built-in-func boundp
268 (elisp-bool (prim not
269 (eq? void (reference-variable value-slot-module sym))))))
270 (built-in-func fboundp
272 (elisp-bool (prim not
273 (eq? void (reference-variable function-slot-module sym))))))
276 ; Function calls. These must take care of special cases, like using symbols
277 ; or raw lambda-lists as functions!
280 (lambda (func . args)
281 (let ((real-func (cond
283 (reference-variable-with-check function-slot-module
286 (if (and (prim not (null? func))
287 (eq? (prim car func) 'lambda))
288 (compile func #:from 'elisp #:to 'value)
289 (runtime-error "list is not a function" func)))
291 (prim apply (@ (guile) apply) real-func args))))
293 (built-in-func funcall
294 (let ((myapply (fluid-ref apply)))
295 (lambda (func . args)
296 (myapply func args))))
299 ; Throw can be implemented as built-in function.
303 (prim throw 'elisp-exception tag value)))
310 (if x nil-value t-value)))
314 (compile form #:from 'elisp #:to 'value)))