FILE ViewRecExampleClient.Mesa
last changed by Spreitzer at April 23, 1985 3:02:38 pm PST
A sample use of ViewRec
DIRECTORY Rope, IO, ViewerIO, ViewRec, Process;
ViewRecExampleClient: PROGRAM
IMPORTS IO, ViewerIO, ViewRec, Process =
BEGIN
Wasnt: ERROR = CODE;
ROPE: TYPE = Rope.ROPE;
State: TYPE = {x, Y, z};
Foo: TYPE = {xyz, abc, lmn};
FuBar: TYPE = {abc, longNamedElement, de, f, g, hu, i, j, k, l, m};
Fu: TYPE = FuBar[i .. m];
Digit: TYPE = CARDINAL [0 .. 9];
Handle: TYPE = REF Rep;
Rep: TYPE = RECORD [
state: State ← Y,
push: PROC[inst: Instance, number: LONG CARDINAL],
pokes: LONG INTEGER ← 1,
foo: LONG CARDINAL ← 7,
fb: FuBar ← de,
fu: Fu ← k,
d: Digit ← 6,
char: CHAR ← '\n,
bool: BOOLEANFALSE,
f: Foo ← abc,
oof: RECORD [a: REAL, b: INTEGER] ← [-47, 17701],
name: Rope.ROPE ← "fu",
SetState: PROC [h: Handle, to: State] RETURNS [was: State],
MustBe: PROC [h: Handle, what: State ← z, eq: BOOLEAN],
Allowable: PROC [h: Handle, which: ARRAY State OF BOOLEAN ← [FALSE, TRUE, FALSE]],
Negate: PROC [h: Handle],
SetName: PROC [h: Handle, newName: ROPE],
Delay: PROC [milliseconds: CARDINAL],
stuff: SEQUENCE length: CARDINAL OF ARRAY BOOLEAN OF REAL
];
Instance: TYPE = REF InstanceBody;
InstanceBody: TYPE = RECORD[stream: IO.STREAM];
count: CARDINAL ← 0;
SetState: PROC [h: Handle, to: State] RETURNS [was: State] =
BEGIN
was ← h.state;
h.state ← to;
h.pokes ← -(h.pokes + (IF h.pokes < 0 THEN -1 ELSE 1));
END;
MustBe: PROC [h: Handle, what: State ← z, eq: BOOLEAN] =
BEGIN
IF (h.state = what) # eq THEN ERROR Wasnt;
END;
Allowable: PROC [h: Handle, which: ARRAY State OF BOOLEAN ← [FALSE, TRUE, FALSE]] =
{IF NOT which[h.state] THEN ERROR};
Push: PROC[inst: Instance, number: LONG CARDINAL ← 20] = {inst.stream.PutF["given %g\n", IO.card[number]]};
Negate: PROC [h: Handle] = {h.pokes ← - h.pokes};
SetName: PROC [h: Handle, newName: ROPE] = {Delay[5000]; h.name ← newName};
Delay: PROC [milliseconds: CARDINAL] =
{Process.Pause[Process.MsecToTicks[milliseconds]]};
Doit: PROC RETURNS [h: Handle, rv: ViewRec.RecordViewer] =
BEGIN
instance: Instance ← NEW[InstanceBody ← [ViewerIO.CreateViewerStreams[name: IO.PutFR["foo#%g", IO.card[count ← count+1]]].out]];
h ← NEW [Rep[3]];
h.state ← Y;
h.pokes ← 1;
h.foo ← 7;
h.fb ← de;
h.bool ← FALSE;
h.f ← abc;
h.oof ← [-47, 17701];
h.name ← "fu";
h.SetState ← SetState;
h.push ← Push;
h.MustBe ← MustBe;
h.Allowable ← Allowable;
h.Negate ← Negate;
h.SetName ← SetName;
h.Delay ← Delay;
FOR i: CARDINAL IN [0 .. h.length) DO h[i] ← [6.9, 2.7] ENDLOOP;
rv ← ViewRec.ViewRef[agg: h,
specs: ViewRec.BindAllOfATypeFromRefs[h, NEW [Instance ← instance]].
BindingListAppend[ViewRec.BindAllOfATypeFromRefs[h, NEW[Handle ← h]]],
viewerInit: [iconic: FALSE, name: IO.PutFR["Test#%g", IO.card[count]]],
paint: TRUE];
END;
END.