Interface:
class PrettyPrinter {
PrettyPrinter(OutputStream o, int width);
// Create a pretty-printer that formats text onto the
// output stream o while keeping the width of the output
// within width characters if possible
void write(String s);
// Print the string s on the output stream
void begin(int n);
// Start a new block with indentation increased
// by n characters
void end();
// Terminate the most recent outstanding begin
void allowBreak(int n);
// Allow a newline. Indentation will be preserved.
// If a newline is added, indentation will be
// increased locally by n.
void newline(int n);
// Force a newline. Indentation will be preserved.
// The argument n works as for allowBreak.
void flush();
// Send out the current batch of text
// to be formatted, closing all
// outstanding begin's and resetting
// the indentation level to 0.
}
Using the example program:
% java Iota.PrettyPrinter.PPTest 60
Hello World! MOVE(RV, MEM(FP, 12) + TEMP(T1)) MOVE(TEMP(T1), CALL(NAME(g), MEM(FP + 12))) MOVE(TEMP(T1), CALL(NAME(g), MEM(FP + 12)))
% java Iota.PrettyPrinter.PPTest 30
Hello World! MOVE(RV, MEM(FP, 12) + TEMP(T1)) MOVE(TEMP(T1), CALL(NAME(g), MEM(FP + 12))) MOVE(TEMP(T1), CALL(NAME(g), MEM(FP + 12)))
% java Iota.PrettyPrinter.PPTest 10
Hello World! MOVE(RV, MEM(FP, 12) + TEMP(T1)) MOVE(TEMP(T1), CALL(NAME(g), MEM(FP + 12))) MOVE(TEMP(T1), CALL(NAME(g)), MEM(FP + 12)))
The pretty-printer tries to keep higher-precedence expressions from being broken onto separate lines, as these examples demonstrate. If given an impossible demand, such as formatting the second MOVE expression in 10 characters, it does the best it can by introducing newlines wherever it is allowed to. The example program will give a good idea of how to use the pretty-printer to print out IR expressions nicely. Note that the last two expression are identical, but the pretty printer is given different input describing the formatting, resulting in the formatted output above. Either formatting style is reasonable.