@@ -59,6 +59,12 @@ std::string remove_extension(const std::string& filename) {
59
59
return filename.substr (0 , lastdot);
60
60
}
61
61
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
+
62
68
std::string remove_path (const std::string& filename) {
63
69
size_t lastslash = filename.find_last_of (" /" );
64
70
if (lastslash == std::string::npos) return filename;
@@ -288,7 +294,22 @@ int emit_c(const std::string &infile,
288
294
LFORTRAN_ASSERT (diagnostics.has_error ())
289
295
return 3 ;
290
296
}
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
+ }
292
313
return 0 ;
293
314
}
294
315
@@ -1283,6 +1304,7 @@ int main(int argc, char *argv[])
1283
1304
bool show_errors = false ;
1284
1305
bool with_intrinsic_modules = false ;
1285
1306
std::string arg_pass;
1307
+ std::string script_path;
1286
1308
bool arg_no_color = false ;
1287
1309
bool show_llvm = false ;
1288
1310
bool show_asm = false ;
@@ -1349,6 +1371,7 @@ int main(int argc, char *argv[])
1349
1371
app.add_flag (" --indent" , compiler_options.indent , " Indented print ASR/AST" );
1350
1372
app.add_flag (" --tree" , compiler_options.tree , " Tree structure print ASR/AST" );
1351
1373
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." );
1352
1375
app.add_flag (" --disable-main" , compiler_options.disable_main , " Do not generate any code for the `main` function" );
1353
1376
app.add_flag (" --symtab-only" , compiler_options.symtab_only , " Only create symbol tables in ASR (skip executable stmt)" );
1354
1377
app.add_flag (" --time-report" , time_report, " Show compilation time report" );
@@ -1471,6 +1494,17 @@ int main(int argc, char *argv[])
1471
1494
return 1 ;
1472
1495
}
1473
1496
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;
1474
1508
if (arg_backend == " llvm" ) {
1475
1509
backend = Backend::llvm;
1476
1510
} else if (arg_backend == " cpp" ) {
0 commit comments