3 ;;; Copyright (C) 2009 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 built-in
26 ; functions are implemented as predefined function bindings here.
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))))
40 (built-in-func floatp (lambda (num)
41 (elisp-bool (and (real? num)
43 (prim not (integer? num)))))))
45 (built-in-func integerp (lambda (num)
46 (elisp-bool (and (exact? num)
49 (built-in-func numberp (lambda (num)
50 (elisp-bool (real? num))))
52 (built-in-func wholenump (lambda (num)
53 (elisp-bool (and (exact? num)
57 (built-in-func zerop (lambda (num)
58 (elisp-bool (prim = num 0))))
63 (built-in-func = (lambda (num1 num2)
64 (elisp-bool (prim = num1 num2))))
65 (built-in-func /= (lambda (num1 num2)
66 (elisp-bool (prim not (prim = num1 num2)))))
68 (built-in-func < (lambda (num1 num2)
69 (elisp-bool (prim < num1 num2))))
70 (built-in-func <= (lambda (num1 num2)
71 (elisp-bool (prim <= num1 num2))))
72 (built-in-func > (lambda (num1 num2)
73 (elisp-bool (prim > num1 num2))))
74 (built-in-func >= (lambda (num1 num2)
75 (elisp-bool (prim >= num1 num2))))
77 (built-in-func max (lambda (. nums)
78 (prim apply (@ (guile) max) nums)))
79 (built-in-func min (lambda (. nums)
80 (prim apply (@ (guile) min) nums)))
82 (built-in-func abs (@ (guile) abs))
87 (built-in-func float (lambda (num)
92 ; TODO: truncate, floor, ceiling, round.
95 ; Arithmetic functions.
97 (built-in-func 1+ (@ (guile) 1+))
98 (built-in-func 1- (@ (guile) 1-))
99 (built-in-func + (@ (guile) +))
100 (built-in-func - (@ (guile) -))
101 (built-in-func * (@ (guile) *))
102 (built-in-func % (@ (guile) modulo))
104 ; TODO: / with correct integer/real behaviour, mod (for floating-piont values).
107 ; Floating-point rounding operations.
109 (built-in-func ffloor (@ (guile) floor))
110 (built-in-func fceiling (@ (guile) ceiling))
111 (built-in-func ftruncate (@ (guile) truncate))
112 (built-in-func fround (@ (guile) round))
119 (elisp-bool (pair? el))))
122 (elisp-bool (prim not (pair? el)))))
126 (elisp-bool (or (pair? el) (null? el)))))
127 (built-in-func nlistp
129 (elisp-bool (and (prim not (pair? el))
130 (prim not (null? el))))))
134 (elisp-bool (null? el))))
137 ; Accessing list elements.
150 (built-in-func car-safe
155 (built-in-func cdr-safe
168 ((null? tail) nil-value)
169 ((zero? i) (prim car tail))
170 (else (iterate (prim 1- i) (prim cdr tail))))))))
171 (built-in-func nthcdr
178 ((null? tail) nil-value)
180 (else (iterate (prim 1- i) (prim cdr tail))))))))
182 (built-in-func length (@ (guile) length))
187 (built-in-func cons (@ (guile) cons))
188 (built-in-func list (@ (guile) list))
189 (built-in-func make-list
191 (prim make-list len obj)))
193 (built-in-func append (@ (guile) append))
194 (built-in-func reverse (@ (guile) reverse))
195 (built-in-func copy-tree (@ (guile) copy-tree))
197 (built-in-func number-sequence
198 (lambda (from . rest)
199 (if (prim > (prim length rest) 2)
200 (runtime-error "too many arguments for number-sequence"
204 (let ((to (prim car rest))
205 (sep (if (or (null? (prim cdr rest))
206 (eq? nil-value (prim cadr rest)))
210 ((or (eq? nil-value to) (prim = to from)) `(,from))
211 ((and (zero? sep) (prim not (prim = from to)))
212 (runtime-error "infinite list in number-sequence"))
213 ((prim < (prim * to sep) (prim * from sep)) '())
215 (let iterate ((i (prim +
219 (prim abs (prim - to from))
224 (iterate (prim - i sep) (prim cons i result)))))))))))
229 (built-in-func setcar
231 (prim set-car! cell val)
234 (built-in-func setcdr
236 (prim set-cdr! cell val)
240 ; Accessing symbol bindings for symbols known only at runtime.
242 (built-in-func symbol-value
244 (reference-variable-with-check value-slot-module sym)))
245 (built-in-func symbol-function
247 (reference-variable-with-check function-slot-module sym)))
251 (set-variable! value-slot-module sym value)))
254 (set-variable! function-slot-module sym value)))
256 (built-in-func makunbound
258 (set-variable! value-slot-module sym void)
260 (built-in-func fmakunbound
262 (set-variable! function-slot-module sym void)
265 (built-in-func boundp
267 (elisp-bool (prim not
268 (eq? void (reference-variable value-slot-module sym))))))
269 (built-in-func fboundp
271 (elisp-bool (prim not
272 (eq? void (reference-variable function-slot-module sym))))))
275 ; Function calls. These must take care of special cases, like using symbols
276 ; or raw lambda-lists as functions!
279 (lambda (func . args)
280 (let ((real-func (cond
282 (reference-variable-with-check function-slot-module
285 (if (and (prim not (null? func))
286 (eq? (prim car func) 'lambda))
287 (compile func #:from 'elisp #:to 'value)
288 (runtime-error "list is not a function" func)))
290 (prim apply (@ (guile) apply) real-func args))))
292 (built-in-func funcall
293 (let ((myapply (fluid-ref apply)))
294 (lambda (func . args)
295 (myapply func args))))
298 ; Throw can be implemented as built-in function.
302 (prim throw 'elisp-exception tag value)))
309 (if x nil-value t-value)))
313 (compile form #:from 'elisp #:to 'value)))