add parsing word to roman
parent
003000b7cf
commit
edb78de4a7
|
@ -43,3 +43,6 @@ HELP: roman/mod
|
||||||
{ $values { "str1" "a string" } { "str2" "a string" } { "str3" "a string" } { "str4" "a string" } }
|
{ $values { "str1" "a string" } { "str2" "a string" } { "str3" "a string" } { "str4" "a string" } }
|
||||||
{ $description "Computes the quotient and remainder of two Roman numerals." }
|
{ $description "Computes the quotient and remainder of two Roman numerals." }
|
||||||
{ $see-also roman* roman/i /mod } ;
|
{ $see-also roman* roman/i /mod } ;
|
||||||
|
|
||||||
|
HELP: ROMAN:
|
||||||
|
{ $description "A parsing word that reads the next token and converts it to an integer." } ;
|
||||||
|
|
|
@ -36,3 +36,5 @@ USING: arrays kernel math roman roman.private sequences tools.test ;
|
||||||
[ "i" ] [ "iii" "ii" roman/i ] unit-test
|
[ "i" ] [ "iii" "ii" roman/i ] unit-test
|
||||||
[ "i" "ii" ] [ "v" "iii" roman/mod ] unit-test
|
[ "i" "ii" ] [ "v" "iii" roman/mod ] unit-test
|
||||||
[ "iii" "iii" roman- ] must-fail
|
[ "iii" "iii" roman- ] must-fail
|
||||||
|
|
||||||
|
[ 30 ] [ ROMAN: xxx ] unit-test
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: arrays assocs kernel math math.order math.vectors
|
USING: arrays assocs kernel math math.order math.vectors
|
||||||
namespaces make quotations sequences sequences.lib
|
namespaces make quotations sequences sequences.lib
|
||||||
sequences.private strings unicode.case ;
|
sequences.private strings unicode.case lexer parser ;
|
||||||
IN: roman
|
IN: roman
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: roman-digits ( -- seq )
|
: roman-digits ( -- seq )
|
||||||
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" } ;
|
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" } ;
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ ERROR: roman-range-error n ;
|
||||||
] [
|
] [
|
||||||
first2 swap -
|
first2 swap -
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: >roman ( n -- str )
|
: >roman ( n -- str )
|
||||||
|
@ -49,11 +51,13 @@ PRIVATE>
|
||||||
] map sum ;
|
] map sum ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: 2roman> ( str1 str2 -- m n )
|
: 2roman> ( str1 str2 -- m n )
|
||||||
[ roman> ] bi@ ;
|
[ roman> ] bi@ ;
|
||||||
|
|
||||||
: binary-roman-op ( str1 str2 quot -- str3 )
|
: binary-roman-op ( str1 str2 quot -- str3 )
|
||||||
>r 2roman> r> call >roman ; inline
|
>r 2roman> r> call >roman ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: roman+ ( str1 str2 -- str3 )
|
: roman+ ( str1 str2 -- str3 )
|
||||||
|
@ -70,3 +74,5 @@ PRIVATE>
|
||||||
|
|
||||||
: roman/mod ( str1 str2 -- str3 str4 )
|
: roman/mod ( str1 str2 -- str3 str4 )
|
||||||
[ /mod ] binary-roman-op >r >roman r> ;
|
[ /mod ] binary-roman-op >r >roman r> ;
|
||||||
|
|
||||||
|
: ROMAN: scan roman> parsed ; parsing
|
||||||
|
|
Loading…
Reference in New Issue