@@ -59,6 +59,12 @@ std::string remove_extension(const std::string& filename) {
5959 return filename.substr (0 , lastdot);
6060}
6161
62+ std::string extract_extension (const std::string& filename) {
63+ size_t lastdot = filename.find_last_of (" ." );
64+ if (lastdot == std::string::npos) return " " ;
65+ return filename.substr (lastdot);
66+ }
67+
6268std::string remove_path (const std::string& filename) {
6369 size_t lastslash = filename.find_last_of (" /" );
6470 if (lastslash == std::string::npos) return filename;
@@ -288,7 +294,22 @@ int emit_c(const std::string &infile,
288294 LFORTRAN_ASSERT (diagnostics.has_error ())
289295 return 3 ;
290296 }
291- std::cout << res.result ;
297+ if ( !compiler_options.c_postprocessor_script .empty () ) {
298+ std::string ccode_file = remove_extension (infile) + " _generated.c" ;
299+ std::ofstream ccode_out (ccode_file);
300+ ccode_out << res.result ;
301+ std::string command = " python " + compiler_options.c_postprocessor_script + " " + ccode_file;
302+ system (command.c_str ());
303+ ccode_out.close ();
304+ std::ifstream ccode_in (ccode_file);
305+ std::string modified_result = " " ;
306+ while ( std::getline (ccode_in, modified_result) ) {
307+ std::cout << modified_result << " \n " ;
308+ }
309+ ccode_in.close ();
310+ } else {
311+ std::cout << res.result ;
312+ }
292313 return 0 ;
293314}
294315
@@ -1283,6 +1304,7 @@ int main(int argc, char *argv[])
12831304 bool show_errors = false ;
12841305 bool with_intrinsic_modules = false ;
12851306 std::string arg_pass;
1307+ std::string script_path;
12861308 bool arg_no_color = false ;
12871309 bool show_llvm = false ;
12881310 bool show_asm = false ;
@@ -1349,6 +1371,7 @@ int main(int argc, char *argv[])
13491371 app.add_flag (" --indent" , compiler_options.indent , " Indented print ASR/AST" );
13501372 app.add_flag (" --tree" , compiler_options.tree , " Tree structure print ASR/AST" );
13511373 app.add_option (" --pass" , arg_pass, " Apply the ASR pass and show ASR (implies --show-asr)" );
1374+ app.add_option (" --custom-c-postprocessor" , script_path, " The script for processing the C code generated by --show-c command." );
13521375 app.add_flag (" --disable-main" , compiler_options.disable_main , " Do not generate any code for the `main` function" );
13531376 app.add_flag (" --symtab-only" , compiler_options.symtab_only , " Only create symbol tables in ASR (skip executable stmt)" );
13541377 app.add_flag (" --time-report" , time_report, " Show compilation time report" );
@@ -1471,6 +1494,17 @@ int main(int argc, char *argv[])
14711494 return 1 ;
14721495 }
14731496
1497+ if ( !script_path.empty () && extract_extension (script_path) != " .py" ) {
1498+ std::cerr << " Only a Python script is supported as a postprocessor for generated C code." << std::endl;
1499+ return 1 ;
1500+ }
1501+
1502+ if ( !script_path.empty () && !LFortran::path_exists (script_path) ) {
1503+ std::cerr << script_path << " is inaccessible or doesn't exist on your system." << std::endl;
1504+ return 1 ;
1505+ }
1506+
1507+ compiler_options.c_postprocessor_script = script_path;
14741508 if (arg_backend == " llvm" ) {
14751509 backend = Backend::llvm;
14761510 } else if (arg_backend == " cpp" ) {
0 commit comments