yasos.sld 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. (define-library (yasos)
  2. (import (scheme base))
  3. (export define-operation define-predicate
  4. object object-with-ancestors operate-as)
  5. (begin
  6. (define-record-type <yasos-object>
  7. (make-instance dispatcher)
  8. instance?
  9. (dispatcher instance-dispatcher))
  10. ;; DEFINE-OPERATION
  11. (define-syntax define-operation
  12. (syntax-rules ()
  13. ((_ (name inst arg ...) xpr . xprs) ; ordinary argument list
  14. (define name
  15. (letrec ((self
  16. (lambda (inst arg ...)
  17. (cond
  18. ((and (instance? inst)
  19. ((instance-dispatcher inst) self))
  20. => (lambda (operation) (operation inst arg ...)))
  21. (else xpr . xprs)))))
  22. self)))
  23. ((_ (name inst . args) xpr . xprs) ; dotted argument list
  24. (define name
  25. (letrec ((self
  26. (lambda (inst . args)
  27. (cond
  28. ((and (instance? inst)
  29. ((instance-dispatcher inst) self))
  30. => (lambda (operation)
  31. (apply operation inst args)))
  32. (else xpr . xprs)))))
  33. self)))
  34. ((_ (name inst . args)) ;; no body
  35. (define-operation (name inst . args)
  36. (error "Operation not handled" 'name inst)))))
  37. ;; DEFINE-PREDICATE
  38. (define-syntax define-predicate
  39. (syntax-rules ()
  40. ((define-predicate <name>)
  41. ;;=>
  42. (define-operation (<name> obj) #f))))
  43. ;; OBJECT
  44. (define-syntax object
  45. (syntax-rules ()
  46. ((_ ((name inst . args) xpr . xprs) ...)
  47. (let ((table
  48. (list (cons name
  49. (lambda (inst . args) xpr . xprs)) ...)))
  50. (make-instance
  51. (lambda (op)
  52. (cond
  53. ((assq op table) => cdr)
  54. (else #f))))))))
  55. ;; OBJECT with MULTIPLE INHERITANCE {First Found Rule}
  56. (define-syntax object-with-ancestors
  57. (syntax-rules ()
  58. ((object-with-ancestors ((<ancestor1> <init1>) ...) <operation> ...)
  59. ;;=>
  60. (let ((<ancestor1> <init1>) ...)
  61. (let ((child (object <operation> ...)))
  62. (make-instance
  63. (lambda (op)
  64. (or ((instance-dispatcher child) op)
  65. ((instance-dispatcher <ancestor1>) op) ...))))))))
  66. ;; OPERATE-AS {a.k.a. send-to-super}
  67. ;; used in operations/methods
  68. (define-syntax operate-as
  69. (syntax-rules ()
  70. ((operate-as <component> <op> <composit> <arg> ...)
  71. ;;=>
  72. (((instance-dispatcher <component>) <op>) <composit> <arg> ...))))))