Scheme>SchemeFiles.Mesa
Last Edited by: Spreitzer, February 12, 1984 10:22 pm
DIRECTORY FS, IO, List, OrderedSymbolTableRef, Rope, SchemeRep, SchemeEditing, ViewerOps, ViewerTools, ViewRec;
SchemeFiles:
CEDAR
PROGRAM
IMPORTS FS, IO, List, OrderedSymbolTableRef, Rope, SchemeEditing, ViewerOps, ViewerTools, ViewRec
EXPORTS SchemeEditing =
BEGIN OPEN SchemeRep, SchemeEditing;
writeCount: INT ← 0;
Clear:
PUBLIC
PROC [session: Session] =
BEGIN
ViewerTools.SetContents[viewer: session.typesMenu, contents: NIL];
FOR le:
LIST
OF Editor ← session.editors, le.rest
WHILE le #
NIL
DO
ViewerOps.DestroyViewer[le.first.v];
ENDLOOP;
session.editors ← NIL;
session.mostRecentEditor ← NIL;
session.typesTable.DeleteAllItems[];
session.picsTable.DeleteAllItems[];
END;
ReadFile:
PUBLIC
PROC [session: Session, file:
ROPE, additively:
BOOLEAN ←
TRUE] =
BEGIN ENABLE Collision => {session.ctlRV.DisplayMessage[IO.PutFR["Collision on CellType %g --- you lose", IO.rope[ct.name]]]; Clear[session]; CONTINUE};
in: IO.STREAM ← FS.StreamOpen[fileName: file];
IF NOT additively THEN Clear[session];
DO
[] ← in.SkipWhitespace[];
IF in.EndOf[] THEN EXIT;
ReadCellType[in, session];
ENDLOOP;
END;
Collision: ERROR [ct: CellType] = CODE;
ReadCellType:
PROC [from:
IO.
STREAM, session: Session] =
BEGIN
cellTypeName: ROPE ← from.GetRopeLiteral[];
cellType: CellType ← NARROW[session.typesTable.Lookup[cellTypeName]];
IF cellType # NIL THEN ERROR Collision[cellType];
cellType ← NewCellType[session, cellTypeName, TRUE];
cellType.otherProps ← ReadPropList[from];
cellType.expansion ← ReadPictureDef[from, session];
WHILE NotDot[from] DO ReadIcon[from, session, cellType] ENDLOOP;
EatDot[from];
WHILE NotDot[from] DO ReadPort[from, session, cellType] ENDLOOP;
EatDot[from];
WHILE NotDot[from] DO ReadNet[from, session, cellType] ENDLOOP;
EatDot[from];
WHILE NotDot[from] DO ReadComponent[from, session, cellType] ENDLOOP;
EatDot[from];
END;
ReadIcon:
PROC [from:
IO.
STREAM, session: Session, cellType: CellType] =
BEGIN
pic: PictureDef ← ReadPictureDef[from, session];
cellType.icons.Insert[pic];
END;
ReadPort:
PROC [from:
IO.
STREAM, session: Session, cellType: CellType] =
BEGIN
portName: ROPE ← from.GetRopeLiteral[];
props: PropList ← ReadPropList[from];
pointName: ROPE ← from.GetRopeLiteral[];
labelFlag: ROPE ← from.GetRopeLiteral[];
port: Port ← CreatePort[cellType, portName, NARROW[cellType.expansion.points.Lookup[pointName]]];
port.otherProps ← props;
IF labelFlag.Equal["noLabel"] THEN NULL
ELSE
IF labelFlag.Equal["label"]
THEN
BEGIN
labelName: ROPE ← from.GetRopeLiteral[];
port.label ← NARROW[cellType.expansion.objects.Lookup[labelName]];
port.label.labelOf ← port;
END
ELSE ERROR;
END;
ReadNet:
PROC [from:
IO.
STREAM, session: Session, cellType: CellType] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
props: PropList ← ReadPropList[from];
net: Net ← NEW [NetRep ← [name: name, otherProps: props]];
cellType.nets.Insert[net];
WHILE NotDot[from]
DO
class: ROPE ← from.GetTokenRope[].token;
IF class.Equal["point"]
THEN {name:
ROPE ← from.GetRopeLiteral[];
point: Point ← NARROW[cellType.expansion.points.Lookup[name]];
net.stuff ← CONS[point, net.stuff]; point.net ← net}
ELSE
IF class.Equal["line"]
THEN {name:
ROPE ← from.GetRopeLiteral[];
line: Line ← NARROW[cellType.expansion.objects.Lookup[name]];
net.stuff ← CONS[line, net.stuff]; line.net ← net}
ELSE
IF class.Equal["text"]
THEN {name:
ROPE ← from.GetRopeLiteral[];
text: Text ← NARROW[cellType.expansion.objects.Lookup[name]];
net.stuff ← CONS[text, net.stuff]; text.labelOf ← net}
ELSE ERROR;
ENDLOOP;
END;
ReadComponent:
PROC [from:
IO.
STREAM, session: Session, cellType: CellType] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
typeName: ROPE ← from.GetRopeLiteral[];
of: CellType ← NARROW[session.typesTable.Lookup[typeName]];
instName: ROPE ← from.GetRopeLiteral[];
inst: PictureInstance ← NARROW[cellType.expansion.objects.Lookup[instName]];
props: PropList ← ReadPropList[from];
comp: Component ← CreateComponent[in: cellType, of: of, name: name, inst: inst];
labelFlag: ROPE ← from.GetTokenRope[].token;
comp.otherProps ← props;
IF labelFlag.Equal["noLabel"] THEN NULL
ELSE IF labelFlag.Equal["label"] THEN comp.label ← NARROW[cellType.expansion.objects.Lookup[from.GetRopeLiteral[]]]
ELSE ERROR;
END;
ReadPictureDef:
PROC [from:
IO.
STREAM, session: Session]
RETURNS [pic: PictureDef] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
pic ← NewPicture[session, name];
WHILE NotDot[from] DO ReadCoord[from, pic] ENDLOOP; EatDot[from];
WHILE NotDot[from] DO ReadPoint[from, pic] ENDLOOP; EatDot[from];
WHILE NotDot[from] DO ReadObject[from, pic, session] ENDLOOP; EatDot[from];
END;
ReadCoord:
PROC [from:
IO.
STREAM, pic: PictureDef] =
BEGIN
name, axisName, parentCode, parentName: ROPE;
axis: Axis;
z, dz: REAL;
parent: REF ANY;
exported: BOOLEAN;
name ← from.GetRopeLiteral[];
axisName ← from.GetTokenRope[].token;
IF axisName.Equal["X"] THEN axis ← X ELSE IF axisName.Equal["Y"] THEN axis ← Y ELSE ERROR;
exported ← from.GetBool[];
z ← from.GetReal[];
dz ← from.GetReal[];
parentCode ← from.GetTokenRope[].token;
parentName ← from.GetRopeLiteral[];
IF parentCode.Equal["n"] THEN parent ← NIL
ELSE IF parentCode.Equal["c"] THEN parent ← EnsureCoord[pic, parentName]
ELSE IF parentCode.Equal["p"] THEN parent ← EnsurePictureInstance[pic, parentName]
ELSE ERROR;
NewCoord[axis: axis, at: z, pic: pic, parent: parent, name: name].exported ← exported;
END;
EnsureCoord:
PUBLIC
PROC [pic: PictureDef, name:
ROPE]
RETURNS [coord: Coord] =
BEGIN
coord ← NARROW[pic.coordsByName.Lookup[name]];
IF coord = NIL THEN pic.coordsByName.Insert[coord ← NEW [CoordRep ← [name: name]]];
END;
EnsurePictureInstance:
PUBLIC
PROC [pic: PictureDef, name:
ROPE]
RETURNS [pi: PictureInstance] =
BEGIN
pi ← NARROW[pic.objects.Lookup[name]];
IF pi = NIL THEN pic.objects.Insert[pi ← NEW [PictureInstanceRep ← [name: name]]];
END;
ReadPoint:
PROC [from:
IO.
STREAM, pic: PictureDef] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
xName: ROPE ← from.GetRopeLiteral[];
yName: ROPE ← from.GetRopeLiteral[];
exported: BOOLEAN ← from.GetBool[];
fromPort: ROPE ← from.GetRopeLiteral[];
piName: ROPE ← from.GetRopeLiteral[];
point: Point ← NewPoint[pic, name, [NARROW[pic.coordsByName.Lookup[xName]], NARROW[pic.coordsByName.Lookup[yName]]]];
point.exported ← exported;
point.fromPort ← fromPort;
IF piName.Length[] > 0 THEN point.fromPI ← EnsurePictureInstance[pic, piName];
END;
ReadObject:
PROC [from:
IO.
STREAM, pic: PictureDef, session: Session] =
BEGIN
class: ROPE ← from.GetTokenRope[].token;
IF class.Equal["line"] THEN ReadLine[from, pic]
ELSE IF class.Equal["text"] THEN ReadText[from, pic]
ELSE IF class.Equal["pi"] THEN ReadPictureInstance[from, pic, session]
ELSE ERROR;
END;
ReadLine:
PROC [from:
IO.
STREAM, pic: PictureDef] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
aName: ROPE ← from.GetRopeLiteral[];
bName: ROPE ← from.GetRopeLiteral[];
[] ← NewLine[pic, NARROW[pic.points.Lookup[aName]], NARROW[pic.points.Lookup[bName]]];
END;
ReadText:
PROC [from:
IO.
STREAM, pic: PictureDef] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
orgName: ROPE ← from.GetRopeLiteral[];
rope: ROPE ← from.GetRopeLiteral[];
NewText[pic, NARROW[pic.points.Lookup[orgName]], rope];
END;
ReadPictureInstance:
PROC [from:
IO.
STREAM, pic: PictureDef, session: Session] =
BEGIN
name: ROPE ← from.GetRopeLiteral[];
orgName: ROPE ← from.GetRopeLiteral[];
defName: ROPE ← from.GetRopeLiteral[];
def: PictureDef ← NARROW[session.picsTable.Lookup[defName]];
pi: PictureInstance ← CreateInstance[in: pic, of: def, org: NARROW[pic.points.Lookup[orgName]]];
WHILE NotDot[from]
DO
cName: ROPE ← from.GetRopeLiteral[];
x: REF ANY ← pic.coordsByName.Lookup[cName];
pi.coords.Insert[x];
ENDLOOP;
EatDot[from];
WHILE NotDot[from]
DO
pName: ROPE ← from.GetRopeLiteral[];
x: REF ANY ← pic.points.Lookup[pName];
pi.points.Insert[x];
ENDLOOP;
EatDot[from];
END;
ReadPropList:
PROC [from:
IO.
STREAM]
RETURNS [propList: PropList] =
BEGIN
propList ← NIL;
WHILE NotDot[from]
DO
key, value: REF ANY;
key ← from.GetRefAny[];
value ← from.GetRefAny[];
propList ← List.PutAssoc[key: propList, val: value, aList: propList];
ENDLOOP;
EatDot[from];
END;
NotDot:
PROC [from:
IO.
STREAM]
RETURNS [not:
BOOLEAN] =
{[] ← from.SkipWhitespace[];
not ← from.PeekChar[] # '.};
EatDot:
PROC [from:
IO.
STREAM] = {
char: CHAR ← from.GetChar[];
IF char # '. THEN ERROR};
WriteFile:
PUBLIC
PROC [session: Session, file:
ROPE] =
BEGIN
EnsureWritten:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
BEGIN
stop ← FALSE;
WITH any
SELECT
FROM
cellType: CellType => {
IF cellType.writtenFor = thisWrite THEN RETURN;
cellType.components.EnumerateIncreasing[EnsureWritten];
WriteCellType[out, cellType]};
comp: Component => [] ← EnsureWritten[comp.type];
ENDCASE => ERROR;
END;
out: IO.STREAM ← FS.StreamOpen[fileName: file, accessOptions: create];
thisWrite: INT ← writeCount ← writeCount + 1;
session.typesTable.EnumerateIncreasing[EnsureWritten];
out.Close[];
END;
WriteCellType:
PROC [to:
IO.
STREAM, cellType: CellType] =
BEGIN
WriteIcon:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
BEGIN
pic: PictureDef ← NARROW[any];
stop ← FALSE;
WritePictureDef[to, pic];
END;
WritePort:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{port: Port ← NARROW[any];
stop ← FALSE;
to.PutF[" \"%q\"", IO.rope[port.name]];
WritePropList[to, port.otherProps];
to.PutF[" \"%q\"", IO.rope[port.pt.name]];
IF port.label # NIL THEN to.PutF[" label \"%q\"", IO.rope[port.label.name]] ELSE to.PutRope[" noLabel"]};
WriteNet:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{net: Net ← NARROW[any];
stop ← FALSE;
IF net.eatenBy # NIL THEN ERROR;
to.PutF[" \"%q\"", IO.rope[net.name]];
WritePropList[to, net.otherProps];
FOR l:
LORA ← net.stuff, l.rest
WHILE l #
NIL
DO
WITH l.first
SELECT
FROM
point: Point => to.PutF[" point \"%q\"", IO.rope[point.name]];
line: Line => to.PutF[" line \"%q\"", IO.rope[line.name]];
label: Text => to.PutF[" text \"%q\"", IO.rope[label.name]];
ENDCASE => ERROR;
ENDLOOP;
to.PutRope[" ."]};
WriteComponent:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{component: Component ← NARROW[any];
stop ← FALSE;
to.PutF[" %g %g %g", IO.refAny[component.name], IO.refAny[component.type.name], IO.refAny[component.inst.name]];
WritePropList[to, component.otherProps];
IF component.label # NIL THEN to.PutF[" label \"%q\"", IO.rope[component.label.name]] ELSE to.PutRope[" noLabel"]};
to.PutF[" \"%q\"", IO.rope[cellType.name]];
WritePropList[to, cellType.otherProps];
WritePictureDef[to, cellType.expansion];
cellType.icons.EnumerateIncreasing[WriteIcon];
to.PutRope[" ."];
cellType.ports.EnumerateIncreasing[WritePort];
to.PutRope[" ."];
cellType.nets.EnumerateIncreasing[WriteNet];
to.PutRope[" ."];
cellType.components.EnumerateIncreasing[WriteComponent];
to.PutRope[" ."];
END;
axisNames: PUBLIC ARRAY Axis OF ROPE ← ["X", "Y"];
WritePictureDef:
PROC [to:
IO.
STREAM, pic: PictureDef] =
BEGIN
WriteCoord:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{coord: Coord ← NARROW[any]; stop ← FALSE;
to.PutF[" %g %g %g %g %g", IO.refAny[coord.name], IO.rope[axisNames[coord.axis]], IO.bool[coord.exported], IO.real[coord.z], IO.real[coord.dz]];
IF coord.parent #
NIL
THEN
WITH coord.parent
SELECT
FROM
c: Coord => to.PutF[" c %g", IO.refAny[c.name]];
pi: PictureInstance => to.PutF[" p %g", IO.refAny[pi.name]];
ENDCASE => ERROR
ELSE to.PutRope[" n \"\""]};
WritePoint:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{point: Point ← NARROW[any];
stop ← FALSE;
to.PutF[" \"%q\" \"%q\" \"%q\" %g", IO.rope[point.name], IO.rope[point.c[X].name], IO.rope[point.c[Y].name], IO.bool[point.exported]];
to.PutF[" \"%q\" \"%q\"", IO.rope[point.fromPort], IO.rope[IF point.fromPI # NIL THEN point.fromPI.name ELSE NIL]]};
WriteObject:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{stop ← FALSE;
WITH any
SELECT
FROM
line: Line => to.PutF[" line \"%q\" \"%q\" \"%q\"", IO.rope[line.name], IO.rope[line.a.name], IO.rope[line.b.name]];
text: Text => to.PutF[" text \"%q\" \"%q\" \"%q\"", IO.rope[text.name], IO.rope[text.org.name], IO.rope[text.rope]];
pi: PictureInstance => {
to.PutF[" pi \"%q\" \"%q\" \"%q\"",
IO.rope[pi.name], IO.rope[pi.org.name], IO.rope[pi.of.name]];
pi.coords.EnumerateIncreasing[WritePIC];
to.PutRope[" ."];
pi.points.EnumerateIncreasing[WritePIP];
to.PutRope[" ."]};
ENDCASE => ERROR};
WritePIC:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{c: Coord ← NARROW[any]; stop ← FALSE;
to.PutF[" \"%q\"", IO.rope[c.name]]};
WritePIP:
PROC [any:
REF
ANY]
RETURNS [stop:
BOOLEAN] =
{p: Point ← NARROW[any]; stop ← FALSE;
to.PutF[" \"%q\"", IO.rope[p.name]]};
to.PutF[" \"%q\"", IO.rope[pic.name]];
pic.coordsByName.EnumerateIncreasing[WriteCoord];
to.PutRope[" ."];
pic.points.EnumerateIncreasing[WritePoint];
to.PutRope[" ."];
pic.objects.EnumerateIncreasing[WriteObject];
to.PutRope[" ."];
END;
WritePropList:
PROC [to:
IO.
STREAM, propList: PropList] =
BEGIN
FOR propList ← propList, propList.rest
WHILE propList #
NIL
DO
to.PutF[" %g %g", IO.refAny[propList.first.key], IO.refAny[propList.first.val]];
ENDLOOP;
to.PutRope[" ."];
END;
END.