M2-Planet/cc.c

121 lines
3.5 KiB
C
Raw Normal View History

2017-11-05 14:23:17 +00:00
/* Copyright (C) 2016 Jeremiah Orians
* This file is part of M2-Planet.
2017-11-05 14:23:17 +00:00
*
* M2-Planet is free software: you can redistribute it and/or modify
2017-11-05 14:23:17 +00:00
* 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.
*
* M2-Planet is distributed in the hope that it will be useful,
2017-11-05 14:23:17 +00:00
* 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 M2-Planet. If not, see <http://www.gnu.org/licenses/>.
2017-11-05 14:23:17 +00:00
*/
2018-02-24 02:18:23 +00:00
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"cc.h"
/* The core functions */
void initialize_types();
2018-05-26 21:33:00 +01:00
struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename);
struct token_list* reverse_list(struct token_list* head);
struct token_list* program();
void recursive_output(struct token_list* i, FILE* out);
2018-02-24 02:18:23 +00:00
int match(char* a, char* b);
void file_print(char* s, FILE* f);
char* parse_string(char* string);
2018-02-24 02:18:23 +00:00
int main(int argc, char** argv)
{
hold_string = calloc(MAX_STRING, sizeof(char));
int DEBUG = FALSE;
2018-02-24 02:18:23 +00:00
FILE* in = stdin;
FILE* destination_file = stdout;
2018-02-24 02:18:23 +00:00
int i = 1;
while(i <= argc)
2017-11-05 14:23:17 +00:00
{
2018-02-24 02:18:23 +00:00
if(NULL == argv[i])
{
2018-02-24 02:18:23 +00:00
i = i + 1;
}
else if(match(argv[i], "-f") || match(argv[i], "--file"))
2018-02-24 02:18:23 +00:00
{
2018-05-26 21:33:00 +01:00
char* name = argv[i + 1];
in = fopen(name, "r");
2018-02-24 02:18:23 +00:00
if(NULL == in)
{
2018-02-24 02:18:23 +00:00
file_print("Unable to open for reading file: ", stderr);
2018-05-26 21:33:00 +01:00
file_print(name, stderr);
2018-08-31 21:32:53 +01:00
file_print("\n Aborting to avoid problems\n", stderr);
2018-02-24 02:18:23 +00:00
exit(EXIT_FAILURE);
}
2018-05-26 21:33:00 +01:00
global_token = read_all_tokens(in, global_token, name);
2018-02-24 02:18:23 +00:00
i = i + 2;
}
else if(match(argv[i], "-o") || match(argv[i], "--output"))
2018-02-24 02:18:23 +00:00
{
destination_file = fopen(argv[i + 1], "w");
if(NULL == destination_file)
{
2018-02-24 02:18:23 +00:00
file_print("Unable to open for writing file: ", stderr);
file_print(argv[i + 1], stderr);
2018-08-31 21:32:53 +01:00
file_print("\n Aborting to avoid problems\n", stderr);
exit(EXIT_FAILURE);
}
2018-02-24 02:18:23 +00:00
i = i + 2;
}
else if(match(argv[i], "-g") || match(argv[i], "--debug"))
{
DEBUG = TRUE;
i = i + 1;
}
else if(match(argv[i], "-h") || match(argv[i], "--help"))
2018-02-24 02:18:23 +00:00
{
2018-08-31 21:32:53 +01:00
file_print(" -f input file\n -o output file\n --help for this message\n --version for file version\n", stdout);
2018-02-24 02:18:23 +00:00
exit(EXIT_SUCCESS);
}
else if(match(argv[i], "-V") || match(argv[i], "--version"))
2018-02-24 02:18:23 +00:00
{
2018-08-31 21:32:53 +01:00
file_print("M2-Planet v1.0.0\n", stderr);
2018-02-24 02:18:23 +00:00
exit(EXIT_SUCCESS);
}
else
{
2018-08-31 21:32:53 +01:00
file_print("UNKNOWN ARGUMENT\n", stdout);
2018-02-24 02:18:23 +00:00
exit(EXIT_FAILURE);
}
2017-11-05 14:23:17 +00:00
}
/* Deal with special case of wanting to read from standard input */
if(stdin == in)
{
2018-05-26 21:33:00 +01:00
global_token = read_all_tokens(in, global_token, "STDIN");
}
if(NULL == global_token)
2017-11-05 14:23:17 +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);
}
global_token = reverse_list(global_token);
2017-11-05 14:23:17 +00:00
initialize_types();
2018-07-27 00:51:44 +01:00
reset_hold_string();
struct token_list* output_list = program();
/* Output the program we have compiled */
file_print("\n# Core program\n", destination_file);
recursive_output(output_list, destination_file);
if(DEBUG) file_print("\n:ELF_data\n", destination_file);
file_print("\n# Program global variables\n", destination_file);
recursive_output(globals_list, destination_file);
file_print("\n# Program strings\n", destination_file);
recursive_output(strings_list, destination_file);
if(!DEBUG) file_print("\n:ELF_end\n", destination_file);
return EXIT_SUCCESS;
2017-11-05 14:23:17 +00:00
}