Skip to content

Commit

Permalink
Make parse_simple_number impossible to reach end without returning
Browse files Browse the repository at this point in the history
Make sure that it's not possible to reach the end of the function
without returning or throwing. This fixes a compiler warning about this.

Also change the comments from // style to /* */ which is MoarVM's
recommended style.

Make an if else chain into a switch for greater clairity.

Remove some else's that were unneeded.
  • Loading branch information
samcv committed Jul 5, 2018
1 parent 29cc327 commit 0eb2530
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/strings/parse_num.c
Expand Up @@ -47,7 +47,8 @@ int static get_cp(MVMThreadContext *tc, MVMCodepointIter *ci, MVMCodepoint *cp)
}
}

static void parse_error(MVMThreadContext *tc, MVMString *s, const char* reason) {
MVM_NO_RETURN static void parse_error(MVMThreadContext *tc, MVMString *s, const char* reason) MVM_NO_RETURN_ATTRIBUTE;
MVM_NO_RETURN static void parse_error(MVMThreadContext *tc, MVMString *s, const char* reason) {
char* got = MVM_string_utf8_c8_encode_C_string(tc, s);
char *waste[] = { got, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "Can't convert '%s' to num: %s", got, reason);
Expand Down Expand Up @@ -199,7 +200,7 @@ static int match_word(MVMThreadContext *tc, MVMCodepointIter *ci, MVMCodepoint

static double parse_simple_number(MVMThreadContext *tc, MVMCodepointIter *ci, MVMCodepoint *cp, MVMString *s) {
double sign;
// Handle NaN here, to make later parsing simpler
/* Handle NaN here, to make later parsing simpler */

if (match_word(tc, ci, cp, "NaN", s)) {
return MVM_num_nan(tc);
Expand All @@ -218,26 +219,22 @@ static double parse_simple_number(MVMThreadContext *tc, MVMCodepointIter *ci, MV
if (*cp == '<') {
get_cp(tc, ci, cp);
body = parse_int_frac_exp(tc, ci, cp, s, radix, 0);
if (*cp == '>') {
if (*cp == '>') { /* > */
get_cp(tc, ci, cp);
return sign * body;
}
else {
parse_error(tc, s, "malformed ':radix<>' style radix number, expecting '>' after the body");
}
parse_error(tc, s, "malformed ':radix<>' style radix number, expecting '>' after the body");
}
else if (*cp == 171) { // «
else if (*cp == 171) { /* « */
get_cp(tc, ci, cp);
body = parse_int_frac_exp(tc, ci, cp, s, radix, 0);
if (*cp == 187) { // »
if (*cp == 187) { /* » */
get_cp(tc, ci, cp);
return sign * body;
}
else {
parse_error(tc, s, "malformed ':radix«»' style radix number, expecting '>' after the body");
}
parse_error(tc, s, "malformed ':radix«»' style radix number, expecting '>' after the body");
}
else if (*cp == '[') { // «
else if (*cp == '[') {
double result = 0;
get_cp(tc, ci, cp);
while (*cp != ']' && MVM_string_ci_has_more(tc, ci)) {
Expand All @@ -247,31 +244,30 @@ static double parse_simple_number(MVMThreadContext *tc, MVMCodepointIter *ci, MV
get_cp(tc, ci, cp);
}
}
if (*cp == ']') { // »
if (*cp == ']') {
get_cp(tc, ci, cp);
return sign * result;
}
else {
parse_error(tc, s, "malformed ':radix[]' style radix number, expecting ']' after the body");
}
parse_error(tc, s, "malformed ':radix[]' style radix number, expecting ']' after the body");
}
parse_error(tc, s, "malformed ':radix' style number. Expected <, [ or « after ':radix'");
}
else if (*cp == '0') {
int radix = 0;

get_cp(tc, ci, cp);
if (*cp == 'b') radix = 2;
else if (*cp == 'o') radix = 8;
else if (*cp == 'd') radix = 10;
else if (*cp == 'x') radix = 16;

switch (*cp) {
case 'b': radix = 2; break;
case 'o': radix = 8; break;
case 'd': radix = 10; break;
case 'x': radix = 16; break;
}
if (radix) {
get_cp(tc, ci, cp);
if (*cp == '_') get_cp(tc, ci, cp);
return sign * parse_int_frac_exp(tc, ci, cp, s, radix, 1);
} else {
return sign * parse_int_frac_exp(tc, ci, cp, s, 10, 1);
}
return sign * parse_int_frac_exp(tc, ci, cp, s, 10, 1);
}
else {
return sign * parse_int_frac_exp(tc, ci, cp, s, 10, 0);
Expand Down

0 comments on commit 0eb2530

Please sign in to comment.