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); else if (c >= 'a' && c <= 'z')
default: break; {
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"); printf("You managed to call a hex function without a hex value!!!\n");
exit(EXIT_FAILURE); 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,11 +61,11 @@ int main(int argc, char *argv[])
do do
{ {
c = getchar(); c = getchar();
switch(c) if (c == '#')
{ purge_line_comments();
case '0' ... '9': else if ((c >= '0' && c <= '9')
case 'a' ... 'f': || (c >= 'a' && c <= 'z')
case 'A' ... 'F': || (c >= 'A' && c <= 'Z'))
{ {
if(!toggle) if(!toggle)
{ {
@ -72,12 +76,8 @@ int main(int argc, char *argv[])
{ {
sum = (sum * 16) + hex(c); sum = (sum * 16) + hex(c);
toggle = false; toggle = false;
putc(sum, stdout); fputc(sum, stdout);
} }
break;
}
case '#': purge_line_comments();
default: break;
} }
}while(c != EOF); }while(c != EOF);