/* main.c -- Entry point for LRL bootstrap transpiler Copyright © 2020 Samuel Lidén Borell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "bootstrap.h" #include #include static FILE *output_file; static const char *progname; static const char *current_file; void error_tok(const struct Token *tok, const char *s) { error_linecol(tok->line, tok->column, s); } void error_linecol(int line, int column, const char *s) { fprintf(stderr, "%s:%d:%d: %s\n", current_file, line, column, s); } NORETURN void out_of_memory(void) { fprintf(stderr, "%s: Out of memory.\n", progname); abort(); } NORETURN void internal_error(int error_id) { fprintf(stderr, "Internal Compiler Error %d\n", error_id); abort(); } static void show_help(void) { printf("usage: %s [OPTION]... FILE... -o OUTPUT\n" "\n" "LRL Bootstrap Transpiler. Outputs C or LLVM code (TODO decide which).\n" "\n", progname); } static void invalid_option(const char *arg) { fprintf(stderr, "%s: invalid option: %s\n", progname, arg); } static void missing_argument(const char *arg) { fprintf(stderr, "%s: option '%s' requires an argument\n", progname, arg); } static int parse_files(int argc, char **argv, int decls_only) { int optparse = 1; int has_files = 0; int ok = 1; int argleft; char **argp; /* Loop through all filename arguments */ optparse = 1; for (argleft = argc-1, argp = argv+1; argleft; argleft--, argp++) { FILE *file; const char *arg = *argp; if (arg[0] == '-') { if (arg[1] == '\0') { /*ok &= parse_file(stdin, decls_only);*/ fprintf(stderr, "%s: parsing from standard in is currently not supported\n", progname); ok = 0; has_files = 1; continue; } else if (optparse) { if (arg[1] == '-') { if (arg[2] == '\0') optparse = 0; } else if (arg[1] == 'o') { argleft--; argp++; } continue; } } file = fopen(arg, "r"); if (!file) { perror(arg); return 0; } current_file = arg; ok &= parse_file(file, decls_only); has_files = 1; fclose(file); } if (!has_files) { fprintf(stderr, "%s: no input files\n", progname); return 0; } return ok; } int main(int argc, char **argv) { int optparse = 1; int argleft; char **argp; /*const char *output_filename = "a.out";*/ const char *output_filename = "a_out.c"; progname = argv[0]; /* Parse option arguments (and skip filename arguments) */ for (argleft = argc-1, argp = argv+1; argleft; argleft--, argp++) { const char *arg = *argp; if (arg[0] == '-') { if (arg[1] == '\0' || !optparse) continue; /* stdin/filename argument */ if (arg[1] == '-') { /* Long option */ if (arg[2] == '\0') { optparse = 0; } else if (!strcmp(arg+2, "help")) { show_help(); return EXIT_SUCCESS; } else { invalid_option(arg); return 2; } } else if (arg[2] != '\0') { /* Long option with only one dash (or a combination of multiple short options, but that is not supported) */ invalid_option(arg); return 2; } else { /* Short option */ switch (arg[1]) { case 'o': /* Output file */ if (!--argleft) { missing_argument(arg); return 2; } output_filename = *(++argp); break; default: invalid_option(arg); return 2; } } } } /* First, parse all declarations */ init_builtins(); fprintf(stderr, "-- stage1\n"); if (!parse_files(argc, argv, 1)) { return EXIT_FAILURE; } /* Second, output the declarations, taking dependencies into account */ fprintf(stderr, "-- stage2\n"); if (strcmp(output_filename, "-")) { output_file = fopen(output_filename, "w"); if (!output_file) { perror(output_filename); return EXIT_FAILURE; } } else { output_file = stdout; } output_set_file(output_file); output_dependencies(); /* Finally, parse all source files again and output C source / LLVM IR (or an executable using LLVM?) */ fprintf(stderr, "-- stage3\n"); if (!parse_files(argc, argv, 0)) { fclose(output_file); return EXIT_FAILURE; } if (output_file != stdout) { fclose(output_file); } return EXIT_SUCCESS; }