%begin
   %externalroutinespec Phex %alias "IMP_PRINT_HEX"(%integer N, P)
   %constinteger Max Opcodes = 100
   %integer N = 0
   %string(4)%array Opcode(1:Max Opcodes)
   %bytearray         Type(1:Max Opcodes)
   %integerarray     Value(1:Max Opcodes)
   %string(255) Ops = ""

   %routine Read Binary(%integername N)
      %integer S, Err = 0
      Skipsymbol %while Nextsymbol = ' ' %or Nextsymbol = NL
      N = 0
      %cycle
         Readsymbol(S);  %exit %if S = NL
         %continue %if S <= ' '
         Err = S %unless S = '0' %or S = '1'
         N = N<<1 ! (S-'0')
      %repeat
      %if Err # 0 %start
         Printstring("Illegal binary digit ")
         Printsymbol(Err)
         Printstring("' for ".Ops)
         Newline
      %finish
   %end

   %routine Read Opcodes
      %integer     Ty, Val, S
      %on 9 %start
         %return
      %finish
      %cycle
         Skipsymbol %while Nextsymbol = ' ' %or Nextsymbol = NL
         Ops = ""
         %cycle
            Readsymbol(S);  %exit %if S = ' '
            S = S-32 %if 'a' <= S <= 'z'
            Ops = Ops.Tostring(S)
         %repeat
         %if Length(Ops) > 4 %start
            Printstring("Illegal opcode length '".Ops."'")
            Newline
            Length(Ops) = 4
         %finish
         Read(Ty)
         Read Binary(Val)
         %if N = Max Opcodes %start
            Printstring("Too many opcodes");  Newline
            %stop
         %finish
         N = N+1
         Opcode(N) = Ops
         Type(N) = Ty
         Value(N) = Val
      %repeat
   %end

   %routine Print Opcodes
      %integer J, Left = 0
      %string(4) S
      Printstring("%constinteger Max Opcode =");  Write(N, 1);  Newlines(2)
      Printstring("%conststring(4)%array Opcode(1:Max Opcode) =")
      %for J = 1, 1, N %cycle
         Left = Left-1
         Left = 10 %and Newline %if Left <= 0
         S = Opcode(J)
         Spaces(5-Length(S))
         Printstring("""".S."""")
         Printsymbol(',') %unless J = N
      %repeat
      Newlines(2)
   %end

   %routine Print Types
      %integer J, Left = 0
      %string(4) S
      Printstring("%constbytearray Type(1:Max Opcode) =")
      %for J = 1, 1, N %cycle
         Left = Left-1
         Left = 10 %and Newline %if Left <= 0
         Write(Type(J), 6)
         Printsymbol(',') %unless J = N
      %repeat
      Newlines(2)
   %end

   %routine Print Value
      %integer J, Left = 0
      %string(4) S
      Printstring("%constintegerarray Value(1:Max Opcode) =")
      %for J = 1, 1, N %cycle
         Left = Left-1
         Left = 6 %and Newline %if Left <= 0
         Printstring("16_")
         Phex(Value(J), 8)
         Printsymbol(',') %unless J = N
      %repeat
      Newlines(2)
   %end

   %routine Sort Opcodes
      %integer J, Flag, Limit, W
      %string(4) T
      Limit = N
      %cycle
         Limit = Limit-1;  %exit %if Limit <= 0
         Flag = 0
         %for J = 1, 1, Limit %cycle
            %if Opcode(J) > Opcode(J+1) %start
               T = Opcode(J);  Opcode(J) = Opcode(J+1);  Opcode(J+1) = T
               W =   Type(J);    Type(J) =   Type(J+1);    Type(J+1) = W
               W =  Value(J);   Value(J) =  Value(J+1);   Value(J+1) = W
               Flag = 1
            %finish
         %repeat
      %repeat %until Flag = 0
   %end

   Open Input(1, "MCODE.OPS")
   Select Input(1)

   Read Opcodes
   Write(N, 0);  Printstring(" opcodes input");  Newline
   Sort Opcodes
   Open Output(1, "MTABLES.INC");  Select Output(1)
   Print Opcodes
   Print Types
   Print Value
   Select Output(0)
   Printstring("MTABLES.INC complete");  Newline
%endofprogram
