mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-10 05:50:26 +02:00
Use more `match' in (system base compile)
* module/system/base/compile.scm (validate-options): New helper. (compile-file, compile-and-load, compile): Call the new helper. (compile-passes, compile-fold, find-language-joint): (default-language-joiner, decompile-passes, decompile-fold): Use more "match".
This commit is contained in:
parent
cdb9030f45
commit
6bb996ec66
1 changed files with 59 additions and 36 deletions
|
@ -21,6 +21,7 @@
|
||||||
#:use-module (system base language)
|
#:use-module (system base language)
|
||||||
#:use-module (system base message)
|
#:use-module (system base message)
|
||||||
#:use-module (ice-9 receive)
|
#:use-module (ice-9 receive)
|
||||||
|
#:use-module (ice-9 match)
|
||||||
#:export (compiled-file-name
|
#:export (compiled-file-name
|
||||||
compile-file
|
compile-file
|
||||||
compile-and-load
|
compile-and-load
|
||||||
|
@ -127,6 +128,28 @@
|
||||||
(and (false-if-exception (ensure-directory (dirname f)))
|
(and (false-if-exception (ensure-directory (dirname f)))
|
||||||
f))))
|
f))))
|
||||||
|
|
||||||
|
(define (validate-options opts)
|
||||||
|
(define (validate-warnings warnings)
|
||||||
|
(match warnings
|
||||||
|
(() (values))
|
||||||
|
((w . warnings)
|
||||||
|
(unless (lookup-warning-type w)
|
||||||
|
(warning 'unsupported-warning #f w))
|
||||||
|
(validate-warnings warnings))))
|
||||||
|
(match opts
|
||||||
|
(() (values))
|
||||||
|
((kw arg . opts)
|
||||||
|
(match kw
|
||||||
|
(#:warnings (validate-warnings arg))
|
||||||
|
((? keyword?) (values))
|
||||||
|
(_
|
||||||
|
;; Programming error.
|
||||||
|
(warn "malformed options list: not a keyword" kw)))
|
||||||
|
(validate-options opts))
|
||||||
|
(_
|
||||||
|
;; Programming error.
|
||||||
|
(warn "malformed options list: expected keyword and arg pair" opts))))
|
||||||
|
|
||||||
(define* (compile-file file #:key
|
(define* (compile-file file #:key
|
||||||
(output-file #f)
|
(output-file #f)
|
||||||
(from (current-language))
|
(from (current-language))
|
||||||
|
@ -134,6 +157,7 @@
|
||||||
(env (default-environment from))
|
(env (default-environment from))
|
||||||
(opts '())
|
(opts '())
|
||||||
(canonicalization 'relative))
|
(canonicalization 'relative))
|
||||||
|
(validate-options opts)
|
||||||
(with-fluids ((%file-port-name-canonicalization canonicalization))
|
(with-fluids ((%file-port-name-canonicalization canonicalization))
|
||||||
(let* ((comp (or output-file (compiled-file-name file)
|
(let* ((comp (or output-file (compiled-file-name file)
|
||||||
(error "failed to create path for auto-compiled file"
|
(error "failed to create path for auto-compiled file"
|
||||||
|
@ -156,6 +180,7 @@
|
||||||
(define* (compile-and-load file #:key (from (current-language)) (to 'value)
|
(define* (compile-and-load file #:key (from (current-language)) (to 'value)
|
||||||
(env (current-module)) (opts '())
|
(env (current-module)) (opts '())
|
||||||
(canonicalization 'relative))
|
(canonicalization 'relative))
|
||||||
|
(validate-options opts)
|
||||||
(with-fluids ((%file-port-name-canonicalization canonicalization))
|
(with-fluids ((%file-port-name-canonicalization canonicalization))
|
||||||
(read-and-compile (open-input-file file)
|
(read-and-compile (open-input-file file)
|
||||||
#:from from #:to to #:opts opts
|
#:from from #:to to #:opts opts
|
||||||
|
@ -167,33 +192,39 @@
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
(define (compile-passes from to opts)
|
(define (compile-passes from to opts)
|
||||||
(map cdr
|
(match (lookup-compilation-order from to)
|
||||||
(or (lookup-compilation-order from to)
|
(((langs . passes) ...) passes)
|
||||||
(error "no way to compile" from "to" to))))
|
(_ (error "no way to compile" from "to" to))))
|
||||||
|
|
||||||
(define (compile-fold passes exp env opts)
|
(define (compile-fold passes exp env opts)
|
||||||
(let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
|
(let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
|
||||||
(if (null? passes)
|
(match passes
|
||||||
(values x e cenv)
|
(() (values x e cenv))
|
||||||
(receive (x e new-cenv) ((car passes) x e opts)
|
((pass . passes)
|
||||||
(lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
|
(receive (x e new-cenv) (pass x e opts)
|
||||||
|
(lp passes x e (if first? new-cenv cenv) #f))))))
|
||||||
|
|
||||||
(define (find-language-joint from to)
|
(define (find-language-joint from to)
|
||||||
(let lp ((in (reverse (or (lookup-compilation-order from to)
|
(match (lookup-compilation-order from to)
|
||||||
(error "no way to compile" from "to" to))))
|
(((langs . passes) ...)
|
||||||
(lang to))
|
(or (let lp ((langs langs))
|
||||||
(cond ((null? in) to)
|
(match langs
|
||||||
((language-joiner lang) lang)
|
(() #f)
|
||||||
(else
|
((lang . langs)
|
||||||
(lp (cdr in) (caar in))))))
|
(or (lp langs)
|
||||||
|
(and (language-joiner lang)
|
||||||
|
lang)))))
|
||||||
|
to))
|
||||||
|
(_ (error "no way to compile" from "to" to))))
|
||||||
|
|
||||||
(define (default-language-joiner lang)
|
(define (default-language-joiner lang)
|
||||||
(lambda (exps env)
|
(lambda (exps env)
|
||||||
(if (and (pair? exps) (null? (cdr exps)))
|
(match exps
|
||||||
(car exps)
|
((exp) exp)
|
||||||
|
(_
|
||||||
(error
|
(error
|
||||||
"Multiple expressions read and compiled, but language has no joiner"
|
"Multiple expressions read and compiled, but language has no joiner"
|
||||||
lang))))
|
lang)))))
|
||||||
|
|
||||||
(define (read-and-parse lang port cenv)
|
(define (read-and-parse lang port cenv)
|
||||||
(let ((exp ((language-reader lang) port cenv)))
|
(let ((exp ((language-reader lang) port cenv)))
|
||||||
|
@ -236,16 +267,7 @@
|
||||||
(to 'value)
|
(to 'value)
|
||||||
(env (default-environment from))
|
(env (default-environment from))
|
||||||
(opts '()))
|
(opts '()))
|
||||||
|
(validate-options opts)
|
||||||
(let ((warnings (memq #:warnings opts)))
|
|
||||||
(if (pair? warnings)
|
|
||||||
(let ((warnings (cadr warnings)))
|
|
||||||
;; Sanity-check the requested warnings.
|
|
||||||
(for-each (lambda (w)
|
|
||||||
(or (lookup-warning-type w)
|
|
||||||
(warning 'unsupported-warning #f w)))
|
|
||||||
warnings))))
|
|
||||||
|
|
||||||
(receive (exp env cenv)
|
(receive (exp env cenv)
|
||||||
(compile-fold (compile-passes from to opts) x env opts)
|
(compile-fold (compile-passes from to opts) x env opts)
|
||||||
exp))
|
exp))
|
||||||
|
@ -256,15 +278,16 @@
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
(define (decompile-passes from to opts)
|
(define (decompile-passes from to opts)
|
||||||
(map cdr
|
(match (lookup-decompilation-order from to)
|
||||||
(or (lookup-decompilation-order from to)
|
(((langs . passes) ...) passes)
|
||||||
(error "no way to decompile" from "to" to))))
|
(_ (error "no way to decompile" from "to" to))))
|
||||||
|
|
||||||
(define (decompile-fold passes exp env opts)
|
(define (decompile-fold passes exp env opts)
|
||||||
(if (null? passes)
|
(match passes
|
||||||
(values exp env)
|
(() (values exp env))
|
||||||
(receive (exp env) ((car passes) exp env opts)
|
((pass . passes)
|
||||||
(decompile-fold (cdr passes) exp env opts))))
|
(receive (exp env) (pass exp env opts)
|
||||||
|
(decompile-fold passes exp env opts)))))
|
||||||
|
|
||||||
(define* (decompile x #:key
|
(define* (decompile x #:key
|
||||||
(env #f)
|
(env #f)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue