VariablesImpl.mesa
Last Edited by: Arnon, June 10, 1985 4:19:22 pm PDT
DIRECTORY
Rope,
IO,
Convert,
Variables;
VariablesImpl: CEDAR PROGRAM
IMPORTS Rope, IO, Convert
EXPORTS Variables =
BEGIN OPEN Variables;
Conversion
ReadVariableSeq: PUBLIC PROC [in: IO.STREAM] RETURNS [V: VariableSeq] ~ {
puncChar: CHAR;
nextVar: Rope.ROPE;
length: NAT ← 0;
ReadVLFail: PUBLIC ERROR [subclass: ATOM ← $Unspecified] = CODE;
VList, VListTail: LIST OF Rope.ROPENIL;
[]← in.SkipWhitespace[];
puncChar ← in.GetChar[ ];
[]← in.SkipWhitespace[];
IF puncChar # '( THEN ReadVLFail[$LeftParenExpected];
WHILE puncChar # ') DO
nextVar ← in.GetID[];
length ← length + 1;
[]← in.SkipWhitespace[];
IF VList=NIL THEN VList ← VListTail ←LIST[nextVar] ELSE
{ VListTail.rest ← LIST[nextVar]; VListTail ← VListTail.rest };
puncChar ← in.GetChar[];
[]← in.SkipWhitespace[];
IF puncChar # ') THEN IF puncChar # ', THEN ReadVLFail[$CommaExpected];
ENDLOOP;
V ← NEW[VariableSeqRec[length]];
FOR i:NAT IN [1..length] DO
V[i] ← VList.first;
VList ← VList.rest;
ENDLOOP;
};
VariableSeqFromRope: PUBLIC PROC [in: Rope.ROPE] RETURNS [V: VariableSeq]={
VSStream: IO.STREAMIO.RIS[in];
RETURN[ ReadVariableSeq[ VSStream ] ];
};
VariableSeqToRope: PUBLIC PROC [V: VariableSeq] RETURNS [out: Rope.ROPE]={
out ← "(";
FOR i:NAT IN [1..V.lengthPlus1) DO
out ← Rope.Concat[ out, V[i] ];
IF i < V.lengthPlus1 - 1 THEN out ← Rope.Concat[ out, "," ];
ENDLOOP;
out ← Rope.Concat[ out, ")" ];
};
WriteVariableSeq: PUBLIC PROC [V: VariableSeq, out: IO.STREAM] = {
Write in a reasonable format
VSRope: Rope.ROPE ← VariableSeqToRope[V];
out.PutF["\n %g \n", IO.rope[VSRope] ];
};
Operations
VariableIndex: PUBLIC PROC [var: Rope.ROPE, V: VariableSeq] RETURNS [CARDINAL] ~ {
Variable indices are positive integers, thus if var = V[i], then i returned.
FOR i:NAT IN [1..V.lengthPlus1) DO
IF Rope.Equal[var, V[i]] THEN RETURN[i];
ENDLOOP;
RETURN[0];
};
VSRemoveMainVariable: PUBLIC PROC [V: VariableSeq] RETURNS [VariableSeq] ~ {
W: VariableSeq ← NEW[VariableSeqRec[V.lengthPlus1 - 2]];
FOR i:NAT IN [1..W.lengthPlus1) DO W[i] ← V[i] ENDLOOP;
RETURN[W];
};
MainVariable: PUBLIC PROC [V: VariableSeq] RETURNS [mainVar: VariableSeq] ~ {
mainVar ← NEW[VariableSeqRec[1]];
mainVar[1] ← V[V.lengthPlus1 - 1];
};
VariableFirstChar: PUBLIC PROC [char: CHAR, V: VariableSeq] RETURNS [BOOL] ~ {
FOR i:NAT IN [1..V.lengthPlus1) DO
IF Rope.Equal[
Convert.RopeFromChar[from: char, quote: FALSE],
Rope.Substr[V[i], 0, 1]
] THEN RETURN[TRUE];
ENDLOOP;
RETURN[FALSE];
};
END.