TJaMRopeImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Maureen Stone, February 14, 1985 6:42:18 pm PST
Doug Wyatt, March 23, 1985 4:26:59 pm PST
DIRECTORY
Convert USING [RopeFromInt, RopeFromReal],
Rope USING [Concat, Equal, Fetch, Find, Length, ROPE, Substr],
TJaM USING [Frame, 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: PUBLIC PROC[frame: Frame] ~ {
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: PUBLIC PROC[frame: Frame] ~ {
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: PUBLIC PROC[frame: Frame] ~ {
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: PUBLIC PROC[frame: Frame] ~ {
i: INT ~ PopInt[frame];
rope: ROPE ~ PopRope[frame];
c: CHAR ~ rope.Fetch[i];
PushInt[frame, ORD[c]];
};
ApplyConcat: PUBLIC PROC[frame: Frame] ~ {
b: ROPE ~ PopRope[frame];
a: ROPE ~ PopRope[frame];
PushRope[frame, Rope.Concat[a, b]];
};
ApplySubstr: PUBLIC PROC[frame: Frame] ~ {
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.