@@ -60,6 +60,12 @@ std::string remove_extension(const std::string& filename) {
60
60
return filename.substr (0 , lastdot);
61
61
}
62
62
63
+ std::string extract_extension (const std::string& filename) {
64
+ size_t lastdot = filename.find_last_of (" ." );
65
+ if (lastdot == std::string::npos) return " " ;
66
+ return filename.substr (lastdot);
67
+ }
68
+
63
69
std::string remove_path (const std::string& filename) {
64
70
size_t lastslash = filename.find_last_of (" /" );
65
71
if (lastslash == std::string::npos) return filename;
@@ -289,7 +295,22 @@ int emit_c(const std::string &infile,
289
295
LFORTRAN_ASSERT (diagnostics.has_error ())
290
296
return 3 ;
291
297
}
292
- std::cout << res.result ;
298
+ if ( !compiler_options.c_postprocessor_script .empty () ) {
299
+ std::string ccode_file = remove_extension (infile) + " _generated.c" ;
300
+ std::ofstream ccode_out (ccode_file);
301
+ ccode_out << res.result ;
302
+ std::string command = " python " + compiler_options.c_postprocessor_script + " " + ccode_file;
303
+ system (command.c_str ());
304
+ ccode_out.close ();
305
+ std::ifstream ccode_in (ccode_file);
306
+ std::string modified_result = " " ;
307
+ while ( std::getline (ccode_in, modified_result) ) {
308
+ std::cout << modified_result << " \n " ;
309
+ }
310
+ ccode_in.close ();
311
+ } else {
312
+ std::cout << res.result ;
313
+ }
293
314
return 0 ;
294
315
}
295
316
@@ -1297,6 +1318,7 @@ int main(int argc, char *argv[])
1297
1318
bool show_errors = false ;
1298
1319
bool with_intrinsic_modules = false ;
1299
1320
std::string arg_pass;
1321
+ std::string script_path;
1300
1322
bool arg_no_color = false ;
1301
1323
bool show_llvm = false ;
1302
1324
bool show_asm = false ;
@@ -1363,6 +1385,7 @@ int main(int argc, char *argv[])
1363
1385
app.add_flag (" --indent" , compiler_options.indent , " Indented print ASR/AST" );
1364
1386
app.add_flag (" --tree" , compiler_options.tree , " Tree structure print ASR/AST" );
1365
1387
app.add_option (" --pass" , arg_pass, " Apply the ASR pass and show ASR (implies --show-asr)" );
1388
+ app.add_option (" --custom-c-postprocessor" , script_path, " The script for processing the C code generated by --show-c command." );
1366
1389
app.add_flag (" --disable-main" , compiler_options.disable_main , " Do not generate any code for the `main` function" );
1367
1390
app.add_flag (" --symtab-only" , compiler_options.symtab_only , " Only create symbol tables in ASR (skip executable stmt)" );
1368
1391
app.add_flag (" --time-report" , time_report, " Show compilation time report" );
@@ -1485,6 +1508,17 @@ int main(int argc, char *argv[])
1485
1508
return 1 ;
1486
1509
}
1487
1510
1511
+ if ( !script_path.empty () && extract_extension (script_path) != " .py" ) {
1512
+ std::cerr << " Only a Python script is supported as a postprocessor for generated C code." << std::endl;
1513
+ return 1 ;
1514
+ }
1515
+
1516
+ if ( !script_path.empty () && !LFortran::path_exists (script_path) ) {
1517
+ std::cerr << script_path << " is inaccessible or doesn't exist on your system." << std::endl;
1518
+ return 1 ;
1519
+ }
1520
+
1521
+ compiler_options.c_postprocessor_script = script_path;
1488
1522
if (arg_backend == " llvm" ) {
1489
1523
backend = Backend::llvm;
1490
1524
} else if (arg_backend == " cpp" ) {
0 commit comments