RobotIOImpl.mesa
Created Saturday, June 9, 1984 4:24 pm PDT
Last edited by Eric Nickell, July 6, 1985 1:29:56 pm PDT
This interface provide the tools to read and write compiled robot images to and from the disk.
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.STREAMFS.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
IO.Put[s,time[robot.timeOfSource], char['\n]]; --Time the source version was last edited
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: ROPENIL] RETURNS [robot: RobotDefs.Robot] ~ {
OPEN IO;
s: STREAMFS.StreamOpen[fileName: Rope.Concat[r,".RobotC"], accessOptions: read, wDir: wDir];
robot ← NEW[RobotDefs.RobotRec];
robot.creatorGetLineRope[s];  --Creator's name
robot.version ← GetReal[s];  --Assembler Version
robot.timeOfSourceGetTime[s];  --Time the source version was last edited
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: BOOLTRUE, wDir: ROPENIL] 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
Here, we need to attempt assembly
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 => {
Complain if no .robotC file either
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
}];
};
};
}.