scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 6decf612c382dab28f910aca45b6e34a842fc4c5
parent 64add66232ba4cf148aab299b3fa999444fe6e8e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 23 Apr 2014 15:09:10 +0200

Add character constants

Diffstat:
lex.c | 26+++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lex.c b/lex.c @@ -110,6 +110,7 @@ escape(char *s) { char c; +repeat: switch (getc(yyin)) { case '\\': c = '\''; break; case 'a': c = '\a'; break; @@ -125,7 +126,9 @@ escape(char *s) case 'u': /* TODO: */ case '\n': ++linenum; - return s; + if ((c = getc(yyin)) == '\\') + goto repeat; + break; default: warn(1, "unknown escape sequence"); return s; @@ -136,6 +139,25 @@ escape(char *s) } static uint8_t +character(void) +{ + static char c; + register Symbol *sym; + + getc(yyin); /* discard the initial ' */ + c = getc(yyin); + if (c == '\\') + escape(&c); + if (getc(yyin) != '\'') + error("invalid character constant"); + sym = install("", NS_IDEN); + sym->u.i = c; + sym->type = inttype; + yynlval.sym = sym; + return CONSTANT; +} + +static uint8_t string(void) { static char buf[STRINGSIZ+1]; @@ -386,6 +408,8 @@ next(void) yyntoken = number(); else if (c == '"') yyntoken = string(); + else if (c == '\'') + yyntoken = character(); else yyntoken = operator();