GNU TeXmacsUser manualReferenceAboutEnglish
The experimental evaluator in
Style/

1.Status

The directory

src/src/Style/

contains a second implementation of the evaluator, written with the aim of computing the style rewriting of a whole document (the tree obtained by expanding all macros) incrementally, through systematic memoization. It is a prototype:

It is nevertheless useful to know about it, both because its code is easy to confuse with the real evaluator (the function names differ only in evaluate_* versus exec_*) and because it illustrates a different design.

2.Persistent environments

Style/Environment/environment.hpp

defines an abstract class environment_rep with methods contains, read, write, remove and print, keyed by integers: variable names are converted to integers with make_tree_label. The implementations are

assoc_environment

A small array of key/value pairs, used for the bindings of one with, assign or macro application.

basic_environment

An open hash table stored in an array of hash_nodes whose size is a power of two.

list_environment

A linked list of basic environments, looked up from the head; it counts lookup misses and flattens itself with compress when there are too many.

std_environment

(in

std_environment.cpp

) The environment actually used by the evaluator. It has a flag pure, a list of local variables env, a link next to the enclosing environment, an accelerated lookup list accel and a list args of macro argument frames.

Environments are never modified in place by the evaluator. Instead, the functions assign, begin_with and end_with (and macro_down, macro_redown, macro_up in the classical variant, see below) replace the global std_env by a new environment which shares most of its structure with the old one. A with pushes a pure environment; an assign inside it produces an impure one; end_with_environment computes which assignments must survive the end of the with and re-applies them to the enclosing environment. Each of these operations is itself memoized, so that applying the same change to the same environment returns the very same environment object.

3.Memoization

A memorizer (

Style/Memorizer/memorizer.hpp

) represents one computation: its inputs, its outputs and the memorizers of its sub-computations. Memorizers are hash-consed in a global table: the constructor memorizer (memorizer_rep*) looks for an existing memorizer of the same type which is equal to the new one, and uses it instead. For evaluation, the key is the pair formed by the identities (pointers, see weak_hash and weak_equal) of the input environment and of the input tree:

tree
evaluate (tree t) {
  if (is_atomic (t)) return t;
  …
  memorizer mem= evaluate_memorizer (std_env, t);
  if (is_memorized (mem)) {
    …
    std_env= mem->get_environment ();
    return mem->get_tree ();
  }
  memorize_start ();
  tree r= evaluate_impl (t);
  decorate_ip (t, r);
  mem->set_tree (r);
  mem->set_environment (std_env);
  memorize_end ();
  …
  return mem->get_tree ();
}

Because the environments are persistent and shared, and because the clean copy of the document shares all unmodified subtrees between versions, evaluating a slightly modified document with evaluate (environment env, tree t) finds most sub-computations in the table and only recomputes the parts on the path of the modification. Whether a memorizer counts as already computed is decided by is_memorized, a test on its reference count. The functions memorize_initialize, memorize_start, memorize_end and memorize_finalize maintain the stack from which the tree of sub-computations is built.

The results receive inverse paths through decorate_ip (t, r), which attaches decorate_right (obtain_ip (t)) to all detached parts of the result, and through transfer_ip for componentwise evaluation.

4.Macro expansion by substitution

environment.hpp

defines ALTERNATIVE_MACRO_EXPANSION (the alternative, CLASSICAL_MACRO_EXPANSION, is commented out). In this mode evaluate_compound does not push argument frames. It builds an assoc_environment binding the parameters to the argument trees and substitutes them into the body with expand (tree t, assoc_environment env) (

Style/Evaluate/evaluate_macro.cpp

):

The substituted body is then decorated with the inverse path of the application and evaluated. The classical variant, with macro_down and macro_up managing a stack of argument frames inside std_environment, mirrors the implementation in Typeset/Env/.

5.Relation with Typeset/Env/

The two evaluators implement the same primitives with the same names (compare evaluate_with and edit_env_rep::exec_with, rewrite and edit_env_rep::rewrite, the re-entrancy variable reenter_rewrite_env and current_rewrite_env), but they do not share code, and the Style evaluator has not been kept in sync with all later additions to

env_exec.cpp

(themes, animations, several graphical primitives). The typesetter only uses edit_env_rep::exec. If you fix a bug in the semantics of a primitive, the place to fix it is Typeset/Env/ (together with the corresponding typesetting code in

Typeset/Concat/

and

Typeset/Bridge/

).

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".