NodeStyleWorks2Impl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Written by Bill Paxton, January 1981
Paxton, June 3, 1983 3:35 pm
Maxwell, January 6, 1983 10:05 am
Russ Atkinson, March 7, 1985 3:37:01 am PST
Paul Rovner, August 10, 1983 4:43 pm
Rick Beach, March 28, 1985 9:56:35 am PST
Michael Plass, May 22, 1985 9:30:26 am PDT
Doug Wyatt, May 26, 1985 1:33:30 pm PDT
DIRECTORY
Ascii USING [Lower],
Atom USING [GetPName, MakeAtomFromRefText],
MessageWindow USING [Append, Clear],
NodeStyle USING [DataEntry, DataList, Ref],
NodeStyleWorks USING [bindingDict, executingName, StyleError, StyleForFrame],
RefText USING [ObtainScratch, ReleaseScratch],
Rope USING [ActionType, Concat, Fetch, Length, Map, ROPE, Translate],
TJaM USING [APut, Array, AtomFromRope, Cmd, CommandProc, CvLit, CvX, Def, Dict, Execute, Frame, Load, NewArray, Object, Pop, PopAtom, PopInt, PopReal, PopRope, Push, PushBool, PushInt, PushReal, PushRope, Put, Register, RopeFromAtom, StackIsEmpty, TopType, TryToLoad];
Support Procs
GetCommand:
PUBLIC
PROC [frame: Frame, name:
ATOM]
RETURNS [TJaM.Cmd] ~ {
known: BOOL;
obj: Object;
[known, obj] ← TJaM.TryToLoad[frame, name];
IF NOT known THEN ERROR;
RETURN [TypeCheckCommand[obj]];
};
ForceLowerName:
PUBLIC PROC [n:
ATOM]
RETURNS [
ATOM] ~ {
IF n#
NIL
THEN {
rope: ROPE ~ Atom.GetPName[n];
CheckLower: Rope.ActionType ~ {quit ← c IN ['A..'Z]};
IF Rope.Map[base: rope, action: CheckLower]
THEN {
len: NAT ~ Rope.Length[rope];
text: REF TEXT ~ RefText.ObtainScratch[len];
FOR i: NAT IN[0..len) DO text[i] ← Ascii.Lower[Rope.Fetch[rope, i]] ENDLOOP;
text.length ← len;
n ← Atom.MakeAtomFromRefText[text];
RefText.ReleaseScratch[text];
};
};
RETURN [n];
};
ForceLowerRope:
PUBLIC PROC [r:
ROPE]
RETURNS [
ROPE] ~ {
ForceCharLower:
PROC [old:
CHAR]
RETURNS [new:
CHAR] ~ {
RETURN [Ascii.Lower[old]] };
RETURN [Rope.Translate[base: r, translator: ForceCharLower]];
};
PopName:
PUBLIC
PROC [frame: Frame]
RETURNS [name:
ATOM] ~ {
ok: BOOL ← TRUE;
obj: Object ← NIL;
IF TJaM.StackIsEmpty[frame] THEN ok ← FALSE;
IF ok
THEN
SELECT TJaM.TopType[frame]
FROM
atom => RETURN [TJaM.PopAtom[frame]];
rope => RETURN [TJaM.AtomFromRope[TJaM.PopRope[frame]]]
ENDCASE => { ok ← FALSE; obj ← TJaM.Pop[frame]; };
IF
NOT ok
THEN {
TJaM.PushRope[frame, " -- found where a name was expected."];
TJaM.Push[frame, obj];
StyleError[frame, 2];
};
};
TryToPopName:
PUBLIC
PROC [frame: Frame]
RETURNS [name:
ATOM, ok:
BOOL] ~ {
IF
NOT TJaM.StackIsEmpty[frame]
THEN
SELECT TJaM.TopType[frame]
FROM
atom => RETURN [name: TJaM.PopAtom[frame], ok: TRUE];
rope => RETURN [name: TJaM.AtomFromRope[TJaM.PopRope[frame]], ok: TRUE];
ENDCASE;
RETURN[name: NIL, ok: FALSE];
TryToPopReal:
PUBLIC
PROC [frame: Frame]
RETURNS [value:
REAL, ok:
BOOL] ~ {
IF
NOT TJaM.StackIsEmpty[frame]
THEN
SELECT TJaM.TopType[frame]
FROM
number => RETURN [value: TJaM.PopReal[frame], ok: TRUE];
ENDCASE;
RETURN[value: 0, ok: FALSE];
TryToPopRope:
PUBLIC
PROC [frame: Frame]
RETURNS [rope:
ROPE, ok:
BOOL] ~ {
IF
NOT TJaM.StackIsEmpty[frame]
THEN
SELECT TJaM.TopType[frame]
FROM
atom => RETURN [rope: TJaM.RopeFromAtom[TJaM.PopAtom[frame]], ok: TRUE];
rope => RETURN [rope: TJaM.PopRope[frame], ok: TRUE];
ENDCASE;
RETURN[rope: NIL, ok: FALSE];
TypeCheckName:
PUBLIC
PROC [obj: Object]
RETURNS [
ATOM] ~ {
WITH obj
SELECT
FROM
x: ATOM => RETURN [x];
x: ROPE => RETURN [TJaM.AtomFromRope[x]];
ENDCASE;
ERROR;
};
TypeCheckDict:
PUBLIC
PROC [obj: Object]
RETURNS [TJaM.Dict] ~ {
WITH obj
SELECT
FROM
x: TJaM.Dict => RETURN [x];
ENDCASE;
ERROR;
};
TypeCheckCommand:
PUBLIC
PROC [obj: Object]
RETURNS [TJaM.Cmd] ~ {
WITH obj
SELECT
FROM
x: TJaM.Cmd => RETURN [x];
ENDCASE;
ERROR;
};
Readonly Style Variables
IsCommentOp: TJaM.CommandProc ~ {
style: Ref ← StyleForFrame[frame];
TJaM.PushBool[frame, style.isComment];
};
IsPrintOp: TJaM.CommandProc ~ {
style: Ref ← StyleForFrame[frame];
TJaM.PushBool[frame, style.print];
};
NestingLevelOp: TJaM.CommandProc ~ {
style: Ref ← StyleForFrame[frame];
TJaM.PushInt[frame, style.nestingLevel];
};
StyleParam Implementation
StyleParamOp: TJaM.CommandProc ~ {
called to declare a special style parameter
initialValue: Object ← TJaM.Pop[frame]; -- the initial value
name: ATOM ← PopName[frame]; -- the parameter name
key: ATOM;
array: TJaM.Array;
[key, array] ← SpecialOpArray[name, $SpecialOp];
TJaM.Def[frame, name, TJaM.CvX[array]]; -- store the definition
TJaM.Def[frame, key, initialValue]; -- store the initial value
};
SpecialOpArray:
PUBLIC PROC [name:
ATOM, op: Object]
RETURNS [key:
ATOM, array: TJaM.Array] ~ {
create a 2-element array with (name, objectToExecute)
key ← StyleParamKey[name];
array ← TJaM.NewArray[2];
TJaM.APut[array, 0, TJaM.CvLit[key]];
TJaM.APut[array, 1, op];
};
StyleParamKey:
PUBLIC
PROC [name:
ATOM]
RETURNS [key:
ATOM] ~ {
create a key which is "!!name" (sort of unique, don't you think)
key ← TJaM.AtomFromRope[Rope.Concat["!!", TJaM.RopeFromAtom[name]]];
};
SpecialOp: TJaM.CommandProc ~ {
like DoStyleOperation, but for special parameters
aName: BOOL;
name: ATOM;
var: ATOM;
style: Ref ← StyleForFrame[frame];
Error:
PROC ~ {
TJaM.Push[frame, name];
TJaM.PushRope[frame, "has illegal qualifier:"];
TJaM.Push[frame, var];
StyleError[frame, 3];
};
FindObject:
PROC
RETURNS [Object] ~ {
FOR x: DataList ← style.dataList, x.next
UNTIL x=
NIL
DO
xx: REF DataEntry.object ~ NARROW[x];
IF xx.name = var THEN RETURN [xx.object];
ENDLOOP;
TJaM.Push[frame, var];
TJaM.Execute[frame, load]; -- get the initial value
RETURN [TJaM.Pop[frame]];
};
Store:
PROC [ob: Object] ~ {
style.dataList ← NEW[DataEntry ← [style.dataList, object[var, ob]]];
};
Load: PROC ~ { TJaM.Push[frame, FindObject[]] };
AddReal:
PROC [inc:
REAL] ~ {
value: REAL;
Load[];
value ← TJaM.PopReal[frame];
SetReal[value+inc];
};
SetReal:
PROC [x:
REAL] ~ {
TJaM.PushReal[frame, x];
Store[TJaM.Pop[frame]];
};
SetName:
PROC [n:
ATOM] ~ {
TJaM.Push[frame, n];
Store[TJaM.Pop[frame]];
};
Percent:
PROC [percent:
REAL] ~ {
value: REAL;
Load[];
value ← TJaM.PopReal[frame];
SetReal[(percent/100)*value];
};
var ← PopName[frame]; -- the name of the special parameter
[name, aName] ← TryToPopName[frame];
IF
NOT aName
THEN Store[TJaM.Pop[frame]]
store the object as new value
ELSE
SELECT name
FROM
$the => Load[];
$bigger =>
BEGIN
[name, aName] ← TryToPopName[frame];
IF
NOT aName
THEN AddReal[TJaM.PopReal[frame]]
ELSE
IF name = $percent
THEN Percent[100+TJaM.PopReal[frame]]
ELSE { Error; RETURN };
END;
$smaller =>
BEGIN
[name, aName] ← TryToPopName[frame];
IF
NOT aName
THEN AddReal[-TJaM.PopReal[frame]]
ELSE
IF name = $percent
THEN Percent[100-TJaM.PopReal[frame]]
ELSE { Error; RETURN };
END;
$percent => Percent[TJaM.PopReal[frame]];
ENDCASE => SetName[name];
};
RegisterStyleCommand:
PUBLIC
PROC [frame: Frame, name:
ATOM,
proc: TJaM.CommandProc] ~ {
TJaM.Register[frame, name, proc];
-- add it to the binding dictionary
TJaM.Put[bindingDict, name, TJaM.Load[frame, name]];
RegisterStyleLiteral:
PUBLIC
PROC [frame: Frame, name:
ATOM] ~ {
-- add it to the binding dictionary
TJaM.Put[bindingDict, name, name];
-- add it to the current dictionary
TJaM.Def[frame, name, TJaM.CvLit[name]];
};
ReportStyleErrorOp: TJaM.CommandProc ~ {
num: INT ← TJaM.PopInt[frame];
msg: ROPE;
ok: BOOL;
MessageWindow.Clear[];
IF executingName #
NIL
THEN {
TJaM.PushRope[frame, "style rule. "];
TJaM.Push[frame, executingName];
TJaM.PushRope[frame, "Error in"];
num ← num+3;
};
UNTIL num=0
DO
[msg, ok] ← TryToPopRope[frame];
IF NOT ok THEN EXIT;
MessageWindow.Append[msg];
num ← num-1;
IF num # 0 THEN MessageWindow.Append[" "];
ENDLOOP;
};
load: PUBLIC TJaM.Cmd;
get: PUBLIC TJaM.Cmd;
run: PUBLIC TJaM.Cmd;
RegisterWorks2:
PUBLIC
PROC [frame: Frame] ~ {
register the various style commands and JaM commands in this module
TJaM.Register[frame, $isComment, IsCommentOp];
TJaM.Register[frame, $isPrint, IsPrintOp];
TJaM.Register[frame, $nestingLevel, NestingLevelOp];
TJaM.Register[frame, $StyleParam, StyleParamOp];
TJaM.Register[frame, $SpecialOp, SpecialOp];
TJaM.Register[frame, $ReportStyleError, ReportStyleErrorOp];