From 4301066c76c72c1306455500cfb022d7bcd8a552 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Tue, 19 Feb 2019 05:02:37 +0000 Subject: [PATCH] fixed a bug in string_to_fpdec(). supports the plus sign before a numeric literal --- moo/kernel/test-001.moo | 2 ++ moo/kernel/test-003.moo | 5 +++-- moo/lib/comp.c | 11 ++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/moo/kernel/test-001.moo b/moo/kernel/test-001.moo index 5515378..d366253 100644 --- a/moo/kernel/test-001.moo +++ b/moo/kernel/test-001.moo @@ -234,7 +234,9 @@ extend MyObject [ ((-10.19 scale: 0) scale) = (10 scale) ], [ (-9p10 scale) = (-10.000000000 scale) ], [ (-9p10.123 scale) = (-10.123000000 scale) ], + [ (+3p100.1 + 16rffff + +5p1.22 + -5p1.223) = 65635.09700 ], + ## ========================= [ | b | b := [:n | (n > 0) ifTrue: [ n * (b value: n - 1)] ifFalse: [1]]. diff --git a/moo/kernel/test-003.moo b/moo/kernel/test-003.moo index 89ebce2..ee82da1 100644 --- a/moo/kernel/test-003.moo +++ b/moo/kernel/test-003.moo @@ -29,8 +29,9 @@ class MyObject(Object) ##rec := [ :y :z | (108.0000000000000000000000 - (815.000000000000000000 - (1500.0000000000000000 div: z) div: y)) truncate: 18. ]. ##rec := [ :y :z | (108.0000000000000000000000 - (815.000000000000000000 - (1500.0000000000000000 div: z) div: y)) truncate: 16. ]. ##rec := [ :y :z | 108.0000000000000000000000 - (815.000000000000000000 - (1500.0000000000000000 div: z) div: y) ]. - rec := [ :y :z | (108.0 scale: 22) - ((815 scale: 18) - ((1500 scale: 16) div: z) div: y) ]. + ##rec := [ :y :z | (108.0 scale: 22) - ((815 scale: 18) - ((1500 scale: 16) div: z) div: y) ]. ##rec := [ :y :z | 108.000000000000000000000000000000 - (815.00000000000000000000000000 - (1500.0000000000000000 div: z) div: y) ]. + rec := [ :y :z | 22p108 - (18p815 - (16p1500 div: z) div: y) ]. ## results := %( 4.0, 425 div: 100.0 ) asOrderedCollection. @@ -90,6 +91,6 @@ class MyObject(Object) | tb | tb := tc at: idx. System log(System.Log.INFO, idx asString, (if (tb value) { ' PASS' } else { ' FAIL' }), S'\n'). - ] + ]. } } diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 0a2914a..4dde456 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -709,13 +709,14 @@ static moo_oop_t string_to_fpdec (moo_t* moo, moo_oocs_t* str, int prescaled) moo_oow_t pos, len; moo_oow_t scale = 0, xscale = 0; moo_oop_t v; - int base = 10; + int base = 10, dotted = 0; pos = str->len; while (pos > 0) { if (str->ptr[--pos] == '.') { + dotted = 1; scale = str->len - pos - 1; MOO_ASSERT (moo, scale > 0); MOO_ASSERT (moo, scale <= MOO_SMOOI_MAX); @@ -725,7 +726,7 @@ static moo_oop_t string_to_fpdec (moo_t* moo, moo_oocs_t* str, int prescaled) } pos = 0; - len = str->len - 1; + len = str->len - dotted; if (str->ptr[pos] == '+' || str->ptr[pos] == '-') { if (str->ptr[pos] - '+') base = -10; @@ -1530,6 +1531,8 @@ static int get_numlit (moo_t* moo, int negated) moo_oow_t scale = 0; fixed_point: + SET_TOKEN_TYPE (moo, (xscale > 0? MOO_IOTOK_SCALEDFPDECLIT: MOO_IOTOK_FPDECLIT)); + period = moo->c->lxc; GET_CHAR_TO (moo, c); if (!is_digitchar(c)) @@ -1567,8 +1570,6 @@ static int get_numlit (moo_t* moo, int negated) while (is_digitchar(c)); MOO_ASSERT (moo, scale > 0 && scale <= MOO_SMOOI_MAX); - - SET_TOKEN_TYPE (moo, (xscale > 0? MOO_IOTOK_SCALEDFPDECLIT: MOO_IOTOK_FPDECLIT)); unget_char (moo, &moo->c->lxc); } } @@ -1823,7 +1824,7 @@ static int get_binsel (moo_t* moo) GET_CHAR (moo); /* special case if a minus is followed by a digit immediately */ - if (oc == '-' && is_digitchar(moo->c->lxc.c)) return get_numlit (moo, 1); + if ((oc == '-' || oc == '+') && is_digitchar(moo->c->lxc.c)) return get_numlit (moo, 1); #if 1 /* up to 2 characters only */