Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bcc640

Browse files
authored
[asr->python] Initial ASR to Python implementation (#2362)
1 parent 55c145b commit 0bcc640

21 files changed

+897
-1
lines changed

run_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def is_included(backend):
2626
llvm_dbg = is_included("llvm_dbg")
2727
cpp = is_included("cpp")
2828
c = is_included("c")
29+
python = is_included("python")
2930
is_cumulative = is_included("cumulative")
3031
wat = is_included("wat")
3132
run = is_included("run")
@@ -133,6 +134,11 @@ def is_included(backend):
133134
else:
134135
run_test(filename, "c", "lpython --no-color --show-c {infile}",
135136
filename, update_reference, extra_args)
137+
138+
if python:
139+
run_test(filename, "python", "lpython --no-color --show-python {infile}",
140+
filename, update_reference, extra_args)
141+
136142
if wat:
137143
run_test(filename, "wat", "lpython --no-color --show-wat {infile}",
138144
filename, update_reference, extra_args)

src/bin/lpython.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <libasr/codegen/asr_to_llvm.h>
1414
#include <libasr/codegen/asr_to_cpp.h>
1515
#include <libasr/codegen/asr_to_c.h>
16+
#include <libasr/codegen/asr_to_python.h>
1617
#include <libasr/codegen/asr_to_py.h>
1718
#include <libasr/codegen/asr_to_x86.h>
1819
#include <libasr/codegen/asr_to_wasm.h>
@@ -55,7 +56,7 @@ using LCompilers::CompilerOptions;
5556
using LCompilers::LPython::parse_python_file;
5657

5758
enum class Backend {
58-
llvm, cpp, c, x86, wasm, wasm_x86, wasm_x64
59+
llvm, cpp, c, x86, wasm, wasm_x86, wasm_x64, python
5960
};
6061

6162

@@ -385,6 +386,56 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
385386
return 0;
386387
}
387388

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+
388439
int emit_wat(const std::string &infile,
389440
const std::string &runtime_library_dir,
390441
CompilerOptions &compiler_options)
@@ -1490,6 +1541,7 @@ int main(int argc, char *argv[])
14901541
bool show_asr = false;
14911542
bool show_cpp = false;
14921543
bool show_c = false;
1544+
bool show_python = false;
14931545
bool show_document_symbols = false;
14941546
bool show_errors = false;
14951547
bool with_intrinsic_modules = false;
@@ -1556,6 +1608,7 @@ int main(int argc, char *argv[])
15561608
app.add_flag("--show-llvm", show_llvm, "Show LLVM IR for the given file and exit");
15571609
app.add_flag("--show-cpp", show_cpp, "Show C++ translation source for the given python file and exit");
15581610
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");
15591612
app.add_flag("--show-asm", show_asm, "Show assembly for the given file and exit");
15601613
app.add_flag("--show-wat", show_wat, "Show WAT (WebAssembly Text Format) and exit");
15611614
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[])
17891842
return emit_c(arg_file, runtime_library_dir, lpython_pass_manager,
17901843
compiler_options);
17911844
}
1845+
if (show_python) {
1846+
return emit_python(arg_file, runtime_library_dir, compiler_options);
1847+
}
17921848
if (show_wat) {
17931849
return emit_wat(arg_file, runtime_library_dir, compiler_options);
17941850
}

src/libasr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(SRC
1818
codegen/asr_to_cpp.cpp
1919
codegen/asr_to_c.cpp
2020
codegen/asr_to_julia.cpp
21+
codegen/asr_to_python.cpp
2122
codegen/asr_to_fortran.cpp
2223
codegen/asr_to_py.cpp
2324
codegen/x86_assembler.cpp

0 commit comments

Comments
 (0)