Skip to content

Commit a18035d

Browse files
remember global functions and variables declared
1 parent 2a9e597 commit a18035d

File tree

8 files changed

+579
-90
lines changed

8 files changed

+579
-90
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,6 @@ integration_tests/array_02_decl
234234
integration_tests/array_02_decl.c
235235
integration_tests/expr_12
236236
integration_tests/expr_12.c
237+
238+
# Interactive Shell
239+
/input

src/bin/lpython.cpp

Lines changed: 96 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -794,22 +794,24 @@ int emit_llvm(const std::string &infile,
794794
}
795795

796796
int interactive_python_repl(
797-
LCompilers::PassManager& pass_manager,
797+
// LCompilers::PassManager& pass_manager,
798798
CompilerOptions &compiler_options,
799-
bool time_report)
799+
bool /*time_report*/)
800800
{
801801
Allocator al(4*1024);
802-
802+
compiler_options.interactive = true;
803+
LCompilers::PythonCompiler fe(compiler_options);
803804
LCompilers::diag::Diagnostics diagnostics;
804-
LCompilers::LocationManager lm;
805+
LCompilers::LocationManager lm;
805806
std::vector<std::pair<std::string, double>> times;
806-
std::string infile;
807-
807+
LCompilers::PythonCompiler::EvalResult r;
808+
LCompilers::PassManager pass_manager;
809+
pass_manager.use_default_passes();
810+
808811
std::string code_string;
809812
std::cout << ">>> ";
810813
size_t cell_count = 0;
811814
for (std::string input; std::getline(std::cin, input);) {
812-
813815
if (input == "exit" || input == "quit") // exit condition
814816
return 0;
815817

@@ -833,76 +835,100 @@ int interactive_python_repl(
833835
{
834836
cell_count++;
835837
LCompilers::LocationManager::FileLocations fl;
836-
infile = "stdin" + std::to_string(cell_count);
837-
fl.in_filename = infile;
838+
fl.in_filename = "input";
839+
std::ofstream out("input");
840+
out << code_string;
838841
lm.files.push_back(fl);
839842
lm.init_simple(code_string);
840843
lm.file_ends.push_back(code_string.size());
841844
}
842845

843-
/* parse and run */
844-
// parsing string to AST
845-
auto parsing_start = std::chrono::high_resolution_clock::now();
846-
// ???: change `parse_python_file` to `parse` or `parse_string`
847-
LCompilers::Result<LCompilers::LPython::AST::Module_t*> r = LCompilers::LPython::parse(al, code_string, 0, diagnostics);
848-
auto parsing_end = std::chrono::high_resolution_clock::now();
849-
times.push_back(std::make_pair("Parsing", std::chrono::duration<double, std::milli>(parsing_end - parsing_start).count()));
850-
std::cerr << diagnostics.render(lm, compiler_options);
851-
if (!r.ok) {
852-
LCOMPILERS_ASSERT(diagnostics.has_error())
853-
print_time_report(times, time_report);
854-
return 1;
846+
try {
847+
auto evaluation_start_time = std::chrono::high_resolution_clock::now();
848+
LCompilers::Result<LCompilers::PythonCompiler::EvalResult>
849+
res = fe.evaluate(code_string, false, lm, pass_manager, diagnostics);
850+
std::cerr << diagnostics.render(lm, compiler_options);
851+
if (res.ok) {
852+
r = res.result;
853+
} else {
854+
LCOMPILERS_ASSERT(diagnostics.has_error())
855+
code_string = "";
856+
std::cout << ">>> ";
857+
continue;
858+
}
859+
860+
auto evaluation_end_time = std::chrono::high_resolution_clock::now();
861+
times.push_back(std::make_pair("evalution " + std::to_string(cell_count), std::chrono::duration
862+
<double, std::milli>(evaluation_start_time - evaluation_end_time).count()));
863+
864+
} catch (const LCompilers::LCompilersException &e) {
865+
std::cerr << "Internal Compiler Error: Unhandled exception" << std::endl;
866+
std::vector<LCompilers::StacktraceItem> d = e.stacktrace_addresses();
867+
get_local_addresses(d);
868+
get_local_info(d);
869+
std::cerr << stacktrace2str(d, LCompilers::stacktrace_depth);
870+
std::cerr << e.name() + ": " << e.msg() << std::endl;
871+
872+
code_string = "";
873+
std::cout << ">>> ";
874+
continue;
855875
}
856876

857-
// AST -> ASR
858-
LCompilers::LPython::AST::ast_t* ast = (LCompilers::LPython::AST::ast_t*)r.result;
859-
diagnostics.diagnostics.clear();
860-
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
861-
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
862-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
863-
!(false && compiler_options.po.disable_main), "__main__", infile);
864-
865-
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
866-
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
867-
std::cerr << diagnostics.render(lm, compiler_options);
868-
if (!r1.ok) {
869-
LCOMPILERS_ASSERT(diagnostics.has_error())
870-
print_time_report(times, time_report);
871-
return 2;
872-
}
873-
LCompilers::ASR::TranslationUnit_t* asr = r1.result;
874-
diagnostics.diagnostics.clear();
875-
876-
LCompilers::PythonCompiler fe(compiler_options);
877-
LCompilers::LLVMEvaluator e(compiler_options.target);
878-
std::unique_ptr<LCompilers::LLVMModule> m;
879-
auto asr_to_llvm_start = std::chrono::high_resolution_clock::now();
880-
LCompilers::Result<std::unique_ptr<LCompilers::LLVMModule>>
881-
res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
882-
auto asr_to_llvm_end = std::chrono::high_resolution_clock::now();
883-
times.push_back(std::make_pair("ASR to LLVM", std::chrono::duration<double, std::milli>(asr_to_llvm_end - asr_to_llvm_start).count()));
884-
885-
std::cerr << diagnostics.render(lm, compiler_options);
886-
if (!res.ok) {
887-
LCOMPILERS_ASSERT(diagnostics.has_error())
888-
print_time_report(times, time_report);
889-
return 3;
877+
switch (r.type) {
878+
case (LCompilers::PythonCompiler::EvalResult::integer4) : {
879+
// if (verbose) std::cout << "Return type: integer" << std::endl;
880+
// if (verbose) section("Result:");
881+
std::cout << r.i32 << std::endl;
882+
break;
883+
}
884+
case (LCompilers::PythonCompiler::EvalResult::integer8) : {
885+
// if (verbose) std::cout << "Return type: integer(8)" << std::endl;
886+
// if (verbose) section("Result:");
887+
std::cout << r.i64 << std::endl;
888+
break;
889+
}
890+
case (LCompilers::PythonCompiler::EvalResult::real4) : {
891+
// if (verbose) std::cout << "Return type: real" << std::endl;
892+
// if (verbose) section("Result:");
893+
std::cout << std::setprecision(8) << r.f32 << std::endl;
894+
break;
895+
}
896+
case (LCompilers::PythonCompiler::EvalResult::real8) : {
897+
// if (verbose) std::cout << "Return type: real(8)" << std::endl;
898+
// if (verbose) section("Result:");
899+
std::cout << std::setprecision(17) << r.f64 << std::endl;
900+
break;
901+
}
902+
case (LCompilers::PythonCompiler::EvalResult::complex4) : {
903+
// if (verbose) std::cout << "Return type: complex" << std::endl;
904+
// if (verbose) section("Result:");
905+
std::cout << std::setprecision(8) << "(" << r.c32.re << ", " << r.c32.im << ")" << std::endl;
906+
break;
907+
}
908+
case (LCompilers::PythonCompiler::EvalResult::complex8) : {
909+
// if (verbose) std::cout << "Return type: complex(8)" << std::endl;
910+
// if (verbose) section("Result:");
911+
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
912+
break;
913+
}
914+
case (LCompilers::PythonCompiler::EvalResult::statement) : {
915+
// if (verbose) {
916+
// std::cout << "Return type: none" << std::endl;
917+
// section("Result:");
918+
// std::cout << "(statement)" << std::endl;
919+
// }
920+
break;
921+
}
922+
case (LCompilers::PythonCompiler::EvalResult::none) : {
923+
// if (verbose) {
924+
// std::cout << "Return type: none" << std::endl;
925+
// section("Result:");
926+
// std::cout << "(nothing to execute)" << std::endl;
927+
// }
928+
break;
929+
}
930+
default : throw LCompilers::LCompilersException("Return type not supported");
890931
}
891-
m = std::move(res.result);
892-
893-
bool call_init = false;
894-
bool call_stmts = false;
895-
if (m->get_return_type("__module___main_____main__global_init") == "void")
896-
call_init = true;
897-
if (m->get_return_type("__module___main_____main__global_stmts") == "void")
898-
call_stmts = true;
899-
900-
e.add_module(std::move(m));
901-
if (call_init)
902-
e.voidfn("__module___main_____main__global_init");
903-
if (call_stmts)
904-
e.voidfn("__module___main_____main__global_stmts");
905-
/* end parse and run */
906932

907933
code_string = "";
908934
std::cout << ">>> ";
@@ -1955,7 +1981,7 @@ int main(int argc, char *argv[])
19551981
compiler_options.po.disable_main = true;
19561982
compiler_options.emit_debug_line_column = false;
19571983
compiler_options.generate_object_code = false;
1958-
return interactive_python_repl(lpython_pass_manager, compiler_options, time_report);
1984+
return interactive_python_repl(compiler_options, time_report);
19591985
}
19601986

19611987
// TODO: for now we ignore the other filenames, only handle

src/lpython/parser/parser.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ Result<LPython::AST::Module_t*> parse(Allocator &al, const std::string &s,
4242
p.result.p, p.result.size(), p.type_ignore.p, p.type_ignore.size());
4343
}
4444

45+
Result<LPython::AST::ast_t*> parse_to_ast(Allocator &al, const std::string &s,
46+
uint32_t prev_loc, diag::Diagnostics &diagnostics)
47+
{
48+
Parser p(al, diagnostics);
49+
try {
50+
p.parse(s, prev_loc);
51+
} catch (const parser_local::TokenizerError &e) {
52+
Error error;
53+
diagnostics.diagnostics.push_back(e.d);
54+
return error;
55+
} catch (const parser_local::ParserError &e) {
56+
Error error;
57+
diagnostics.diagnostics.push_back(e.d);
58+
return error;
59+
}
60+
61+
Location l;
62+
if (p.result.size() == 0) {
63+
l.first=0;
64+
l.last=0;
65+
} else {
66+
l.first=p.result[0]->base.loc.first;
67+
l.last=p.result[p.result.size()-1]->base.loc.last;
68+
}
69+
return LPython::AST::make_Interactive_t(al, l, p.result.p, p.result.size());
70+
}
71+
4572
void Parser::parse(const std::string &input, uint32_t prev_loc)
4673
{
4774
inp = input;

src/lpython/parser/parser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef LPYTHON_PARSER_PARSER_H
22
#define LPYTHON_PARSER_PARSER_H
33

4+
#include "lpython/python_ast.h"
45
#include <libasr/containers.h>
56
#include <libasr/diagnostics.h>
67
#include <lpython/parser/tokenizer.h>
@@ -35,6 +36,10 @@ Result<LPython::AST::Module_t*> parse(Allocator &al,
3536
const std::string &s, uint32_t prev_loc,
3637
diag::Diagnostics &diagnostics);
3738

39+
Result<LPython::AST::ast_t*> parse_to_ast(Allocator &al,
40+
const std::string &s, uint32_t prev_loc,
41+
diag::Diagnostics &diagnostics);
42+
3843
Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
3944
const std::string &runtime_library_dir,
4045
const std::string &infile,

0 commit comments

Comments
 (0)