add parsing word to roman

db4
Doug Coleman 2008-10-21 20:30:26 -05:00
parent 003000b7cf
commit edb78de4a7
3 changed files with 12 additions and 1 deletions

View File

@ -43,3 +43,6 @@ HELP: roman/mod
{ $values { "str1" "a string" } { "str2" "a string" } { "str3" "a string" } { "str4" "a string" } }
{ $description "Computes the quotient and remainder of two Roman numerals." }
{ $see-also roman* roman/i /mod } ;
HELP: ROMAN:
{ $description "A parsing word that reads the next token and converts it to an integer." } ;

View File

@ -36,3 +36,5 @@ USING: arrays kernel math roman roman.private sequences tools.test ;
[ "i" ] [ "iii" "ii" roman/i ] unit-test
[ "i" "ii" ] [ "v" "iii" roman/mod ] unit-test
[ "iii" "iii" roman- ] must-fail
[ 30 ] [ ROMAN: xxx ] unit-test

View File

@ -2,10 +2,11 @@
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs kernel math math.order math.vectors
namespaces make quotations sequences sequences.lib
sequences.private strings unicode.case ;
sequences.private strings unicode.case lexer parser ;
IN: roman
<PRIVATE
: roman-digits ( -- seq )
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" } ;
@ -34,6 +35,7 @@ ERROR: roman-range-error n ;
] [
first2 swap -
] if ;
PRIVATE>
: >roman ( n -- str )
@ -49,11 +51,13 @@ PRIVATE>
] map sum ;
<PRIVATE
: 2roman> ( str1 str2 -- m n )
[ roman> ] bi@ ;
: binary-roman-op ( str1 str2 quot -- str3 )
>r 2roman> r> call >roman ; inline
PRIVATE>
: roman+ ( str1 str2 -- str3 )
@ -70,3 +74,5 @@ PRIVATE>
: roman/mod ( str1 str2 -- str3 str4 )
[ /mod ] binary-roman-op >r >roman r> ;
: ROMAN: scan roman> parsed ; parsing