! Copyright (C) 2007 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays assocs effects fry generalizations grouping kernel lexer macros math math.order math.vectors namespaces parser effects.parser quotations sequences sequences.private splitting.monotonic stack-checker strings unicode.case words ; IN: roman = ( ch1 ch2 -- ? ) [ roman-digit-index ] bi@ >= ; : roman>n ( ch -- n ) roman-digit-index roman-values nth ; : (roman>) ( seq -- n ) [ [ roman>n ] map ] [ all-eq? ] bi [ sum ] [ first2 swap - ] if ; PRIVATE> : >roman ( n -- str ) roman-range-check roman-values roman-digits [ [ /mod swap ] dip concat ] 2map "" concat-as nip ; : >ROMAN ( n -- str ) >roman >upper ; : roman> ( str -- n ) >lower [ roman>= ] monotonic-split [ (roman>) ] map-sum ; ] _ napply @ [ >roman ] _ napply ] ; PRIVATE> << SYNTAX: ROMAN-OP: scan-word [ name>> "roman" prepend create-in ] keep 1quotation '[ _ binary-roman-op ] complete-effect define-declared ; >> ROMAN-OP: + ( x y -- z ) ROMAN-OP: - ( x y -- z ) ROMAN-OP: * ( x y -- z ) ROMAN-OP: /i ( x y -- z ) ROMAN-OP: /mod ( x y -- z w ) SYNTAX: ROMAN: scan-token roman> suffix! ;