![]() |
femtolisp: loading files, and the modules | ![]() |
init-femtolisp.scm
is loaded with the load of femtolisp. It loads
r5rs-femtolisp.scm
, then
boot-femtolisp.scm
, both as plain global definitions; from there on load is the one of
boot-femtolisp.scm
. It then loads the module (kernel boot compat-femtolisp), and the
init-kernel.scm
and
init-texmacs.scm
which all the interpreters share.
femtolisp has one global environment, and the modules of TeXmacs define the same private names over and over (more than two hundred names are private to several modules). So the private definitions of a module are renamed. When the file of a module is loaded (%load-forms):
Its forms are all read first, and scanned (%scan-definitions) for the plain define and define-macro at the top level, also inside begin, if, when, unless and cond.
Each such name f of the module (a b) is given the global name f@a/b (%module-declare-private!).
Each form is then expanded and compiled with *current-module* bound to the module. The expander and the compiler of femtolisp call the hook resolve-global on every global name (patch 0003);
boot-femtolisp.scm
defines it to map the private names of the current module to their global names. This covers the variables, the functions, set! and the macros.
The public definitions are global under their own names: define-public, define-public-macro, provide-public, tm-define, tm-define-macro, tm-menu, tm-widget, menu-bind, tm-property, export. A name which a module defines both with a plain define and with tm-define stays private in it, as in Guile, where the binding of the module hides the one of the user module.
More exactly: only a name written as a plain define or define-macro at one of the places which are scanned is private. Every other definition is global, also the ones which a macro makes (define-table, define-preferences…). And a plain define of a name is made public only by a define-public, define-public-macro, provide-public, export or re-export of the same name in the file.
A module is the vector #(module name privates), kept in the table *modules*; privates is the table of its private names. In the user module *current-module* is #f and nothing is renamed; (current-module) then gives the symbol texmacs-user.
What differs from Guile:
:use, use-modules, inherit-modules and import-from load the modules; they do not restrict what a module sees. Every public name is visible everywhere, as with S7.
The global name of a private definition can be used to reach it from outside, for debugging: (top-level-value (symbol "f@a/b")), or (module-ref (resolve-module '(a b)) 'f).
kernel/texmacs/tm-define.scm
has branches for femtolisp, close to those for S7:
a definition is (define-global! 'f value): global under its quoted name, which resolve-global does not rename;
former and the table of the names of the defined functions read the global function with (top-level-value 'f), also in a module which defines f privately;
tm-define-macro defines the macro with define-public-macro;
lazy-define finds the function with module-ref.
femtolisp expands the macros of a form when it compiles it; Guile and S7 expand them when they first evaluate it. The code of TeXmacs relies on the second behaviour in three ways, each of which was a source of bugs:
A function uses a macro defined later, in a module loaded later (delayed, in
tm-plugins.scm
).
Code which never runs is never expanded: a call of a macro which fails to expand, in a widget which a configuration never shows, is harmless.
A macro with a side effect when it is expanded has it only if the code runs: (when (supports-coq?) (lazy-keyboard …)) declared the keyboard of Coq without Coq.
The lazy function bodies of the next chapter answer the first two for the code inside functions: a body is expanded when it is first called. The third remains for the forms at the top level of a file, which are expanded when the file is loaded, whatever they test. Such macros were changed to have their effect when the code runs: lazy-format, lazy-keyboard, lazy-define. A new macro must do the same: its expansion may compute, it must not register anything.
With TEXMACS_FL_EAGER=1 the bodies are expanded when the file is loaded, as femtolisp does by itself. Two older devices then take over: a call of a name which is neither bound, nor a macro, nor defined by the file being loaded when it is compiled, is compiled at its first evaluation (the hook compile-unknown-call, patch 0010, and %late-call), and a macro whose expansion fails gives code which raises the error when it runs (*defer-macro-errors*, patch 0013).
The functions of system.lsp and compiler.lsp call each other by their global names, and TeXmacs defines public functions with the same names (make-label, print…), which broke the compiler. The boot image is therefore made by compiling these two files twice (patch 0009): the second time, their references to each other go to private names %fl:name, so TeXmacs may redefine the public ones.