![]() |
Macro expansion and evaluation | ![]() |
A TeXmacs document is a tree. Only a fixed set of tags (concat, with, frac, macro, arg, …) is known to the C++ kernel; all other tags, such as section, theorem or strong, are macros which are defined in style files by assignments like
<assign|hello|<macro|name|Hello name!>> |
Such definitions are stored in the environment, a map from names to trees which also contains all ordinary typesetting variables (font-size, par-width, mode, …). When the typesetter meets a tag which is not a built-in primitive, it looks up the tag name in the environment and, if a macro is found there, expands it.
This chapter explains how this works inside the kernel. It covers:
the typesetting environment, edit_env, and the way it is initialized from style files;
the evaluator edit_env_rep::exec, which computes the value of a tree (used for environment variables, conditions, lengths and so on);
macro expansion during typesetting, which is done lazily by the typesetter itself so that the arguments of a macro remain editable and keep their correct source locations;
the data relation descriptor (DRD), which records properties of tags such as their arity or the accessibility of their children, and which is computed to a large extent from the macro definitions;
the experimental memoizing evaluator in
Style/
.
The semantics of the individual primitives from the point of view of a style file writer are documented in the chapter on primitives for writing style files, in particular the macro primitives, the environment primitives and the evaluation control primitives. The present chapter is about the implementation. The line and page breaking algorithms, and the general organization of the typesetter in bridges and concaters, are described in the chapter on the typesetter.
All C++ file names below are relative to
src/src/
, and all Scheme file names are relative to
src/TeXmacs/progs/
.
The single most important thing to understand about TeXmacs macros is that there are two quite different ways in which a macro application is processed.
The method tree edit_env_rep::exec (tree t) in
Typeset/Env/env_exec.cpp
computes the value of t in the current environment: macros are expanded, arguments are substituted, arithmetic and string primitives are computed, if-conditions are decided, and the result is a new tree. For instance, with the above definition of hello, evaluating (hello "world") yields (concat "Hello " "world" "!"). The result is a fresh tree which is not attached to the document, so it has no meaningful source location. Evaluation is used whenever the typesetter needs a value: the new values in a with, the condition of an if, a length, the value of value, the arguments of extern, and so on.
When the typesetter meets (hello "world") in the document, it does not call exec on it. Instead it pushes a frame which binds the macro parameter name to the source subtree "world" together with its source path, and then typesets the macro body (concat "Hello " (arg "name") "!") directly. The strings of the body get a decoration path (they are not editable), while the arg primitive typesets the original subtree with its own inverse path, so that the cursor can be moved into world and the user can edit it. The code for this lives in
Typeset/Concat/concat_macro.cpp
(inline material) and in the bridges
Typeset/Bridge/bridge_compound.cpp
,
bridge_argument.cpp
etc. (paragraph-level material).
Both mechanisms share the same environment object and the same stacks of macro arguments (edit_env_rep::macro_arg and edit_env_rep::macro_src). They must be kept consistent: several functions in
env_exec.cpp
carry comments such as this case must be kept consistent with concater_rep::typeset_if.
There is a third, related, mechanism: rewriting (edit_env_rep::rewrite). A few primitives (extern, include, with-package, map-args, rewrite-inactive) are first rewritten into an ordinary tree, which is then typeset (or evaluated). Rewriting is designed so that parts of the result which come from the document keep their source location.
Finally, the directory
Style/
contains an experimental, memoizing re-implementation of the evaluator which works on persistent environments. It is only compiled when TeXmacs is configured with the ENABLE_EXPERIMENTAL option of
CMakeLists.txt
(which defines the preprocessor symbol EXPERIMENTAL) and it is not used for the actual typesetting; see the experimental evaluator.
Typeset/env.hpp
Declaration of the class edit_env_rep, the Env_* categories of environment variables and various constants.
Typeset/Env/env.cpp
Construction of the environment, global manipulations (write_env, patch_env, read_env) and the bookkeeping which allows bridges to cache their effect on the environment (local_start, local_update, local_end).
Typeset/Env/env_exec.cpp
The evaluator: exec, rewrite, the partial evaluator exec_until, expand and depends, and the implementation of all computational primitives.
Typeset/Env/env_semantics.cpp
The categories of the built-in environment variables (initialize_default_var_type) and the update methods which recompute cached C++ fields (fonts, colors, page parameters, …) when a variable changes.
Typeset/Env/env_default.cpp
The default environment default_env, built by initialize_default_env.
Typeset/Env/env_length.cpp
Length arithmetic and length units.
Typeset/Env/env_inactive.cpp
Rewriting of inactive markup (source code display of macros).
Typeset/Env/env_animate.cpp
Evaluation of animations.
Typeset/Concat/concat_macro.cpp
Inline typesetting of with, assign, macro applications, arg, mark, expand-as, eval, rewritable and executable primitives.
Typeset/Bridge/bridge_compound.cpp,
bridge_argument.cpp,
bridge_with.cpp,
bridge_rewrite.cpp,
bridge_eval.cpp,
bridge_expand_as.cpp
The corresponding paragraph-level (incremental) typesetting.
Data/Drd/
The data relation descriptor: drd_info (
drd_info.hpp
), tag_info (
tag_info.hpp
), the standard DRD for built-in tags (
drd_std.cpp
), the global access modes (
drd_mode.hpp
) and the names of built-in environment variables (
vars.hpp
).
Data/Document/new_style.cpp
Computation and caching of the environment and the DRD of a style.
Edit/Editor/edit_typeset.cpp
Initialization of the environment of a buffer (typeset_preamble, typeset_prepare) and computation of the environment at the cursor (typeset_exec_until).
Style/
The experimental memoizing evaluator.
kernel/texmacs/tm-secure.scm
The Scheme side of the security check for extern.