perm filename EAUX2A.2[EAL,HE]1 blob sn#676460 filedate 1982-09-27 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00005 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	{$NOMAIN	Editor: Misc routines }
C00027 00003	{ Externally defined routines from elsewhere: }
C00029 00004	(* aux routines: flushVar, eMakeNewVar *)
C00034 00005	(* initialization routines: initEditor & initOuterBlock *)
C00038 ENDMK
C⊗;
{$NOMAIN	Editor: Misc routines }

const

	(* Constants from EDIT *)
      	maxLines = 28;		(* smaller on the 11 than on the 10 *)
      	maxPPLines = 18;
      	maxBpts = 25;
      	maxTBpts = 20;	(* max could be exceeded by huge case stmnt *)
	listinglength = 2000;	(* Length of Listingarray *)

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

type

  byte = 0..255;	(* doesn't really belong here, but... *)
  ascii = char; 
  atext = text;

{ Define all the pointer types here }

strngp = ↑strng;
statementp = ↑statement;
varidefp = ↑varidef;
nodep = ↑node;
identp = ↑ident;
tokenp = ↑token;
reswordp = ↑resword;
pdbp = ↑pdb;
envheaderp = ↑envheader;
enventryp = ↑enventry;
environp = ↑environment;
cmoncbp = ↑cmoncb;
linerecp = ↑linerec;
cursorpp = ↑cursorp;	{Ron's style, not mine}

(* This one is used whenever a pointer is needed for which the 	*)
(* definition is missing from this file; naturally, all 	*)
(* pointers use the same space 					*)

dump = ↑integer;

(* 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;

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;


(* 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, operatetype, opentype, closetype, centertype,
		stoptype, retrytype,
		requiretype, definetype, macrotype, commenttype, dimdeftype,
		setbasetype, wristtype, tovaltype, declaretype, emptytype);
		(* more??? *)

statement = packed record
		next, last: statementp; (* ↑ to lexical tokens? *)
		stlab: varidefp;
		exprs: nodep;	(* any expressions used by this statement *)
		nlines: integer;
		bpt: 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);
    tovaltype:	    (vstr: strngp; vlen: integer; waitp: boolean);
		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, durnode,
		sfacnode, wobblenode, swtnode, nullingnode, wristnode, cwnode,
		arrivalnode, departingnode,
		ffnode, forcenode, stiffnode, gathernode, cmonnode, errornode,
		calcnode, arraydefnode, bnddefnode, bndvalnode,
		waitlistnode, procdefnode, tlistnode, dimnode, commentnode);

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,
		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: dump);
	transtype: (t: dump);
	strngtype: (length: integer; str: strngp) ); (* also used by commentnodes *)
    listnode:	(lval: nodep);
    errornode:	(eexpr: nodep);
    calcnode: 	(rigid, frame1: boolean; other: dump; case tvarp: boolean of 
		    false: (tval: dump); true: (tvar: enventryp) );
    arraydefnode: (numdims: 1..10; bounds: nodep; combnds: boolean);
    bnddefnode:	(lower, upper: nodep);
    bndvalnode:	(lb, ub, mult: integer);
    waitlistnode: (who: pdbp; when: integer);
    procdefnode:(ptype: datatypes; level: 0..255;
		    pname, paramlist: varidefp; body: statementp);
    tlistnode:	(tok: tokenp);
    dimnode:	(time, distance, angle, dforce: integer);
	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);

clsetypes = (approachtype,arrivaltype,departuretype,departingtype,durationtype,
		errortype,forcetype,forceframetype,forcewristtype,gathertype,
		nildeproachtype,nonullingtype,nullingtype,stiffnesstype,
		torquetype,velocitytype,wobbletype,
		cwtype,ccwtype,stopwaittimetype,angularvelocitytype,
		fxtype,fytype,fztype,mxtype,mytype,mztype,
		t1type,t2type,t3type,t4type,t5type,t6type,tbltype);

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

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;
	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: dump;		(* 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: dump;		(* 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: dump);
  transtype: (t: dump);
  frametype: (f: dump);
  eventtype: (evt: dump);
  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: dump;
	   fbits: integer;			(* bits for force sensing *)
	   oldcmon: cmoncbp;			(* for debugger *)
	 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 ALMAIN *)
    b:boolean;		
    ch:ascii;
    ltime: real;

	(* From PARSE *)
    reswords: array [0..26] of reswordp;
    idents: array [0..26] of identp;
    macrostack: array [1..10] of tokenp;
    curmacstack: array [1..10] of varidefp;
    macrodepth: integer;
    curchar, maxchar, curline: integer;
    curBlock,newDeclarations: statementp;
    curProc: varidefp;
    pnode: nodep;
    nodim, distancedim, timedim, angledim,
      forcedim, torquedim, veldim, angveldim: varidefp;
    fvstiffdim, mvstiffdim: nodep;
(*  filedepth: integer;
    curpage: integer;
    sysVars,unVars: varidefp;
    errcount: integer;
    outerBlock: statementp;
    curVariable: varidefp;
    curMotion: statementp;
    endOk,coendOk: integer;
    moveLevel: integer;
    curErrhandler, curCmon: statementp;	*)
    d1: array[1..13] of dump;
    endOfLine, backup, expandmacros, flushcomments, dimCheck: boolean;
    semiseen, shownline: boolean;
    eofError: boolean;
    inMove,inCoblock: boolean;
    curtoken: token;
    file1,file2,file3,file4,file5: atext;
    line: linestr;

	(* From INTERP *)
    curInt, activeInts, readQueue, allPdbs: pdbp;
(*  curEnv, sysEnv: envheaderp;
    clkQueue: nodep;
    allEvents: dump;
    STLevel: integer;		
    etime: integer;		
    curtime: integer; 		
    stime: integer;		
    msg: dump;			
    inputp: integer;		
    debugLevel: integer;	*)
    d10: array[1..11] of integer;
    tSingleThreadMode: boolean;
    resched, running, escapeI, singleThreadMode: boolean;
    msgp: boolean;		(* flag set if any messages pending *)
    inputReady: boolean;
    inputLine: array [1..20] of ascii;

	(* From EDIT *)
    lines: array [1..maxLines] of linerecp; 
    ppLines: array [1..maxPPLines] of linerecp;	
    marks: array [1..20] of integer;
    cursorStack: array [1..15] of cursorp;
    bpts: array [1..maxBpts] of statementp;
    tbpts: array [1..maxTBpts] of statementp;
    debugPdbs: array [0..10] of pdbp;
    screenheight,dispHeight: integer;
    ppBufp,oppBufp,ppOffset,ppSize,nmarks: integer;
    lbufp,cursor,ocur,cursorLine,fieldnum,lineNum,findLine,pcLine: integer;
    firstDline,topDline,botDline,firstLine,lastLine: integer;
    freeLines,oldLines: linerecp;
    findStmnt: statementp;
    nbpts,ntbpts: integer;
    eCurInt: pdbp;
    dProg: statementp;	
    smartTerminal: boolean; 
    setUp,setExpr,setCursor,dontPrint,outFilep,newVarOk,collect: boolean;
    eBackup: boolean;			
    eSingleThreadMode: boolean;	
    listing: listingarray;
    lbuf: array [1..160] of ascii;
    ppBuf: array [1..100] of ascii;
    outFile: atext;
    eCurToken: token;			

	(* Various device & variable pointers *)
    speedfactor: enventryp;
    barm: dump;

	(* Various constant pointers *)
    xhat,yhat,zhat,nilvect: dump;
    niltrans: dump;
    bpark, ypark, gpark, rpark: dump;		(* arm park positions *)

{ Externally defined routines from elsewhere: }

	(* From ALLOC *)
procedure relVaridef(n: varidefp);				external;
function newEheader: envheaderp; 				external;
function newEnvironment: environp;				external;
function newEentry: enventryp;					external;

	(* From PAUX2 *)
procedure relExpr(n: nodep);					external;

	(* From IAUX1A *)
function getELev(hdr: envheaderp): integer;			external;
procedure makeVar(e: enventryp; vari: varidefp; tbits: integer);external;
function getEntry (level, offset: byte): enventryp; 		external;

	(* From DISP *)
function initScreen(var l: listingarray): integer; 		external;	

	(* From CALLER *)
procedure e2akillVar(e: enventryp);				external;
procedure e2aInterp(dLev,which: integer);			external;


procedure eAux2aGet; external;
procedure eAux2aGet;  begin end;
(* aux routines: flushVar, eMakeNewVar *)

 procedure flushVar(oldvar: varidefp); external;
 procedure flushVar;
  var v,vp,vo: varidefp; j: integer; pold: pdbp;
  begin
  with oldvar↑ do
   begin				(* flush old unused variable *)
   with eCurInt↑.env↑ do
    if procp then j := proc↑.level else j := block↑.level;
   pold := CurInt;
   CurInt := eCurInt;		(* so we use right process to access var *)
   if level <= j then
     e2aKillVar(getEntry(level, offset));	(* active block flush its value *)
   CurInt := pold;			(* restore current process *)
   vtype := freevartype;
   name := nil;
   if odd(tbits) then relExpr(a);	(* free up old array bounds list *)
   if next = nil then
     begin			(* last variable, so we can release it *)
     v := curBlock↑.variables;
     vo := nil;
     while (v <> nil) and (v <> oldvar) do
      begin if v↑.vtype <> freevartype then vo := v; v := v↑.next end;
     if vo = nil then
       begin			(* no variables in block *)
       v := curBlock↑.variables;
       while v <> nil do begin vp := v↑.next; relVaridef(v); v := vp end;
       curBlock↑.variables := nil;
       curBlock↑.numvars := 0;
       end
      else
       begin
       v := vo↑.next;
       vo↑.next := nil;
       j := 0;
       while v <> nil do
	begin j := j + 1; vp := v↑.next; relVaridef(v); v := vp end;
       curBlock↑.numvars := curBlock↑.numvars - j;
       end
     end
    else tbits := 0;
   end;
 end;

procedure eMakeNewVar(newvar: varidefp); external;
procedure eMakeNewVar;
 var i,j,k: integer; env: environp; envhdr: envheaderp;
 begin
 envhdr := eCurInt↑.env;
 while newvar↑.level < getELev(envhdr) do
  envhdr := envhdr↑.parent;			(* move up a level *)
 if newvar↑.level = getELev(envhdr) then
   begin				(* active block - make a new variable *)
   i := newvar↑.offset div 10;			(* which environment block *)
   env := envhdr↑.env[0];
   if env = nil then
     begin
     env := newEnvironment;
     env↑.next := nil;
     for k := 0 to 9 do env↑.vals[k] := nil;
     envhdr↑.env[0] := env;
     end;
   for j := 1 to i do
    if env↑.next <> nil then env := env↑.next
     else
      begin
      env↑.next := newEnvironment;
      env := env↑.next;
      env↑.next := nil;
      for k := 0 to 9 do env↑.vals[k] := nil;
      if j < 5 then envhdr↑.env[j] := env;
      end;
   j := newvar↑.offset mod 10;			(* entry in environment block *)
   env↑.vals[j] := newEentry;		(* get environment entry for the variable *)
   with env↑.vals[j]↑ do
    begin
    etype := newvar↑.vtype;		(* copy datatype of variable *)
    if etype = rottype then etype := transtype; (* rots are transes internally *)
    end;
   makeVar(env↑.vals[j],newvar,newvar↑.tbits);
   end;
 end;

(* initialization routines: initEditor & initOuterBlock *)

procedure initEditor; external;
procedure initEditor;
 var i: integer;
 begin
 for i := 1 to listinglength do listing[i] := ' ';
 for i := 1 to 160 do lbuf[i] := ' ';
 for i := 1 to maxLines do lines[i] := nil;
 for i := 1 to 10 do cursorStack[i].st := nil;
 lbufp := 0;
 cursor := 0;
 new(freeLines);
 with freeLines↑ do
  begin next := nil; start := 191; length := listinglength - 190 end;
 oldLines := nil;
 for i := 1 to maxPPLines do ppLines[i] := nil;	(* init page printer *)
 ppBufp := 0;
 oppBufp := 0;
 ppOffset := 1;
 ppSize := 3;
 screenheight := initScreen(listing);
 dispHeight := screenHeight - 5;  (* header + trailer lines + page printer *)
 smartTerminal := screenheight < 30;	(* for now *)
 newDeclarations := nil;
 flushcomments := true;
 eBackup := false;
 macrodepth := 0;
 dimCheck := false;		(* assume no dimension checking *)
 newVarOk := true;
 end;

procedure initOuterBlock (which:integer); external;
procedure initOuterBlock ;
 var i: integer; s: statementp; envhdr: envheaderp;
 begin
 eCurInt := curInt;
 debugPdbs[0] := eCurInt;
 with eCurInt↑ do
  begin
  spc := dprog;
  sdef := dprog;
  linenum := 2;
  end;
 pcLine := 2;
 s := dprog↑.pcode↑.bcode;
 s↑.bpt := true;				(* just deal with the BEGIN *)
 e2aInterp(0,which);				(* Initialize outermost block *)
 s↑.bpt := false;				(* done with bpt now *)
 while s↑.stype <> endtype do s := s↑.next;	(* find block END *)
 s↑.bpt := true;	(* so we'll never flush outer block's variables *)
 if eCurInt↑.env↑.parent = nil then		(* = sysEnv *)
   begin
   envhdr := newEheader;
   with envhdr↑ do
    begin
    parent := eCurInt↑.env;
    block := dprog↑.pcode;
    procp := false;
    for i := 0 to 4 do env[i] := nil;
    varcnt := 0;
    end;
   with eCurInt↑ do
    begin
    level := 1;
    env := envhdr;
    end;
   end;
 end;