unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* custom module loading and compilation
@ 2024-05-11  8:02 Paul Jarc
  2024-05-13 13:54 ` Nala Ginrut
  2024-05-23 21:05 ` Paul Jarc
  0 siblings, 2 replies; 3+ messages in thread
From: Paul Jarc @ 2024-05-11  8:02 UTC (permalink / raw)
  To: guile-user

Hi.  I'm writing a custom module system that avoids the standard
search path and list-of-symbols naming scheme.  Instead of using
use-modules and define-module, I have a roughly similar procedure,
let's call it my-load, that creates a module with make-module and
loads a file into it with primitive-load.  I want to make my-load
automatically available within the modules I load so that they can use
it to load other modules too.  So my-load adds itself to each new
module using module-define! before calling primitive-load.

This works when I run Guile with --no-auto-compile, but with
compilation enabled, I get:
;;; Unbound variable: my-load
How can I make the binding visible to the compiler?

;;main.scm
(define (my-load path-to-macros)
  (let ((new-module (make-module 0 `(,(resolve-interface '(guile))))))
    (module-define! new-module 'my-load my-load)
    (save-module-excursion
      (lambda ()
        (set-current-module new-module)
        (primitive-load path-to-macros)))
    (let ((interface '(my-macro)))
      (for-each (lambda (sym)
                  (module-define! (current-module) sym
                                  (module-ref new-module sym)))
                interface))))
(eval-when (compile load eval)
  (my-load "/path/to/macros1.scm"))
(display (my-macro 5))
(newline)

;;macros1.scm
(eval-when (compile load eval)
  (my-load "/path/to/macros2.scm"))
(display (my-macro 3))
(newline)

;;macros2.scm
(define-syntax my-macro
  (syntax-rules ()
    ((_ x) (+ x x))))



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: custom module loading and compilation
  2024-05-11  8:02 custom module loading and compilation Paul Jarc
@ 2024-05-13 13:54 ` Nala Ginrut
  2024-05-23 21:05 ` Paul Jarc
  1 sibling, 0 replies; 3+ messages in thread
From: Nala Ginrut @ 2024-05-13 13:54 UTC (permalink / raw)
  To: Paul Jarc; +Cc: guile-user

Hi Paul!
I don't know why you don't use the standard module definition and
import/export interface, but if you do want to play modules with
the introspection functions, here's a hint for you:

---------------------------------------------------------------------------
 (define (export-all-from-module! module-name)
   (let ((mod (resolve-module module-name)))
     (module-for-each (lambda (s m)
                        (module-add! (current-module) s m)) mod))))
---------------------------------------------------------------------------

This function can be used to export all symbols from a specified
module-name. You may modify it for your case.

Best regards.

On Sat, May 11, 2024 at 5:03 PM Paul Jarc <prj@case.edu> wrote:

> Hi.  I'm writing a custom module system that avoids the standard
> search path and list-of-symbols naming scheme.  Instead of using
> use-modules and define-module, I have a roughly similar procedure,
> let's call it my-load, that creates a module with make-module and
> loads a file into it with primitive-load.  I want to make my-load
> automatically available within the modules I load so that they can use
> it to load other modules too.  So my-load adds itself to each new
> module using module-define! before calling primitive-load.
>
> This works when I run Guile with --no-auto-compile, but with
> compilation enabled, I get:
> ;;; Unbound variable: my-load
> How can I make the binding visible to the compiler?
>
> ;;main.scm
> (define (my-load path-to-macros)
>   (let ((new-module (make-module 0 `(,(resolve-interface '(guile))))))
>     (module-define! new-module 'my-load my-load)
>     (save-module-excursion
>       (lambda ()
>         (set-current-module new-module)
>         (primitive-load path-to-macros)))
>     (let ((interface '(my-macro)))
>       (for-each (lambda (sym)
>                   (module-define! (current-module) sym
>                                   (module-ref new-module sym)))
>                 interface))))
> (eval-when (compile load eval)
>   (my-load "/path/to/macros1.scm"))
> (display (my-macro 5))
> (newline)
>
> ;;macros1.scm
> (eval-when (compile load eval)
>   (my-load "/path/to/macros2.scm"))
> (display (my-macro 3))
> (newline)
>
> ;;macros2.scm
> (define-syntax my-macro
>   (syntax-rules ()
>     ((_ x) (+ x x))))
>
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: custom module loading and compilation
  2024-05-11  8:02 custom module loading and compilation Paul Jarc
  2024-05-13 13:54 ` Nala Ginrut
@ 2024-05-23 21:05 ` Paul Jarc
  1 sibling, 0 replies; 3+ messages in thread
From: Paul Jarc @ 2024-05-23 21:05 UTC (permalink / raw)
  To: guile-user

I wrote:
> This works when I run Guile with --no-auto-compile, but with
> compilation enabled, I get:
> ;;; Unbound variable: my-load
> How can I make the binding visible to the compiler?

In case anyone else runs into this: I solved it by wrapping the
definition of my-load in (eval-when (expand eval load) ...).



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-23 21:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-11  8:02 custom module loading and compilation Paul Jarc
2024-05-13 13:54 ` Nala Ginrut
2024-05-23 21:05 ` Paul Jarc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).