primitive.scm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey
  3. ; Eval'ing and type-checking code for primitives.
  4. (define-record-type primitive
  5. (id ; for debugging & making tables
  6. arg-predicates ; predicates for checking argument types
  7. eval ; evaluation function
  8. source ; close-compiled source (if any)
  9. expander ; convert call to one using primops
  10. expands-in-place? ; does the expander expand the definition in-line?
  11. inference-rule ; type inference rule
  12. )
  13. ())
  14. (define make-primitive primitive-maker)
  15. (define-record-discloser type/primitive
  16. (lambda (primitive)
  17. (list 'primitive (primitive-id primitive))))
  18. (define (eval-primitive primitive args)
  19. (cond ((not (primitive? primitive))
  20. (user-error "error while evaluating: ~A is not a procedure" primitive))
  21. ((args-okay? args (primitive-arg-predicates primitive))
  22. (apply (primitive-eval primitive) args))
  23. (else
  24. (user-error "error while evaluating: type error ~A"
  25. (cons (primitive-id primitive) args)))))
  26. ; PREDICATES is a (possibly improper) list of predicates that should match
  27. ; ARGS.
  28. (define (args-okay? args predicates)
  29. (cond ((atom? predicates)
  30. (if predicates
  31. (every? predicates args)
  32. #t))
  33. ((null? args)
  34. #f)
  35. ((car predicates)
  36. (and ((car predicates) (car args))
  37. (args-okay? (cdr args) (cdr predicates))))
  38. (else
  39. (args-okay? (cdr args) (cdr predicates)))))