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