-- BBContext.mesa
-- Russ Atkinson, October 26, 1982 1:27 pm

DIRECTORY
Rope USING [ROPE],
RTBasic USING [TV],
WorldVM USING [World];

BBContext: CEDAR DEFINITIONS
= BEGIN OPEN Rope, RTBasic, WorldVM;

Context: TYPE = REF ContextRep;
ContextRep: TYPE = PRIVATE RECORD
[world: World ← NIL,
headLF: TVNIL,
headGF: TVNIL,
active: BOOLTRUE
];

ContextForLocalFrame: PROC [lf: TV] RETURNS [Context];
-- returns context for the given local frame
-- the world and gf components are inherited

ContextForGlobalFrame: PROC [gf: TV] RETURNS [Context];
-- returns context for the given global frame
-- the world component is inherited

ContextForWorld: PROC [world: World] RETURNS [Context];
-- returns context for the given world
-- the gf and lf components will be null

GetContents: PROC [context: Context] RETURNS [world: World, gf,lf: TV];
-- atomic proc to examine a world
-- will return all NILs if the context is no longer active

DestroyContext: PROC [context: Context];
-- invalidates the given context (unless it is the default context)

ActionContinuation: TYPE = {continue, quit};

FrameAction: TYPE = PROC [lf: TV] RETURNS [ActionContinuation ← continue];

EnumerateFramesInContext: PROC [context: Context, action: FrameAction];

StackSearch: PROC
[context: Context, name: ROPE, case: BOOLTRUE, depth: INTEGER ← 100]
RETURNS [gf,lf,tv: TV];
-- search stack starting at startLF for named variable
-- if case is true then case of variable name matters
-- if lf = NIL and gf = NIL then variable was not found
-- if gf # NIL then variable was in global frame
-- if lf # NIL then variable was in local frame
-- we will not look more than depth levels in the stack

GlobalFrameSearch: PROC
[context: Context, frameName,itemName: ROPENIL, case: BOOLTRUE]
RETURNS [gf, tv: TV];
-- context = NIL => use the default
-- itemName = NIL => just search for the global frame named by frameName
-- case = FALSE => ignore case of global frame

RecordSearch: PROC
[record: TV, name: ROPE, case: BOOLTRUE]
RETURNS [base, tv: TV];
-- search record for named component (auto-dereferencing)
-- base # NIL => tv is OK
-- base = NIL => component not found

SetDefaultGlobalContext: PROC [context: Context] RETURNS [ROPE];
-- set the default global context for searching
-- returns NIL if successful, error msg if not

GetDefaultGlobalContext: PROC RETURNS [Context];
-- returns the default global frame tv

FindMatchingGlobalFrames: PROC
[world: World, pattern: ROPENIL, action: FindAction];
-- enumerates the gf,tv pairs with names that match (*-matching)
-- names given by gfn and tvn
-- if action[gf,tv,name] = quit, then stop

FindAction: TYPE = PROC [gf: TV, name: ROPE] RETURNS [ActionContinuation ← continue];
-- type of proc used to process matching global frames

IsStarted: PROC [gf: TV] RETURNS [BOOL];

END.