Pretty printing the environment tree

In section , we will explain how to display an Exp environment in a separate view. To pretty print the environment tree in this view, we add a specialized pretty printer named env to our modular pretty printer std.

First, add the name env to the Package resource for the std pretty printer in
contrib/Exp/pprinters/pprinters.rdb:


Centaur.Exp.ppml.std.Package: (env basic)

Then define the env pretty printer in contrib/Exp/pprinters/env/Exp-env.ppml to be:

 
   prettyprinter env of Exp is
     rules
         env(**pair) -> [<v> (**pair)] ;

         pair(*variable, *integer) -> 
             [<hv 1,3,0> env::*variable "=" env::*integer] ;

         env::integer *x -> 
              [<h> in class = env-number : integerpp(*x)] ;
         env::variable 'RESULT' -> 
              [<h> in class = env-result : "RESULT"] ;
         env::variable *x -> 
              [<h> in class = env-name : stringpp(*x)] ;
   end prettyprinter

The program formats the environment as a vertical series of pairs having the form identifier = value. Note the use of the env Ppml context. When the pair operator is matched, we know that we are in the midst of pretty printing the environment. We prefix the recursive calls on the variable and integer subtrees with the env context so that normal rules for printing these operators will be hidden and only the special environment rules will match. If we had not used a context, during the recursive call we would have lost the information about being in an environment.

The three rules preceded by the env context format the variable and integer of each pair. The RESULT is treated separately thanks to pattern matching the name of the variable. These rules actually only serve to define special resource classes for special fonts and colors; we could have otherwise used the basic integer and variable rules.

Please add the corresponding resources to contrib/Exp/pprinters/env/env.rdb:


!Colors
*visual-color*formalism-Exp.Pprinter.Format.class-env-name.foreground: black
*visual-color*formalism-Exp.Pprinter.Format.class-env-number.foreground: \resbs
RoyalBlue4
*visual-color*formalism-Exp.Pprinter.Format.class-env-result.foreground: red

!Fonts
*formalism-Exp.Pprinter.Format.class-env-name.font: \resbs
-adobe-helvetica-*-r-*-*-14-100-*-*-*-82-*-*
*formalism-Exp.Pprinter.Format.class-env-number.font : \resbs
-b&h-*-bold-r-*-*-12-*-*-*-*-79-*-*
*formalism-Exp.Pprinter.Format.class-env-result.font: \resbs
-*-new century schoolbook-*-i-*-*-17-*-*-*-*-*-*-*

And of course, reset the database from the main menu.


Tutorial