commit 3a4b6eedd35f30fc914d7e21270f659e11d1a318
parent 5bfb14d894427d16c3118cfc16b6f8b72138785a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 21 May 2015 21:35:28 +0200
Simplify fill() and convert it to moreinput()
Diffstat:
M | cc1/cpp.c | | | 4 | ++-- |
M | cc1/lex.c | | | 94 | +++++++++++++++++++++++++++++++++++-------------------------------------------- |
2 files changed, 43 insertions(+), 55 deletions(-)
diff --git a/cc1/cpp.c b/cc1/cpp.c
@@ -281,7 +281,7 @@ preprocessor(char *p)
for (++p; isspace(*p); ++p)
/* nothing */;
if (*p == '\0')
- return NULL;
+ return p;
for (q = p; isalpha(*q); ++q)
/* nothing */;
if ((n = q - p) == 0)
@@ -296,7 +296,7 @@ preprocessor(char *p)
/* nothing */;
if (*q != '\0')
error("trailing characters after preprocessor directive");
- return NULL;
+ return q;
}
incorrect:
error("incorrect preprocessor directive");
diff --git a/cc1/lex.c b/cc1/lex.c
@@ -59,7 +59,8 @@ addinput(char *fname)
ip = xmalloc(sizeof(Input));
ip->fname = fname;
ip->next = input;
- ip->line = NULL;
+ ip->begin = ip->p = ip->line = xmalloc(INPUTSIZ);
+ *ip->begin = '\0';
ip->nline = nline;
ip->fp = fp;
input = ip;
@@ -116,7 +117,7 @@ readchar(void)
{
int c;
FILE *fp;
-
+
repeat:
while (feof(input->fp) && !eof)
delinput();
@@ -129,9 +130,11 @@ repeat:
goto repeat;
ungetc(c, fp);
c = '\\';
- }
- if (c == '\n' && ++input->nline == 0)
+ } else if (c == EOF) {
+ goto repeat;
+ } else if (c == '\n' && ++input->nline == 0) {
die("input file '%s' too long", getfname());
+ }
return c;
}
@@ -185,25 +188,17 @@ readline(void)
}
static bool
-fill(void)
+moreinput(void)
{
char *p;
- int c;
repeat:
- if (eof)
- return 0;
- if (input->begin && *input->begin != '\0')
- return 1;
- if (!input->line)
- input->line = xmalloc(INPUTSIZ);
+ *(p = input->line) = '\0';
readline();
- if (*input->line == '\0')
- goto repeat;
- if ((p = preprocessor(input->line)) == NULL)
+ if ((p = preprocessor(p)) == '\0')
goto repeat;
input->p = input->begin = p;
- return 1;
+ return *p != '\0';
}
static void
@@ -369,21 +364,10 @@ repeat:
if (c == '\0')
error("missing terminating '\"' character");
- ++input->p;
-
- for (;;) {
- input->begin = input->p;
- if (isspace((c = *input->p))) {
- ++input->p;
- } else if (c == '\0' && fill()) {
- continue;
- } else if (c == '"') {
- goto repeat;
- } else {
- break;
- }
- }
+ input->begin = input->p + 1;
+ if (ahead() == '"')
+ goto repeat;
*bp = '\0';
sym = newsym(NS_IDEN);
sym->u.s = xstrdup(buf);
@@ -506,32 +490,44 @@ setnamespace(int ns)
lex_ns = ns;
}
+static void
+skipspaces(void)
+{
+ char *p;
+
+repeat:
+ for (p = input->begin; isspace(*p); ++p)
+ /* nothing */;
+ if (*p == '\0') {
+ if (!moreinput())
+ return;
+ goto repeat;
+ }
+ input->begin = input->p = p;
+}
+
unsigned
next(void)
{
char c;
-repeat:
- if (!fill()) {
+ skipspaces();
+ if (eof) {
strcpy(yytext, "<EOF>");
return yytoken = EOFTOK;
}
- while (isspace(*input->begin))
- ++input->begin;
- if ((c = *(input->p = input->begin)) == '\0')
- goto repeat;
- if (isalpha(c) || c == '_') {
+ c = *input->begin;
+ if (isalpha(c) || c == '_')
yytoken = iden();
- } else if (isdigit(c)) {
+ else if (isdigit(c))
yytoken = number();
- } else if (c == '"') {
+ else if (c == '"')
yytoken = string();
- } else if (c == '\'') {
+ else if (c == '\'')
yytoken = character();
- } else {
+ else
yytoken = operator();
- }
lex_ns = NS_IDEN;
return yytoken;
}
@@ -552,16 +548,8 @@ expect(unsigned tok)
char
ahead(void)
{
- int c;
-
-repeat:
- if (!fill())
- return EOFTOK;
- while (isspace(c = *input->begin))
- ++input->begin;
- if (c == '\0')
- goto repeat;
- return c;
+ skipspaces();
+ return *input->begin;
}
void
@@ -596,7 +584,7 @@ discard(void)
goto jump;
break;
}
- if (!fill())
+ if (!moreinput())
exit(-1);
}
jump: