primitive.scm 1.4 KB

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