void ofc_parse_file_delete(ofc_parse_file_t* file) { if (!file) return; ofc_parse_stmt_list_delete(file->stmt); ofc_sparse_delete(file->source); free(file); }
ofc_sparse_t* ofc_prep(ofc_file_t* file) { ofc_sparse_t* unformat = ofc_prep_unformat(file); if (!unformat) return NULL; ofc_sparse_t* condense = ofc_prep_condense(unformat); ofc_sparse_delete(unformat); return condense; }
int main(int argc, const char* argv[]) { global_opts = OFC_GLOBAL_OPTS_DEFAULT; ofc_lang_opts_t lang_opts = OFC_LANG_OPTS_DEFAULT; ofc_file_t* file = NULL; if (!ofc_cliarg_parse(argc, argv, &file, &lang_opts, &global_opts)) return EXIT_FAILURE; ofc_sparse_t* condense = ofc_prep(file); if (!condense) { if (ofc_file_no_errors()) ofc_file_error(file, NULL, "Failed to preprocess source file"); return EXIT_FAILURE; } ofc_file_delete(file); ofc_parse_stmt_list_t* program = ofc_parse_file(condense); if (!program) { if (ofc_file_no_errors()) ofc_file_error(file, NULL, "Failed to parse program"); ofc_sparse_delete(condense); return EXIT_FAILURE; } if (global_opts.parse_print) { ofc_colstr_t* cs = ofc_colstr_create(72, 0); if (!ofc_parse_stmt_list_print(cs, 0, program)) { ofc_file_error(file, NULL, "Failed to print parse tree"); ofc_parse_stmt_list_delete(program); ofc_sparse_delete(condense); return EXIT_FAILURE; } ofc_colstr_fdprint(cs, STDOUT_FILENO); ofc_colstr_delete(cs); } ofc_sema_scope_t* sema = NULL; if (!global_opts.parse_only) { sema = ofc_sema_scope_global( &lang_opts, program); if (!sema) { if (ofc_file_no_errors()) ofc_file_error(file, NULL, "Program failed semantic analysis"); ofc_parse_stmt_list_delete(program); ofc_sparse_delete(condense); return EXIT_FAILURE; } } if (global_opts.sema_print) { ofc_colstr_t* cs = ofc_colstr_create(72, 0); if (!ofc_sema_scope_print(cs, 0, sema)) { ofc_file_error(file, NULL, "Failed to print semantic tree"); ofc_colstr_delete(cs); ofc_sema_scope_delete(sema); ofc_parse_stmt_list_delete(program); ofc_sparse_delete(condense); return EXIT_FAILURE; } ofc_colstr_fdprint(cs, STDOUT_FILENO); ofc_colstr_delete(cs); } ofc_sema_scope_delete(sema); ofc_parse_stmt_list_delete(program); ofc_sparse_delete(condense); return EXIT_SUCCESS; }
unsigned ofc_parse_stmt_include( const ofc_sparse_t* src, const char* ptr, ofc_parse_debug_t* debug, ofc_parse_stmt_t* stmt, ofc_parse_stmt_list_t* list) { unsigned dpos = ofc_parse_debug_position(debug); unsigned i = ofc_parse_keyword( src, ptr, debug, OFC_PARSE_KEYWORD_INCLUDE); if (i == 0) return 0; unsigned l = 0; ofc_string_t* spath = ofc_parse_character( src, &ptr[i], debug, &l); if (!spath) { ofc_parse_debug_rewind(debug, dpos); return 0; } i += l; if (!ofc_is_end_statement(&ptr[i], NULL)) { ofc_parse_debug_rewind(debug, dpos); return 0; } /* Don't rewind debug after this point because we know we have a valid include statement. */ char path[spath->size + 1]; memcpy(path, spath->base, spath->size); path[spath->size] = '\0'; const char* include_path = ofc_sparse_get_include(src); char* rpath = ofc_sparse_include_path(src, path); stmt->include.file = ofc_file_create_include( rpath, ofc_sparse_lang_opts(src), include_path); if (!stmt->include.file) { ofc_sparse_error(src, ofc_str_ref(ptr, i), "Can't open include file '%s'", rpath); free(rpath); return 0; } free(rpath); stmt->include.src = ofc_prep(stmt->include.file); if (!ofc_parse_file_include( stmt->include.src, list, debug)) { ofc_sparse_delete(stmt->include.src); ofc_file_delete(stmt->include.file); return 0; } stmt->type = OFC_PARSE_STMT_INCLUDE; return i; }