mirror of
https://github.com/pnx/m16vm
synced 2026-07-03 11:43:39 +02:00
as/lexer/number.c: should not stop parsing if we overflow.
This commit is contained in:
parent
502f284ebd
commit
a105d5087e
1 changed files with 9 additions and 3 deletions
|
|
@ -55,10 +55,16 @@ int lexer_read_num_dec(FILE *fp, int neg, int *out) {
|
||||||
int c, val = 0, oflow = 0;
|
int c, val = 0, oflow = 0;
|
||||||
|
|
||||||
while((c = fgetc(fp)) != EOF) {
|
while((c = fgetc(fp)) != EOF) {
|
||||||
|
|
||||||
if (!lexer_is_num(c)) {
|
if (!lexer_is_num(c)) {
|
||||||
ungetc(c, fp);
|
ungetc(c, fp);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignore if we have overflowed, but keep reading all numbers.
|
||||||
|
if (oflow)
|
||||||
|
continue;
|
||||||
|
|
||||||
val = (val * 10) + (c - '0');
|
val = (val * 10) + (c - '0');
|
||||||
|
|
||||||
// Cool trick here.
|
// Cool trick here.
|
||||||
|
|
@ -67,10 +73,10 @@ int lexer_read_num_dec(FILE *fp, int neg, int *out) {
|
||||||
if (val > (0x80 - !neg)) {
|
if (val > (0x80 - !neg)) {
|
||||||
// Truncate value.
|
// Truncate value.
|
||||||
val = 0x80 - !neg;
|
val = 0x80 - !neg;
|
||||||
oflow = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Set overflow flag.
|
||||||
|
oflow = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = neg ? -1 * val : val;
|
*out = neg ? -1 * val : val;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue