2024-05-08 20:10:14 +00:00
|
|
|
#include <ast/ast.h>
|
2024-04-12 15:28:20 +00:00
|
|
|
#include <stdlib.h>
|
2024-04-12 15:24:12 +00:00
|
|
|
#include <sys/log.h>
|
2024-05-07 11:04:22 +00:00
|
|
|
#include <yacc/parser.tab.h>
|
2024-05-13 14:13:49 +00:00
|
|
|
#include <sys/col.h>
|
|
|
|
#include <lex/util.h>
|
2024-05-30 19:06:03 +00:00
|
|
|
#include <io/files.h>
|
|
|
|
#include <assert.h>
|
2024-02-04 15:23:32 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
extern void yyrestart(FILE *);
|
2024-04-12 15:24:12 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
[[maybe_unused]]
|
2024-05-14 12:03:45 +00:00
|
|
|
AST_NODE_PTR root;
|
2024-05-30 19:06:03 +00:00
|
|
|
[[maybe_unused]]
|
|
|
|
ModuleFile *current_file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compile the specified file into AST
|
|
|
|
* @param ast
|
|
|
|
* @param file
|
|
|
|
* @return EXIT_SUCCESS in case the parsing was success full anything lese if not
|
|
|
|
*/
|
|
|
|
[[nodiscard("AST may be in invalid state")]]
|
|
|
|
[[gnu::nonnull(1), gnu::nonnull(1)]]
|
|
|
|
static size_t compile_file_to_ast(AST_NODE_PTR ast, ModuleFile *file) {
|
|
|
|
assert(file->path != NULL);
|
|
|
|
assert(ast != NULL);
|
|
|
|
|
|
|
|
file->handle = fopen(file->path, "r");
|
|
|
|
|
|
|
|
if (file->handle == NULL) {
|
|
|
|
INFO("unable to open file: %s", file->path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("parsing file: %s", file->path);
|
|
|
|
// setup global state
|
|
|
|
root = ast;
|
|
|
|
current_file = file;
|
|
|
|
yyin = file->handle;
|
|
|
|
yyrestart(yyin);
|
|
|
|
lex_reset();
|
|
|
|
|
|
|
|
yyparse();
|
|
|
|
|
|
|
|
// clean up global state
|
|
|
|
// current_file = NULL;
|
|
|
|
root = NULL;
|
|
|
|
yyin = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-26 14:21:54 +00:00
|
|
|
|
2024-04-12 15:29:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Log a debug message to inform about beginning exit procedures
|
2024-05-07 11:04:22 +00:00
|
|
|
*
|
2024-04-12 15:29:39 +00:00
|
|
|
*/
|
2024-05-08 20:10:14 +00:00
|
|
|
void notify_exit(void) { DEBUG("Exiting gemstone..."); }
|
2024-04-12 15:28:20 +00:00
|
|
|
|
2024-04-23 13:27:11 +00:00
|
|
|
/**
|
|
|
|
* @brief Closes File after compiling.
|
2024-05-07 11:04:22 +00:00
|
|
|
*
|
2024-04-23 13:27:11 +00:00
|
|
|
*/
|
|
|
|
|
2024-05-08 20:10:14 +00:00
|
|
|
void close_file(void) {
|
2024-05-30 19:06:03 +00:00
|
|
|
if (NULL != yyin) {
|
|
|
|
fclose(yyin);
|
|
|
|
}
|
2024-04-23 13:27:11 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 15:29:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Run compiler setup here
|
2024-05-07 11:04:22 +00:00
|
|
|
*
|
2024-04-12 15:29:39 +00:00
|
|
|
*/
|
2024-05-08 20:10:14 +00:00
|
|
|
void setup(void) {
|
2024-05-30 19:06:03 +00:00
|
|
|
// setup preample
|
2024-04-12 15:29:39 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
log_init();
|
|
|
|
DEBUG("starting gemstone...");
|
2024-04-12 15:24:12 +00:00
|
|
|
|
2024-05-07 11:04:22 +00:00
|
|
|
#if LOG_LEVEL <= LOG_LEVEL_DEBUG
|
2024-05-30 19:06:03 +00:00
|
|
|
atexit(¬ify_exit);
|
2024-05-07 11:04:22 +00:00
|
|
|
#endif
|
2024-04-12 15:28:20 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
// actual setup
|
|
|
|
AST_init();
|
2024-05-07 11:04:22 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
col_init();
|
2024-05-13 14:13:49 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
lex_init();
|
2024-05-13 14:13:49 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
DEBUG("finished starting up gemstone...");
|
2024-04-22 11:41:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 12:25:49 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
setup();
|
|
|
|
atexit(close_file);
|
|
|
|
|
|
|
|
ModuleFileStack files;
|
|
|
|
files.files = NULL;
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
printf("Compiling file: %s\n\n", argv[i]);
|
2024-05-07 11:04:22 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
TokenLocation location = {
|
|
|
|
.line_start = 0,
|
|
|
|
.line_end = 0,
|
|
|
|
.col_start = 0,
|
|
|
|
.col_end = 0
|
|
|
|
};
|
|
|
|
AST_NODE_PTR ast = AST_new_node(location, AST_Module, NULL);
|
|
|
|
ModuleFile *file = push_file(&files, argv[i]);
|
2024-05-07 11:04:22 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
if (compile_file_to_ast(ast, file) == EXIT_SUCCESS) {
|
|
|
|
// TODO: parse AST to semantic values
|
|
|
|
// TODO: backend codegen
|
|
|
|
}
|
2024-04-23 12:25:49 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
AST_delete_node(ast);
|
2024-04-26 14:21:54 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
print_file_statistics(file);
|
|
|
|
}
|
2024-04-30 12:01:00 +00:00
|
|
|
|
2024-05-30 19:46:44 +00:00
|
|
|
if (files.files == NULL) {
|
|
|
|
printf("No input files, nothing to do.\n\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
print_unit_statistics(&files);
|
2024-04-22 10:53:48 +00:00
|
|
|
|
2024-05-30 19:06:03 +00:00
|
|
|
delete_files(&files);
|
|
|
|
return 0;
|
2024-05-08 20:10:14 +00:00
|
|
|
}
|