Skip to content

Commit 257af83

Browse files
Fix ASR verify pass error while using Interactive
1 parent b90bbdd commit 257af83

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

src/libasr/asr_scopes.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <libasr/asr_scopes.h>
55
#include <libasr/asr_utils.h>
6+
#include <libasr/pass/pass_utils.h>
67

78
std::string lcompilers_unique_ID;
89

@@ -39,14 +40,13 @@ void SymbolTable::mark_all_variables_external(Allocator &al) {
3940
case (ASR::symbolType::Function) : {
4041
ASR::Function_t *v = ASR::down_cast<ASR::Function_t>(a.second);
4142
ASR::FunctionType_t* v_func_type = ASR::down_cast<ASR::FunctionType_t>(v->m_function_signature);
42-
if ((v_func_type->m_abi != ASR::abiType::Intrinsic) &&
43-
(v_func_type->m_abi != ASR::abiType::Interactive)) {
44-
v->m_dependencies = nullptr;
45-
v->n_dependencies = 0;
43+
if (v_func_type->m_abi != ASR::abiType::Interactive) {
44+
v_func_type->m_abi = ASR::abiType::Interactive;
4645
v->m_body = nullptr;
4746
v->n_body = 0;
47+
PassUtils::UpdateDependenciesVisitor ud(al);
48+
ud.visit_Function(*v);
4849
}
49-
v_func_type->m_abi = ASR::abiType::Interactive;
5050
break;
5151
}
5252
case (ASR::symbolType::Module) : {

src/libasr/asr_verify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
420420
}
421421

422422
void visit_Function(const Function_t &x) {
423+
ASR::FunctionType_t* x_func_type = ASR::down_cast<ASR::FunctionType_t>(x.m_function_signature);
424+
if (x_func_type->m_abi == abiType::Interactive) {
425+
require(x.n_body == 0,
426+
"The Function::n_body should be 0 if abi set to Interactive");
427+
}
423428
std::vector<std::string> function_dependencies_copy = function_dependencies;
424429
function_dependencies.clear();
425430
function_dependencies.reserve(x.n_dependencies);

src/lpython/python_evaluator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ PythonCompiler::PythonCompiler(CompilerOptions compiler_options)
4141

4242
PythonCompiler::~PythonCompiler() = default;
4343

44+
Result<PythonCompiler::EvalResult> PythonCompiler::evaluate2(const std::string &code) {
45+
LocationManager lm;
46+
LCompilers::PassManager lpm;
47+
lpm.use_default_passes();
48+
{
49+
LCompilers::LocationManager::FileLocations fl;
50+
fl.in_filename = "input";
51+
std::ofstream out("input");
52+
out << code;
53+
lm.files.push_back(fl);
54+
lm.init_simple(code);
55+
lm.file_ends.push_back(code.size());
56+
}
57+
diag::Diagnostics diagnostics;
58+
return evaluate(code, false, lm, lpm, diagnostics);
59+
}
4460

4561
Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
4662
#ifdef HAVE_LFORTRAN_LLVM

src/lpython/python_evaluator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class PythonCompiler
5656
const std::string &code_orig, bool verbose, LocationManager &lm,
5757
LCompilers::PassManager& pass_manager, diag::Diagnostics &diagnostics);
5858

59+
Result<PythonCompiler::EvalResult> evaluate2(const std::string &code);
60+
5961
Result<LCompilers::LPython::AST::ast_t*> get_ast2(
6062
const std::string &code_orig, diag::Diagnostics &diagnostics);
6163

src/lpython/tests/test_llvm.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,20 @@ define float @f()
607607
float r = e.floatfn("f");
608608
CHECK(std::abs(r - 8) < 1e-6);
609609
}
610+
611+
TEST_CASE("FortranEvaluator 1") {
612+
CompilerOptions cu;
613+
cu.po.disable_main = true;
614+
cu.emit_debug_line_column = false;
615+
cu.generate_object_code = false;
616+
cu.interactive = true;
617+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
618+
PythonCompiler e(cu);
619+
LCompilers::Result<PythonCompiler::EvalResult>
620+
r = e.evaluate2("i: i32 = 3 % 1");
621+
CHECK(r.ok);
622+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
623+
r = e.evaluate2("i");
624+
CHECK(r.ok);
625+
CHECK(r.result.type == PythonCompiler::EvalResult::none); // TODO: change to integer4 and check the value once printing top level expressions is implemented
626+
}

0 commit comments

Comments
 (0)