Skip to content

Commit

Permalink
Merge branch 'main' into delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Thirumalai-Shaktivel committed May 19, 2022
2 parents a7eafa3 + e389389 commit 82cb5d7
Show file tree
Hide file tree
Showing 33 changed files with 1,783 additions and 687 deletions.
2 changes: 2 additions & 0 deletions grammar/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ module LPython
| ConstantBool(bool value, string? kind)
| ConstantFloat(float value, string? kind)
| ConstantComplex(float re, float im, string? kind)
| ConstantEllipsis(string? kind)
| ConstantNone(string? kind)

-- the following expression can appear in assignment context
| Attribute(expr value, identifier attr, expr_context ctx)
Expand Down
5 changes: 5 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def main():
mod_to_asr = test.get("mod_to_asr", False)
llvm = test.get("llvm", False)
cpp = test.get("cpp", False)
c = test.get("c", False)
obj = test.get("obj", False)
x86 = test.get("x86", False)
bin_ = test.get("bin", False)
Expand Down Expand Up @@ -83,6 +84,10 @@ def main():
run_test("cpp", "lpython --no-color --show-cpp {infile}",
filename, update_reference, extra_args)

if c:
run_test("c", "lpython --no-color --show-c {infile}",
filename, update_reference, extra_args)

if tokens:
run_test("tokens", "lpython --no-color --show-tokens {infile} -o {outfile}",
filename, update_reference, extra_args)
Expand Down
46 changes: 46 additions & 0 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <lpython/semantics/python_ast_to_asr.h>
#include <libasr/codegen/asr_to_llvm.h>
#include <libasr/codegen/asr_to_cpp.h>
#include <libasr/codegen/asr_to_c.h>
#include <libasr/codegen/asr_to_py.h>
#include <libasr/codegen/asr_to_x86.h>
#include <lpython/python_evaluator.h>
Expand Down Expand Up @@ -223,6 +224,46 @@ int emit_cpp(const std::string &infile,
return 0;
}

int emit_c(const std::string &infile,
const std::string &runtime_library_dir,
CompilerOptions &compiler_options)
{
Allocator al(4*1024);
LFortran::diag::Diagnostics diagnostics;
LFortran::LocationManager lm;
lm.in_filename = infile;
std::string input = LFortran::read_file(infile);
lm.init_simple(input);
LFortran::Result<LFortran::LPython::AST::ast_t*> r = parse_python_file(
al, runtime_library_dir, infile, diagnostics, compiler_options.new_parser);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r.ok) {
return 1;
}
LFortran::LPython::AST::ast_t* ast = r.result;

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
return 2;
}
LFortran::ASR::TranslationUnit_t* asr = r1.result;

diagnostics.diagnostics.clear();
auto res = LFortran::asr_to_c(al, *asr, diagnostics);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!res.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
return 3;
}
std::cout << res.result;
return 0;
}

#ifdef HAVE_LFORTRAN_LLVM

int emit_llvm(const std::string &infile,
Expand Down Expand Up @@ -542,6 +583,7 @@ int main(int argc, char *argv[])
bool show_ast = false;
bool show_asr = false;
bool show_cpp = false;
bool show_c = false;
bool with_intrinsic_modules = false;
std::string arg_pass;
bool arg_no_color = false;
Expand Down Expand Up @@ -595,6 +637,7 @@ int main(int argc, char *argv[])
app.add_flag("--show-asr", show_asr, "Show ASR for the given python file and exit");
app.add_flag("--show-llvm", show_llvm, "Show LLVM IR for the given file and exit");
app.add_flag("--show-cpp", show_cpp, "Show C++ translation source for the given python file and exit");
app.add_flag("--show-c", show_c, "Show C translation source for the given python file and exit");
app.add_flag("--show-asm", show_asm, "Show assembly for the given file and exit");
app.add_flag("--show-stacktrace", compiler_options.show_stacktrace, "Show internal stacktrace on compiler errors");
app.add_flag("--with-intrinsic-mods", with_intrinsic_modules, "Show intrinsic modules in ASR");
Expand Down Expand Up @@ -787,6 +830,9 @@ int main(int argc, char *argv[])
if (show_cpp) {
return emit_cpp(arg_file, runtime_library_dir, compiler_options);
}
if (show_c) {
return emit_c(arg_file, runtime_library_dir, compiler_options);
}
if (show_llvm) {
#ifdef HAVE_LFORTRAN_LLVM
return emit_llvm(arg_file, runtime_library_dir, compiler_options);
Expand Down
1 change: 1 addition & 0 deletions src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ configure_file(config.h.in config.h)

set(SRC
codegen/asr_to_cpp.cpp
codegen/asr_to_c.cpp
codegen/asr_to_py.cpp
codegen/x86_assembler.cpp
codegen/asr_to_x86.cpp
Expand Down
Loading

0 comments on commit 82cb5d7

Please sign in to comment.