Skip to content

Commit

Permalink
Fix ASR verify pass error while using Interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed May 23, 2024
1 parent b90bbdd commit 257af83
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/libasr/asr_scopes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <libasr/asr_scopes.h>
#include <libasr/asr_utils.h>
#include <libasr/pass/pass_utils.h>

std::string lcompilers_unique_ID;

Expand Down Expand Up @@ -39,14 +40,13 @@ void SymbolTable::mark_all_variables_external(Allocator &al) {
case (ASR::symbolType::Function) : {
ASR::Function_t *v = ASR::down_cast<ASR::Function_t>(a.second);
ASR::FunctionType_t* v_func_type = ASR::down_cast<ASR::FunctionType_t>(v->m_function_signature);
if ((v_func_type->m_abi != ASR::abiType::Intrinsic) &&
(v_func_type->m_abi != ASR::abiType::Interactive)) {
v->m_dependencies = nullptr;
v->n_dependencies = 0;
if (v_func_type->m_abi != ASR::abiType::Interactive) {
v_func_type->m_abi = ASR::abiType::Interactive;
v->m_body = nullptr;
v->n_body = 0;
PassUtils::UpdateDependenciesVisitor ud(al);
ud.visit_Function(*v);
}
v_func_type->m_abi = ASR::abiType::Interactive;
break;
}
case (ASR::symbolType::Module) : {
Expand Down
5 changes: 5 additions & 0 deletions src/libasr/asr_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
}

void visit_Function(const Function_t &x) {
ASR::FunctionType_t* x_func_type = ASR::down_cast<ASR::FunctionType_t>(x.m_function_signature);
if (x_func_type->m_abi == abiType::Interactive) {
require(x.n_body == 0,
"The Function::n_body should be 0 if abi set to Interactive");
}
std::vector<std::string> function_dependencies_copy = function_dependencies;
function_dependencies.clear();
function_dependencies.reserve(x.n_dependencies);
Expand Down
16 changes: 16 additions & 0 deletions src/lpython/python_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ PythonCompiler::PythonCompiler(CompilerOptions compiler_options)

PythonCompiler::~PythonCompiler() = default;

Result<PythonCompiler::EvalResult> PythonCompiler::evaluate2(const std::string &code) {
LocationManager lm;
LCompilers::PassManager lpm;
lpm.use_default_passes();
{
LCompilers::LocationManager::FileLocations fl;
fl.in_filename = "input";
std::ofstream out("input");
out << code;
lm.files.push_back(fl);
lm.init_simple(code);
lm.file_ends.push_back(code.size());
}
diag::Diagnostics diagnostics;
return evaluate(code, false, lm, lpm, diagnostics);
}

Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
#ifdef HAVE_LFORTRAN_LLVM
Expand Down
2 changes: 2 additions & 0 deletions src/lpython/python_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class PythonCompiler
const std::string &code_orig, bool verbose, LocationManager &lm,
LCompilers::PassManager& pass_manager, diag::Diagnostics &diagnostics);

Result<PythonCompiler::EvalResult> evaluate2(const std::string &code);

Result<LCompilers::LPython::AST::ast_t*> get_ast2(
const std::string &code_orig, diag::Diagnostics &diagnostics);

Expand Down
17 changes: 17 additions & 0 deletions src/lpython/tests/test_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,20 @@ define float @f()
float r = e.floatfn("f");
CHECK(std::abs(r - 8) < 1e-6);
}

TEST_CASE("FortranEvaluator 1") {
CompilerOptions cu;
cu.po.disable_main = true;
cu.emit_debug_line_column = false;
cu.generate_object_code = false;
cu.interactive = true;
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
PythonCompiler e(cu);
LCompilers::Result<PythonCompiler::EvalResult>
r = e.evaluate2("i: i32 = 3 % 1");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::none);
r = e.evaluate2("i");
CHECK(r.ok);
CHECK(r.result.type == PythonCompiler::EvalResult::none); // TODO: change to integer4 and check the value once printing top level expressions is implemented
}

0 comments on commit 257af83

Please sign in to comment.