Skip to content

Commit

Permalink
Merge pull request lcompilers#1451 from Shaikh-Ubaid/ast_asr_json
Browse files Browse the repository at this point in the history
AST/R: Support Json Printing
  • Loading branch information
certik committed Jan 21, 2023
2 parents 45626d7 + 93a07df commit f804b84
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 37 deletions.
14 changes: 14 additions & 0 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ int emit_ast(const std::string &infile,
if (compiler_options.tree) {
std::cout << LCompilers::LPython::pickle_tree_python(*ast,
compiler_options.use_colors) << std::endl;
} else if (compiler_options.json) {
LCompilers::LocationManager lm;
{
LCompilers::LocationManager::FileLocations fl;
fl.in_filename = infile;
lm.files.push_back(fl);
std::string input = LCompilers::read_file(infile);
lm.init_simple(input);
lm.file_ends.push_back(input.size());
}
std::cout << LCompilers::LPython::pickle_json(*ast, lm) << std::endl;
} else {
std::cout << LCompilers::LPython::pickle_python(*ast,
compiler_options.use_colors, compiler_options.indent) << std::endl;
Expand Down Expand Up @@ -196,6 +207,8 @@ int emit_asr(const std::string &infile,
if (compiler_options.tree) {
std::cout << LCompilers::LPython::pickle_tree(*asr,
compiler_options.use_colors, with_intrinsic_modules) << std::endl;
} else if (compiler_options.json) {
std::cout << LCompilers::LPython::pickle_json(*asr, lm) << std::endl;
} else {
std::cout << LCompilers::LPython::pickle(*asr, compiler_options.use_colors,
compiler_options.indent, with_intrinsic_modules) << std::endl;
Expand Down Expand Up @@ -1396,6 +1409,7 @@ int main(int argc, char *argv[])
app.add_flag("--no-color", arg_no_color, "Turn off colored AST/ASR");
app.add_flag("--indent", compiler_options.indent, "Indented print ASR/AST");
app.add_flag("--tree", compiler_options.tree, "Tree structure print ASR/AST");
app.add_flag("--json", compiler_options.json, "Print ASR/AST Json format");
app.add_option("--pass", arg_pass, "Apply the ASR pass and show ASR (implies --show-asr)");
app.add_option("--skip-pass", skip_pass, "Skip an ASR pass in default pipeline");
app.add_flag("--disable-main", compiler_options.disable_main, "Do not generate any code for the `main` function");
Expand Down
132 changes: 129 additions & 3 deletions src/lpython/pickle.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
#include <string>

#include <lpython/pickle.h>
#include <lpython/pickle.h>
#include <lpython/bigint.h>
#include <lpython/python_ast.h>
#include <libasr/asr_utils.h>
#include <libasr/string_utils.h>
#include <libasr/location.h>

namespace LCompilers::LPython {

/* -----------------------------------------------------------------------*/
// ASR
/********************** AST Pickle *******************/
class PickleVisitor : public AST::PickleBaseVisitor<PickleVisitor>
{
public:
std::string get_str() {
return s;
}
};

std::string pickle_python(AST::ast_t &ast, bool colors, bool indent) {
PickleVisitor v;
v.use_colors = colors;
v.indent = indent;
v.visit_ast(ast);
return v.get_str();
}

/********************** ASR Pickle *******************/
class ASRPickleVisitor :
public ASR::PickleBaseVisitor<ASRPickleVisitor>
{
Expand Down Expand Up @@ -90,6 +106,23 @@ std::string pickle(ASR::TranslationUnit_t &asr, bool colors, bool indent, bool s
return pickle((ASR::asr_t &)asr, colors, indent, show_intrinsic_modules);
}

/********************** AST Pickle Tree *******************/
class ASTTreeVisitor : public AST::TreeBaseVisitor<ASTTreeVisitor>
{
public:
std::string get_str() {
return s;
}
};

std::string pickle_tree_python(AST::ast_t &ast, bool colors) {
ASTTreeVisitor v;
v.use_colors = colors;
v.visit_ast(ast);
return v.get_str();
}

/********************** ASR Pickle Tree *******************/
class ASRTreeVisitor :
public ASR::TreeBaseVisitor<ASRTreeVisitor>
{
Expand All @@ -114,4 +147,97 @@ std::string pickle_tree(ASR::TranslationUnit_t &asr, bool colors, bool show_intr
return pickle_tree((ASR::asr_t &)asr, colors, show_intrinsic_modules);
}

/********************** AST Pickle Json *******************/
class ASTJsonVisitor :
public LPython::AST::JsonBaseVisitor<ASTJsonVisitor>
{
public:
using LPython::AST::JsonBaseVisitor<ASTJsonVisitor>::JsonBaseVisitor;

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

std::string pickle_json(LPython::AST::ast_t &ast, LocationManager &lm) {
ASTJsonVisitor v(lm);
v.visit_ast(ast);
return v.get_str();
}

/********************** ASR Pickle Json *******************/
class ASRJsonVisitor :
public ASR::JsonBaseVisitor<ASRJsonVisitor>
{
public:
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) {
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("}");
}
};

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

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

}
10 changes: 9 additions & 1 deletion src/lpython/pickle.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#ifndef LFORTRAN_PICKLE_H
#define LFORTRAN_PICKLE_H

#include <lpython/python_ast.h>
#include <libasr/asr.h>
#include <libasr/location.h>

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);

std::string pickle_json(AST::ast_t &ast, LocationManager &lm);
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm);
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm);

}

#endif // LFORTRAN_PICKLE_H
31 changes: 0 additions & 31 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6166,37 +6166,6 @@ Result<ASR::TranslationUnit_t*> body_visitor(Allocator &al, LocationManager &lm,
return tu;
}

class PickleVisitor : public AST::PickleBaseVisitor<PickleVisitor>
{
public:
std::string get_str() {
return s;
}
};

std::string pickle_python(AST::ast_t &ast, bool colors, bool indent) {
PickleVisitor v;
v.use_colors = colors;
v.indent = indent;
v.visit_ast(ast);
return v.get_str();
}

class TreeVisitor : public AST::TreeBaseVisitor<TreeVisitor>
{
public:
std::string get_str() {
return s;
}
};

std::string pickle_tree_python(AST::ast_t &ast, bool colors) {
TreeVisitor v;
v.use_colors = colors;
v.visit_ast(ast);
return v.get_str();
}

std::string get_parent_dir(const std::string &path) {
int idx = path.size()-1;
while (idx >= 0 && path[idx] != '/' && path[idx] != '\\') idx--;
Expand Down
2 changes: 0 additions & 2 deletions src/lpython/semantics/python_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace LCompilers::LPython {

std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false);
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager &lm,
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options,
bool main_module, std::string file_path, bool allow_implicit_casting=false);
Expand Down

0 comments on commit f804b84

Please sign in to comment.