scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit c6e3c000059d37084d67ec3bde819cbe82299920
parent b043794874408ea4331d53ba978ad8cb5c0efe4e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  5 Oct 2013 22:35:59 +0200

Add octal and hexadecimal numbers

C support numbers in decimal, octal and hexadecimal. Octal numbers
begin with 0, and hexadecimal numbers begin with 0x. This patch
adds support for octal and hexadecimal, although it doesn't check
the base of the number when insert in the symbol table, so maybe
two numbers with different bases are allocated to the same symbol.

Diffstat:
Mlex.c | 40+++++++++++++++++++++++++++++++++++-----
1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/lex.c b/lex.c @@ -21,19 +21,49 @@ static FILE *yyin; static char number(void) { - register char *bp; - register char ch; + register char *bp, ch; + static char base; + + if ((ch = getc(yyin)) == '0') { + if (toupper(ch = getc(yyin)) == 'X') { + base = 16; + } else { + base = 8; + ungetc(ch, yyin); + } + } else { + base = 10; + ungetc(ch, yyin); + } for (bp = yytext; bp < yytext + IDENTSIZ; *bp++ = ch) { - if (!isdigit(ch = getc(yyin))) + ch = getc(yyin); + switch (base) { + case 8: + if (ch >= '7') + goto end; + /* passthru */ + case 10: + if (!isdigit(ch)) + goto end; + break; + case 16: + if (!isxdigit(ch)) + goto end; break; + } } - if (bp == yytext + IDENTSIZ) + +end: if (bp == yytext + IDENTSIZ) error("identifier too long %s", yytext); *bp = '\0'; ungetc(ch, yyin); + + /* + * TODO: insert always. don't depend of the base and check size + */ yyval.sym = lookup(yytext, NS_ANY); - yyval.sym->val = atoi(yytext); + yyval.sym->val = strtol(yytext, NULL, base); return CONSTANT; }