PrettyPrintCommands.Mesa
Last Edited by: Spreitzer, September 19, 1985 7:15:05 pm PDT
Mike Spreitzer July 30, 1986 7:55:35 pm PDT
DIRECTORY AMTypes, Commander, Interpreter, IO, List, PrintTV, Rope, StructuredStreams, SymTab, UnparserBuffer;
PrettyPrintCommands: CEDAR PROGRAM
IMPORTS Commander, Interpreter, IO, List, PrintTV, Rope, StructuredStreams, SymTab, UnparserBuffer =
BEGIN
ROPE: TYPE = Rope.ROPE;
depth: INT ← 4;
maxLength: INT ← 32;
width: INT ← 60;
verbose: BOOLFALSE;
PrettyPrint: PROC [cmd: Commander.Handle] RETURNS [result: REF ANYNIL, msg: ROPENIL]--Commander.CommandProc-- =
BEGIN
thisDepth: INT ← depth;
thisMaxLength: INT ← maxLength;
thisWidth: INT ← width;
thisVerbose: BOOLEAN ← verbose;
noResult: BOOL;
exprRope: ROPE;
val: AMTypes.TV;
errRope: ROPE;
in: IO.STREAM ← IO.RIS[cmd.commandLine];
out: IO.STREAM ← cmd.out;
ubh: UnparserBuffer.Handle ← UnparserBuffer.NewInittedHandle[[output: [stream[out]]]];
put: IO.STREAM;
symTab: SymTab.Ref;
DO
switch: CHAR;
sticky: BOOLEANFALSE;
[] ← in.SkipWhitespace[];
IF in.EndOf[] THEN
RETURN [NIL, "No expression to PrettyPrint"];
IF in.PeekChar[] # '/ THEN EXIT;
IF in.GetChar[] # '/ THEN ERROR;
IF in.EndOf[] THEN RETURN [NIL, "No switch after slash"];
IF in.PeekChar[] = '/ THEN
BEGIN
IF in.GetChar[] # '/ THEN ERROR;
sticky ← TRUE;
IF in.EndOf[] THEN RETURN [NIL, "No switch after double slash"];
END;
switch ← in.GetChar[];
[] ← in.SkipWhitespace[];
IF (IF NOT in.EndOf[] THEN in.PeekChar[] = ': ELSE FALSE) THEN [] ← in.GetChar[];
IF Rope.Find["vVwWdDlL", Rope.FromChar[switch]] >= 0 THEN {
IF in.EndOf[] THEN
RETURN [NIL, "No value for switch"];
}
ELSE RETURN [NIL, "Invalid switch"];
SELECT switch FROM
'v, 'V => {thisVerbose ← in.GetBool[]; IF sticky THEN verbose ← thisVerbose};
'l, 'L => {thisMaxLength ← in.GetInt[]; IF sticky THEN maxLength ← thisMaxLength};
'd, 'D => {thisDepth ← in.GetInt[]; IF sticky THEN depth ← thisDepth};
'w, 'W => {thisWidth ← in.GetInt[]; IF sticky THEN width ← thisWidth};
ENDCASE => NULL;
ENDLOOP;
ubh.margin ← thisWidth;
put ← StructuredStreams.Create[ubh];
exprRope ← in.GetLineRope[];
TRUSTED {symTab ← LOOPHOLE[List.Assoc[$SymTab, cmd.propertyList]]};
IF symTab = NIL THEN {
symTab ← SymTab.Create[];
[] ← List.PutAssoc[key: $SymTab, val: symTab, aList: cmd.propertyList];
};
[val, errRope, noResult] ← Interpreter.Evaluate[rope: exprRope, symTab: symTab];
IF errRope.Length[] # 0
THEN out.PutRope[Rope.Cat["Error: ", errRope]]
ELSE IF noResult
THEN out.PutRope["(No result)"]
ELSE PrintTV.Print[tv: val, put: put, depth: thisDepth, width: thisMaxLength, verbose: thisVerbose];
put.PutRope["\n"];
put.Close[];
END;
Commander.Register[key: "PrettyPrint", proc: PrettyPrint, interpreted: FALSE, doc: "
PrettyPrint (/v bool | /d int | /l int | /w int)* expression
evaluates the expression and prettily prints it. The switches may give values for the verbosity (default FALSE), depth (default 4), maximum list length (default 32), and target width (default 60) of the printout. Doubling slash makes it sticky."];
END.