]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/roman/roman.factor
Fixes #2966
[factor.git] / basis / roman / roman.factor
index 66fb3b302a492cfb7176446001e974c215a14a2d..edff040dacf15da26baae8b9f84bc7b8a0c80b1a 100644 (file)
@@ -1,10 +1,9 @@
 ! Copyright (C) 2007 Doug Coleman.
-! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays assocs fry generalizations grouping
-kernel lexer macros make math math.order math.vectors
-namespaces parser quotations sequences sequences.private
-splitting.monotonic stack-checker strings unicode.case
-words effects ;
+! See https://factorcode.org/license.txt for BSD license.
+USING: accessors combinators.smart effects.parser
+generalizations grouping kernel lexer math math.order
+parser quotations sequences splitting.monotonic strings unicode
+words ;
 IN: roman
 
 <PRIVATE
@@ -17,57 +16,57 @@ CONSTANT: roman-values
 
 ERROR: roman-range-error n ;
 
-: roman-range-check ( n -- )
-    dup 1 3999 between? [ drop ] [ roman-range-error ] if ;
+: roman-range-check ( n -- )
+    dup 1 10000 between? [ roman-range-error ] unless ;
 
 : roman-digit-index ( ch -- n )
     1string roman-digits index ; inline
 
-: roman<= ( ch1 ch2 -- ? )
+: roman-digit>= ( ch1 ch2 -- ? )
     [ roman-digit-index ] bi@ >= ;
 
-: roman>n ( ch -- n )
+: roman-digit-value ( ch -- n )
     roman-digit-index roman-values nth ;
 
-: (>roman) ( n -- )
-    roman-values roman-digits [
-        [ /mod swap ] dip <repetition> concat %
-    ] 2each drop ;
-
-: (roman>) ( seq -- n )
-    [ [ roman>n ] map ] [ all-eq? ] bi
+: roman-value ( seq -- n )
+    [ [ roman-digit-value ] map ] [ all-eq? ] bi
     [ sum ] [ first2 swap - ] if ;
 
 PRIVATE>
 
 : >roman ( n -- str )
-    dup roman-range-check [ (>roman) ] "" make ;
+    roman-range-check
+    roman-values roman-digits [
+        [ /mod swap ] dip <repetition> concat
+    ] 2map "" concat-as nip ;
 
 : >ROMAN ( n -- str ) >roman >upper ;
 
 : roman> ( str -- n )
-    >lower [ roman<= ] monotonic-split [ (roman>) ] sigma ;
+    >lower [ roman-digit>= ] monotonic-split
+    [ roman-value ] map-sum ;
 
 <PRIVATE
 
 MACRO: binary-roman-op ( quot -- quot' )
-    dup infer [ in>> swap ] [ out>> ] bi
+    [ inputs ] [ ] [ outputs ] tri
     '[ [ roman> ] _ napply @ [ >roman ] _ napply ] ;
 
 PRIVATE>
 
 <<
+
 SYNTAX: ROMAN-OP:
-    scan-word [ name>> "roman" prepend create-in ] keep
+    scan-word [ name>> "roman" prepend create-word-in ] keep
     1quotation '[ _ binary-roman-op ]
-    dup infer [ in>> ] [ out>> ] bi
-    [ "string" <repetition> ] bi@ <effect> define-declared ;
+    scan-effect define-declared ;
+
 >>
 
-ROMAN-OP: +
-ROMAN-OP: -
-ROMAN-OP: *
-ROMAN-OP: /i
-ROMAN-OP: /mod
+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 roman> parsed ;
+SYNTAX: ROMAN: scan-token roman> suffix! ;