On Oct 13 2023, 10:48 UTC, felix.winkelm...@bevuta.com wrote:
Hi, Pietro!

Very nice. I'm on the road right now, but will take a deeper look at
this in the next days. Thanks!

Thanks. I have attached a 3rd version of the patch to avoid emitting a
cyclic dependency error when a file includes the same module it defines.

This allows us to instantiate *and* use a functor in the same file:

    ; we import the functor and what we want to parametrize it with
    (import (func))
    (import (parm))

    ; we instantiate the functor...
    ; (this file now exports the module inst)
    (module inst = (func parm))

    ; ... and use it right away
    ; (this file now depends on the module inst)
    (include inst)
    (some-function-defined-by-the-functor)


Thanks,


--
Pietro Cerutti
I have pledged to give 10% of income to effective charities
and invite you to join me - https://givingwhatwecan.org
Index: csm.scm
===================================================================
--- csm.scm     (revision 42979)
+++ csm.scm     (working copy)
@@ -80,6 +80,7 @@
 (define programs '())
 (define dry-run #f)
 (define *mod* #f)
+(define *functor-params* '())
 (define *r7rs* #f)
 (define *imports* #f)
 (define *fname* #f)
@@ -119,6 +120,7 @@
 
 (define (read-source fname prefix r7rs)
   (fluid-let ((*mod* #f)
+              (*functor-params* '())
               (*fname* (path fname prefix))
               (*prefix* prefix)
               (*r7rs* r7rs)
@@ -158,10 +160,21 @@
   (match x
     (('cond-expand clauses ...)
       (scan-form `(begin ,@(scan-cond-expand clauses))))
-    (('module name _ . body)
+    (('functor (name (fparams _) ...) . body)
       (when *mod* (fail "multiple module definitions in file ~a" *ctxt*))
+      (set! *functor-params* (map ->string fparams))
       (set! *mod* (canonical-name name))
       (scan-form `(begin ,@body)))
+    (('module name '=  (fname fparams ...))
+      (when *mod* (fail "multiple module definitions in file ~a" *ctxt*))
+      (set! *mod* (canonical-name name))
+      (scan-import fname)
+      (map scan-import fparams))
+    ((or ('module name _ . body)
+         ('module name '=  _ . body))
+      (when *mod* (fail "multiple module definitions in file ~a" *ctxt*))
+      (set! *mod* (canonical-name name))
+      (scan-form `(begin ,@body)))
     (('define-library name . body)
       (when *mod* (fail "multiple library definitions in file ~a" *ctxt*))
       (set! *mod* (canonical-name name))
@@ -235,8 +248,10 @@
     (((or 'only 'prefix 'except 'rename) imp . _) 
       (scan-import imp))
     (_ (let ((name (canonical-name imp)))
-         (push! name *imports*)
-         (when syntax (mark-syntax-module name))))))
+         (unless (or (member name *functor-params* string=)
+                     (and *mod* (string= name *mod*)))
+           (push! name *imports*)
+           (when syntax (mark-syntax-module name)))))))
 
 (define (path fname #!optional prefix)
   (cond ((string-prefix? "/" fname) fname)

Reply via email to