%option "-low-nonstandard-nocheck-nodiag-nostack-noline"

%constbytearray l fives(1 : 4) = '?', 'd', 'l', 'v'
%constbytearray l ones (1 : 4) = 'm', 'c', 'x', 'i'

%externalroutine print lower Roman(%integer i)
   %integer n
   %switch a(0 : 9)
      %unless 0 < i < 4000 %start
         printstring("? ")
         write(i, 0)
         %return
      %finish
      %for n = 1, 1, 4 %cycle
         -> a(i // 1000)
a(0):    ! nothing at all
         -> next term
a(1):    ! i
         print symbol(l ones(n))
         -> next term
a(2):    ! ii
         print symbol(l ones(n))
         print symbol(l ones(n))
         -> next term
a(3):    ! iii
         print symbol(l ones(n))
         print symbol(l ones(n))
         print symbol(l ones(n))
         -> next term
a(4):    ! iv
         print symbol(l ones(n))
         print symbol(l fives(n))
         -> next term
a(5):    ! v
         print symbol(l fives(n))
         -> next term
a(6):    ! vi
         print symbol(l fives(n))
         print symbol(l ones(n))
         -> next term
a(7):    ! vii
         print symbol(l fives(n))
         print symbol(l ones(n))
         print symbol(l ones(n))
         -> next term
a(8):    ! viii
         print symbol(l fives(n))
         print symbol(l ones(n))
         print symbol(l ones(n))
         print symbol(l ones(n))
         -> next term
a(9):    ! ix
         print symbol(l ones(n))
         print symbol(l ones(n - 1))
next term:
         i = rem(i * 10, 10 000)
      %repeat
%end


%constbytearray U fives(1 : 4) = '?', 'D', 'L', 'V'
%constbytearray U ones (1 : 4) = 'M', 'C', 'X', 'I'

%externalroutine print UPPER Roman(%integer i)
   %integer n
   %switch a(0 : 9)
      %unless 0 < i < 4000 %start
         printstring("? ")
         write(i, 0)
         %return
      %finish
      %for n = 1, 1, 4 %cycle
         -> a(i // 1000)
a(0):    ! nothing at all
         -> next term
a(1):    ! i
         print symbol(U ones(n))
         -> next term
a(2):    ! ii
         print symbol(U ones(n))
         print symbol(U ones(n))
         -> next term
a(3):    ! iii
         print symbol(U ones(n))
         print symbol(U ones(n))
         print symbol(U ones(n))
         -> next term
a(4):    ! iv
         print symbol(U ones(n))
         print symbol(U fives(n))
         -> next term
a(5):    ! v
         print symbol(U fives(n))
         -> next term
a(6):    ! vi
         print symbol(U fives(n))
         print symbol(U ones(n))
         -> next term
a(7):    ! vii
         print symbol(U fives(n))
         print symbol(U ones(n))
         print symbol(U ones(n))
         -> next term
a(8):    ! viii
         print symbol(U fives(n))
         print symbol(U ones(n))
         print symbol(U ones(n))
         print symbol(U ones(n))
         -> next term
a(9):    ! ix
         print symbol(U ones(n))
         print symbol(U ones(n - 1))
next term:
         i = rem(i * 10, 10 000)
      %repeat
%end

%end %of %file
