diff options
-rw-r--r-- | cc.h | 9 | ||||
-rw-r--r-- | main.c | 12 | ||||
-rw-r--r-- | pp.c | 14 | ||||
-rw-r--r-- | scan.c | 38 |
4 files changed, 47 insertions, 26 deletions
@@ -1,3 +1,5 @@ +#include <stdio.h> + struct func; enum tokenkind { @@ -374,10 +376,13 @@ _Noreturn void error(const struct location *, const char *, ...); /* scan */ -int scanfrom(const char *file); +void scanfrom(const char *, FILE *); +void scanopen(void); void scan(struct token *); -void ppinit(const char *); +/* preprocessor */ + +void ppinit(void); void next(void); _Bool peek(int); @@ -37,12 +37,18 @@ main(int argc, char *argv[]) targinit(target); - if (argc > 1) - usage(); if (output && !freopen(output, "w", stdout)) fatal("open %s:", output); - ppinit(argv[0]); + if (argc) { + while (argc--) + scanfrom(argv[argc], NULL); + scanopen(); + } else { + scanfrom("<stdin>", stdin); + } + + ppinit(); if (pponly) { while (tok.kind != TEOF) { tokprint(&tok); @@ -9,6 +9,12 @@ static struct token pending; +void +ppinit(void) +{ + next(); +} + static void keyword(struct token *tok) { @@ -92,14 +98,6 @@ keyword(struct token *tok) } } -void -ppinit(const char *file) -{ - if (scanfrom(file) < 0) - fatal("open %s:", file); - next(); -} - static void nextinto(struct token *t) { @@ -409,20 +409,11 @@ again: } } -int -scanfrom(const char *name) +void +scanfrom(const char *name, FILE *file) { struct scanner *s; - FILE *file; - if (name) { - file = fopen(name, "r"); - if (!file) - return -1; - } else { - file = stdin; - name = "<stdin>"; - } s = xmalloc(sizeof(*s)); s->file = file; s->buf.str = NULL; @@ -433,9 +424,28 @@ scanfrom(const char *name) s->loc.line = 1; s->loc.col = 0; s->next = scanner; + if (file) + nextchar(s); scanner = s; - nextchar(s); - return 0; +} + +void +scanopen(void) +{ + if (!scanner->file) { + scanner->file = fopen(scanner->loc.file, "r"); + if (!scanner->file) + fatal("open %s:", scanner->loc.file); + nextchar(scanner); + } +} + +static void +scanclose(void) +{ + fclose(scanner->file); + free(scanner->buf.str); + free(scanner); } void @@ -446,7 +456,9 @@ scan(struct token *t) t->kind = scankind(scanner); if (t->kind != TEOF || !scanner->next) break; + scanclose(); scanner = scanner->next; + scanopen(); } if (scanner->usebuf) { t->lit = bufget(&scanner->buf); |