-- TexState.mesa

-- last written by Doug Wyatt, November 10, 1979  6:57 PM

DIRECTORY
	TexDefs: FROM "TexDefs",
	TexMathDefs: FROM "TexMathDefs",
	TexMemDefs: FROM "TexMemDefs",
	TexTableDefs: FROM "TexTableDefs";

TexState: PROGRAM
IMPORTS TexMemDefs,TexTableDefs
EXPORTS TexTableDefs SHARES TexTableDefs =
BEGIN OPEN TexMathDefs,TexDefs,TexTableDefs;

globalhang: HangSpec;

GlobalHangIndent: PUBLIC PROCEDURE RETURNS[HangSpec] =
	BEGIN RETURN[globalhang] END;
SetGlobalHangIndent: PUBLIC PROCEDURE[hang: HangSpec] =
	BEGIN globalhang←hang END;

DimnParamTable: TYPE = ARRAY DimnParamType OF Dimn;
dimns: POINTER TO DimnParamTable←NIL;

DimnParam: PUBLIC PROCEDURE[t: DimnParamType] RETURNS[Dimn] =
	BEGIN RETURN[dimns[t]] END;
SetDimnParam: PUBLIC PROCEDURE[t: DimnParamType, d: Dimn] =
	BEGIN dimns[t]←d END;

KountTable: TYPE = ARRAY Digit OF INTEGER;
kount: POINTER TO KountTable;

Kount: PUBLIC PROCEDURE[d: Digit] RETURNS[INTEGER] =
	BEGIN RETURN[kount[d]] END;
SetKount: PUBLIC PROCEDURE[d: Digit, k: INTEGER] =
	BEGIN kount[d]←k END;

SpaceFactorTable: TYPE = ARRAY Char OF SpaceFactor;
sftable: POINTER TO SpaceFactorTable←NIL;

SfTable: PUBLIC PROCEDURE[c: Char] RETURNS[SpaceFactor] =
	BEGIN RETURN[sftable[c]] END;
SetSfTable: PUBLIC PROCEDURE[c: Char, sf: SpaceFactor] =
	BEGIN sftable[c]←sf END;

DelimIndex: TYPE = [0..377B];
nullDelimIndex: DelimIndex=LAST[DelimIndex];
ndelims: DelimIndex←0;
maxdelims: DelimIndex=14;
DelimIndexTable: TYPE = PACKED ARRAY Char OF DelimIndex;
DelimTable: TYPE = ARRAY DelimIndex[0..maxdelims) OF Delimiter;
delimindex: POINTER TO DelimIndexTable;
delimtable: POINTER TO DelimTable;

AddDelimiter: PUBLIC PROCEDURE[c: Char, delim: Delimiter] =
	BEGIN
	IF ndelims=maxdelims THEN ERROR;
	delimtable[delimindex[c]←ndelims]←delim; ndelims←ndelims+1;
	END;

DelimLookup: PUBLIC PROCEDURE[c: Char]
	RETURNS[defined: BOOLEAN, delim: Delimiter] =
	BEGIN
	i: DelimIndex←delimindex[c];
	IF i=nullDelimIndex THEN RETURN[FALSE, nullDelimiter]
	ELSE RETURN[TRUE,delimtable[i]];
	END;


StateInit: PROCEDURE =
	BEGIN OPEN TexMemDefs;
	globalhang←nullHang;
	dimns←AllocMem[SIZE[DimnParamTable]];
	kount←AllocMem[SIZE[KountTable]];
	sftable←AllocMem[SIZE[SpaceFactorTable]];
	delimindex←AllocMem[SIZE[DelimIndexTable]];
	delimtable←AllocMem[SIZE[DelimTable]];
	dimns↑←ALL[0];
	kount↑←ALL[0];
	sftable↑←ALL[0];
	delimindex↑←ALL[nullDelimIndex];
	ndelims←0;
	START TexStateInit;
	END;

StateInit;

END.