<> <> <> <> <<>> DIRECTORY BasicTime USING [GMT, Period], FS USING [Error, ExpandName, FileInfo, StreamOpen], IO USING [bool, char, Close, GetBool, GetInt, GetLineRope, GetReal, int, Put, PutF, PutRope, real, rope, STREAM], RobotDefs USING [Robot, RobotRec], RobotIO, RobotTParser USING [AssembleRobot], Rope USING [Cat, Concat, ROPE, Substr]; RobotIOImpl: CEDAR PROGRAM IMPORTS BasicTime, FS, IO, RobotTParser, Rope EXPORTS RobotIO ~ { OPEN RobotIO; WriteRobot: PUBLIC PROC [r: Rope.ROPE, robot: RobotDefs.Robot] ~ { OPEN IO; s: IO.STREAM _ FS.StreamOpen[Rope.Concat[r,".RobotC"], create]; IO.PutF[s, "%g\n", IO.rope[robot.creator]]; --Creator's name IO.Put[s,real[robot.version], char['\n]]; --Assembler Version <> FOR loc: CARDINAL IN [0..256) DO --Code IO.Put[s,int[robot.code[loc]], char[' ]]; ENDLOOP; FOR row: CARDINAL IN [0..16) DO --Picture FOR col: CARDINAL IN [0..16) DO IO.Put[s,bool[robot.pic[row][col]], char[' ]]; ENDLOOP; ENDLOOP; IO.Close[s]; }; ReadRobot: PUBLIC PROC [r: ROPE, wDir: ROPE _ NIL] RETURNS [robot: RobotDefs.Robot] ~ { OPEN IO; s: STREAM _ FS.StreamOpen[fileName: Rope.Concat[r,".RobotC"], accessOptions: read, wDir: wDir]; robot _ NEW[RobotDefs.RobotRec]; robot.creator _ GetLineRope[s]; --Creator's name robot.version _ GetReal[s]; --Assembler Version <> FOR loc: CARDINAL IN [0..256) DO --Code robot.code[loc] _ GetInt[s]; ENDLOOP; FOR row: CARDINAL IN [0..16) DO --Picture FOR col: CARDINAL IN [0..16) DO robot.pic[row][col] _ GetBool[s]; ENDLOOP; ENDLOOP; Close[s]; }; AssembleRobot: PUBLIC PROC [r: ROPE, log: IO.STREAM, onlyIfNew: BOOL _ TRUE, wDir: ROPE _ NIL] RETURNS [success: BOOLEAN] ~ { name: ROPE; createTime: BasicTime.GMT; robot: RobotDefs.Robot; [fullFName: name, created: createTime] _ FS.FileInfo[name: Rope.Cat[r, ".robot"], wDir: wDir ! FS.Error => {GOTO Fail}]; IF onlyIfNew AND BasicTime.Period[to: FS.FileInfo[name: Rope.Cat[r, ".robotc"], wDir: wDir ! FS.Error => CONTINUE].created, from: createTime] > 0 THEN RETURN [TRUE]; --I.e. we don't need to assemble <> IO.PutRope[log, Rope.Cat["Assembling ", name, ".\n"]]; [robot, success, ] _ RobotTParser.AssembleRobot[name, log]; IF success THEN { WriteRobot[Rope.Substr[base: name, len: FS.ExpandName[name].cp.ext.start-1], robot]; } ELSE { IO.PutRope[log, " Assembly failed.\n"]; }; EXITS Fail => { <> success _ TRUE; [] _ FS.FileInfo[name: Rope.Cat[r, ".robotc"], wDir: wDir ! FS.Error => { IO.PutRope[log, Rope.Cat[r, " not found.\n"]]; success _ FALSE; CONTINUE }]; }; }; }.