lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Let define-session-public place variables natively into parser (issu


From: dak
Subject: Re: Let define-session-public place variables natively into parser (issue 300110043 by address@hidden)
Date: Thu, 26 May 2016 23:03:44 -0700

Reviewers: carl.d.sorensen_gmail.com,

Message:
On 2016/05/27 04:23:29, Carl wrote:
Looks very nice to me.  I couldn't have done it, but I love how it
works.

Can you elaborate on the "couldn't have done it" part?
define-session-public copies some ugly code (concerning undead
structures or something) from define-session.  Is it that, or the
heavily underdocumented module system of Guile that's involved here?
Because I have a patch reorganizing the session stuff in limbo that
would remove/replace the undead machinery with something saner.
Possibly giving fewer useful diagnostics, but we haven't had any
_useful_ hint from that area for years.

We certainly want to minimize "couldn't have done it" code since we
don't want to depend on particular persons for the continued well-being
of LilyPond.

Description:
Let define-session-public place variables natively into parser

Putting them as native variables in the parser module (rather than
using export/import) makes `set!' and `define' equivalent rather than
having `define' create a shadowing definition of the session variable.
That is important in order to avoid the values of the variable
diverging between parser module and `lily'.

Please review this at https://codereview.appspot.com/300110043/

Affected files (+18, -4 lines):
  M scm/lily.scm


Index: scm/lily.scm
diff --git a/scm/lily.scm b/scm/lily.scm
index 5ead4e6c9cf9da25e8f12bcf08cdfa145692f613..ce3486cf3f4e70f9479fa6b4ac91e8a596c5a490 100644
--- a/scm/lily.scm
+++ b/scm/lily.scm
@@ -86,6 +86,7 @@
 ;;

 (define lilypond-declarations '())
+(define lilypond-exports '())
 (define after-session-hook (make-hook))

 (define-public (call-after-session thunk)
@@ -115,10 +116,14 @@ session has started."
   `(,add-session-variable ',name ,value))

 (defmacro-public define-session-public (name value)
-  "Like @code{define-session}, but also exports @var{name}."
-  `(begin
-     (define-session ,name ,value)
-     (export ,name)))
+ "Like @code{define-session}, but also exports @var{name} into parser modules."
+  (define (add-session-variable name value)
+    (if (ly:undead? lilypond-declarations)
+        (ly:error (_ "define-session-public used after session start")))
+    (let ((var (make-variable value)))
+      (module-add! (current-module) name var)
+      (set! lilypond-exports (acons name var lilypond-exports))))
+  `(,add-session-variable ',name ,value))

 (define (session-terminate)
   (if (ly:undead? lilypond-declarations)
@@ -158,7 +163,16 @@ variables to their value after the initial call of @var{thunk}."
                  (module-add! (current-module) (car p) var))))
          (ly:get-undead lilypond-declarations)))
       (begin
+        ;; import all public session variables natively into parser
+        ;; module.  That makes them behave identically under define/set!
+        (for-each (lambda (v)
+                    (module-add! (current-module) (car v) (cdr v)))
+                  lilypond-exports)
+        ;; Initialize first session
         (thunk)
+        ;; lilypond-exports is no longer needed since we will grab its
+        ;; values from (current-module).
+        (set! lilypond-exports #f)
         (set! lilypond-interfaces
               (filter (lambda (m) (eq? 'interface (module-kind m)))
                       (module-uses (current-module))))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]