From 35bd2abc71bbb2483321cc9955e96bd75eb5fecb Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 20 Jul 2008 04:05:09 -0500 Subject: [PATCH] Fix more math.parser stuff --- core/math/parser/parser-tests.factor | 6 ++++++ core/math/parser/parser.factor | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/core/math/parser/parser-tests.factor b/core/math/parser/parser-tests.factor index 0da0b8fb25..c16a031690 100755 --- a/core/math/parser/parser-tests.factor +++ b/core/math/parser/parser-tests.factor @@ -101,4 +101,10 @@ unit-test [ "-1.0/0.0" ] [ -1.0 0.0 / number>string ] unit-test +[ 0.0/0.0 ] [ "0/0." string>number ] unit-test + +[ 1.0/0.0 ] [ "1/0." string>number ] unit-test + +[ -1.0/0.0 ] [ "-1/0." string>number ] unit-test + [ "-0.0" ] [ -0.0 number>string ] unit-test diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index 718410d293..1cb2ae6cdf 100755 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -55,8 +55,9 @@ SYMBOL: negative? dup [ (base>) ] [ drop 0 swap ] if ; : string>ratio ( str -- a/b ) + "-" ?head dup negative? set swap "/" split1 (base>) >r whole-part r> - 3dup and and [ / + ] [ 3drop f ] if ; + 3dup and and [ / + swap [ neg ] when ] [ 2drop 2drop f ] if ; : valid-digits? ( seq -- ? ) { @@ -66,20 +67,22 @@ SYMBOL: negative? } cond ; : string>integer ( str -- n/f ) + "-" ?head swap string>digits dup valid-digits? - [ radix get digits>integer ] [ drop f ] if ; + [ radix get digits>integer swap [ neg ] when ] [ 2drop f ] if ; PRIVATE> : base> ( str radix -- n/f ) [ - CHAR: . over member? [ - string>float + CHAR: / over member? [ + string>ratio ] [ - "-" ?head dup negative? set >r - CHAR: / over member? - [ string>ratio ] [ string>integer ] if - r> [ dup [ neg ] when ] when + CHAR: . over member? [ + string>float + ] [ + string>integer + ] if ] if ] with-radix ;