|
13 | 13 | #include <libasr/codegen/asr_to_llvm.h>
|
14 | 14 | #include <libasr/codegen/asr_to_cpp.h>
|
15 | 15 | #include <libasr/codegen/asr_to_c.h>
|
| 16 | +#include <libasr/codegen/asr_to_python.h> |
16 | 17 | #include <libasr/codegen/asr_to_py.h>
|
17 | 18 | #include <libasr/codegen/asr_to_x86.h>
|
18 | 19 | #include <libasr/codegen/asr_to_wasm.h>
|
@@ -55,7 +56,7 @@ using LCompilers::CompilerOptions;
|
55 | 56 | using LCompilers::LPython::parse_python_file;
|
56 | 57 |
|
57 | 58 | enum class Backend {
|
58 |
| - llvm, cpp, c, x86, wasm, wasm_x86, wasm_x64 |
| 59 | + llvm, cpp, c, x86, wasm, wasm_x86, wasm_x64, python |
59 | 60 | };
|
60 | 61 |
|
61 | 62 |
|
@@ -385,6 +386,56 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
|
385 | 386 | return 0;
|
386 | 387 | }
|
387 | 388 |
|
| 389 | +int emit_python(const std::string &infile, |
| 390 | + const std::string &runtime_library_dir, |
| 391 | + CompilerOptions &compiler_options) |
| 392 | +{ |
| 393 | + Allocator al(4*1024); |
| 394 | + LCompilers::diag::Diagnostics diagnostics; |
| 395 | + LCompilers::LocationManager lm; |
| 396 | + { |
| 397 | + LCompilers::LocationManager::FileLocations fl; |
| 398 | + fl.in_filename = infile; |
| 399 | + lm.files.push_back(fl); |
| 400 | + std::string input = LCompilers::read_file(infile); |
| 401 | + lm.init_simple(input); |
| 402 | + lm.file_ends.push_back(input.size()); |
| 403 | + } |
| 404 | + LCompilers::Result<LCompilers::LPython::AST::ast_t*> r = parse_python_file( |
| 405 | + al, runtime_library_dir, infile, diagnostics, 0, compiler_options.new_parser); |
| 406 | + std::cerr << diagnostics.render(lm, compiler_options); |
| 407 | + if (!r.ok) { |
| 408 | + return 1; |
| 409 | + } |
| 410 | + LCompilers::LPython::AST::ast_t* ast = r.result; |
| 411 | + |
| 412 | + diagnostics.diagnostics.clear(); |
| 413 | + |
| 414 | + // AST -> ASR |
| 415 | + LCompilers::Result<LCompilers::ASR::TranslationUnit_t*> |
| 416 | + r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile); |
| 417 | + std::cerr << diagnostics.render(lm, compiler_options); |
| 418 | + if (!r1.ok) { |
| 419 | + LCOMPILERS_ASSERT(diagnostics.has_error()) |
| 420 | + return 2; |
| 421 | + } |
| 422 | + LCompilers::ASR::TranslationUnit_t* asr = r1.result; |
| 423 | + |
| 424 | + diagnostics.diagnostics.clear(); |
| 425 | + |
| 426 | + // ASR -> LPython |
| 427 | + bool color = false; |
| 428 | + int indent = 0; |
| 429 | + LCompilers::Result<std::string> res = LCompilers::asr_to_python(al, *asr, diagnostics, compiler_options, color, indent); |
| 430 | + std::cerr << diagnostics.render(lm, compiler_options); |
| 431 | + if (!res.ok) { |
| 432 | + LCOMPILERS_ASSERT(diagnostics.has_error()) |
| 433 | + return 3; |
| 434 | + } |
| 435 | + std::cout << res.result; |
| 436 | + return 0; |
| 437 | +} |
| 438 | + |
388 | 439 | int emit_wat(const std::string &infile,
|
389 | 440 | const std::string &runtime_library_dir,
|
390 | 441 | CompilerOptions &compiler_options)
|
@@ -1490,6 +1541,7 @@ int main(int argc, char *argv[])
|
1490 | 1541 | bool show_asr = false;
|
1491 | 1542 | bool show_cpp = false;
|
1492 | 1543 | bool show_c = false;
|
| 1544 | + bool show_python = false; |
1493 | 1545 | bool show_document_symbols = false;
|
1494 | 1546 | bool show_errors = false;
|
1495 | 1547 | bool with_intrinsic_modules = false;
|
@@ -1556,6 +1608,7 @@ int main(int argc, char *argv[])
|
1556 | 1608 | app.add_flag("--show-llvm", show_llvm, "Show LLVM IR for the given file and exit");
|
1557 | 1609 | app.add_flag("--show-cpp", show_cpp, "Show C++ translation source for the given python file and exit");
|
1558 | 1610 | app.add_flag("--show-c", show_c, "Show C translation source for the given python file and exit");
|
| 1611 | + app.add_flag("--show-python", show_python, "Show Python translation source for the given python file and exit"); |
1559 | 1612 | app.add_flag("--show-asm", show_asm, "Show assembly for the given file and exit");
|
1560 | 1613 | app.add_flag("--show-wat", show_wat, "Show WAT (WebAssembly Text Format) and exit");
|
1561 | 1614 | app.add_flag("--show-stacktrace", compiler_options.show_stacktrace, "Show internal stacktrace on compiler errors");
|
@@ -1789,6 +1842,9 @@ int main(int argc, char *argv[])
|
1789 | 1842 | return emit_c(arg_file, runtime_library_dir, lpython_pass_manager,
|
1790 | 1843 | compiler_options);
|
1791 | 1844 | }
|
| 1845 | + if (show_python) { |
| 1846 | + return emit_python(arg_file, runtime_library_dir, compiler_options); |
| 1847 | + } |
1792 | 1848 | if (show_wat) {
|
1793 | 1849 | return emit_wat(arg_file, runtime_library_dir, compiler_options);
|
1794 | 1850 | }
|
|
0 commit comments