Make hex.c more mescc friendly to simplify bootstrapping

This commit is contained in:
Jan Nieuwenhuizen 2017-11-19 09:52:14 +01:00
parent c4cd93bc24
commit 14843efa5e
1 changed files with 27 additions and 27 deletions

View File

@ -35,21 +35,25 @@ void purge_line_comments()
int hex(char c) int hex(char c)
{ {
switch(c) if (c >= '0' && c <= '9')
{ {
case '0' ... '9': return (c - 48); return (c - 48);
case 'a' ... 'f': return (c - 87);
case 'A' ... 'F': return (c - 55);
default: break;
} }
else if (c >= 'a' && c <= 'z')
printf("You managed to call a hex function without a hex value!!!\n"); {
exit(EXIT_FAILURE); return (c - 87);
}
else if (c >= 'A' && c <= 'Z')
{
return (c - 55);
}
printf("You managed to call a hex function without a hex value!!!\n");
exit(EXIT_FAILURE);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char c; int c;
int sum; int sum;
bool toggle; bool toggle;
toggle = false; toggle = false;
@ -57,27 +61,23 @@ int main(int argc, char *argv[])
do do
{ {
c = getchar(); c = getchar();
switch(c) if (c == '#')
purge_line_comments();
else if ((c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))
{ {
case '0' ... '9': if(!toggle)
case 'a' ... 'f':
case 'A' ... 'F':
{ {
if(!toggle) sum = hex(c);
{ toggle = true;
sum = hex(c); }
toggle = true; else
} {
else sum = (sum * 16) + hex(c);
{ toggle = false;
sum = (sum * 16) + hex(c); fputc(sum, stdout);
toggle = false;
putc(sum, stdout);
}
break;
} }
case '#': purge_line_comments();
default: break;
} }
}while(c != EOF); }while(c != EOF);