TJaMRopeImpl.mesa
Copyright Ó 1985, 1986, 1991, 1992 by Xerox Corporation. All rights reserved.
Maureen Stone, February 14, 1985 6:42:18 pm PST
Doug Wyatt, November 27, 1992 1:39 pm PST
DIRECTORY
Convert USING [RopeFromInt, RopeFromReal],
Rope USING [Concat, Equal, Fetch, Find, Length, ROPE, Substr],
TJaM USING [CommandProc, Number, Pop, PopInt, PopRope, ProduceError, PushBool, PushInt, PushRope, RegisterPrimitive, RopeFromAtom];
TJaMRopeImpl: CEDAR PROGRAM
IMPORTS Convert, Rope, TJaM
~ BEGIN OPEN TJaM;
ROPE: TYPE ~ Rope.ROPE;
ApplySearch: CommandProc ~ {
s: ROPE ~ PopRope[frame];
t: ROPE ~ PopRope[frame];
slen: INT ~ s.Length[];
i: INT ~ t.Find[s];
IF i>=0
THEN {
PushRope[frame, t.Substr[i+slen]]; -- part of t following match
PushRope[frame, s]; -- part of t matching s
PushRope[frame, t.Substr[0, i]]; -- part of t preceding match
PushBool[frame, TRUE];
}
ELSE {
PushRope[frame, t]; -- no match, just push t
PushBool[frame, FALSE];
};
};
ApplyASearch: CommandProc ~ {
s: ROPE ~ PopRope[frame];
t: ROPE ~ PopRope[frame];
slen: INT ~ s.Length[];
IF s.Equal[t.Substr[0, slen]]
THEN {
PushRope[frame, t.Substr[slen]]; -- part of t following match
PushRope[frame, s]; -- part of t matching s
PushBool[frame, TRUE];
}
ELSE {
PushRope[frame, t]; -- no match, just push t
PushBool[frame, FALSE];
};
};
ApplyConvertToString: CommandProc ~ {
x: REF ~ Pop[frame];
rope: ROPE;
WITH x
SELECT
FROM
x: Number =>
WITH n: x
SELECT
FROM
int => rope ¬ Convert.RopeFromInt[n.int];
real => rope ¬ Convert.RopeFromReal[n.real];
ENDCASE => ProduceError[bug];
x: ROPE => rope ¬ x;
x: ATOM => rope ¬ RopeFromAtom[x];
ENDCASE => rope ¬ "--nostringval--";
PushRope[frame, rope];
};
ApplySGet: CommandProc ~ {
i: INT ~ PopInt[frame];
rope: ROPE ~ PopRope[frame];
c: CHAR ~ rope.Fetch[i];
PushInt[frame, ORD[c]];
};
ApplyConcat: CommandProc ~ {
b: ROPE ~ PopRope[frame];
a: ROPE ~ PopRope[frame];
PushRope[frame, Rope.Concat[a, b]];
};
ApplySubstr: CommandProc ~ {
len: INT ~ PopInt[frame];
start: INT ~ PopInt[frame];
rope: ROPE ~ PopRope[frame];
PushRope[frame, rope.Substr[start, len]];
};
RegisterPrimitive[".search", ApplySearch];
RegisterPrimitive[".asearch", ApplyASearch];
RegisterPrimitive[".cvs", ApplyConvertToString];
RegisterPrimitive[".ropeconcat", ApplyConcat];
RegisterPrimitive[".substring", ApplySubstr];
END.