-- TTT.mesa
-- Last Modified On November 30, 1982 2:21 pm By Paul Rovner
DIRECTORY
Convert,
IO,
Process USING[Detach],
Rope,
AMModel,
WorldVM;
TTT: PROGRAM
IMPORTS Convert, IO, Process, Rope, AMModel, WorldVM
= BEGIN OPEN Rope, AMModel;

stream: IO.Handle;
in: IO.Handle;

stopEverything: BOOLFALSE;

DelWatcher: SAFE PROC = TRUSTED
{DO [] ← in.GetChar[ ! ANY => {stopEverything ← TRUE; EXIT}]
ENDLOOP;
};

root: Context = RootContext[WorldVM.LocalWorld[]];

--root: Context = RootContext[WorldVM.GetWorld["Jeeves"]];

PrintContextTree: PROC[root: Context, indentation: INT] =
{ proc: PROC[context: Context] RETURNS[stop: BOOLFALSE] =
{IF stopEverything THEN RETURN[TRUE];
FOR i: INT IN [1..indentation] DO stream.PutRope[" "]; ENDLOOP;
stream.PutRope["Context named "];
stream.PutRope[ContextName[context]];
stream.PutRope[", class = "];
SELECT ContextClass[context] FROM
model => stream.PutRope["model"];
prog => stream.PutRope["prog"];
ENDCASE => ERROR;
stream.PutRope["\n"];
PrintContextTree[context, indentation + 4];
};

-- START PrintContextTree HERE
[] ← ContextChildren[root, proc];
};

PrintRootSections: PROC[root: Context, indentation: INT] =
{ proc: PROC[context: Context] RETURNS[stop: BOOLFALSE] =
{section: Section;
IF stopEverything THEN RETURN[TRUE];
section ← ContextSection[context];
FOR i: INT IN [1..indentation] DO stream.PutRope[" "]; ENDLOOP;
stream.PutRope["Top-level section named "];
stream.PutRope[SectionName[section]];
stream.PutRope["\n"];
PrintSectionTree[section, indentation + 4]};

-- START PrintRootSections HERE
[] ← ContextChildren[root, proc];
};

SourceRangeRope: PROC[source: Source] RETURNS[ans: ROPENIL] =
{ WITH s: source SELECT FROM
entire => ans ← "( Entire File )";
field => ans ← Rope.Cat["( first: ",
Convert.ValueToRope[[signed[s.firstCharIndex]]],
", last: ",
Convert.ValueToRope[[signed[s.lastCharIndex]]],
" )"];
ENDCASE => ERROR;
};

PrintSectionTree
: PROC[root: Section, indentation: INT] =
{proc: PROC[section: Section] RETURNS[stop: BOOLFALSE] =
{sectionName: ROPENIL;
IF stopEverything THEN RETURN[TRUE];
FOR i: INT IN [1..indentation] DO stream.PutRope[" "]; ENDLOOP;
stream.PutRope["Section named "];
sectionName ← SectionName[section ! ANY => CONTINUE];
stream.PutRope[IF sectionName = NIL
THEN "--{can't get section name}--"
ELSE sectionName];
stream.PutRope[", class = "];
SELECT SectionClass[section] FROM
model => stream.PutRope["model"];
prog => stream.PutRope["prog"];
interface => stream.PutRope["interface"];
proc => stream.PutRope["proc"];
statement => {stream.PutRope["statement, source range: "];
stream.PutRope[SourceRangeRope[SectionSource[section]]]};
ENDCASE => ERROR;
stream.PutRope["\n"];
PrintSectionTree[section, indentation + 4]};

-- START PrintSectionTree HERE
[] ← SectionChildren[root, proc];
};



-- START HERE

[in: in, out: stream] ← IO.CreateViewerStreams["TTT.log"];

stream.PutRope["Children of the root context ( "];
stream.PutRope[ContextName[root]];
stream.PutRope[" ) ...\n"];

Process.Detach[FORK DelWatcher[]];

PrintContextTree[root, 4];

--PrintRootSections[root, 4];

END.