File: primitive.scm

package info (click to toggle)
scheme48 1.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,980 kB
  • ctags: 14,127
  • sloc: lisp: 76,272; ansic: 71,514; sh: 3,026; makefile: 637
file content (46 lines) | stat: -rw-r--r-- 1,446 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
; Copyright (c) 1993-2008 by Richard Kelsey.  See file COPYING.

; Eval'ing and type-checking code for primitives.

(define-record-type primitive
  (id                ; for debugging & making tables
   arg-predicates    ; predicates for checking argument types
   eval              ; evaluation function
   source            ; close-compiled source (if any)
   expander          ; convert call to one using primops
   expands-in-place? ; does the expander expand the definition in-line?
   inference-rule    ; type inference rule
   )
  ())

(define make-primitive primitive-maker)

(define-record-discloser type/primitive
  (lambda (primitive)
    (list 'primitive (primitive-id primitive))))

(define (eval-primitive primitive args)
  (cond ((not (primitive? primitive))
	 (user-error "error while evaluating: ~A is not a procedure" primitive))
	((args-okay? args (primitive-arg-predicates primitive))
	 (apply (primitive-eval primitive) args))
	(else
	 (user-error "error while evaluating: type error ~A"
		     (cons (primitive-id primitive) args)))))

; PREDICATES is a (possibly improper) list of predicates that should match
; ARGS.

(define (args-okay? args predicates)
  (cond ((atom? predicates)
	 (if predicates
	     (every? predicates args)
	     #t))
	((null? args)
	 #f)
	((car predicates)
	 (and ((car predicates) (car args))
	      (args-okay? (cdr args) (cdr predicates))))
	(else
	 (args-okay? (cdr args) (cdr predicates)))))