aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2020-03-07 22:17:23 -0800
committerMichael Forney <mforney@mforney.org>2020-03-16 02:20:34 -0700
commit83965726d5757ad4a68c8e2e756e6a31d59a614d (patch)
treed9148b444e2974dd9d066ceee88cf8baf6bd7c37
parent717d4f68f426d1edbf10b846448c63390840ffc5 (diff)
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.
-rw-r--r--cc.h9
-rw-r--r--main.c12
-rw-r--r--pp.c14
-rw-r--r--scan.c38
4 files changed, 47 insertions, 26 deletions
diff --git a/cc.h b/cc.h
index 62c9780..3e6eecf 100644
--- 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
index e1b3324..2cf5bcb 100644
--- 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
index 8db0848..a92ee26 100644
--- 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
index 5464cde..ef2a2ca 100644
--- 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);