Constants

Before compiling this program, we should make a few improvements. It would be helpful to localize separator information at the beginning of our program so that if we wanted to change spacing, we could just change one constant instead of lots of numbers. To declare constants in Ppml, we use the optional constant section, which must be headed by the keyword constant. To replace the number 2 in our boxes with a constant which we call tab, we write:


prettyprinter basic of Exp is

constant
   tab = 2 ;

rules
   *x !0 -> [<h> "..."] ;

   exp_s(**exps,*exp)   -> [<v 0,0> ([<h 0> **exps ";"]) *exp] ;

   plus  (*exp1, *exp2) -> [<hv 1,tab,0> *exp1  "+"  *exp2] ;
   minus (*exp1, *exp2) -> [<hv 1,tab,0> *exp1  "-"  *exp2] ;
   prod  (*exp1, *exp2) -> [<hv 1,tab,0> *exp1  "*"  *exp2] ;      

   uminus(*exp)         -> [<h 0> "-" *exp ] ;      

   assign(*var,  *exp2) -> [<hv 1,tab,0> *var   ":=" *exp2] ;

end prettyprinter


Tutorial