primitive.scm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/prescheme/primitive.scm
  8. ;;;
  9. ;;; Eval'ing and type-checking code for primitives.
  10. (define-module (ps-compiler prescheme primitive)
  11. #:use-module (prescheme scheme48)
  12. #:use-module (prescheme s48-defrecord)
  13. #:use-module (prescheme record-discloser)
  14. #:use-module (ps-compiler util util)
  15. #:export (primitive?
  16. make-primitive
  17. eval-primitive
  18. primitive-id
  19. primitive-source
  20. primitive-expander
  21. primitive-expands-in-place?
  22. primitive-inference-rule))
  23. (define-record-type primitive
  24. (id ;; for debugging & making tables
  25. arg-predicates ;; predicates for checking argument types
  26. eval ;; evaluation function
  27. source ;; close-compiled source (if any)
  28. expander ;; convert call to one using primops
  29. expands-in-place? ;; does the expander expand the definition in-line?
  30. inference-rule ;; type inference rule
  31. )
  32. ())
  33. (define make-primitive primitive-maker)
  34. (define-record-discloser type/primitive
  35. (lambda (primitive)
  36. (list 'primitive (primitive-id primitive))))
  37. (define (eval-primitive primitive args)
  38. (cond ((not (primitive? primitive))
  39. (user-error "error while evaluating: ~A is not a procedure" primitive))
  40. ((args-okay? args (primitive-arg-predicates primitive))
  41. (apply (primitive-eval primitive) args))
  42. (else
  43. (user-error "error while evaluating: type error ~A"
  44. (cons (primitive-id primitive) args)))))
  45. ;; PREDICATES is a (possibly improper) list of predicates that should match
  46. ;; ARGS.
  47. (define (args-okay? args predicates)
  48. (cond ((atom? predicates)
  49. (if predicates
  50. (every? predicates args)
  51. #t))
  52. ((null? args)
  53. #f)
  54. ((car predicates)
  55. (and ((car predicates) (car args))
  56. (args-okay? (cdr args) (cdr predicates))))
  57. (else
  58. (args-okay? (cdr args) (cdr predicates)))))