guile-user
[Top][All Lists]
Advanced

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

Re: defining macros within eval


From: Jean Abou Samra
Subject: Re: defining macros within eval
Date: Sun, 16 Oct 2022 16:07:59 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1

Le 16/10/2022 à 11:39, Paul Jarc a écrit :
Hi.  I'm updating some old code to work with newer versions of Guile.
This example used to work with 1.8, but gives an error with 2.2 and
later:

(begin
   (eval '(define-syntax-rule (rule x) x) (current-module))
   (display (rule "ok\n")))

ERROR: Wrong type to apply: #<syntax-transformer rule>

The error happens for define-syntax-rule and define-macro, but not
plain define.



In Guile 2 and 3, the main way to run code is to byte-compile it.
This is what happens by default (Guile will print a note the first
time: "auto-compilation is enabled, ..."). In this mode, Guile will
first compile the .scm file into a .go bytecode file. This requires
doing all the macro expansion. Since the code run by eval is not
necessarily known at compile-time, it can't define macros used by
the compiled code.

What happens here is that the eval call just adds a syntax transformer
in the current module, and it would be used if it had been available
at the time of compilation, but it is too late, and it is just looked
up in the module and applied as a normal function, which fails.



It happens when eval is within begin or let, but not at
the top level.


Kind of. Something like

(eval '(define-syntax-rule (rule x) x) (current-module))
(display (rule "ok\n"))

will work in the REPL but not in a script, because in the REPL the
expansion is done step-by-step (since the result for an S-expr is
printed as soon as you enter it), where as in a file, it is done
in batch.


Is there some way to make this work?  In my real code,
the expression is read from a file, where it might be a macro
definition or anything else, and it's evaluated in a different module
from the current one.


You cannot byte-compile code in advance if it uses macros that
are only known dynamically. What you can do is using the evaluator
to run your code instead of the compiler. For example, if you set
GUILE_AUTO_COMPILE=0 and clear the bytecode cache (for me it's under
~/.cache/guile/ccache), running a script will no longer used compiled
bytecode but go through the evaluator, and in this case it works.

Be aware, though, that debugging evaluated code is a bit of a hell
because you won't get source locations for error messages, and
backtraces won't be in terms of the source code being run but in
terms of the source code of Guile's evaluator, the file
module/ice-9/eval.scm in the Guile source code.

Regards,
Jean




reply via email to

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