1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs fry generalizations grouping
4 kernel lexer macros make math math.order math.vectors
5 namespaces parser quotations sequences sequences.private
6 splitting.monotonic stack-checker strings unicode.case
12 CONSTANT: roman-digits
13 { "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" }
15 CONSTANT: roman-values
16 { 1000 900 500 400 100 90 50 40 10 9 5 4 1 }
18 ERROR: roman-range-error n ;
20 : roman-range-check ( n -- )
21 dup 1 3999 between? [ drop ] [ roman-range-error ] if ;
23 : roman-digit-index ( ch -- n )
24 1string roman-digits index ; inline
26 : roman<= ( ch1 ch2 -- ? )
27 [ roman-digit-index ] bi@ >= ;
30 roman-digit-index roman-values nth ;
33 roman-values roman-digits [
34 [ /mod swap ] dip <repetition> concat %
37 : (roman>) ( seq -- n )
38 [ [ roman>n ] map ] [ all-eq? ] bi
39 [ sum ] [ first2 swap - ] if ;
44 dup roman-range-check [ (>roman) ] "" make ;
46 : >ROMAN ( n -- str ) >roman >upper ;
49 >lower [ roman<= ] monotonic-split [ (roman>) ] sigma ;
53 MACRO: binary-roman-op ( quot -- quot' )
54 [ infer in>> ] [ ] [ infer out>> ] tri
55 '[ [ roman> ] _ napply @ [ >roman ] _ napply ] ;
61 scan-word [ name>> "roman" prepend create-in ] keep
62 1quotation '[ _ binary-roman-op ]
63 dup infer [ in>> ] [ out>> ] bi
64 [ "string" <repetition> ] bi@ <effect> define-declared ;
73 SYNTAX: ROMAN: scan roman> parsed ;