language.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; Multi-language support
  2. ;; Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system base language)
  19. #:use-module (system base syntax)
  20. #:export (define-language language? lookup-language make-language
  21. language-name language-title language-reader
  22. language-printer language-parser
  23. language-compilers language-decompilers language-evaluator
  24. language-joiner language-make-default-environment
  25. lookup-compilation-order lookup-decompilation-order
  26. invalidate-compilation-cache! default-environment
  27. *current-language* current-language))
  28. ;;;
  29. ;;; Language class
  30. ;;;
  31. (define-record/keywords <language>
  32. name
  33. title
  34. reader
  35. printer
  36. (parser #f)
  37. (compilers '())
  38. (decompilers '())
  39. (evaluator #f)
  40. (joiner #f)
  41. (make-default-environment make-fresh-user-module))
  42. (define-macro (define-language name . spec)
  43. `(begin
  44. (invalidate-compilation-cache!)
  45. (define ,name (make-language #:name ',name ,@spec))))
  46. (define (lookup-language name)
  47. (let ((m (resolve-module `(language ,name spec))))
  48. (if (module-bound? m name)
  49. (module-ref m name)
  50. (error "no such language" name))))
  51. (define *compilation-cache* '())
  52. (define *decompilation-cache* '())
  53. (define (invalidate-compilation-cache!)
  54. (set! *decompilation-cache* '())
  55. (set! *compilation-cache* '()))
  56. (define (compute-translation-order from to language-translators)
  57. (cond
  58. ((not (language? to))
  59. (compute-translation-order from (lookup-language to) language-translators))
  60. (else
  61. (let lp ((from from) (seen '()))
  62. (cond
  63. ((not (language? from))
  64. (lp (lookup-language from) seen))
  65. ((eq? from to) (reverse! seen))
  66. ((memq from seen) #f)
  67. (else (or-map (lambda (pair)
  68. (lp (car pair) (acons from (cdr pair) seen)))
  69. (language-translators from))))))))
  70. (define (lookup-compilation-order from to)
  71. (let ((key (cons from to)))
  72. (or (assoc-ref *compilation-cache* key)
  73. (let ((order (compute-translation-order from to language-compilers)))
  74. (set! *compilation-cache*
  75. (acons key order *compilation-cache*))
  76. order))))
  77. (define (lookup-decompilation-order from to)
  78. (let ((key (cons from to)))
  79. (or (assoc-ref *decompilation-cache* key)
  80. ;; trickery!
  81. (let ((order (and=>
  82. (compute-translation-order to from language-decompilers)
  83. reverse!)))
  84. (set! *decompilation-cache* (acons key order *decompilation-cache*))
  85. order))))
  86. (define (default-environment lang)
  87. "Return the default compilation environment for source language LANG."
  88. ((language-make-default-environment
  89. (if (language? lang) lang (lookup-language lang)))))
  90. ;;;
  91. ;;; Current language
  92. ;;;
  93. (define *current-language* (make-fluid 'scheme))
  94. (define (current-language)
  95. (fluid-ref *current-language*))