<> <> <> <> 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.