perm filename EAUX3B.2[EAL,HE]3 blob sn#704706 filedate 1983-04-08 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00008 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	{$NOMAIN	Editor: More aux routines }
C00031 00003	{ Externally defined routines from elsewhere: }
C00034 00004	(* aux routine: varDefine *)
C00039 00005	(* routines for breakpoints: setBpt,clrBpt,clrAllBpts,setTBpt,stepStmnt,clrTBpts *)
C00048 00006	(* debugging routines: tracePdb, trace *)
C00053 00007	(* redrawDisplay *)
C00054 00008	(* collectStmnt *)
C00057 ENDMK
C⊗;
{$NOMAIN	Editor: More aux routines }

(* definition of record types & global variables used by AL *)

const 

  maxLines = 28;
  maxPPLines = 12;
  maxBpts = 25;
  maxTBpts = 20;	(* max could be exceeded by huge case stmnt *)
  listinglength = 2000;	(* Length of Listingarray *)

type

(* random type declarations for OMSI/SAIL compatibility *)

 ascii = char;
 atext = text;
 byte = 0..255;	

(* Here are all the pointer-type definitions.  Since the various 	*)
(* records reference each other so much, we have to put them all here.	*)

vectorp = ↑vector;
transp = ↑trans;
strngp = ↑strng;
eventp = ↑event;
framep = ↑frame;
statementp = ↑statement;
varidefp = ↑varidef;
nodep = ↑node;
identp = ↑ident;
tokenp = ↑token;
reswordp = ↑resword;
pdbp = ↑pdb;
envheaderp = ↑envheader;
enventryp = ↑enventry;
environp = ↑environment;
cmoncbp = ↑cmoncb;
messagep = ↑message;
linerecp = ↑linerec;


(* datatype definitions *)

datatypes = (pconstype, varitype, svaltype, vectype, rottype, transtype,
	     frametype, eventtype, strngtype, labeltype, proctype, arraytype,
	     reftype, valtype, cmontype, nulltype, undeftype,
	     dimensiontype, mactype, macargtype, freevartype);

scalar = real;
vector = record  refcnt: integer; val: array [1..3] of real end;
trans = record  refcnt: integer; val: array [1..3,1..4] of real end;

cstring = packed array [1..10] of ascii;
c4str = packed array [1..4] of ascii;
c5str = packed array [1..5] of ascii;
c20str = packed array [1..20] of ascii;
linestr = packed array [1..130] of ascii;

strng = record
	  next: strngp;
	  ch: cstring;
	end;

event = record
	  next: eventp;		(* all events are on one big list *)
	  count: integer;
	  waitlist: pdbp;
	end;

frame = record
	  vari: varidefp;	(* back pointer to variable name & info *)
	  calcs: nodep;		(* affixment info *)
	  case ftype: boolean of	(* frame = true, device = false *)
  true:	    (valid: integer; val, fdepr: transp; dcntr: integer; dev: framep);
  false:    (mech: integer; case sdev: boolean of
		true: (sdest: real); false: (tdest,appr,depr: transp));
		(* sdev = true for scalar devices, false for frames *)
	end;


(* statement definitions *)

stmntypes = (progtype, blocktype, coblocktype, endtype, coendtype,
		fortype, iftype, whiletype, untiltype, casetype,
		calltype, returntype,
		printtype, prompttype, pausetype, aborttype, assigntype,
		signaltype, waittype, enabletype, disabletype, cmtype,
		affixtype, unfixtype,
		movetype,jtmovetype,operatetype,opentype,closetype,centertype,
		floattype, stoptype, retrytype,
		requiretype, definetype, macrotype, commenttype, dimdeftype,
		setbasetype, wristtype, saytype, declaretype, emptytype,
		evaltype, armmagictype);
		(* more??? *)

statement = packed record
		next, last: statementp;
		stlab: varidefp;
		exprs: nodep;	(* any expressions used by this statement *)
		nlines: integer;
		bpt,bad: boolean;
		case stype: stmntypes of

    progtype:	    (pcode: statementp; errors: integer);
    blocktype,
    declaretype,
    endtype,
    coendtype:	    (bcode, bparent: statementp; blkid: identp;
			level, numvars: 0..255; variables: varidefp);
    coblocktype:    (threads: nodep; nthreads: integer; cblkid: identp);
    fortype:	    (forvar, initial, step, final: nodep; fbody: statementp);
    whiletype,
    untiltype:	    (cond: nodep; body: statementp);
    casetype:	    (index: nodep; range, ncases: integer; caselist: nodep);
    iftype:	    (icond: nodep; thn, els: statementp);
    cmtype:	    (oncond: nodep; conclusion: statementp;
			deferCm, exprCm: boolean; cdef: varidefp);
    prompttype,
    printtype,
    aborttype,
    saytype:	    (plist: nodep; debugLev: integer);
    assigntype:     (what, aval: nodep);
		end;



(* auxiliary definitions: variable, etc. *)

varidef = packed record
	    next,dnext: varidefp;
	    name: identp;
	    level: 0..255;	(* environment level *)
	    offset: 0..255;	(* environment offset *)
	    dtype: varidefp;	(* to hold the dimension info *)
	    tbits: 0..15;  (* special type bits: array = 1, proc = 2, ref = 4 & ? *)
	    dbits: 0..15;	(* for use by debugger/interpreter *)
	    case vtype: datatypes of
  arraytype:  (a: nodep);
  proctype:   (p: nodep);
  labeltype,
  cmontype:   (s: statementp);
  mactype:    (mdef: statementp);
  macargtype: (marg: tokenp);
  pconstype:  (c: nodep);
  dimensiontype: (dim: nodep);
	  end;



(* definition of the ubiquitous NODE record *)

nodetypes = (exprnode, leafnode, listnode, clistnode, colistnode, forvalnode,
		deprnode, viaptnode, apprnode, destnode, byptnode, durnode,
		sfacnode, wobblenode, swtnode, nullingnode, wristnode, cwnode,
		arrivalnode, departingnode,
		ffnode, forcenode, stiffnode, gathernode, cmonnode, errornode,
		calcnode, arraydefnode, bnddefnode, bndvalnode,
		waitlistnode, procdefnode, tlistnode, dimnode, commentnode,
		linearnode, elbownode, shouldernode, flipnode, wrtnode,
		loadnode,velocitynode);

exprtypes =  (	svalop,					(* scalar operators *)
		sltop, sleop, seqop, sgeop, sgtop, sneop,	(* relations *)
		notop, orop, xorop, andop, eqvop,		(* logical *)
		saddop, ssubop, smulop, sdivop, snegop, sabsop, (* scalar ops *)
		sexpop, maxop, minop, intop, idivop, modop,
		sqrtop, logop, expop, timeop,			(* functions *)
		sinop, cosop, tanop, asinop, acosop, atan2op,	(* trig *)
		vdotop, vmagnop, tmagnop,
		vecop,					(* vector operators *)
		vmakeop, unitvop, vaddop, vsubop, crossvop, vnegop,
		svmulop, vsmulop, vsdivop, tvmulop, wrtop,
		tposop, taxisop,
		transop,				(* trans operators *)
		tmakeop, torientop, ttmulop, tvaddop, tvsubop, tinvrtop,
		vsaxwrop, constrop, ftofop, deproachop, fmakeop, vmkfrcop,
		ioop,					(* i/o operators *)
		queryop, inscalarop,
		specop,					(* special operators *)
		arefop, callop, grinchop, macroop, vmop, adcop, dacop, jointop,
		badop,
		addop, subop, negop, mulop, divop, absop); (* for parsing *)

leaftypes = pconstype..strngtype;

reltypes = sltop..sgtop;
forcetypes = (force,absforce,torque,abstorque,angvelocity);

node = record
	next: nodep;
	case ntype: nodetypes of
    exprnode:	(op: exprtypes; arg1, arg2, arg3: nodep; elength: integer);
    leafnode:	(case ltype: leaftypes of
	varitype:  (vari: varidefp; vid: identp);
	pconstype: (cname: varidefp; pcval: nodep);
	svaltype:  (s: scalar; wid: integer);
	vectype:   (v: vectorp);
	transtype: (t: transp);
	strngtype: (length: integer; str: strngp) ); (* also used by commentnodes *)
    listnode:	(lval: nodep);
    clistnode:	(cval: integer; stmnt: statementp; clast: nodep);
    colistnode:	(prev: nodep; cstmnt: statementp);
    forvalnode:	(fvar: enventryp; fstep: scalar);
    cmonnode:	(cmon: statementp; errhandlerp: boolean);
    waitlistnode: (who: pdbp; when: integer);
    procdefnode:(ptype: datatypes; level: 0..255;
		    pname, paramlist: varidefp; body: statementp);
	end;



(* records for parser: ident, token, resword *)

ident = record
	    next: identp;
	    length: integer;
	    name: strngp;
	    predefined: varidefp;
	  end;


tokentypes = (reswdtype, identtype, constype, comnttype, delimtype, labeldeftype,
		macpartype);

constypes = svaltype..strngtype;

reswdtypes = (stmnttype, filtype, clsetype, decltype, optype, edittype);

filtypes = (abouttype,alongtype,attype,bytype,defertype,dotype,elsetype,
		errmodestype,fromtype,handtype,intype,nonrigidlytype,rigidlytype,
		sourcefiletype,steptype,thentype,totype,untltype,viatype,
		withtype,worldtype,zeroedtype,oftype,wheretype,nowaittype,
		ontype,offtype,ppsizetype,collecttype,alltype,lextype,
		notype,righttype,lefttype,uptype,downtype,motiontype);

clsetypes = (approachtype,arrivaltype,departuretype,departingtype,durationtype,
		errortype,forcetype,forceframetype,forcewristtype,gathertype,
		nildeproachtype,nullingtype,stiffnesstype,
		torquetype,velocitytype,wobbletype,
		cwtype,ccwtype,stopwaittimetype,angularvelocitytype,
		respecttype,elbowtype,shouldertype,fliptype,lineartype,
		jointspacetype,loadtype);

edittypes = (getcmd,savecmd,insertcmd,renamecmd,startcmd,gocmd,proceedcmd,
		stepcmd,sstepcmd,nstepcmd,gstepcmd,executecmd,setcmd,tracecmd,
		breakcmd,unbreakcmd,tbreakcmd,definecmd,markcmd,unmarkcmd,
		popcmd,atcmd,calibratecmd);

token = record
	  next: tokenp;
	  case ttype: tokentypes of
constype:   (cons: nodep);
comnttype:  (len: integer; str: strngp);
delimtype:  (ch: ascii);
reswdtype:  (case rtype: reswdtypes of
	stmnttype: (stmnt: stmntypes);
	filtype:   (filler: filtypes);
	clsetype:  (clause: clsetypes);
	decltype:  (decl: datatypes);
	optype:	   (op: exprtypes);
	edittype:  (ed: edittypes) );
identtype:  (id: identp);
labeldeftype: (lab: varidefp);
macpartype: (mpar: varidefp);
	end;


resword = record
	  next: reswordp;
	  length: integer;
	  name: strngp;
	  case rtype: reswdtypes of
	stmnttype:  (stmnt: stmntypes);
	filtype:    (filler: filtypes);
	clsetype:   (clause: clsetypes);
	decltype:   (decl: datatypes);
	optype:	    (op: exprtypes);
	edittype:  (ed: edittypes);
	  end;



(* process descriptor blocks & environment record definitions *)

queuetypes = (nullqueue,nowrunning,runqueue,inputqueue,eventqueue,sleepqueue,
		forcewait,devicewait,joinwait,proccall);

pdb = packed record
	nextpdb,next: pdbp;	(* for list of all/active pdb's *)
	level: 0..255;		(* lexical level *)
	mode: 0..255;		(* expression/statement/sub-statement *)
	priority: 0..255;	(* probably never greater than 3? *)
	status: queuetypes;	(* what are we doing *)
	env: envheaderp;
	spc: statementp;	(* current statement *)
	epc: nodep;		(* current expression (if any) *)
	sp: nodep;		(* intermediate value stack *)
	cm: cmoncbp;		(* if we're a cmon point to our definition *)
	mech: framep;		(* current device being used *)
	linenum: integer;	(* used by editor/debugger *)
	 case procp: boolean of	(* true if we're a procedure *)
true:  (opdb: pdbp;		(* pdb to restore when procedure exits *)
	pdef: nodep);		(* procedure definition node *)
false: (evt: eventp;		(* event to signal when process goes away *)
	sdef: statementp);	(* first statement where process was defined *)
      end;


envheader = packed record
	      parent: envheaderp;
	      env: array [0..4] of environp;
	      varcnt: 0..255;		(* # of variables in use ??? *)
		case procp: boolean of  (* true if we're a procedure *)
	true: (proc: nodep);
	false:(block: statementp);
	    end;

enventry = record
	    case etype: datatypes of
  svaltype:  (s: scalar);
  vectype:   (v: vectorp);
  transtype: (t: transp);
  frametype: (f: framep);
  eventtype: (evt: eventp);
  strngtype: (length: integer; str: strngp);
  cmontype:  (c: cmoncbp);
  proctype:  (p: nodep; penv: envheaderp);
  reftype:   (r: enventryp);
  arraytype: (a: envheaderp; bnds: nodep);
	   end;


environment = record
		next: environp;
		vals: array [0..9] of enventryp;
	      end;


cmoncb = record
	   running, enabled: boolean;		(* cmon's status *)
	   cmon: statementp;
	   pdb: pdbp;
	   evt: eventp;
	   fbits: integer;			(* bits for force sensing *)
	   oldcmon: cmoncbp;			(* for debugger *)
	 end;



(* definition of AL-ARM messages *)

msgtypes = (initarmscmd,calibcmd,killarmscmd,wherecmd,
	    abortcmd,stopcmd,movehdrcmd,movesegcmd,
	    centercmd,operatecmd,movedonecmd,signalcmd,
	    setccmd,forcesigcmd,forceoffcmd,biasoncmd,biasoffcmd,setstiffcmd,
	    zerowristcmd,wristcmd,gathercmd,getgathercmd,readadccmd,writedaccmd,
	    errorcmd,floatcmd,setloadcmd,
	    armmagiccmd,realcmd,vectorcmd,transcmd);

errortypes = (noerror,noarmsol,timerr,durerr,toolong,featna,
	      unkmess,srvdead,adcdead,nozind,exjtfc,paslim,nopower,badpot,devbusy,
	      baddev,timout,panicb,nocart,cbound,badparm);

message = record
	   cmd: msgtypes;
	   ok: boolean;
	   case integer of
	1:   (dev, bits, n: integer;
(*	     (dev, bits, n, evt: integer;	(* for arm code version *)
	      evt: eventp;
	      dur: real;
	      case integer of
		1: (v1,v2,v3: real);
		2: (sfac,wobble,pos: real);
		3: (val,angle,mag: real);
		4: (max,min: real);
		5: (error: errortypes));
	2:   (fv1,fv2,fv3,mv1,mv2,mv3: real);	(* may never use these... *)
	3:   (t: array [1..6] of real);
	  end;

interr = record
         case integer of
           0: (i: integer);
	   1: (err,foo: errortypes);
	 end;



(* print related records: *)

cursorp = record
	  cline,ind: integer;
	  case stmntp: boolean of
  true:    (st: statementp);
  false:   (nd: nodep);
	 end;

linerec = record
	next: linerecp;
	start,length: integer
       end;

listingarray = packed array [0..listinglength] of ascii;



(* global variables *)

var
	(* from EDIT *)
    listing: listingarray;  (* first 150 chars are used by expression editor *)
			    (* next 40 by header & trailer lines *)
{*} cursorStack: array [1..15] of cursorp;	{These are BIG records! }
    lbuf: array [1..160] of ascii;
    ppBuf: array [1..100] of ascii;
    lines: array [1..maxLines] of linerecp; (* what's on the screen + some *)
    ppLines: array [1..maxPPLines] of linerecp;	(* for page printer *)
    marks: array [1..20] of integer;
    reswords: array [0..26] of reswordp;
    idents: array [0..26] of identp;
    macrostack: array [1..10] of tokenp;
    curmacstack: array [1..10] of varidefp;
    screenheight,dispHeight: integer;
    ppBufp,oppBufp,ppOffset,ppSize,nmarks: integer;
    lbufp,cursor,ocur,cursorLine,fieldnum,lineNum,findLine,pcLine: integer;
    firstDline,topDline,botDline,firstLine,lastLine,curLine: integer;
    freeLines,oldLines: linerecp;
    sysVars: varidefp;
    dProg: statementp;
    curBlock, newDeclarations, findStmnt: statementp;
    macrodepth: integer;
    filedepth, errCount, sCursor: integer;
    curChar, maxChar, curFLine, curPage: integer;
    nodim, distancedim, timedim, angledim,
      forcedim, torquedim, veldim, angveldim: varidefp;
    fvstiffdim, mvstiffdim: nodep;
    pnode: nodep;
    smartTerminal: boolean; (* true = insert/delete, false = redraw line *)
    setUp,setExpr,setCursor,dontPrint,outFilep,collect,fParse,sParse,
      eofError,endOfLine,backup,expandmacros,flushcomments,checkDims,
      shownLine: boolean;
    curtoken: token;
    file1,file2,file3,file4,file5,outFile: atext;

    bpts: array [1..maxBpts] of statementp;	(* debugging crap *)
    tbpts: array [1..maxTBpts] of statementp;
    debugPdbs: array [0..10] of pdbp;
    nbpts,ntbpts,debugLevel: integer;
    eCurInt: pdbp;
    STLevel: integer;		(* set by GO *)
    singleThreadMode,tSingleThreadMode: boolean;

	(* from INTERP *)
    inputLine: array [1..20] of ascii;
    talk: text;			(* for using the speech synthesizer *)
    curInt, activeInts, readQueue, allPdbs: pdbp;
    sysEnv: envheaderp;
    clkQueue: nodep;
    allEvents: eventp;
    etime: integer;		(* used by eval *)
    curtime: integer; (* who knows where this will get updated - an ast? *)
    stime: integer;		(* used for clock queue on 10 *)
    msg: messagep;		(* for AL-ARM interaction *)
    inputp: integer;		(* current offset into inputLine array above *)
    resched, running, escapeI, iSingleThreadMode: boolean;
    msgp: boolean;		(* flag set if any messages pending *)
    inputReady: boolean;

(* various constant pointers *)
    xhat,yhat,zhat,nilvect: vectorp;
    niltrans: transp;
    gpark, rpark: transp;		(* arm park positions *)

(* various device & variable pointers *)
    speedfactor: enventryp;
    garm: framep;

{ Externally defined routines from elsewhere: }

	(* XX?? From EDEBUG XX *)
procedure executeStmnt(st: statementp; which: integer);		external;

	(* XX?? From EEXPAR XX *)
function exprParse: nodep;					external;

	(* From ALLOC *)
function newNode: nodep;					external;
function newStatement: statementp;				external;

	(* From EAUX1A *)
procedure out1Line(line,start,length: integer); 		external;
procedure clearLine(i: integer);				external;
procedure borderLines;						external;
procedure adjustDisplay;					external;
function varLookup(id: identp): varidefp;			external;

	(* From EAUX1C *)
function evalOrder(what,last: nodep; pcons: boolean): nodep;	external;

	(* From EAUX2C *)
procedure displayLines(var pfrom: integer);			external;
procedure insertLines(start,number,coff: integer);		external;

	(* From ETOKEN *)
procedure getToken;						external;
procedure getDelim(char: ascii);				external;

	(* From EPUTST *)
procedure putstmnt(s: statementp; indent, plevel: integer);	external;

	(* From PP *)
procedure ppLine; 						external;
procedure ppOutNow; 						external;
procedure ppChar(ch: ascii); 					external;
procedure pp5(ch: c5str; length: integer); 			external;
procedure pp10(ch: cstring; length: integer); 			external;
procedure pp10L(ch: cstring; length: integer);			external;
procedure pp20(ch: c20str; length: integer); 			external;
procedure pp20L(ch: c20str; length: integer); 			external;
procedure ppInt(i: integer); 					external;
procedure ppReal(r: real); 					external;
procedure ppStrng(length: integer; s: strngp); 			external;
procedure ppDtype(d: datatypes);				external;
procedure ppDelChar; 						external;

	(* From DISP *)
procedure outLine(line,col,start,length: integer); 		external;

(* aux routine: varDefine *)

procedure varDefine; external;
procedure varDefine;
 var vp: varidefp; n,np: nodep; s: statementp; b: boolean; i: integer;

    procedure prntStrng(length: integer; s: strngp);        (* Copied from IAUX2 *)
     begin
     ppStrng(length,s);
     ppOutNow;
     end;

 begin
 b := true;
 if (cursorStack[cursor].stmntp) and
    (cursorStack[cursor-1].stmntp) then
   if cursorStack[cursor-1].st↑.stype = blocktype then
     begin
     b := false;
     getToken;
     with curToken do
      while ttype = identtype do
       begin
       vp := varLookup(id);		(* look up the variable *)
       if vp <> nil then
	 if vp↑.tbits <> 2 then		(* make sure its not a procedure *)
	   begin
	   backup := true;
	   np := exprParse;		(* now go turn it into a node *)
	   s := newStatement;
	   with s↑ do			(* make up a new assignment stmnt *)
	    begin
	    stype := evaltype;
	    what := np;
	    next := s;			(* so dFreePdb doesn't flush us *)
	    last := s;
	    exprs := evalorder(np,nil,true);	(* we want its current value *)
(* XX Rick needs to set WHICH parameter for call below XX *)
(* XX This is a big problem!!!!!!!! *)
	    executeStmnt(s,-854);	(* aval will be set by INTERP *)
	    stype := assigntype;
	  (* *** if vector then should append dimension info, but... *** *)
	    with aval↑ do
	     if (ltype = vectype) or (ltype = transtype) then
	       v↑.refcnt := v↑.refcnt + 1;	(* so it doesn't disappear *)
	    with what↑ do
	     if ntype = leafnode then np := nil
	      else if op = arefop then np := arg2
	      else if arg1↑.ntype = leafnode then np := nil
	      else np := arg1↑.arg2;
	    if np <> nil then
	      np := evalorder(np,nil,true);  (* deal with subscripts *)
	    exprs := evalorder(aval,np,true);
	    end;
	   with cursorStack[cursor] do
	    begin
	    s↑.next := st;		(* splice in the assignment statement *)
	    s↑.last := st↑.last;
	    st↑.last := s;
	    with s↑.last↑ do
	     if next = st then next := s else bcode := s;
	    i := cline;
	    cline := cline + 1;		(* update where cursor now is *)
	    cursorLine := cline;
	    end;
	   insertLines(i,1,1);		(* make space in display for it *)
	   firstLine := i;
	   lastLine := i;
	   setCursor := false;
	   curLine := 0;
	   putStmnt(dProg,0,99);		(* write & display new line *)
	   adjustDisplay;			(* make sure cursor is on screen *)
	   displayLines(lineNum);
	   end
	  else
	   begin
	   pp20L('Can''t assign to proc',20); pp10('edure:    ',7);
	   prntStrng(id↑.length,id↑.name);
	   pp20(' -- will ignore it. ',19); ppLine;
	   end
	else
	 begin
	 pp20L('Undefined variable: ',20);
	 prntStrng(id↑.length,id↑.name);
	 pp20(' -- will ignore it. ',19); ppLine;
	 end;
       getToken;
       if not endOfLine then
	 begin backup := true; getDelim(',') end;
       getToken;		(* get next variable name *)
       end;
     end;
 if b then
   begin
   pp20('Can''t insert an assi',20); pp20('gnment here - Sorry.',20);
   ppLine;
   end;
 end;

(* routines for breakpoints: setBpt,clrBpt,clrAllBpts,setTBpt,stepStmnt,clrTBpts *)

procedure setBpt(st: statementp); external;
procedure setBpt;
 var i: integer;
 begin
 if not st↑.bpt then		(* don't do anything if bpt already set *)
   begin
   nbpts := nbpts + 1;
   if nbpts > maxBpts then
     begin
     pp20L('Gack - too many BPTs',20); ppLine;
     bpts[1]↑.bpt := false;			(* flush oldest bpt *)
     for i := 2 to maxBpts do bpts[i-1] := bpts[i];
     nbpts := maxBpts;
     end;
   bpts[nbpts] := st;
   st↑.bpt := true;
   end;
 end;

procedure clrBpt(st: statementp); external;	(* Also appears in FREE *)
procedure clrBpt;
 var i: integer; b: boolean;
 begin
 if st↑.bpt then		(* don't do anything if bpt not set *)
   begin
   b := true;
   for i := 1 to nbpts do
    if b then b := bpts[i] <> st	(* first find statement in list *)
     else bpts[i-1] := bpts[i];		(* then compact the list *)
   if not b then
     begin
     st↑.bpt := false;			(* clear it only if we set it *)
     bpts[nbpts] := nil;
     nbpts := nbpts - 1;
     end;
  (* else wonder how the bpt got set? *)
   end;
 end;

procedure clrAllBpts; external;
procedure clrAllBpts;
 var i: integer;
 begin
 for i := 1 to nbpts do
  begin bpts[i]↑.bpt := false; bpts[i] := nil end;
 nbpts := 0;
 end;

procedure setTBpt(st: statementp); external;
procedure setTBpt;
 var i: integer;
 begin
 with st↑ do
  if not bpt then	(* don't do anything if bpt already set *)
    begin
    ntbpts := ntbpts + 1;
    if ntbpts > maxTBpts then
      begin
      pp20L('Gack - too many temp',20); pp10('orary BPTs',10); ppLine;
      tbpts[1]↑.bpt := false;			(* flush oldest bpt *)
      for i := 2 to maxTBpts do tbpts[i-1] := tbpts[i];
      ntbpts := maxTBpts;
      end;
    tbpts[ntbpts] := st;
    bpt := true;
    end;
 end;

procedure stepStmnt(bpttype: integer); external;
procedure stepStmnt;
 var i: integer; st: statementp; n: nodep; 

 procedure setTBptsAux(st: statementp);
  begin
  if st <> nil then
   with st↑ do
    if stype = endtype then
      begin
      if bparent↑.stype = fortype then setTBpt(bparent↑.fbody)
       else if bparent↑.stype <> cmtype then setTBpt(bparent)
       else setTBpt(bparent↑.conclusion);	(* do we really want this??? *)
      if bparent↑.next <> nil then setTBptsAux(bparent↑.next);
      if bparent↑.stype = blocktype then setTBpt(st);
      end
     else if stype = coendtype then setTBpt(st↑.bparent↑.next)
     else if (stype = returntype) and (next = nil) and eCurInt↑.procp then
      begin					(* appended return *)
      setTBptsAux(eCurInt↑.opdb↑.spc↑.next)	(* stop after we get back *)
      end
     else setTBpt(st);
  end;

 begin (* stepStmnt *)

(* bpttype = 1  single step descending to lower levels + procedure calls
(*	   = 2    "     "	"	"   "	   "   + no procedure calls
(*	   = 3    "     "   but stay at current level
(*	   = 4   step up to next higher lexical level *)

 st := eCurInt↑.spc;			(* find where we're stepping from *)

 with st↑ do
  if stype = progtype then st := pcode;

 if bpttype <= 3 then
   begin
   if bpttype = 1 then
     begin			(* look if any procedure calls in st↑.exprs *)
     n := st↑.exprs;
     while n <> nil do
      with n↑ do
       if (ntype = exprnode) and (op = callop) then
	 begin			 (* set a bpt at first statement in procedure *)
	 setTBpt(arg1↑.vari↑.p↑.body);
	 n := nil;
	 end
	else n := next;
     end;

   if bpttype <= 2 then
     with st↑ do			(* look if can descend down a level *)
      case stype of
blocktype:  setTBpt(bcode);
coblocktype:setTBpt(threads↑.cstmnt);	(* always goes there first *)
fortype:    setTBpt(fbody);
whiletype,
untiltype:  setTBpt(body);
casetype:   begin
	    n := caselist;
	    while n <> nil do begin setTBpt(n↑.stmnt); n := n↑.next end;
	    end;
iftype:	    begin
	    setTBpt(thn);
	    if els <> nil then setTBpt(els);
	    end;
cmtype:	    setTBpt(conclusion);
(* *** what about cmon's & then code in motion statements ??? *** *)
otherwise  {do nothing};
       end;

   if (st↑.stype = returntype) and eCurInt↑.procp then
     begin			(* figure out where procedure returns to *)
     if bpttype = 1 then
       begin			(* check if expression will call another proc *)
       n := eCurInt↑.epc;
       while n <> nil do
	with n↑ do
	 if (ntype = exprnode) and (op = callop) then
	   begin		(* set a bpt at first statement in procedure *)
	   setTBpt(arg1↑.vari↑.p↑.body);
	   n := nil;
	   end
	  else n := next;
       end;
     setTBptsAux(eCurInt↑.opdb↑.spc↑.next)	(* stop after we get back *)
     end
    else
     begin
     setTBptsAux(st↑.next);		(* just stop at next stmnt *)
     setTBptsAux(st);			(* in case we're the body of a loop *)
     end;
   end
  else
   begin				(* deal with going up a level *)
   while st <> nil do
    if (st↑.stype<>coendtype) and (st↑.stype<>endtype) then st := st↑.next
     else
      with st↑.bparent↑ do
       if stype = progtype then st := nil
	else if (stype = blocktype) or (stype = fortype) then
	 begin setTBptsAux(next); st := nil end
	else begin setTBptsAux(st); st := nil end; 
   if eCurInt↑.procp then 			(* we may exit the procedure *)
     setTBptsAux(eCurInt↑.opdb↑.spc↑.next);	(* so stop after we get back *)
   end;

 end (* stepStmnt *);


procedure clrTBpts; external;	(* Also appears in EDEBUG *)
procedure clrTBpts;
 var i: integer;
 begin
 for i := 1 to ntbpts do
  begin tbpts[i]↑.bpt := false; tbpts[i] := nil end;
 ntbpts := 0;
 end;

(* debugging routines: tracePdb, trace *)

procedure tracePdb(p: pdbp); external;
procedure tracePdb;
 var n,np: nodep;
 begin
 if p = curInt then pp10('(active)  ',9);
 with p↑ do
  case status of
nowrunning,
runqueue:   pp10('running   ',8);
inputqueue: pp20('input wait          ',11);
eventqueue: pp20('event wait          ',11);
forcewait:  pp20('force sensing wait  ',19);
sleepqueue: pp10('sleeping  ',9);
joinwait:   pp20('process join wait   ',18);
devicewait: pp20('motion wait         ',12);
otherwise  {do nothing};
   end;
 while p↑.procp do
  begin
  with p↑.pdef↑.pname↑.name↑ do
   ppStrng(length,name);		(* tell procedure name *)
  pp5(':    ',2);
  ppInt(p↑.linenum);
  if ppbufp > 60 then ppLine else pp5('     ',3);
  p := p↑.opdb;
  end;
 if p↑.cm <> nil then pp10('(cmon)/   ',7)	(* ??? tell anything else ??? *)
  else if p↑.linenum = 0 then pp10('(tty:)/***',10)
  else pp10('(main)/   ',7);
 if p↑.linenum > 0 then ppInt(p↑.linenum);
 ppLine;
 end;

procedure trace(all: boolean); external;
procedure trace;
 var i,j: integer;

 procedure traceAux(p: pdbp; plev: integer);
  begin
  if p <> nil then
    with p↑ do
     begin
     traceAux(p↑.nextpdb,plev);		(* do the oldest first *)
     if (status <> nullqueue) and (status <> proccall) and 
	(priority >= 10 * plev) then
       begin
       pp10L('Process   ',8); ppInt(i); ppChar(' ');
       tracePdb(p);
       i := i + 1;
       end;
     end;
  end;

 begin
 if all then
   begin
   i := 1;
   for j := debugLevel downto 0 do
    begin
    if j > 0 then
      begin
      pp20L('Immediate execution ',20); pp10('level:    ',7);
      ppInt(j); ppLine;
      end
     else if debugLevel > 0 then
      begin pp20L('Program execution   ',17); ppLine end;
    if j > 0 then traceAux(debugPdbs[j],j);
    traceAux(allPdbs,j);
    end;
   end
  else
   begin
   tracePdb(eCurInt);
   end;
 end;

(* redrawDisplay *)

procedure redrawDisplay; external;
procedure redrawDisplay;
 var i: integer;
 begin
 for i := 1 to dispHeight do
  if lines[firstDline+i-1] <> nil then
    with lines[firstDline+i-1]↑ do
     out1Line(i,start,length)
   else clearLine(i);
 borderLines;
 for i := 1 to ppSize do
  if ppLines[i] <> nil then
    with ppLines[i]↑ do				(* redraw pp too *)
     outLine(dispHeight+1+i,1,start,length)
   else clearLine(dispHeight+1+i);
 oppBufp := 0; ppOutNow;			(* last line too *)
 end;

(* collectStmnt *)

procedure collectStmnt(s: statementp); external;
procedure collectStmnt;
 var i: integer;
 begin
 if collect then		(* if collecting add it to program *)
   with cursorStack[cursor] do
    if stmntp and cursorStack[cursor-1].stmntp and 
       (cursorStack[cursor-1].st↑.stype = blocktype) then
      begin
      s↑.bpt := false;
      s↑.next := st;			(* splice in the statement *)
      s↑.last := st↑.last;
      st↑.last := s;
      with s↑.last↑ do
       if next = st then next := s else bcode := s;
      setCursor := false;
      setUp := true;			(* need to format new stmnt *)
      curLine := 1;
      putStmnt(s,ind,99);		(* set it up *)
      setUp := false;
      i := cline;
      cline := cline + s↑.nlines;		(* update where cursor now is *)
      cursorLine := cline;
      insertLines(i,s↑.nlines,1);	(* make space in display for it *)
      firstLine := i;
      lastLine := cline - 1;
      setCursor := false;
      curLine := 0;
      putStmnt(dProg,0,99);		(* write & display new line(s) *)
      adjustDisplay;			(* make sure cursor is on screen *)
      displayLines(lineNum);
      end
     else
      begin
      collect := false;
      pp20('Can''t insert here. T',20); pp20('urning collect OFF. ',19);
      ppLine;
      end;
end;