mirror of
https://github.com/pnx/m16vm
synced 2026-07-02 11:33:40 +02:00
as/lexer/number.c: cleaning up lexer_read_num_dec() abit, skipping goto.
This commit is contained in:
parent
969c092e6f
commit
502f284ebd
1 changed files with 9 additions and 8 deletions
|
|
@ -52,7 +52,7 @@ overflow:
|
||||||
|
|
||||||
int lexer_read_num_dec(FILE *fp, int neg, int *out) {
|
int lexer_read_num_dec(FILE *fp, int neg, int *out) {
|
||||||
|
|
||||||
int c, val = 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)) {
|
||||||
|
|
@ -64,14 +64,15 @@ int lexer_read_num_dec(FILE *fp, int neg, int *out) {
|
||||||
// Cool trick here.
|
// Cool trick here.
|
||||||
// because the range is -128 (0x80) to +127 (0x7F)
|
// because the range is -128 (0x80) to +127 (0x7F)
|
||||||
// We can do 0x80 - 1 if it is NOT a negative number.
|
// We can do 0x80 - 1 if it is NOT a negative number.
|
||||||
if (val > (0x80 - !neg))
|
if (val > (0x80 - !neg)) {
|
||||||
goto overflow;
|
// Truncate value.
|
||||||
|
val = 0x80 - !neg;
|
||||||
|
oflow = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = neg ? -1 * val : val;
|
*out = neg ? -1 * val : val;
|
||||||
return 0;
|
return -oflow;
|
||||||
|
|
||||||
overflow:
|
|
||||||
*out = neg ? -1 * 0x80 : 0x7F;
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue