DIRECTORY
  Ascii USING [CR, TAB],
  Format USING [Text],
  ParseDefs USING [commentString],
  PrintingDefs USING [outProc, echoSource],
  Storage USING [AppendChar, CopyString, FreeString];

PrintingImpl: PROGRAM IMPORTS Format, ParseDefs, PrintingDefs, Storage EXPORTS PrintingDefs = {

    
  OutCode: PUBLIC PROCEDURE [s: LONG STRING, indent: CARDINAL] = {
  
    FOR i: CARDINAL IN [0..indent) DO
        Format.Text[PrintingDefs.outProc↑, "    "L];
        ENDLOOP;
    IF s # NIL THEN Format.Text[PrintingDefs.outProc↑, s]; };
    
      
  GetComment: PUBLIC PROCEDURE [] RETURNS [result: LONG STRING] = {
  
    limit, j: CARDINAL;
    newComment: LONG STRING;
    
    result ← Storage.CopyString[s: ""L, longer: 128];
    FOR limit DECREASING IN [0..ParseDefs.commentString.length) DO
        IF ParseDefs.commentString.text[limit] = Ascii.CR THEN EXIT
    REPEAT
        FINISHED => RETURN [result];
    ENDLOOP;
    newComment ← Storage.CopyString[s: ""L, longer: 128];
    FOR j IN [0..limit] DO
        Storage.AppendChar[@result, ParseDefs.commentString.text[j]];
        ENDLOOP;
    FOR j IN (limit..ParseDefs.commentString.length) DO
        Storage.AppendChar[@newComment, ParseDefs.commentString.text[j]];
        ENDLOOP;
    Storage.FreeString[ParseDefs.commentString];
    ParseDefs.commentString ← newComment; };
    
    
  PrintComments: PUBLIC PROCEDURE [s: LONG STRING, indent: CARDINAL] = {
  
    i: CARDINAL;
    buffer: LONG STRING = [1];
    
    IF NOT PrintingDefs.echoSource THEN RETURN;
    IF s.length = 0 THEN RETURN;
    buffer.length ← 1;
    OutCode["--"L, indent + 1];
    i ← 0;
    WHILE i < s.length AND s.text[i] = Ascii.TAB DO
        s.text[i] ← ' ;
        i ← i + 1;
	ENDLOOP;
    i ← 0;
    WHILE i < s.length DO
        SELECT s.text[i] FROM
	    Ascii.CR => {
	        OutCode["\n"L, 0];
	        IF i < s.length - 1 THEN {
		    j: CARDINAL;
		    OutCode["--"L, indent + 1];
		    j ← i + 1;
		    WHILE j < s.length AND s.text[j] = Ascii.TAB DO
		        s.text[j] ← ' ;
		        j ← j + 1;
			ENDLOOP; }; };
	    '- => {
	        IF s.length > i + 1 AND s.text[i + 1] = '-
		    THEN s.text[i + 1] ← '←;
		buffer.text[0] ← s.text[i];
	        OutCode[buffer, 0]; };
	    ENDCASE => {
	    buffer.text[0] ← s.text[i];
	    OutCode[buffer, 0]; };
	i ← i + 1;
        ENDLOOP; };
    
  }.