Skip to content

Commit

Permalink
Implement initial mod_to_asr()
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Sep 24, 2020
1 parent 675c0b2 commit 189fac3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/bin/lfortran.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <lfortran/parser/parser.h>
#include <lfortran/pickle.h>
#include <lfortran/semantics/ast_to_asr.h>
#include <lfortran/mod_to_asr.h>
#include <lfortran/codegen/asr_to_llvm.h>
#include <lfortran/codegen/asr_to_cpp.h>
#include <lfortran/codegen/asr_to_x86.h>
Expand Down Expand Up @@ -901,6 +902,10 @@ int main(int argc, char *argv[])
bool arg_fmt_inplace = false;
bool arg_fmt_no_color = false;

std::string arg_mod_file;
bool arg_mod_show_asr = false;
bool arg_mod_no_color = false;

CLI::App app{"LFortran: modern interactive LLVM-based Fortran compiler"};
// Standard options compatible with gfortran, gcc or clang
// We follow the established conventions
Expand Down Expand Up @@ -939,10 +944,17 @@ int main(int argc, char *argv[])
fmt.add_flag("--indent-unit", arg_fmt_indent_unit, "Indent contents of sub / fn / prog / mod");
fmt.add_flag("--no-color", arg_fmt_no_color, "Turn off color when writing to stdout");

// fmt
// kernel
CLI::App &kernel = *app.add_subcommand("kernel", "Run in Jupyter kernel mode.");
kernel.add_option("-f", arg_kernel_f, "The kernel connection file")->required();

// mod
CLI::App &mod = *app.add_subcommand("mod", "Fortran mod file utilities.");
mod.add_option("file", arg_mod_file, "Mod file (*.mod)")->required();
mod.add_flag("--show-asr", arg_mod_show_asr, "Show ASR for the module");
mod.add_flag("--no-color", arg_mod_no_color, "Turn off colored ASR");


app.get_formatter()->column_width(25);
app.require_subcommand(0, 1);
CLI11_PARSE(app, argc, argv);
Expand All @@ -967,6 +979,17 @@ int main(int argc, char *argv[])
#endif
}

if (mod) {
if (arg_mod_show_asr) {
Allocator al(1024*1024);
LFortran::ASR::TranslationUnit_t *asr;
asr = LFortran::mod_to_asr(al, arg_mod_file);
std::cout << LFortran::pickle(*asr, !arg_mod_no_color) << std::endl;
return 0;
}
return 0;
}

if (arg_backend == "llvm") {
backend = Backend::llvm;
} else if (arg_backend == "cpp") {
Expand Down
2 changes: 2 additions & 0 deletions src/lfortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ set(SRC
ast_to_cpp_hip.cpp
ast_to_openmp.cpp

mod_to_asr.cpp

stacktrace.cpp
)
if (WITH_XEUS)
Expand Down
25 changes: 25 additions & 0 deletions src/lfortran/mod_to_asr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <map>
#include <memory>

#include <lfortran/asr.h>
#include <lfortran/asr_utils.h>
#include <lfortran/mod_to_asr.h>


namespace LFortran {

using ASR::down_cast;
using ASR::down_cast2;

ASR::TranslationUnit_t *mod_to_asr(Allocator &al, std::string filename)
{
SymbolTable *symtab = al.make_new<SymbolTable>(nullptr);
ASR::asr_t *asr;
Location loc;
asr = ASR::make_TranslationUnit_t(al, loc,
symtab, nullptr, 0);
return down_cast2<ASR::TranslationUnit_t>(asr);
}

} // namespace LFortran
12 changes: 12 additions & 0 deletions src/lfortran/mod_to_asr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef LFORTRAN_MOD_TO_ASR_H
#define LFORTRAN_MOD_TO_ASR_H

#include <lfortran/asr.h>

namespace LFortran {

ASR::TranslationUnit_t *mod_to_asr(Allocator &al, std::string filename);

} // namespace LFortran

#endif // LFORTRAN_MOD_TO_ASR_H

0 comments on commit 189fac3

Please sign in to comment.