RobotScannerImpl.mesa
Created Monday, May 21, 1984 12:03 pm PDT
Last edited by Eric Nickell, June 10, 1984 0:24:16 am PDT
DIRECTORY
BasicTime USING [GMT, nullGMT],
Convert USING [IntFromRope],
FS USING [GetInfo, OpenFileFromStream, StreamOpen],
IO USING [EndOfStream, GetCedarTokenRope, GetIndex, TokenKind, STREAM],
RobotEvaluator USING [opOR, opAND, opNOT, opMOD, opLT, opEQ, opGT, opNE, opGE, opLE, opPlus, opMinus, opTimes, opDivide],
RobotOpCodes USING [IsOpCode, OpCode],
RobotScanner, --USING everything
Rope USING [ROPE, Equal];
RobotScannerImpl: CEDAR PROGRAM
IMPORTS Convert, FS, IO, RobotEvaluator, RobotOpCodes, Rope
EXPORTS RobotScanner
~ {
OPEN RobotScanner;
GetTokensFromFile: PUBLIC PROC [file: Rope.ROPE] RETURNS [scannedProgram: ScannedProgram ← NEW[ScannedProgramRec]] ~ {
OPEN RobotEvaluator, RobotOpCodes;
prog: IO.STREAMNIL;
cedarTokenKind: IO.TokenKind ← tokenID;
tokenText: Rope.ROPE;
idx: INT;    --To contain an index into the file
prog ← FS.StreamOpen[file];
scannedProgram.count ← 0;
scannedProgram.token[scannedProgram.count] ← NEW[RTknRec [time, CreateTime[prog],0]];
scannedProgram.count ← scannedProgram.count + 1;
DO
[cedarTokenKind, tokenText] ← IO.GetCedarTokenRope[prog ! IO.EndOfStream => EXIT];
idx ← IO.GetIndex[prog];
scannedProgram.token[scannedProgram.count] ← NEW[RTknRec
SELECT cedarTokenKind FROM
tokenDECIMAL, tokenOCTAL, tokenHEX =>
[tknNum, REFIntFromRope[tokenText]],
tokenID => SELECT TRUE FROM
Rope.Equal[tokenText,"IF"] => [tknIF,NIL],
Rope.Equal[tokenText,"THEN"] => [tknTHEN,NIL],
Rope.Equal[tokenText,"ELSE"] => [tknELSE,NIL],
Rope.Equal[tokenText,"OR"] => [tknOR, opOR],
Rope.Equal[tokenText,"AND"] => [tknAND, opAND],
Rope.Equal[tokenText,"NOT"] => [tknNOT, opNOT],
Rope.Equal[tokenText,"MOD"] => [tknMulop, opMOD],
IsOpCode[tokenText] => [tknInstruction, OpCode[tokenText]],
ENDCASE => [tknID, tokenText],
tokenSINGLE, tokenDOUBLE => SELECT TRUE FROM
Rope.Equal[tokenText,"("] => [tknLParen, NIL],
Rope.Equal[tokenText,")"] => [tknRParen, NIL],
Rope.Equal[tokenText,"["] => [tknLBracket, NIL],
Rope.Equal[tokenText,"]"] => [tknRBracket, NIL],
Rope.Equal[tokenText,"^"] => [tknIndirect, NIL],
Rope.Equal[tokenText,":"] => [tknColon, NIL],
Rope.Equal[tokenText,";"] => [tknSemiColon, NIL],
Rope.Equal[tokenText,","] => [tknComma, NIL],
Rope.Equal[tokenText,"←"] => [tknAssign, NIL],
Rope.Equal[tokenText,"<"] => [tknRelop, opLT],
Rope.Equal[tokenText,"="] => [tknRelop, opEQ],
Rope.Equal[tokenText,">"] => [tknRelop, opGT],
Rope.Equal[tokenText,"+"] => [tknAddop, opPlus],
Rope.Equal[tokenText,"-"] => [tknAddop, opMinus],
Rope.Equal[tokenText,"*"] => [tknMulop, opTimes],
Rope.Equal[tokenText,"/"] => [tknMulop, opDivide],
Rope.Equal[tokenText,"~"] => [tknNOT, opNOT],
Rope.Equal[tokenText,"#"] => [tknImmediate, NIL],
Rope.Equal[tokenText,">="], Rope.Equal[tokenText, "~<"] => [tknRelop, opGE],
Rope.Equal[tokenText,"<="], Rope.Equal[tokenText, "~>"] => [tknRelop, opLE],
Rope.Equal[tokenText, "~="] => [tknRelop, opNE],
ENDCASE => ERROR,
tokenEOF => [tknEOF, NIL], --Never gets seen
ENDCASE => ERROR
];
scannedProgram.token[scannedProgram.count].pc ← idx;
scannedProgram.count ← scannedProgram.count + 1;
ENDLOOP;
scannedProgram.token[scannedProgram.count] ← NEW[RTknRec ← [tknEOF, NIL]];
};
CreateTime: PROC [s: IO.STREAM] RETURNS [t: REF BasicTime.GMTNEW[BasicTime.GMT ← BasicTime.nullGMT]] ~ {
[created: t^] ← FS.GetInfo[FS.OpenFileFromStream[s]];
};
REFIntFromRope: PRIVATE PROC [rope: Rope.ROPE] RETURNS [int: REF INT] ~ {
int ← NEW[INT];
int^ ← Convert.IntFromRope[rope];
};
}.