Skip to content

Commit

Permalink
Merge pull request #2417 from Shaikh-Ubaid/remove_redundant_code
Browse files Browse the repository at this point in the history
Remove redundant code
  • Loading branch information
Shaikh-Ubaid authored Nov 12, 2023
2 parents 03d54d2 + 2591cb8 commit a8dbbc2
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 406 deletions.
13 changes: 7 additions & 6 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#define CLI11_HAS_FILESYSTEM 0
#include <bin/CLI11.hpp>

#include <libasr/stacktrace.h>
#include <lpython/pickle.h>
#include <libasr/pickle.h>
#include <libasr/stacktrace.h>
#include <lpython/semantics/python_ast_to_asr.h>
#include <libasr/codegen/asr_to_llvm.h>
#include <libasr/codegen/asr_to_cpp.h>
Expand Down Expand Up @@ -221,15 +222,15 @@ int emit_asr(const std::string &infile,
pass_manager.apply_passes(al, asr, compiler_options.po, diagnostics);

if (compiler_options.po.tree) {
std::cout << LCompilers::LPython::pickle_tree(*asr,
std::cout << LCompilers::pickle_tree(*asr,
compiler_options.use_colors, with_intrinsic_modules) << std::endl;
} else if (compiler_options.po.json) {
std::cout << LCompilers::LPython::pickle_json(*asr, lm, with_intrinsic_modules) << std::endl;
std::cout << LCompilers::pickle_json(*asr, lm, false, with_intrinsic_modules) << std::endl;
} else if (compiler_options.po.visualize) {
std::string astr_data_json = LCompilers::LPython::pickle_json(*asr, lm, with_intrinsic_modules);
std::string astr_data_json = LCompilers::pickle_json(*asr, lm, false, with_intrinsic_modules);
return visualize_json(astr_data_json, compiler_options.platform);
} else {
std::cout << LCompilers::LPython::pickle(*asr, compiler_options.use_colors,
std::cout << LCompilers::pickle(*asr, compiler_options.use_colors,
compiler_options.indent, with_intrinsic_modules) << std::endl;
}
return 0;
Expand Down Expand Up @@ -1361,7 +1362,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
out = diagnostics.render(lm, compiler_options);
if (asr.ok) {
out += LCompilers::LPython::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
out += LCompilers::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
false /* with_intrinsic_modules */);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libasr/pickle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class ASRPickleVisitor :
}
void visit_Module(const ASR::Module_t &x) {
if (!show_intrinsic_modules &&
startswith(x.m_name, "lfortran_intrinsic_")) {
(x.m_intrinsic || startswith(x.m_name, "lfortran_intrinsic_") || startswith(x.m_name, "numpy"))) {
s.append("(");
if (use_colors) {
s.append(color(style::bold));
s.append(color(fg::magenta));
}
s.append("IntrinsicModule");
s.append(x.m_intrinsic ? "IntrinsicModule" : "Module");
if (use_colors) {
s.append(color(fg::reset));
s.append(color(style::reset));
Expand Down
232 changes: 0 additions & 232 deletions src/lpython/pickle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,129 +28,6 @@ std::string pickle_python(AST::ast_t &ast, bool colors, bool indent) {
return v.get_str();
}

/********************** ASR Pickle *******************/
class ASRPickleVisitor :
public ASR::PickleBaseVisitor<ASRPickleVisitor>
{
public:
bool show_intrinsic_modules;

std::string get_str() {
return s;
}
void visit_symbol(const ASR::symbol_t &x) {
s.append(ASRUtils::symbol_parent_symtab(&x)->get_counter());
s.append(" ");
if (use_colors) {
s.append(color(fg::yellow));
}
s.append(ASRUtils::symbol_name(&x));
if (use_colors) {
s.append(color(fg::reset));
}
}
void visit_IntegerConstant(const ASR::IntegerConstant_t &x) {
s.append("(");
if (use_colors) {
s.append(color(style::bold));
s.append(color(fg::magenta));
}
s.append("IntegerConstant");
if (use_colors) {
s.append(color(fg::reset));
s.append(color(style::reset));
}
s.append(" ");
if (use_colors) {
s.append(color(fg::cyan));
}
s.append(std::to_string(x.m_n));
if (use_colors) {
s.append(color(fg::reset));
}
s.append(" ");
this->visit_ttype(*x.m_type);
s.append(")");
}
void visit_Module(const ASR::Module_t &x) {
// hide intrinsic modules and numpy module by default
if (!show_intrinsic_modules &&
(x.m_intrinsic || startswith(x.m_name, "numpy"))) {
s.append("(");
if (use_colors) {
s.append(color(style::bold));
s.append(color(fg::magenta));
}
s.append(x.m_intrinsic ? "IntrinsicModule" : "Module");
if (use_colors) {
s.append(color(fg::reset));
s.append(color(style::reset));
}
s.append(" ");
s.append(x.m_name);
s.append(")");
} else {
ASR::PickleBaseVisitor<ASRPickleVisitor>::visit_Module(x);
};
}

std::string convert_intrinsic_id(int x) {
std::string s;
if (use_colors) {
s.append(color(style::bold));
s.append(color(fg::green));
}
s.append(ASRUtils::get_intrinsic_name(x));
if (use_colors) {
s.append(color(fg::reset));
s.append(color(style::reset));
}
return s;
}

std::string convert_impure_intrinsic_id(int x) {
std::string s;
if (use_colors) {
s.append(color(style::bold));
s.append(color(fg::green));
}
s.append(ASRUtils::get_impure_intrinsic_name(x));
if (use_colors) {
s.append(color(fg::reset));
s.append(color(style::reset));
}
return s;
}

std::string convert_array_intrinsic_id(int x) {
std::string s;
if (use_colors) {
s.append(color(style::bold));
s.append(color(fg::green));
}
s.append(ASRUtils::get_array_intrinsic_name(x));
if (use_colors) {
s.append(color(fg::reset));
s.append(color(style::reset));
}
return s;
}
};

std::string pickle(ASR::asr_t &asr, bool colors, bool indent,
bool show_intrinsic_modules) {
ASRPickleVisitor v;
v.use_colors = colors;
v.indent = indent;
v.show_intrinsic_modules = show_intrinsic_modules;
v.visit_asr(asr);
return v.get_str();
}

std::string pickle(ASR::TranslationUnit_t &asr, bool colors, bool indent, bool show_intrinsic_modules) {
return pickle((ASR::asr_t &)asr, colors, indent, show_intrinsic_modules);
}

/********************** AST Pickle Tree *******************/
class ASTTreeVisitor : public AST::TreeBaseVisitor<ASTTreeVisitor>
{
Expand All @@ -167,31 +44,6 @@ std::string pickle_tree_python(AST::ast_t &ast, bool colors) {
return v.get_str();
}

/********************** ASR Pickle Tree *******************/
class ASRTreeVisitor :
public ASR::TreeBaseVisitor<ASRTreeVisitor>
{
public:
bool show_intrinsic_modules;

std::string get_str() {
return s;
}

};

std::string pickle_tree(ASR::asr_t &asr, bool colors, bool show_intrinsic_modules) {
ASRTreeVisitor v;
v.use_colors = colors;
v.show_intrinsic_modules = show_intrinsic_modules;
v.visit_asr(asr);
return v.get_str();
}

std::string pickle_tree(ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules) {
return pickle_tree((ASR::asr_t &)asr, colors, show_intrinsic_modules);
}

/********************** AST Pickle Json *******************/
class ASTJsonVisitor :
public LPython::AST::JsonBaseVisitor<ASTJsonVisitor>
Expand All @@ -210,88 +62,4 @@ std::string pickle_json(LPython::AST::ast_t &ast, LocationManager &lm) {
return v.get_str();
}

/********************** ASR Pickle Json *******************/
class ASRJsonVisitor :
public ASR::JsonBaseVisitor<ASRJsonVisitor>
{
public:
bool show_intrinsic_modules;

using ASR::JsonBaseVisitor<ASRJsonVisitor>::JsonBaseVisitor;

std::string get_str() {
return s;
}

void visit_symbol(const ASR::symbol_t &x) {
s.append("\"");
s.append(ASRUtils::symbol_name(&x));
s.append(" (SymbolTable");
s.append(ASRUtils::symbol_parent_symtab(&x)->get_counter());
s.append(")\"");
}

void visit_Module(const ASR::Module_t &x) {
// hide intrinsic modules and numpy module by default
if (!show_intrinsic_modules &&
(x.m_intrinsic || startswith(x.m_name, "numpy"))) {
s.append("{");
inc_indent(); s.append("\n" + indtd);
s.append("\"node\": \"Module\"");
s.append(",\n" + indtd);
s.append("\"fields\": {");
inc_indent(); s.append("\n" + indtd);
s.append("\"name\": ");
s.append("\"" + std::string(x.m_name) + "\"");
s.append(",\n" + indtd);
s.append("\"dependencies\": ");
s.append("[");
if (x.n_dependencies > 0) {
inc_indent(); s.append("\n" + indtd);
for (size_t i=0; i<x.n_dependencies; i++) {
s.append("\"" + std::string(x.m_dependencies[i]) + "\"");
if (i < x.n_dependencies-1) {
s.append(",\n" + indtd);
};
}
dec_indent(); s.append("\n" + indtd);
}
s.append("]");
s.append(",\n" + indtd);
s.append("\"loaded_from_mod\": ");
if (x.m_loaded_from_mod) {
s.append("true");
} else {
s.append("false");
}
s.append(",\n" + indtd);
s.append("\"intrinsic\": ");
if (x.m_intrinsic) {
s.append("true");
} else {
s.append("false");
}
dec_indent(); s.append("\n" + indtd);
s.append("}");
s.append(",\n" + indtd);
append_location(s, x.base.base.loc.first, x.base.base.loc.last);
dec_indent(); s.append("\n" + indtd);
s.append("}");
} else {
ASR::JsonBaseVisitor<ASRJsonVisitor>::visit_Module(x);
}
}
};

std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules) {
ASRJsonVisitor v(lm);
v.show_intrinsic_modules = show_intrinsic_modules;
v.visit_asr(asr);
return v.get_str();
}

std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules) {
return pickle_json((ASR::asr_t &)asr, lm, show_intrinsic_modules);
}

}
10 changes: 1 addition & 9 deletions src/lpython/pickle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ namespace LCompilers::LPython {

// Pickle an ASR node
std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false);
std::string pickle(ASR::asr_t &asr, bool colors=false, bool indent=false,
bool show_intrinsic_modules=false);
std::string pickle(ASR::TranslationUnit_t &asr, bool colors=false,
bool indent=false, bool show_intrinsic_modules=false);

// Print the tree structure
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
std::string pickle_tree(ASR::asr_t &asr, bool colors, bool show_intrinsic_modules);
std::string pickle_tree(ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules);

// Print the ASR in json format
std::string pickle_json(AST::ast_t &ast, LocationManager &lm);
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules);
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules);

}

#endif // LFORTRAN_PICKLE_H
Loading

0 comments on commit a8dbbc2

Please sign in to comment.