Skip to content

Commit 9865069

Browse files
committed
Added load_pycfile for loading LPython's ASR from a pyc file
1 parent a851b13 commit 9865069

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/libasr/modfile.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ std::string save_modfile(const ASR::TranslationUnit_t &m) {
5757
return b.get_str();
5858
}
5959

60+
std::string save_pycfile(const ASR::TranslationUnit_t &m) {
61+
#ifdef WITH_LFORTRAN_BINARY_MODFILES
62+
BinaryWriter b;
63+
#else
64+
TextWriter b;
65+
#endif
66+
// Header
67+
b.write_string(lfortran_modfile_type_string);
68+
b.write_string(LFORTRAN_VERSION);
69+
70+
// AST section: Original module source code:
71+
// Currently empty.
72+
// Note: in the future we can save here:
73+
// * A path to the original source code
74+
// * Hash of the orig source code
75+
// * AST binary export of it (this AST only changes if the hash changes)
76+
77+
// ASR section:
78+
79+
// Export ASR:
80+
// Currently empty.
81+
82+
// Full ASR:
83+
b.write_string(serialize(m));
84+
85+
return b.get_str();
86+
}
87+
6088
ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
6189
bool load_symtab_id, SymbolTable &symtab) {
6290
#ifdef WITH_LFORTRAN_BINARY_MODFILES
@@ -79,4 +107,27 @@ ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
79107
return tu;
80108
}
81109

110+
ASR::TranslationUnit_t* load_pycfile(Allocator &al, const std::string &s,
111+
bool load_symtab_id) {
112+
#ifdef WITH_LFORTRAN_BINARY_MODFILES
113+
BinaryReader b(s);
114+
#else
115+
TextReader b(s);
116+
#endif
117+
118+
std::string file_type = b.read_string();
119+
if (file_type != lfortran_modfile_type_string) {
120+
throw LCompilersException("LFortran Modfile format not recognized");
121+
}
122+
std::string version = b.read_string();
123+
if (version != LFORTRAN_VERSION) {
124+
throw LCompilersException("Incompatible format: LFortran Modfile was generated using version '" + version + "', but current LFortran version is '" + LFORTRAN_VERSION + "'");
125+
}
126+
std::string asr_binary = b.read_string();
127+
ASR::asr_t *asr = deserialize_asr(al, asr_binary, load_symtab_id);
128+
129+
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
130+
return tu;
131+
}
132+
82133
} // namespace LFortran

src/libasr/modfile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ namespace LFortran {
88
// Save a module to a modfile
99
std::string save_modfile(const ASR::TranslationUnit_t &m);
1010

11+
std::string save_pycfile(const ASR::TranslationUnit_t &m);
12+
1113
// Load a module from a modfile
1214
ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
1315
bool load_symtab_id, SymbolTable &symtab);
1416

17+
ASR::TranslationUnit_t* load_pycfile(Allocator &al, const std::string &s,
18+
bool load_symtab_id);
19+
1520
}
1621

1722
#endif // LFORTRAN_MODFILE_H

0 commit comments

Comments
 (0)