bool ofc_parse_file_print( ofc_colstr_t* cs, const ofc_parse_file_t* file) { if (!file) return false; return (ofc_parse_stmt_list_print(cs, 0, file->stmt) && ofc_colstr_writef(cs, "\n")); }
bool ofc_parse_stmt_structure_print( ofc_colstr_t* cs, unsigned indent, const ofc_parse_stmt_t* stmt) { if (!stmt) return false; const char* kwstr; bool slash = true; switch (stmt->type) { case OFC_PARSE_STMT_TYPE: kwstr = "TYPE"; slash = false; break; case OFC_PARSE_STMT_STRUCTURE: kwstr = "STRUCTURE"; break; case OFC_PARSE_STMT_UNION: kwstr = "UNION"; break; case OFC_PARSE_STMT_MAP: kwstr = "MAP"; break; default: return false; } if (!ofc_colstr_atomic_writef(cs, "%s", kwstr)) return false; if (!ofc_sparse_ref_empty(stmt->structure.name)) { if (!ofc_colstr_atomic_writef(cs, " ") || (slash && !ofc_colstr_atomic_writef(cs, "/")) || !ofc_sparse_ref_print(cs, stmt->structure.name) || (slash && !ofc_colstr_atomic_writef(cs, "/"))) return false; } if (!ofc_parse_stmt_list_print( cs, (indent + 1), stmt->structure.block)) return false; if (!ofc_colstr_newline(cs, indent, NULL)) return false; return ofc_colstr_atomic_writef(cs, "END %s", kwstr); }
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; }