From 71d79d99d3368a807c87db54cc3ea13146549cb4 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Sat, 28 Jul 2012 21:21:41 +0200 Subject: [PATCH] parser_init,compile,finish,cleanup - used in main --- main.c | 28 ++++++++++-- parser.c | 137 +++++++++++++++++++++++++++++++++---------------------- 2 files changed, 106 insertions(+), 59 deletions(-) diff --git a/main.c b/main.c index 7ca9fea..9cbf026 100644 --- a/main.c +++ b/main.c @@ -308,6 +308,11 @@ static bool options_parse(int argc, char **argv) { return true; } +bool parser_init(); +bool parser_compile(const char *filename); +bool parser_finish(const char *output); +void parser_cleanup(); + int main(int argc, char **argv) { size_t itr; app_name = argv[0]; @@ -326,6 +331,13 @@ int main(int argc, char **argv) { printf("optimization level = %i\n", (int)opts_O); printf("standard = %i\n", opts_standard); + if (!parser_init()) { + printf("failed to initialize parser\n"); + goto cleanup; + } + + util_debug("COM", "starting ...\n"); + if (items_elements) { printf("Mode: manual\n"); printf("There are %lu items to compile:\n", (unsigned long)items_elements); @@ -336,17 +348,23 @@ int main(int argc, char **argv) { (items_data[itr].type == TYPE_ASM ? "asm" : (items_data[itr].type == TYPE_SRC ? "progs.src" : ("unknown")))))); - } - } else { - printf("Mode: progs.src\n"); - } - util_debug("COM", "starting ...\n"); + if (!parser_compile(items_data[itr].filename)) + goto cleanup; + } + + parser_finish(opts_output); + } else { + printf("Mode: progs.src - not implemented\n"); + } /* stuff */ +cleanup: util_debug("COM", "cleaning ...\n"); + parser_cleanup(); + util_meminfo(); return 0; } diff --git a/parser.c b/parser.c index bb24e63..bb85edf 100644 --- a/parser.c +++ b/parser.c @@ -1051,12 +1051,10 @@ static bool parser_do(parser_t *parser) return true; } -bool parser_compile(const char *filename, const char *datfile) -{ - size_t i; - parser_t *parser; - ir_builder *ir; +static parser_t *parser; +bool parser_init() +{ parser = (parser_t*)mem_a(sizeof(parser_t)); if (!parser) return false; @@ -1065,8 +1063,12 @@ bool parser_compile(const char *filename, const char *datfile) MEM_VECTOR_INIT(parser, globals); MEM_VECTOR_INIT(parser, locals); - parser->lex = lex_open(filename); + return true; +} +bool parser_compile(const char *filename) +{ + parser->lex = lex_open(filename); if (!parser->lex) { printf("failed to open file \"%s\"\n", filename); return false; @@ -1093,54 +1095,12 @@ bool parser_compile(const char *filename, const char *datfile) lex_close(parser->lex); - if (!parser->errors) - { - ir = ir_builder_new("gmqcc_out"); - if (!ir) { - printf("failed to allocate builder\n"); - goto cleanup; - } + return !parser->errors; +} - for (i = 0; i < parser->imm_float_count; ++i) { - if (!ast_global_codegen(parser->imm_float[i], ir)) { - printf("failed to generate global %s\n", parser->imm_float[i]->name); - } - } - for (i = 0; i < parser->imm_string_count; ++i) { - if (!ast_global_codegen(parser->imm_string[i], ir)) { - printf("failed to generate global %s\n", parser->imm_string[i]->name); - } - } - for (i = 0; i < parser->imm_vector_count; ++i) { - if (!ast_global_codegen(parser->imm_vector[i], ir)) { - printf("failed to generate global %s\n", parser->imm_vector[i]->name); - } - } - for (i = 0; i < parser->globals_count; ++i) { - if (!ast_global_codegen(parser->globals[i], ir)) { - printf("failed to generate global %s\n", parser->globals[i]->name); - } - } - for (i = 0; i < parser->functions_count; ++i) { - if (!ast_function_codegen(parser->functions[i], ir)) { - printf("failed to generate function %s\n", parser->functions[i]->name); - } - if (!ir_function_finalize(parser->functions[i]->ir_func)) { - printf("failed to finalize function %s\n", parser->functions[i]->name); - } - } - - ir_builder_dump(ir, printf); - - if (!ir_builder_generate(ir, datfile)) - printf("*** failed to generate output file\n"); - - ir_builder_delete(ir); - } else { - printf("*** there were compile errors\n"); - } - -cleanup: +void parser_cleanup() +{ + size_t i; for (i = 0; i < parser->functions_count; ++i) { ast_delete(parser->functions[i]); } @@ -1159,5 +1119,74 @@ cleanup: MEM_VECTOR_CLEAR(parser, globals); mem_d(parser); - return true; +} + +bool parser_finish(const char *output) +{ + size_t i; + ir_builder *ir; + + if (!parser->errors) + { + ir = ir_builder_new("gmqcc_out"); + if (!ir) { + printf("failed to allocate builder\n"); + return false; + } + + for (i = 0; i < parser->imm_float_count; ++i) { + if (!ast_global_codegen(parser->imm_float[i], ir)) { + printf("failed to generate global %s\n", parser->imm_float[i]->name); + ir_builder_delete(ir); + return false; + } + } + for (i = 0; i < parser->imm_string_count; ++i) { + if (!ast_global_codegen(parser->imm_string[i], ir)) { + printf("failed to generate global %s\n", parser->imm_string[i]->name); + ir_builder_delete(ir); + return false; + } + } + for (i = 0; i < parser->imm_vector_count; ++i) { + if (!ast_global_codegen(parser->imm_vector[i], ir)) { + printf("failed to generate global %s\n", parser->imm_vector[i]->name); + ir_builder_delete(ir); + return false; + } + } + for (i = 0; i < parser->globals_count; ++i) { + if (!ast_global_codegen(parser->globals[i], ir)) { + printf("failed to generate global %s\n", parser->globals[i]->name); + ir_builder_delete(ir); + return false; + } + } + for (i = 0; i < parser->functions_count; ++i) { + if (!ast_function_codegen(parser->functions[i], ir)) { + printf("failed to generate function %s\n", parser->functions[i]->name); + ir_builder_delete(ir); + return false; + } + if (!ir_function_finalize(parser->functions[i]->ir_func)) { + printf("failed to finalize function %s\n", parser->functions[i]->name); + ir_builder_delete(ir); + return false; + } + } + + ir_builder_dump(ir, printf); + + if (!ir_builder_generate(ir, output)) { + printf("*** failed to generate output file\n"); + ir_builder_delete(ir); + return false; + } + + ir_builder_delete(ir); + return true; + } + + printf("*** there were compile errors\n"); + return false; }