Skip to content

Commit 6aed371

Browse files
work towards printing top level expression
1 parent 476d9bb commit 6aed371

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/libasr/pass/global_stmts.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void pass_wrap_global_stmts(Allocator &al,
9191
target = return_var_ref;
9292
idx++;
9393
} else {
94+
// TODO: We will need to add support to return other ASR::ttypeType::*
9495
throw LCompilersException("Return type not supported in interactive mode");
9596
}
9697
ASR::stmt_t* asr_stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, loc, target, value, nullptr));
@@ -119,7 +120,7 @@ void pass_wrap_global_stmts(Allocator &al,
119120
/* a_body */ body.p,
120121
/* n_body */ body.size(),
121122
/* a_return_var */ (return_var ? return_var_ref : nullptr),
122-
(return_var ? ASR::abiType::BindC : ASR::abiType::Source),
123+
ASR::abiType::Source,
123124
ASR::Public, ASR::Implementation,
124125
nullptr,
125126
false, false, false, false, false,

src/lpython/python_evaluator.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,48 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
105105
}
106106

107107
bool call_run_fn = false;
108+
std::string return_type;
108109
if (m->get_return_type(run_fn) != "none") {
109110
call_run_fn = true;
111+
return_type = m->get_return_type(run_fn);
110112
}
111113

112114
e->add_module(std::move(m));
113115
if (call_run_fn) {
114-
e->voidfn(run_fn);
116+
if (return_type == "integer4") {
117+
int32_t r = e->int32fn(run_fn);
118+
result.type = EvalResult::integer4;
119+
result.i32 = r;
120+
} else if (return_type == "integer8") {
121+
int64_t r = e->int64fn(run_fn);
122+
result.type = EvalResult::integer8;
123+
result.i64 = r;
124+
} else if (return_type == "real4") {
125+
float r = e->floatfn(run_fn);
126+
result.type = EvalResult::real4;
127+
result.f32 = r;
128+
} else if (return_type == "real8") {
129+
double r = e->doublefn(run_fn);
130+
result.type = EvalResult::real8;
131+
result.f64 = r;
132+
} else if (return_type == "complex4") {
133+
std::complex<float> r = e->complex4fn(run_fn);
134+
result.type = EvalResult::complex4;
135+
result.c32.re = r.real();
136+
result.c32.im = r.imag();
137+
} else if (return_type == "complex8") {
138+
std::complex<double> r = e->complex8fn(run_fn);
139+
result.type = EvalResult::complex8;
140+
result.c64.re = r.real();
141+
result.c64.im = r.imag();
142+
} else if (return_type == "void") {
143+
e->voidfn(run_fn);
144+
result.type = EvalResult::statement;
145+
} else if (return_type == "none") {
146+
result.type = EvalResult::none;
147+
} else {
148+
throw LCompilersException("FortranEvaluator::evaluate(): Return type not supported");
149+
}
115150
}
116151

117152
if (call_run_fn) {

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,8 +4088,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
40884088

40894089
// Every module goes into a Module_t
40904090
SymbolTable *parent_scope = current_scope;
4091+
ASR::Module_t* module_sym = nullptr;
40914092
if (parent_scope->get_scope().find(module_name) == parent_scope->get_scope().end()) {
4092-
ASR::Module_t* module_sym = nullptr;
40934093
current_scope = al.make_new<SymbolTable>(parent_scope);
40944094
ASR::asr_t *tmp1 = ASR::make_Module_t(al, x.base.base.loc,
40954095
/* a_symtab */ current_scope,
@@ -4103,21 +4103,29 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
41034103
for (size_t i=0; i<x.n_body; i++) {
41044104
visit_stmt(*x.m_body[i]);
41054105
}
4106-
41074106
LCOMPILERS_ASSERT(module_sym != nullptr);
41084107
module_sym->m_dependencies = current_module_dependencies.p;
41094108
module_sym->n_dependencies = current_module_dependencies.size();
41104109
if (!overload_defs.empty()) {
41114110
create_GenericProcedure(x.base.base.loc);
41124111
}
41134112
} else {
4114-
ASR::Module_t* module_sym =
4115-
ASR::down_cast<ASR::Module_t>(parent_scope->resolve_symbol(module_name));
4113+
module_sym = ASR::down_cast<ASR::Module_t>(parent_scope->resolve_symbol(module_name));
41164114
LCOMPILERS_ASSERT(module_sym != nullptr);
41174115
current_scope = module_sym->m_symtab;
41184116
for (size_t i=0; i<x.n_body; i++) {
41194117
visit_stmt(*x.m_body[i]);
41204118
}
4119+
for (size_t i = 0; i < module_sym->n_dependencies; i++) {
4120+
if (module_sym->m_dependencies[i]) {
4121+
current_module_dependencies.push_back(al, module_sym->m_dependencies[i]);
4122+
}
4123+
}
4124+
module_sym->m_dependencies = current_module_dependencies.p;
4125+
module_sym->n_dependencies = current_module_dependencies.size();
4126+
if (!overload_defs.empty()) {
4127+
create_GenericProcedure(x.base.base.loc);
4128+
}
41214129
}
41224130

41234131
global_scope = nullptr;
@@ -6691,8 +6699,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
66916699
// If tmp is a statement and not an expression
66926700
// never cast into expression using ASRUtils::EXPR
66936701
// Just ignore and exit the function naturally.
6694-
if( tmp && !ASR::is_a<ASR::stmt_t>(*tmp) ) {
6695-
LCOMPILERS_ASSERT(ASR::is_a<ASR::expr_t>(*tmp));
6702+
if( tmp && !ASR::is_a<ASR::expr_t>(*tmp) ) {
66966703
tmp = nullptr;
66976704
}
66986705
}

0 commit comments

Comments
 (0)