-- TexNoad.Mesa

-- implementation of procedures in TexNoadDefs
-- last written by Doug Wyatt, December 18, 1979  11:05 PM

DIRECTORY
	TexDebugDefs: FROM "TexDebugDefs",
	TexMathDefs: FROM "TexMathDefs",
	TexMemDefs: FROM "TexMemDefs",
	TexNoadDefs: FROM "TexNoadDefs",
	TexNodeDefs: FROM "TexNodeDefs";

TexNoad: PROGRAM
IMPORTS TexDebugDefs,TexMemDefs
EXPORTS TexNoadDefs =
BEGIN OPEN TexMathDefs,TexNoadDefs;

-- eventually, some of these procedures should be made INLINE
-- and moved to TexNoadDefs

InitNoadList: PUBLIC PROCEDURE RETURNS[NoadListPtr] =
	BEGIN
	list: NoadListPtr←TexMemDefs.Alloc[mzone,SIZE[listhead Noad]];
	list↑←[link: NIL, body: listhead[last: list]]; RETURN[list];
	END;
FinishNoadList: PUBLIC PROCEDURE[list: NoadListPtr] RETURNS[NoadPtr] =
	BEGIN RETURN[list.link] END;
StoreNoad: PUBLIC PROCEDURE[list: NoadListPtr, p: NoadPtr] =
	BEGIN list.last.link←p; list.last←p END;

MakeScriptedNoad: PUBLIC PROCEDURE[operand: MField,
	mtype: MType, stype: SType]
	RETURNS[ScriptedNoadPtr] =
	BEGIN
	p: POINTER TO ScriptedNoad←TexMemDefs.Alloc[mzone,SIZE[ScriptedNoad]];
	p↑←[link: NIL, body: common[mtype: mtype, box: NIL, cvariant: scripted[
			operand: operand, subscr: nilMField, supscr: nilMField,
				svariant: none[]]]];
	SELECT stype FROM
		none => NULL;
		op => p.svariant←op[limitswitch: FALSE];
		sqrt => p.svariant←sqrt[];
		over => p.svariant←over[];
		under => p.svariant←under[];
		ENDCASE => ERROR;
	RETURN[p];
	END;

MakeCharNoad: PUBLIC PROCEDURE[tmchar: TMChar] RETURNS[ScriptedNoadPtr] =
	BEGIN
	p: POINTER TO ScriptedNoad←TexMemDefs.Alloc[mzone,SIZE[ScriptedNoad]];
	t: MType←tmchar.type;
	p↑←[link: NIL, body: common[mtype: t, box: NIL, cvariant: scripted[
			operand: [mchar[tmchar.mchar]], subscr: nilMField, supscr: nilMField,
				svariant: none[]]]];
	IF t=Op THEN p.svariant←op[limitswitch: FALSE];
	RETURN[p];
	END;

MakeAccentNoad: PUBLIC PROCEDURE[accent: MChar]
	RETURNS[ScriptedNoadPtr] =
	BEGIN
	p: POINTER TO ScriptedNoad←TexMemDefs.Alloc[mzone,SIZE[ScriptedNoad]];
	p↑←[link: NIL, body: common[mtype: Ord, box: NIL, cvariant: scripted[
			operand: nilMField, subscr: nilMField, supscr: nilMField,
				svariant: accent[accent]]]];
	RETURN[p];
	END;

MakeAboveNoad: PUBLIC PROCEDURE RETURNS[AboveNoadPtr] =
	BEGIN
	p: POINTER TO AboveNoad←TexMemDefs.Alloc[mzone,SIZE[AboveNoad]];
	p↑←[link: NIL, body: common[mtype: Ord, box: NIL,
			cvariant: above[numerator: nilMField, denominator: nilMField,
				aboverule: 0, ldelim: nullDelimiter, rdelim: nullDelimiter]]];
	RETURN[p];
	END;

MakeDelimNoad: PUBLIC PROCEDURE[dtype: DelimType, delim: Delimiter]
	RETURNS[NoadPtr] =
	BEGIN
	p: POINTER TO delim CommonNoad←TexMemDefs.Alloc[mzone,SIZE[delim CommonNoad]];
	mtype: MType←SELECT dtype FROM left=>Open, right=>Close, ENDCASE=>ERROR;
	p↑←[link: NIL, body: common[mtype: mtype, box: NIL,
			cvariant: delim[dtype: dtype, delim: delim]]];
	RETURN[p];
	END;

MakeNodeNoad: PUBLIC PROCEDURE[n: TexNodeDefs.NodePtr] RETURNS[NoadPtr] =
	BEGIN
	p: POINTER TO node Noad←TexMemDefs.Alloc[mzone,SIZE[node Noad]];
	p↑←[link: NIL, body: node[n]];
	RETURN[p];
	END;

MakeDiscNoad: PUBLIC PROCEDURE[c: MChar] RETURNS[NoadPtr] =
	BEGIN
	p: POINTER TO disc Noad←TexMemDefs.Alloc[mzone,SIZE[disc Noad]];
	p↑←[link: NIL, body: disc[c]];
	RETURN[p];
	END;

MakeStyleNoad: PUBLIC PROCEDURE[s: MathStyle] RETURNS[NoadPtr] =
	BEGIN
	p: POINTER TO style Noad←TexMemDefs.Alloc[mzone,SIZE[style Noad]];
	p↑←[link: NIL, body: style[s]];
	RETURN[p];
	END;

MakeSpaceNoad: PUBLIC PROCEDURE[s: MathSpace] RETURNS[NoadPtr] =
	BEGIN
	p: POINTER TO space Noad←TexMemDefs.Alloc[mzone,SIZE[space Noad]];
	p↑←[link: NIL, body: space[s]];
	RETURN[p];
	END;

mzone: TexMemDefs.ZonePtr←NIL;
mzonerefs: CARDINAL←0;

NoadInit: PROCEDURE =
	BEGIN
	mzone←TexMemDefs.CreateZone[init: 2, grow: 7];
	mzonerefs←0;
	END;

StartNoadAllocation: PUBLIC PROCEDURE =
	BEGIN TexMemDefs.OpenZone[mzone] END;

FinishNoadAllocation: PUBLIC PROCEDURE =
	BEGIN
	IF TexMemDefs.CloseZone[mzone] THEN
		TexDebugDefs.ShowZone[mzone,"Math mode"];
	END;

NoadInit;

END.