From 63206b605c36140f2bd476e054b8318c8b08f472 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 26 Jun 2024 12:45:41 +0100 Subject: support multiple input filenames --- com.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'com.c') diff --git a/com.c b/com.c index 8102497..d95cdb5 100644 --- a/com.c +++ b/com.c @@ -458,8 +458,14 @@ int main(int argc, char **argv) { S->do_disasm = false; S->do_trace = false; + char *infile_names[16]; + int infile_name_n = 0; for (int i = 1; i < argc; i++) { - if (argv[i][0] != '-') break; + if (argv[i][0] != '-') { + infile_names[infile_name_n++] = argv[i]; + CHECK(infile_name_n < 16, "input file count exceeded"); + continue; + } switch (argv[i][1]) { case 'D': CHECK(strlen(argv[i]) > 1, "unknown option a"); @@ -473,21 +479,37 @@ int main(int argc, char **argv) { } } - Compiler com = (Compiler){ 0 }; com.S = S; com.ch = &ch; + if (infile_name_n == 0) infile_names[infile_name_n++] = "-"; + + for (int i = 0; i < infile_name_n; i++) { + char *fname = infile_names[i]; + FILE *infile = NULL; + if (0 == strcmp(fname, "-")) + infile = stdin; + else + infile = fopen(fname, "r"); + + if (infile == NULL) { + perror("fopen"); + exit(1); + } + + AstNode an; + memset(&an, 0, sizeof an); + pcc_context_t *parser = pcc_create(infile); + int rv; + do { + astnode_free(&an); + rv = pcc_parse(parser, &an); + compile_node(&com, an); + } while (rv != 0); + pcc_destroy(parser); + } - AstNode an; - memset(&an, 0, sizeof an); - pcc_context_t *parser = pcc_create(NULL); - int rv; - do { - rv = pcc_parse(parser, &an); - compile_node(&com, an); - } while (rv != 0); - pcc_destroy(parser); compile_opcode(&com, OP_PUTS); compile_opcode(&com, OP_HALT); -- cgit v1.2.3