/* Copyright (C) 2016 Jeremiah Orians * This file is part of stage0. * * stage0 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * stage0 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with stage0. If not, see . */ #include "cc.h" #include /* The core functions */ void initialize_types(); struct token_list* read_all_tokens(FILE* a, struct token_list* current); struct token_list* reverse_list(struct token_list* head); struct token_list* program(struct token_list* out); void recursive_output(struct token_list* i, FILE* out); #if !__MESC__ static #endif struct option long_options[] = { {"file", required_argument, 0, 'f'}, {"output", required_argument, 0, 'o'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {0, 0, 0, 0} }; /* Our essential organizer */ int main(int argc, char **argv) { global_token = NULL; int c; FILE* source_file; FILE* destination_file = stdout; int option_index = 0; while ((c = getopt_long(argc, argv, "f:h:o:V", long_options, &option_index)) != -1) { switch(c) { case 0: break; case 'h': { file_print("Usage: M2-Planet -f FILENAME1 {-f FILENAME2} -o OUTPUT\n", stderr); exit(EXIT_SUCCESS); } case 'f': { #if __MESC__ source_file = open(optarg, O_RDONLY); #else source_file = fopen(optarg, "r"); #endif if(NULL == source_file) { file_print("The file: ", stderr); file_print(optarg, stderr); file_print(" can not be opened!\n", stderr); exit(EXIT_FAILURE); } global_token = read_all_tokens(source_file, global_token); break; } case 'o': { #if __MESC__ destination_file = open(optarg, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR); #else destination_file = fopen(optarg, "w"); #endif if(NULL == destination_file) { file_print("The file: ", stderr); file_print(optarg, stderr); file_print(" can not be opened!\n", stderr); exit(EXIT_FAILURE); } break; } case 'V': { file_print("M2-Planet 0.1\n", stdout); exit(EXIT_SUCCESS); } default: { file_print("Unknown option\n", stderr); exit(EXIT_FAILURE); } } } if(NULL == global_token) { file_print("Either no input files were given or they were empty\n", stderr); exit(EXIT_FAILURE); } global_token = reverse_list(global_token); initialize_types(); struct token_list* output_list = program(NULL); /* Output the program we have compiled */ file_print("\n# Core program\n\n", destination_file); recursive_output(output_list, destination_file); file_print("\n# Program global variables\n\n", destination_file); recursive_output(globals_list, destination_file); file_print("\n# Program strings\n\n", destination_file); recursive_output(strings_list, destination_file); return EXIT_SUCCESS; }