Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate read when paring python asr #2541

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace {
using LCompilers::endswith;
using LCompilers::CompilerOptions;
using LCompilers::LPython::parse_python_file;
using LCompilers::LPython::parse_python_source;

enum class Backend {
llvm, cpp, c, x86, wasm, wasm_x86, wasm_x64
Expand Down Expand Up @@ -190,16 +191,17 @@ int emit_asr(const std::string &infile,
Allocator al(4*1024);
LCompilers::diag::Diagnostics diagnostics;
LCompilers::LocationManager lm;
std::string input;
{
LCompilers::LocationManager::FileLocations fl;
fl.in_filename = infile;
lm.files.push_back(fl);
std::string input = LCompilers::read_file(infile);
input = LCompilers::read_file(infile);
lm.init_simple(input);
lm.file_ends.push_back(input.size());
}
LCompilers::Result<LCompilers::LPython::AST::ast_t*> r1 = parse_python_file(
al, runtime_library_dir, infile, diagnostics, 0, compiler_options.new_parser);
LCompilers::Result<LCompilers::LPython::AST::ast_t*> r1 = parse_python_source(
al, runtime_library_dir, input, diagnostics, 0, compiler_options.new_parser);
std::cerr << diagnostics.render(lm, compiler_options);
if (!r1.ok) {
return 1;
Expand Down
17 changes: 13 additions & 4 deletions src/lpython/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@ std::string unique_filename(const std::string &prefix) {
return filename;
}

Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
Result<LPython::AST::ast_t*> parse_python_source(Allocator &al,
const std::string &/*runtime_library_dir*/,
const std::string &infile,
const std::string &source_code,
diag::Diagnostics &diagnostics,
uint32_t prev_loc,
[[maybe_unused]] bool new_parser) {
LPython::AST::ast_t* ast;
// We will be using the new parser from now on
new_parser = true;
LCOMPILERS_ASSERT(new_parser)
std::string input = read_file(infile);
Result<LPython::AST::Module_t*> res = parse(al, input, prev_loc, diagnostics);
Result<LPython::AST::Module_t*> res = parse(al, source_code, prev_loc, diagnostics);
if (res.ok) {
ast = (LPython::AST::ast_t*)res.result;
} else {
Expand All @@ -133,5 +132,15 @@ Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
return ast;
}

Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
const std::string &runtime_library_dir,
const std::string &infile,
diag::Diagnostics &diagnostics,
uint32_t prev_loc,
[[maybe_unused]] bool new_parser) {
std::string input_source_code = read_file(infile);
return parse_python_source(al, runtime_library_dir, input_source_code, diagnostics, prev_loc, new_parser);
}
Comment on lines +135 to +143
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, refactor the code to not read in parse_python_file function in parser.cpp.



} // namespace LCompilers::LPython
6 changes: 6 additions & 0 deletions src/lpython/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
diag::Diagnostics &diagnostics,
uint32_t prev_loc, bool new_parser);

Result<LPython::AST::ast_t*> parse_python_source(Allocator &al,
const std::string &runtime_library_dir,
const std::string &infile,
diag::Diagnostics &diagnostics,
uint32_t prev_loc, bool new_parser);

} // namespace LCompilers::LPython

#endif
Loading