1 ;;; Multi-language support
3 ;; Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (system base language)
23 #:use-module (system base syntax)
24 #:export (define-language language? lookup-language make-language
25 language-name language-title language-reader
26 language-printer language-parser
27 language-compilers language-decompilers language-evaluator
28 language-joiner language-make-default-environment
30 lookup-compilation-order lookup-decompilation-order
31 invalidate-compilation-cache! default-environment
33 *current-language* current-language))
40 (define-record/keywords <language>
50 (make-default-environment make-fresh-user-module))
52 (define-macro (define-language name . spec)
54 (invalidate-compilation-cache!)
55 (define ,name (make-language #:name ',name ,@spec))))
57 (define (lookup-language name)
58 (let ((m (resolve-module `(language ,name spec))))
59 (if (module-bound? m name)
61 (error "no such language" name))))
63 (define *compilation-cache* '())
64 (define *decompilation-cache* '())
66 (define (invalidate-compilation-cache!)
67 (set! *decompilation-cache* '())
68 (set! *compilation-cache* '()))
70 (define (compute-translation-order from to language-translators)
73 (compute-translation-order from (lookup-language to) language-translators))
75 (let lp ((from from) (seen '()))
77 ((not (language? from))
78 (lp (lookup-language from) seen))
79 ((eq? from to) (reverse! seen))
81 (else (or-map (lambda (pair)
82 (lp (car pair) (acons from (cdr pair) seen)))
83 (language-translators from))))))))
85 (define (lookup-compilation-order from to)
86 (let ((key (cons from to)))
87 (or (assoc-ref *compilation-cache* key)
88 (let ((order (compute-translation-order from to language-compilers)))
89 (set! *compilation-cache*
90 (acons key order *compilation-cache*))
93 (define (lookup-decompilation-order from to)
94 (let ((key (cons from to)))
95 (or (assoc-ref *decompilation-cache* key)
98 (compute-translation-order to from language-decompilers)
100 (set! *decompilation-cache* (acons key order *decompilation-cache*))
103 (define (default-environment lang)
104 "Return the default compilation environment for source language LANG."
105 ((language-make-default-environment
106 (if (language? lang) lang (lookup-language lang)))))
114 (define *current-language* (make-fluid))
116 (define (current-language)
117 (or (fluid-ref *current-language*) 'scheme))