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];
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);
(items_data[itr].type == TYPE_ASM ? "asm" :
(items_data[itr].type == TYPE_SRC ? "progs.src" :
("unknown"))))));
+
+ if (!parser_compile(items_data[itr].filename))
+ goto cleanup;
}
+
+ parser_finish(opts_output);
} else {
- printf("Mode: progs.src\n");
+ printf("Mode: progs.src - not implemented\n");
}
- util_debug("COM", "starting ...\n");
-
/* stuff */
+cleanup:
util_debug("COM", "cleaning ...\n");
+ parser_cleanup();
+
util_meminfo();
return 0;
}
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;
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;
lex_close(parser->lex);
+ return !parser->errors;
+}
+
+void parser_cleanup()
+{
+ size_t i;
+ for (i = 0; i < parser->functions_count; ++i) {
+ ast_delete(parser->functions[i]);
+ }
+ for (i = 0; i < parser->imm_vector_count; ++i) {
+ ast_delete(parser->imm_vector[i]);
+ }
+ for (i = 0; i < parser->imm_string_count; ++i) {
+ ast_delete(parser->imm_string[i]);
+ }
+ for (i = 0; i < parser->imm_float_count; ++i) {
+ ast_delete(parser->imm_float[i]);
+ }
+ for (i = 0; i < parser->globals_count; ++i) {
+ ast_delete(parser->globals[i]);
+ }
+ MEM_VECTOR_CLEAR(parser, globals);
+
+ mem_d(parser);
+}
+
+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");
- goto cleanup;
+ 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, datfile))
+ if (!ir_builder_generate(ir, output)) {
printf("*** failed to generate output file\n");
+ ir_builder_delete(ir);
+ return false;
+ }
ir_builder_delete(ir);
- } else {
- printf("*** there were compile errors\n");
- }
-
-cleanup:
- for (i = 0; i < parser->functions_count; ++i) {
- ast_delete(parser->functions[i]);
- }
- for (i = 0; i < parser->imm_vector_count; ++i) {
- ast_delete(parser->imm_vector[i]);
- }
- for (i = 0; i < parser->imm_string_count; ++i) {
- ast_delete(parser->imm_string[i]);
- }
- for (i = 0; i < parser->imm_float_count; ++i) {
- ast_delete(parser->imm_float[i]);
- }
- for (i = 0; i < parser->globals_count; ++i) {
- ast_delete(parser->globals[i]);
+ return true;
}
- MEM_VECTOR_CLEAR(parser, globals);
- mem_d(parser);
- return true;
+ printf("*** there were compile errors\n");
+ return false;
}