2017-11-05 14:23:17 +00:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cc.h"
|
2018-01-21 03:56:50 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
/* 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);
|
2018-01-25 01:00:22 +00:00
|
|
|
void recursive_output(struct token_list* i, FILE* out);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
#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}
|
|
|
|
};
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2017-11-08 23:05:54 +00:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
/* Our essential organizer */
|
|
|
|
int main(int argc, char **argv)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-01-21 03:56:50 +00:00
|
|
|
global_token = NULL;
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
int c;
|
|
|
|
FILE* source_file;
|
2018-01-25 01:00:22 +00:00
|
|
|
FILE* destination_file = stdout;
|
2018-01-21 03:56:50 +00:00
|
|
|
int option_index = 0;
|
|
|
|
while ((c = getopt_long(argc, argv, "f:h:o:V", long_options, &option_index)) != -1)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-01-21 03:56:50 +00:00
|
|
|
switch(c)
|
2017-12-07 01:28:08 +00:00
|
|
|
{
|
2018-01-21 03:56:50 +00:00
|
|
|
case 0: break;
|
|
|
|
case 'h':
|
2018-01-12 02:46:20 +00:00
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("Usage: M2-Planet -f FILENAME1 {-f FILENAME2} -o OUTPUT\n", stderr);
|
2018-01-21 03:56:50 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2018-01-12 02:46:20 +00:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
case 'f':
|
2018-01-12 02:46:20 +00:00
|
|
|
{
|
2018-01-21 03:56:50 +00:00
|
|
|
#if __MESC__
|
|
|
|
source_file = open(optarg, O_RDONLY);
|
|
|
|
#else
|
|
|
|
source_file = fopen(optarg, "r");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(NULL == source_file)
|
2018-01-12 02:46:20 +00:00
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("The file: ", stderr);
|
|
|
|
file_print(optarg, stderr);
|
|
|
|
file_print(" can not be opened!\n", stderr);
|
2018-01-21 03:56:50 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2018-01-12 02:46:20 +00:00
|
|
|
}
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
global_token = read_all_tokens(source_file, global_token);
|
|
|
|
break;
|
2017-12-07 01:28:08 +00:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
case 'o':
|
2018-01-12 02:46:20 +00:00
|
|
|
{
|
2018-01-21 03:56:50 +00:00
|
|
|
#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)
|
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("The file: ", stderr);
|
|
|
|
file_print(optarg, stderr);
|
|
|
|
file_print(" can not be opened!\n", stderr);
|
2018-01-21 03:56:50 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2018-01-12 02:46:20 +00:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
case 'V':
|
2018-01-12 02:46:20 +00:00
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("M2-Planet 0.1\n", stdout);
|
2018-01-21 03:56:50 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2018-01-12 02:46:20 +00:00
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
default:
|
2017-12-19 20:44:27 +00:00
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("Unknown option\n", stderr);
|
2017-12-19 20:44:27 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-12-15 23:17:29 +00:00
|
|
|
}
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|
2018-01-11 21:31:32 +00:00
|
|
|
|
2018-01-21 03:56:50 +00:00
|
|
|
if(NULL == global_token)
|
2017-11-05 14:23:17 +00:00
|
|
|
{
|
2018-01-25 01:00:22 +00:00
|
|
|
file_print("Either no input files were given or they were empty\n", stderr);
|
2017-11-05 14:23:17 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-01-21 03:56:50 +00:00
|
|
|
global_token = reverse_list(global_token);
|
2017-11-05 14:23:17 +00:00
|
|
|
|
2018-01-11 21:31:32 +00:00
|
|
|
initialize_types();
|
2017-11-08 00:51:23 +00:00
|
|
|
struct token_list* output_list = program(NULL);
|
2018-01-21 03:56:50 +00:00
|
|
|
|
|
|
|
/* Output the program we have compiled */
|
2018-01-25 01:00:22 +00:00
|
|
|
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);
|
2018-01-21 03:56:50 +00:00
|
|
|
return EXIT_SUCCESS;
|
2017-11-05 14:23:17 +00:00
|
|
|
}
|