-- file PGScondefs.Mesa
-- last modified by Satterthwaite, July 14, 1980  1:57 PM

DIRECTORY
  AltoDefs: TYPE USING [CharsPerWord, maxcharcode, PageSize, wordlength],
  BcdDefs: TYPE USING [VersionStamp],
  CharIO: TYPE USING [CR,TAB],
  Mopcodes: TYPE USING [zLI4, zRFS, zSHIFT, zWFS],
  StreamDefs: TYPE USING [StreamHandle];

PGScondefs: DEFINITIONS =
  BEGIN

  -- machine parameters
  cpw: CARDINAL=AltoDefs.CharsPerWord;
  pagesize: CARDINAL=AltoDefs.PageSize;
  wordlength: CARDINAL=AltoDefs.wordlength;
  maxcharcode: CARDINAL=AltoDefs.maxcharcode;
  Bit: PRIVATE TYPE = [0..1];

  GetBit: PRIVATE PROC [word:LongPointer, bit:CARDINAL] RETURNS [Bit] =
    MACHINE CODE {Mopcodes.zLI4; Mopcodes.zSHIFT; Mopcodes.zRFS};
  SetBit: PRIVATE PROC [v:Bit, word:LongPointer, bit:CARDINAL] =
    MACHINE CODE {Mopcodes.zLI4; Mopcodes.zSHIFT; Mopcodes.zWFS};

  -- common compile time constants
  outbuflim: CARDINAL = 90;
  TextPages: CARDINAL = 6;
  TextWords: CARDINAL = TextPages*pagesize;
  TextChars: CARDINAL = TextWords*cpw;

  --PGSScan compile time constants
  maxprod: CARDINAL = 500;
  maxrule: CARDINAL = 255; --this is the current PGSTab limit
  maxrhssymbols: CARDINAL = 2*maxprod;  -- empirical relation
  tokensize: CARDINAL = 14;
  symtabsize: CARDINAL = 350; --initial symbol table size
  rhslim: CARDINAL = 15; --this is the current PGSTab limit, change the grammar?
  alternatelim: CARDINAL = 31;
  psslim:CARDINAL = 1023; -- 10 bit field in itemrec to hold production, symbol
		        -- or state numbers, PGSTab allows an 11 bit field
  wordsfortoken: CARDINAL = (tokensize+cpw-1)/cpw;
  CR: CHARACTER  = CharIO.CR;
  TAB: CHARACTER = CharIO.TAB; -- used by PGSformat
  ControlZ: CHARACTER = 32C;  --Bravo escape character
  query: CHARACTER = '?; -- marker character in PGSformat

  --PGSLALR constants
  maxstatenum: CARDINAL = 400;
  stateext: CARDINAL = 100;
  maxtabentries: CARDINAL = 7000;
  tabext: CARDINAL = 1000;
  maxcontexts: CARDINAL = 960;

  --PGSTab constants
  defaultmarker: CARDINAL = 0;

-- data structures and associated types
  
 -- data structtures using the following types are built by PGSScan
  Symtab:TYPE = --LONG-- DESCRIPTOR FOR PACKED ARRAY OF CHARACTER;
  					-- [0..(symtabsize+1)*tokensize)
  symtabrecord:TYPE = RECORD[link:[0..symtabsize], length:[0..tokensize], used:BOOLEAN];
  Syminfo:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF symtabrecord; -- [0..symtabsize]

  aliasrecord: TYPE = RECORD[terminal,alias:CARDINAL];
  Aliases:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF aliasrecord; -- [0..63]

  tokenrecord: TYPE = RECORD[count:[0..alternatelim], empty:BOOLEAN, index:[0..maxprod]];
  Tokeninfo:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF tokenrecord;--(eofile..totaltokens]

  prodrecord: TYPE = RECORD[count:[0..rhslim], rule:[0..maxrule], chain:BOOLEAN,
		lhs:[0..symtabsize], index:[0..maxrhssymbols]];
  Prodinfo:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF prodrecord; -- [0..maxprod]

  Rhschar:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF CARDINAL; -- [0..maxrhssymbols]

  Options: TYPE = {echo,chain,lists,printlr,printlalr,first,ids};

 -- data structures built and implemented by PGSControl, used by PGSFormat and PGSBcd

  pgsVersion: BcdDefs.VersionStamp;
  sourceVersion: BcdDefs.VersionStamp;
  sourceName: STRING;
  objectVersion: BcdDefs.VersionStamp;

 -- data structures built by PGSScan, implemented by PGSControl, used by PGSLALR and PGSTab

  warningslogged: BOOLEAN;
  eofile: CARDINAL;  -- last terminal
  totaltokens: CARDINAL;  -- size of grammar vocabulary
  numprod: CARDINAL;  -- number of productions
  nextalias: CARDINAL;  -- number of aliased terminal symbols
  flags: ARRAY Options OF BOOLEAN;
  symtab: Symtab;
  syminfo: Syminfo;
  aliases: Aliases;
  tokeninfo: Tokeninfo;
  prodinfo: Prodinfo;
  rhschar: Rhschar;

 -- built by PGSLALR, implemented by PGSControl, used by PGSTab
  slim, tentries, ntentries: CARDINAL;

 -- built by PGSLALR, implemented by PGSControl, used by PGSControl and PGSLALR
  bitstrsize: CARDINAL;

  --data structures types for LR(0) tables in PGSLALR

  stateinforec: TYPE = RECORD[link,nucleus,entries:CARDINAL];
  Stateinfo: TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF stateinforec; --[0,maxstatenum+1];

  itemrec: TYPE = RECORD[tag:[0..3],jf:[0..15],pss:[0..psslim]];
  Table: TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF itemrec; --[0..maxtabentries]
    --after table is built,
    --tag = 3 => pss is a nonterminal symbol, such an entry is followed by an entry:
    --tag = 0 => pss is a next state number
    --tag = 1 => pss is production number, scanreduce, jf=n[p]
    --tag = 2 => pss is production number, reduce, jf=n[p]

  --data structure types for LALR(1) lookahead sets in PGSLALR

  chainrec: TYPE = RECORD[state,link:CARDINAL];
  Backchain: TYPE= --LONG-- DESCRIPTOR FOR ARRAY OF chainrec; --[0..totalshifts] initially

  Stack:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF CARDINAL;  --[0..30) initially
  Chainstack:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF CARDINAL;  --[0..90) initially  

  contextrec:TYPE = RECORD[state,status,link:CARDINAL,item:itemrec];
  Bitsinfo:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF contextrec;  --[0..maxcontexts)
  Bitstring:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF UNSPECIFIED;  --[0..maxcontexts)
  Firstbits:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF UNSPECIFIED; --[eofile+1..totaltokens]

  --data structure types for PGSTab

  hashtabrec:TYPE = MACHINE DEPENDENT RECORD[symptr:[0..255], link:[0..255]];
  Hashtab:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF hashtabrec; --[0..hashval]
  tabrec:TYPE = RECORD[symbol:CARDINAL, item:itemrec];
  Ttab:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF tabrec; --[0..tentries-1];
  Ntab:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF tabrec; --[0..ntentries-1];
  columnrec:TYPE = RECORD[link, count:CARDINAL, item:itemrec];
  Column:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF columnrec; --[0..ntentries];
  statedatarec:TYPE = RECORD[tindex, ntindex:CARDINAL, tlink,ntlink:INTEGER];
  Statedata:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF statedatarec; --[0..slim-1];
  ntdefaultrec:TYPE = RECORD[count:CARDINAL, item:itemrec];
  Ntdefaults:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF ntdefaultrec;
						 --[0..totaltokens-eofile+1];
  Renumber:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF CARDINAL;  --[0..slim-1]
  Vocabindex:TYPE = --LONG-- DESCRIPTOR FOR ARRAY OF CARDINAL;  --[0..eofile-1]

  --data structure types for PGSformat which also uses Symtab and Rhschar

  sinforec:TYPE = RECORD[symptr,link,lhshead:CARDINAL];
  Sinfo:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF sinforec; --[0..symtabsize+1];
  pinforec:TYPE = RECORD[rule:[0..77777B],chain:BOOLEAN,link,rhsptr:CARDINAL];
  Pinfo:TYPE =  --LONG-- DESCRIPTOR FOR ARRAY OF pinforec; --[0..maxprod+1];

-- procedures

 -- implemented by PGSControl
  PGSfail: ERROR;
  outeol: PROC [n:CARDINAL];
  outchar: PROC [c:CHARACTER, n:INTEGER];
  outstring: PROC [string:STRING];
  outtime: PROC;
  outtab: PROC;
  outword: PROC [n:CARDINAL];
  inword: PROC RETURNS [CARDINAL];
  outblock: PROC [address:POINTER, words:CARDINAL];

  signchar: CHARACTER;
  outnum: PROC [val:INTEGER, cols:CARDINAL];

  nextbuffer: PROC RETURNS [
	p:POINTER TO PACKED ARRAY OF CHARACTER, c:CARDINAL, last:BOOLEAN];

  seterrstream: PROC;
  setoutstream: PROC [dotstring:STRING];
  resetoutstream: PROC;
  cleanupstreams: PROC;
  closeoutstream: PROC;
  openwordstream: PROC [scratch: BOOLEAN ← TRUE];
  closewordstream: PROC;

  AllocateSegment: PROC [nwords:CARDINAL] RETURNS [POINTER];
  FreeSegment: PROC [POINTER];

  LongDes: TYPE = -- LONG -- DESCRIPTOR FOR ARRAY OF UNSPECIFIED;
  LongPointer: TYPE = -- LONG -- POINTER;
  makearray: PROC [length,width:CARDINAL] RETURNS [LongDes];
  expand: PROC [des:LongDes, width, ext:CARDINAL] RETURNS [LongDes];

  insertbit: PROC [num:CARDINAL, sink:LongPointer] =
    INLINE {SetBit[1, sink + num/wordlength, num MOD wordlength]};
  findbit: PROC [num:CARDINAL, source:LongPointer] RETURNS [BOOLEAN] =
     INLINE {RETURN [GetBit[source+num/wordlength, num MOD wordlength]#0]};

  orcount: CARDINAL;
  orbits: PROC [source,sink:LongPointer];

 -- implemented by PGSScan

  outtoken: PROC [symbol:CARDINAL] RETURNS [CARDINAL];
  checkout: PROC;

 -- implemented by PGSLALR

  lalrgen: PROC RETURNS [BOOLEAN];

 -- implemented by PGSTab

  tabgen: PROC RETURNS [BOOLEAN];
  outmodule: PROC [typename, modfname: STRING];

 -- implemented by PGSFormat

  Format: PROC [binary, type, module, interface: STRING];
  PrintGrammar: PROC;

 -- implemented by PGSBcd

  WriteBcdHeader: PROC [
      outStream: StreamDefs.StreamHandle,
      binaryId, interfaceId: STRING,
      altoCode: BOOLEAN ← TRUE];
  FixupBcdHeader: PROC;

 -- implemented by PGSSymbols

  WriteSymbols: PROC [s: StreamDefs.StreamHandle, moduleId: STRING];

  END.