1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
with Except;
with Input;
with Screen_Output;
package body Tokens is
----------
-- Next --
----------
function Next return Token is
begin
loop -- until we have read a valid token.
-- Open block to catch exceptions raised for user errors.
Read_A_Valid_Token : declare
Word : String := Input.Next_Word;
begin
-- Figure out which kind of token we have from the first
-- character and delegate the full token recognition to
-- the Read routine in the appropriate Instruction, Values
-- or Values.Operations package.
case Word (Word'First) is
when '0' .. '9' | '.' =>
return Token'(Kind => Val, Val => Values.Read (Word));
when '+' | '*' | '/' =>
return
Token'(Kind => Op, Op => Values.Operations.Read (Word));
when '-' =>
if Word'Length > 1 then
return Token'(Kind => Val, Val => Values.Read (Word));
else
return
Token'(Kind => Op, Op => Values.Operations.Read (Word));
end if;
when 'a' .. 'z'
| 'A' .. 'Z' =>
return
Token'(Kind => Instr, Instr => Instructions.Read (Word));
when others =>
raise Except.User_Error;
end case;
exception
when Except.User_Error =>
Screen_Output.Syntax_Error ("Input ignored till end of line.");
Input.Skip_Line;
end Read_A_Valid_Token;
end loop;
end Next;
-------------
-- Process --
-------------
procedure Process (T : Token) is
begin
case T.Kind is
when Val =>
Values.Process (T.Val);
when Op =>
Values.Operations.Process (T.Op);
when Instr =>
Instructions.Process (T.Instr);
end case;
end Process;
end Tokens;
|