import-def.scm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Marcus Crestani
  3. ; Two macros:
  4. ;
  5. ; (import-definition <id>)
  6. ; ->
  7. ; (define <id> (lookup-imported-binding "<id with - becoming _>"))
  8. ;
  9. ; (import-definition <id> <string id>)
  10. ; ->
  11. ; (define <id> (lookup-imported-binding <string-id>))
  12. ;
  13. ; (import-lambda-definition <id> (<formal> ...))
  14. ; ->
  15. ; (begin
  16. ; (define temp (lookup-imported-binding "<id with - becoming _>"))
  17. ; (define <id>
  18. ; (lambda (<formal> ...)
  19. ; (call-imported-binding temp <formal> ...))))
  20. ;
  21. ; (import-lambda-definition <id> (<formal> ...) <string id>)
  22. ; ->
  23. ; ...same again using <string id> as the imported name...
  24. (define-syntax import-definition
  25. (lambda (exp rename compare)
  26. (let ((id (cadr exp))
  27. (%define (rename 'define))
  28. (%lookup-imported-binding (rename 'lookup-imported-binding)))
  29. (let ((external-id (if (null? (cddr exp))
  30. (list->string (map (lambda (ch)
  31. (if (char=? ch #\-)
  32. #\_
  33. ch))
  34. (string->list
  35. (symbol->string id))))
  36. (caddr exp))))
  37. `(,%define ,id
  38. (,%lookup-imported-binding ,external-id))))))
  39. ; (import-lambda-definition id (formal ...) [external name])
  40. (define-syntax import-lambda-definition
  41. (lambda (exp rename compare)
  42. (let ((id (cadr exp))
  43. (formals (caddr exp))
  44. (%define (rename 'define))
  45. (%begin (rename 'begin))
  46. (%lambda (rename 'lambda))
  47. (%call-imported-binding (rename 'call-imported-binding))
  48. (%lookup-imported-binding (rename 'lookup-imported-binding))
  49. (%binding (rename 'binding)))
  50. (let ((external-id (if (null? (cdddr exp))
  51. (list->string (map (lambda (ch)
  52. (if (char=? ch #\-)
  53. #\_
  54. ch))
  55. (string->list
  56. (symbol->string id))))
  57. (cadddr exp))))
  58. `(,%begin
  59. (,%define ,%binding
  60. (,%lookup-imported-binding ,external-id))
  61. (,%define ,id
  62. (,%lambda ,formals
  63. (,%call-imported-binding ,%binding . ,formals))))))))
  64. (define-syntax import-lambda-definition-2
  65. (lambda (exp rename compare)
  66. (let ((id (cadr exp))
  67. (formals (caddr exp))
  68. (%define (rename 'define))
  69. (%begin (rename 'begin))
  70. (%lambda (rename 'lambda))
  71. (%call-imported-binding-2 (rename 'call-imported-binding-2))
  72. (%lookup-imported-binding (rename 'lookup-imported-binding))
  73. (%binding (rename 'binding)))
  74. (let ((external-id (if (null? (cdddr exp))
  75. (list->string (map (lambda (ch)
  76. (if (char=? ch #\-)
  77. #\_
  78. ch))
  79. (string->list
  80. (symbol->string id))))
  81. (cadddr exp))))
  82. `(,%begin
  83. (,%define ,%binding
  84. (,%lookup-imported-binding ,external-id))
  85. (,%define ,id
  86. (,%lambda ,formals
  87. (,%call-imported-binding-2 ,%binding . ,formals))))))))