language.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; Multi-language support
  2. ;; Copyright (C) 2001, 2009, 2010, 2011, 2013 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-for-humans?
  25. language-make-default-environment
  26. lookup-compilation-order lookup-decompilation-order
  27. invalidate-compilation-cache! default-environment
  28. *current-language*)
  29. #:re-export (current-language))
  30. ;;;
  31. ;;; Language class
  32. ;;;
  33. (define-record/keywords <language>
  34. name
  35. title
  36. reader
  37. printer
  38. (parser #f)
  39. (compilers '())
  40. (decompilers '())
  41. (evaluator #f)
  42. (joiner #f)
  43. (for-humans? #t)
  44. (make-default-environment make-fresh-user-module))
  45. (define-macro (define-language name . spec)
  46. `(begin
  47. (invalidate-compilation-cache!)
  48. (define ,name (make-language #:name ',name ,@spec))))
  49. (define (lookup-language name)
  50. (let ((m (resolve-module `(language ,name spec))))
  51. (if (module-bound? m name)
  52. (module-ref m name)
  53. (error "no such language" name))))
  54. (define *compilation-cache* '())
  55. (define *decompilation-cache* '())
  56. (define (invalidate-compilation-cache!)
  57. (set! *decompilation-cache* '())
  58. (set! *compilation-cache* '()))
  59. (define (compute-translation-order from to language-translators)
  60. (cond
  61. ((not (language? to))
  62. (compute-translation-order from (lookup-language to) language-translators))
  63. (else
  64. (let lp ((from from) (seen '()))
  65. (cond
  66. ((not (language? from))
  67. (lp (lookup-language from) seen))
  68. ((eq? from to) (reverse! seen))
  69. ((memq from seen) #f)
  70. (else (or-map (lambda (pair)
  71. (lp (car pair) (acons from (cdr pair) seen)))
  72. (language-translators from))))))))
  73. (define (lookup-compilation-order from to)
  74. (let ((key (cons from to)))
  75. (or (assoc-ref *compilation-cache* key)
  76. (let ((order (compute-translation-order from to language-compilers)))
  77. (set! *compilation-cache*
  78. (acons key order *compilation-cache*))
  79. order))))
  80. (define (lookup-decompilation-order from to)
  81. (let ((key (cons from to)))
  82. (or (assoc-ref *decompilation-cache* key)
  83. ;; trickery!
  84. (let ((order (and=>
  85. (compute-translation-order to from language-decompilers)
  86. reverse!)))
  87. (set! *decompilation-cache* (acons key order *decompilation-cache*))
  88. order))))
  89. (define (default-environment lang)
  90. "Return the default compilation environment for source language LANG."
  91. ((language-make-default-environment
  92. (if (language? lang) lang (lookup-language lang)))))
  93. ;;;
  94. ;;; Current language
  95. ;;;
  96. ;; Deprecated; use current-language instead.
  97. (define *current-language* (parameter-fluid current-language))