See #1809, as suggested by Zipheir.

felix
From 20b75a5b05a18af36dbae42b2f346e03c802340c Mon Sep 17 00:00:00 2001
From: felix <[email protected]>
Date: Fri, 21 Oct 2022 11:24:56 +0200
Subject: [PATCH] Allow list-notation in -R option

Fixes #1809

Signed-off-by: felix <[email protected]>
---
 batch-driver.scm             | 23 ++++++++++++++++++++++-
 csi.scm                      | 20 +++++++++++++++++++-
 manual/Using the compiler    |  2 +-
 manual/Using the interpreter |  2 +-
 rules.make                   |  2 ++
 5 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/batch-driver.scm b/batch-driver.scm
index bf10dd3f..dfed3aaa 100644
--- a/batch-driver.scm
+++ b/batch-driver.scm
@@ -47,7 +47,9 @@
        chicken.pretty-print
        chicken.process-context
        chicken.string
+        chicken.port
        chicken.time
+        chicken.condition
        chicken.compiler.support
        chicken.compiler.compiler-syntax
        chicken.compiler.core
@@ -282,6 +284,25 @@
           (newline))
         xs) ) )
 
+    (define (string-trim str)
+      (let loop ((front 0) 
+                 (back (sub1 (string-length str))))
+        (cond ((= front back) "")
+              ((char-whitespace? (string-ref str front))
+               (loop (add1 front) back))
+              ((char-whitespace? (string-ref str back))
+               (loop front (sub1 back)))
+              (else (substring str front (add1 back))))))
+       
+    (define (string->extension-name str)
+      (let ((str (string-trim str)))
+        (if (and (positive? (string-length str))
+                 (char=? #\( (string-ref str 0)))
+            (handle-exceptions ex 
+              (##sys#error "invalid import specification" str)
+              (with-input-from-string str read))
+            (string->symbol str))))
+
     (define (arg-val str)
       (let* ((len (string-length str))
             (len1 (- len 1)) )
@@ -497,7 +518,7 @@
     (set! import-forms
       (append
        import-forms
-       (map (lambda (r) `(import ,(string->symbol r)))
+       (map (lambda (r) `(import ,(string->extension-name r)))
            (collect-options 'require-extension))))
 
     (when (memq 'compile-syntax options)
diff --git a/csi.scm b/csi.scm
index 9613319e..18498194 100644
--- a/csi.scm
+++ b/csi.scm
@@ -973,6 +973,24 @@ EOF
 (define-constant complex-options
   '("-D" "-feature" "-I" "-include-path" "-K" "-keyword-style" "-no-feature") )
 
+(define (string-trim str)
+  (let loop ((front 0) 
+             (back (sub1 (string-length str))))
+    (cond ((= front back) "")
+          ((char-whitespace? (string-ref str front))
+           (loop (add1 front) back))
+          ((char-whitespace? (string-ref str back))
+           (loop front (sub1 back)))
+          (else (substring str front (add1 back))))))
+   
+(define (string->extension-name str)
+  (let ((str (string-trim str)))
+    (if (and (positive? (string-length str))
+             (char=? #\( (string-ref str 0)))
+        (handle-exceptions ex 
+          (##sys#error "invalid import specification" str)
+          (with-input-from-string str read))
+        (string->symbol str))))
 
 (define (run)
   (let* ([extraopts (parse-option-string (or (get-environment-variable 
"CSI_OPTIONS") ""))]
@@ -1101,7 +1119,7 @@ EOF
                ((member arg complex-options)
                 (set! args (cdr args)) )
                ((or (string=? "-R" arg) (string=? "-require-extension" arg))
-                (eval `(import ,(string->symbol (cadr args))))
+                (eval `(import ,(string->extension-name (cadr args))))
                 (set! args (cdr args)) )
                ((or (string=? "-e" arg) (string=? "-eval" arg))
                 (evalstring (cadr args))
diff --git a/manual/Using the compiler b/manual/Using the compiler
index 9060bbaa..51cd9ffc 100644
--- a/manual/Using the compiler 
+++ b/manual/Using the compiler 
@@ -176,7 +176,7 @@ the source text should be read from standard input.
 
 ; -raw : Disables the generation of any implicit code that uses the Scheme 
libraries (that is all runtime system files besides {{runtime.c}} and 
{{chicken.h}}). Use this only when you know what you are doing.
 
-; -require-extension NAME : Loads the extension {{NAME}} before the 
compilation process commences. This is identical to adding {{(require-extension 
NAME)}} at the start of the compiled program. If {{-uses NAME}} is also given 
on the command line, then any occurrences of {{-require-extension NAME}} are 
replaced with {{(declare (uses NAME))}}. Multiple names may be given and should 
be separated by commas.
+; -require-extension NAME : Loads the extension {{NAME}} before the 
compilation process commences. This is identical to adding {{(import NAME)}} at 
the start of the compiled program. If {{-uses NAME}} is also given on the 
command line, then any occurrences of {{-require-extension NAME}} are replaced 
with {{(declare (uses NAME))}}. {{NAME}} may be given in list notation, e.g 
{{"(srfi 1)"}}.
 
 ; -setup-mode : When locating extension, search the current directory first. 
By default, extensions are located first in the ''extension repository'', where 
{{chicken-install}} stores compiled extensions and their associated metadata.
 
diff --git a/manual/Using the interpreter b/manual/Using the interpreter
index a74e6dec..8ae57d43 100644
--- a/manual/Using the interpreter      
+++ b/manual/Using the interpreter      
@@ -276,7 +276,7 @@ The options recognized by the interpreter are:
 
 ; -setup-mode : When locating extensions, search the current directory first. 
By default, extensions are located first in the ''extension repository'', where 
{{chicken-install}} stores compiled extensions and their associated metadata.
 
-; -R  -require-extension NAME : Equivalent to evaluating {{(require-extension 
NAME)}}.
+; -R  -require-extension NAME : Equivalent to evaluating {{(import NAME)}}. 
{{NAME}} may be given in list notation, e.g. {{"(srfi 1)"}}.
 
 ; -v  -version : Write the banner with version information to standard output 
and exit.
 
diff --git a/rules.make b/rules.make
index ec714550..cbcf391e 100644
--- a/rules.make
+++ b/rules.make
@@ -537,6 +537,8 @@ batch-driver.c: batch-driver.scm mini-srfi-1.scm \
                chicken.platform.import.scm \
                chicken.pretty-print.import.scm \
                chicken.process-context.import.scm \
+               chicken.condition.import.scm \
+               chicken.port.import.scm \
                chicken.string.import.scm \
                chicken.time.import.scm
 c-platform.c: c-platform.scm mini-srfi-1.scm \
-- 
2.28.0

Reply via email to