'
' Convert Roman Number to Decimal Number
'   http://en.wikipedia.org/wiki/Roman_numerals
'
' September MMXIII, PvE - GPL - requires BaCon 2.3 or higher.
'
'-----------------------------------------------------------------------------

DECLARE term ASSOC int

SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim

IF dim < 2 THEN
    PRINT "Usage: deroman <value>"
    END
END IF

REPEAT
    READ rn$, value
    term(rn$) = value
UNTIL rn$ = "I"
DATA "M", 1000, "CM", 900, "D", 500, "CD", 400, "C", 100, "XC", 90, "L", 50, "XL", 40, "X", 10, "IX", 9, "V", 5, "IV", 4, "I", 1

result = 0

REPEAT
    IF REGEX(UCASE$(arg$[1]), "M|CM|D|CD|C|XC|L|XL|X|IX|V|IV|I") <> 1 THEN
        PRINT "Illegal Roman number!"
        END
    ENDIF

    INCR result, term(MID$(UCASE$(arg$[1]), 1, REGLEN))
    arg$[1] = MID$(arg$[1], REGLEN+1)
UNTIL LEN(arg$[1]) = 0

PRINT result