cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit 83965726d5757ad4a68c8e2e756e6a31d59a614d
parent 717d4f68f426d1edbf10b846448c63390840ffc5
Author: Michael Forney <mforney@mforney.org>
Date:   Sat,  7 Mar 2020 22:17:23 -0800

Allow multiple inputs to main compiler process

This way, we can implement -include in the driver by just passing
an additional input to the compiler.

Diffstat:
Mcc.h | 9+++++++--
Mmain.c | 12+++++++++---
Mpp.c | 14++++++--------
Mscan.c | 38+++++++++++++++++++++++++-------------
4 files changed, 47 insertions(+), 26 deletions(-)

diff --git a/cc.h b/cc.h @@ -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); diff --git a/main.c b/main.c @@ -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); diff --git a/pp.c b/pp.c @@ -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) { diff --git a/scan.c b/scan.c @@ -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);