Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2420448

Browse files
Printing top-level Expressions (#2716)
* printing top level expressions * test cases for i32 and i64 * fix typo * update according to code review suggestions * Update src/lpython/semantics/python_ast_to_asr.cpp Co-authored-by: Shaikh Ubaid <[email protected]> --------- Co-authored-by: Shaikh Ubaid <[email protected]>
1 parent 13bb54e commit 2420448

File tree

3 files changed

+204
-6
lines changed

3 files changed

+204
-6
lines changed

src/lpython/python_evaluator.cpp

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

121121
bool call_run_fn = false;
122+
std::string return_type;
122123
if (m->get_return_type(run_fn) != "none") {
123124
call_run_fn = true;
125+
return_type = m->get_return_type(run_fn);
124126
}
125127

126128
e->add_module(std::move(m));
127129
if (call_run_fn) {
128-
e->voidfn(run_fn);
130+
if (return_type == "integer4") {
131+
int32_t r = e->int32fn(run_fn);
132+
result.type = EvalResult::integer4;
133+
result.i32 = r;
134+
} else if (return_type == "integer8") {
135+
int64_t r = e->int64fn(run_fn);
136+
result.type = EvalResult::integer8;
137+
result.i64 = r;
138+
} else if (return_type == "real4") {
139+
float r = e->floatfn(run_fn);
140+
result.type = EvalResult::real4;
141+
result.f32 = r;
142+
} else if (return_type == "real8") {
143+
double r = e->doublefn(run_fn);
144+
result.type = EvalResult::real8;
145+
result.f64 = r;
146+
} else if (return_type == "complex4") {
147+
std::complex<float> r = e->complex4fn(run_fn);
148+
result.type = EvalResult::complex4;
149+
result.c32.re = r.real();
150+
result.c32.im = r.imag();
151+
} else if (return_type == "complex8") {
152+
std::complex<double> r = e->complex8fn(run_fn);
153+
result.type = EvalResult::complex8;
154+
result.c64.re = r.real();
155+
result.c64.im = r.imag();
156+
} else if (return_type == "void") {
157+
e->voidfn(run_fn);
158+
result.type = EvalResult::statement;
159+
} else if (return_type == "none") {
160+
result.type = EvalResult::none;
161+
} else {
162+
throw LCompilersException("FortranEvaluator::evaluate(): Return type not supported");
163+
}
129164
}
130165

131166
if (call_run_fn) {

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6693,10 +6693,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
66936693
}
66946694
this->visit_expr(*x.m_value);
66956695

6696-
// If tmp is a statement and not an expression
6697-
// never cast into expression using ASRUtils::EXPR
6698-
// Just ignore and exit the function naturally.
6699-
if( tmp && !ASR::is_a<ASR::stmt_t>(*tmp) ) {
6696+
if (eval_count == 0 && tmp && !ASR::is_a<ASR::stmt_t>(*tmp)) {
6697+
// If tmp is a statement and not an expression
6698+
// never cast into expression using ASRUtils::EXPR
6699+
// Just ignore and exit the function naturally.
67006700
LCOMPILERS_ASSERT(ASR::is_a<ASR::expr_t>(*tmp));
67016701
tmp = nullptr;
67026702
}

src/lpython/tests/test_llvm.cpp

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,168 @@ TEST_CASE("PythonCompiler 1") {
619619
LCompilers::Result<PythonCompiler::EvalResult>
620620
r = e.evaluate2("1");
621621
CHECK(r.ok);
622-
CHECK(r.result.type == PythonCompiler::EvalResult::none); // TODO: change to integer4 and check the value once printing top level expressions is implemented
622+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
623+
CHECK(r.result.i32 == 1);
624+
}
625+
626+
TEST_CASE("PythonCompiler i32 expressions") {
627+
CompilerOptions cu;
628+
cu.po.disable_main = true;
629+
cu.emit_debug_line_column = false;
630+
cu.generate_object_code = false;
631+
cu.interactive = true;
632+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
633+
PythonCompiler e(cu);
634+
LCompilers::Result<PythonCompiler::EvalResult>
635+
636+
r = e.evaluate2("1");
637+
CHECK(r.ok);
638+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
639+
CHECK(r.result.i32 == 1);
640+
641+
r = e.evaluate2("1 + 2");
642+
CHECK(r.ok);
643+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
644+
CHECK(r.result.i32 == 3);
645+
646+
r = e.evaluate2("1 - 2");
647+
CHECK(r.ok);
648+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
649+
CHECK(r.result.i32 == -1);
650+
651+
r = e.evaluate2("1 * 2");
652+
CHECK(r.ok);
653+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
654+
CHECK(r.result.i32 == 2);
655+
656+
r = e.evaluate2("3 ** 3");
657+
CHECK(r.ok);
658+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
659+
CHECK(r.result.i32 == 27);
660+
661+
r = e.evaluate2("4 // 2");
662+
CHECK(r.ok);
663+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
664+
CHECK(r.result.i32 == 2);
665+
666+
r = e.evaluate2("4 / 2");
667+
CHECK(r.ok);
668+
CHECK(r.result.type == PythonCompiler::EvalResult::real8);
669+
CHECK(r.result.f64 == 2);
670+
}
671+
672+
TEST_CASE("PythonCompiler i32 declaration") {
673+
CompilerOptions cu;
674+
cu.po.disable_main = true;
675+
cu.emit_debug_line_column = false;
676+
cu.generate_object_code = false;
677+
cu.interactive = true;
678+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
679+
PythonCompiler e(cu);
680+
LCompilers::Result<PythonCompiler::EvalResult>
681+
682+
r = e.evaluate2("i: i32");
683+
CHECK(r.ok);
684+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
685+
r = e.evaluate2("i = 5");
686+
CHECK(r.ok);
687+
CHECK(r.result.type == PythonCompiler::EvalResult::statement);
688+
r = e.evaluate2("i");
689+
CHECK(r.ok);
690+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
691+
CHECK(r.result.i32 == 5);
692+
693+
r = e.evaluate2("j: i32 = 9");
694+
CHECK(r.ok);
695+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
696+
r = e.evaluate2("j");
697+
CHECK(r.ok);
698+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
699+
CHECK(r.result.i32 == 9);
700+
701+
r = e.evaluate2("i + j");
702+
CHECK(r.ok);
703+
CHECK(r.result.type == PythonCompiler::EvalResult::integer4);
704+
CHECK(r.result.i32 == 14);
705+
}
706+
707+
TEST_CASE("PythonCompiler i64 expressions") {
708+
CompilerOptions cu;
709+
cu.po.disable_main = true;
710+
cu.emit_debug_line_column = false;
711+
cu.generate_object_code = false;
712+
cu.interactive = true;
713+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
714+
PythonCompiler e(cu);
715+
LCompilers::Result<PythonCompiler::EvalResult>
716+
717+
r = e.evaluate2("i64(1)");
718+
CHECK(r.ok);
719+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
720+
CHECK(r.result.i64 == 1);
721+
722+
r = e.evaluate2("i64(1) + i64(2)");
723+
CHECK(r.ok);
724+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
725+
CHECK(r.result.i64 == 3);
726+
727+
r = e.evaluate2("i64(1) - i64(2)");
728+
CHECK(r.ok);
729+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
730+
CHECK(r.result.i64 == -1);
731+
732+
r = e.evaluate2("i64(1) * i64(2)");
733+
CHECK(r.ok);
734+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
735+
CHECK(r.result.i64 == 2);
736+
737+
r = e.evaluate2("i64(3) ** i64(3)");
738+
CHECK(r.ok);
739+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
740+
CHECK(r.result.i64 == 27);
741+
742+
r = e.evaluate2("i64(4) // i64(2)");
743+
CHECK(r.ok);
744+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
745+
CHECK(r.result.i64 == 2);
746+
747+
r = e.evaluate2("i64(4) / i64(2)");
748+
CHECK(r.ok);
749+
CHECK(r.result.type == PythonCompiler::EvalResult::real8);
750+
CHECK(r.result.f64 == 2);
751+
}
752+
753+
TEST_CASE("PythonCompiler i64 declaration") {
754+
CompilerOptions cu;
755+
cu.po.disable_main = true;
756+
cu.emit_debug_line_column = false;
757+
cu.generate_object_code = false;
758+
cu.interactive = true;
759+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
760+
PythonCompiler e(cu);
761+
LCompilers::Result<PythonCompiler::EvalResult>
762+
763+
r = e.evaluate2("i: i64");
764+
CHECK(r.ok);
765+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
766+
r = e.evaluate2("i = i64(5)");
767+
CHECK(r.ok);
768+
CHECK(r.result.type == PythonCompiler::EvalResult::statement);
769+
r = e.evaluate2("i");
770+
CHECK(r.ok);
771+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
772+
CHECK(r.result.i64 == 5);
773+
774+
r = e.evaluate2("j: i64 = i64(9)");
775+
CHECK(r.ok);
776+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
777+
r = e.evaluate2("j");
778+
CHECK(r.ok);
779+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
780+
CHECK(r.result.i64 == 9);
781+
782+
r = e.evaluate2("i + j");
783+
CHECK(r.ok);
784+
CHECK(r.result.type == PythonCompiler::EvalResult::integer8);
785+
CHECK(r.result.i64 == 14);
623786
}

0 commit comments

Comments
 (0)