Skip to content

Commit

Permalink
Work around locale based atof computation
Browse files Browse the repository at this point in the history
  • Loading branch information
kovben2004 committed Dec 26, 2024
1 parent 89742be commit ba2174e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions statements/pbrst.l
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>

void yyerror(char *s, ...);
double myatof(char *arr);

int yycolumn = 0;
#define YY_USER_ACTION \
Expand Down Expand Up @@ -130,13 +131,13 @@ REVELATION_OF_JOHN { yylval.strval = strdup(yytext); return REVELATION_OF_JOHN;
"." { return PERIOD; }

/* numbers */
[0-9]+ { yylval.intval = atoi(yytext);
return INTNUM; }
[0-9]+ { yylval.intval = atoi(yytext);
return INTNUM; }

[0-9]+"."[0-9]*"%" |
"."[0-9]+"%" { yylval.strval = strdup(yytext);
yylval.strval[yyleng-1] = 0;
yylval.floatval = atof(yylval.strval) ;
yylval.floatval = myatof(yylval.strval);
return APPROXNUM; }

/* strings */
Expand Down
31 changes: 31 additions & 0 deletions statements/pbrst.y
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ void check_introduction_passage(char *passage, char *ay);
void check_cover(double cover);
void check_unique_prepare();
void check_fragment(char *passage, char *ay_nt, char *ay_ot);
double myatof(char *arr);
}

%%
Expand Down Expand Up @@ -655,6 +656,36 @@ yyerror(char *s, ...)
va_end(ap);
}

// Taken from https://stackoverflow.com/a/67521458/1044586
double myatof(char *arr)
{
double val = 0;
int afterdot=0;
double scale=1;
int neg = 0;

if (*arr == '-') {
arr++;
neg = 1;
}
while (*arr) {
if (afterdot) {
scale = scale/10;
val = val + (*arr-'0')*scale;
} else {
if (*arr == '.')
afterdot++;
else
val = val * 10.0 + (*arr - '0');
}
arr++;
}
if(neg) return -val;
else return val;
}



typedef struct yy_buffer_state * YY_BUFFER_STATE;
extern int yyparse();
extern YY_BUFFER_STATE yy_scan_string(char * str);
Expand Down

0 comments on commit ba2174e

Please sign in to comment.