Skip to content

Commit 269345b

Browse files
authored
Merge pull request #1752 from Shaikh-Ubaid/asr_json_modules
ASR: Support modules in json format
2 parents 51ad8b4 + 8506de2 commit 269345b

File tree

7 files changed

+1090
-44
lines changed

7 files changed

+1090
-44
lines changed

run_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def is_included(backend):
2121
ast = is_included("ast")
2222
ast_new = is_included("ast_new")
2323
asr = is_included("asr")
24+
asr_json = is_included("asr_json")
2425
llvm = is_included("llvm")
2526
llvm_dbg = is_included("llvm_dbg")
2627
cpp = is_included("cpp")
@@ -80,6 +81,15 @@ def is_included(backend):
8081
update_reference,
8182
extra_args)
8283

84+
if asr_json:
85+
run_test(
86+
filename,
87+
"asr_json",
88+
"lpython --show-asr --json {infile} -o {outfile}",
89+
filename,
90+
update_reference,
91+
extra_args)
92+
8393
if pass_ is not None:
8494
cmd = "lpython --pass=" + pass_ + \
8595
" --show-asr --indent --no-color {infile} -o {outfile}"

src/bin/lpython.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ int emit_asr(const std::string &infile,
210210
std::cout << LCompilers::LPython::pickle_tree(*asr,
211211
compiler_options.use_colors, with_intrinsic_modules) << std::endl;
212212
} else if (compiler_options.json) {
213-
std::cout << LCompilers::LPython::pickle_json(*asr, lm) << std::endl;
213+
std::cout << LCompilers::LPython::pickle_json(*asr, lm, with_intrinsic_modules) << std::endl;
214214
} else {
215215
std::cout << LCompilers::LPython::pickle(*asr, compiler_options.use_colors,
216216
compiler_options.indent, with_intrinsic_modules) << std::endl;

src/lpython/pickle.cpp

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ class ASRJsonVisitor :
185185
public ASR::JsonBaseVisitor<ASRJsonVisitor>
186186
{
187187
public:
188+
bool show_intrinsic_modules;
189+
188190
using ASR::JsonBaseVisitor<ASRJsonVisitor>::JsonBaseVisitor;
189191

190192
std::string get_str() {
@@ -200,59 +202,64 @@ class ASRJsonVisitor :
200202
}
201203

202204
void visit_Module(const ASR::Module_t &x) {
203-
s.append("{");
204-
inc_indent(); s.append("\n" + indtd);
205-
s.append("\"node\": \"Module\"");
206-
s.append(",\n" + indtd);
207-
s.append("\"fields\": {");
208-
inc_indent(); s.append("\n" + indtd);
209-
s.append("\"name\": ");
210-
s.append("\"" + std::string(x.m_name) + "\"");
211-
s.append(",\n" + indtd);
212-
s.append("\"dependencies\": ");
213-
s.append("[");
214-
if (x.n_dependencies > 0) {
205+
if (x.m_intrinsic && !show_intrinsic_modules) { // do not show intrinsic modules by default
206+
s.append("{");
207+
inc_indent(); s.append("\n" + indtd);
208+
s.append("\"node\": \"Module\"");
209+
s.append(",\n" + indtd);
210+
s.append("\"fields\": {");
215211
inc_indent(); s.append("\n" + indtd);
216-
for (size_t i=0; i<x.n_dependencies; i++) {
217-
s.append("\"" + std::string(x.m_dependencies[i]) + "\"");
218-
if (i < x.n_dependencies-1) {
219-
s.append(",\n" + indtd);
220-
};
212+
s.append("\"name\": ");
213+
s.append("\"" + std::string(x.m_name) + "\"");
214+
s.append(",\n" + indtd);
215+
s.append("\"dependencies\": ");
216+
s.append("[");
217+
if (x.n_dependencies > 0) {
218+
inc_indent(); s.append("\n" + indtd);
219+
for (size_t i=0; i<x.n_dependencies; i++) {
220+
s.append("\"" + std::string(x.m_dependencies[i]) + "\"");
221+
if (i < x.n_dependencies-1) {
222+
s.append(",\n" + indtd);
223+
};
224+
}
225+
dec_indent(); s.append("\n" + indtd);
226+
}
227+
s.append("]");
228+
s.append(",\n" + indtd);
229+
s.append("\"loaded_from_mod\": ");
230+
if (x.m_loaded_from_mod) {
231+
s.append("true");
232+
} else {
233+
s.append("false");
234+
}
235+
s.append(",\n" + indtd);
236+
s.append("\"intrinsic\": ");
237+
if (x.m_intrinsic) {
238+
s.append("true");
239+
} else {
240+
s.append("false");
221241
}
222242
dec_indent(); s.append("\n" + indtd);
223-
}
224-
s.append("]");
225-
s.append(",\n" + indtd);
226-
s.append("\"loaded_from_mod\": ");
227-
if (x.m_loaded_from_mod) {
228-
s.append("true");
229-
} else {
230-
s.append("false");
231-
}
232-
s.append(",\n" + indtd);
233-
s.append("\"intrinsic\": ");
234-
if (x.m_intrinsic) {
235-
s.append("true");
243+
s.append("}");
244+
s.append(",\n" + indtd);
245+
append_location(s, x.base.base.loc.first, x.base.base.loc.last);
246+
dec_indent(); s.append("\n" + indtd);
247+
s.append("}");
236248
} else {
237-
s.append("false");
249+
ASR::JsonBaseVisitor<ASRJsonVisitor>::visit_Module(x);
238250
}
239-
dec_indent(); s.append("\n" + indtd);
240-
s.append("}");
241-
s.append(",\n" + indtd);
242-
append_location(s, x.base.base.loc.first, x.base.base.loc.last);
243-
dec_indent(); s.append("\n" + indtd);
244-
s.append("}");
245251
}
246252
};
247253

248-
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm) {
254+
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules) {
249255
ASRJsonVisitor v(lm);
256+
v.show_intrinsic_modules = show_intrinsic_modules;
250257
v.visit_asr(asr);
251258
return v.get_str();
252259
}
253260

254-
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm) {
255-
return pickle_json((ASR::asr_t &)asr, lm);
261+
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules) {
262+
return pickle_json((ASR::asr_t &)asr, lm, show_intrinsic_modules);
256263
}
257264

258265
}

src/lpython/pickle.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace LCompilers::LPython {
1313
bool show_intrinsic_modules=false);
1414
std::string pickle(ASR::TranslationUnit_t &asr, bool colors=false,
1515
bool indent=false, bool show_intrinsic_modules=false);
16-
16+
1717
// Print the tree structure
1818
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
1919
std::string pickle_tree(ASR::asr_t &asr, bool colors, bool show_intrinsic_modules);
2020
std::string pickle_tree(ASR::TranslationUnit_t &asr, bool colors, bool show_intrinsic_modules);
2121

2222
std::string pickle_json(AST::ast_t &ast, LocationManager &lm);
23-
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm);
24-
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm);
23+
std::string pickle_json(ASR::asr_t &asr, LocationManager &lm, bool show_intrinsic_modules);
24+
std::string pickle_json(ASR::TranslationUnit_t &asr, LocationManager &lm, bool show_intrinsic_modules);
2525

2626
}
2727

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr_json-modules_02-70a491a",
3+
"cmd": "lpython --show-asr --json {infile} -o {outfile}",
4+
"infile": "tests/../integration_tests/modules_02.py",
5+
"infile_hash": "dcb00ac27cbbcdec61d81f1df9e852ba81a2197e7804ec89cab76e44",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "asr_json-modules_02-70a491a.stdout",
9+
"stdout_hash": "adc3ed3cc499317e76d8755750cbd9eee8f78810d3b83ecf6cfcef15",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}

0 commit comments

Comments
 (0)