Skip to content

Commit 290c2b3

Browse files
support printing boolean in REPL (#2728)
* support printing `boolean` in REPL * fix failing test (wrongly written test)
1 parent 8aadf8c commit 290c2b3

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

src/bin/lpython.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,12 @@ int interactive_python_repl(
986986
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
987987
break;
988988
}
989+
case (LCompilers::PythonCompiler::EvalResult::boolean) : {
990+
if (verbose) std::cout << "Return type: logical" << std::endl;
991+
if (verbose) section("Result:");
992+
std::cout << (r.b ? "True" : "False") << std::endl;
993+
break;
994+
}
989995
case (LCompilers::PythonCompiler::EvalResult::string) : {
990996
if (verbose) std::cout << "Return type: str" << std::endl;
991997
if (verbose) section("Result:");

src/libasr/codegen/evaluator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ std::string LLVMModule::get_return_type(const std::string &fn_name)
102102
return "real4";
103103
} else if (type->isDoubleTy()) {
104104
return "real8";
105+
} else if (type->isIntegerTy(1)) {
106+
return "logical";
105107
} else if (type->isIntegerTy(8)) {
106108
return "integer1";
107109
} else if (type->isIntegerTy(16)) {

src/libasr/pass/global_stmts.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ void pass_wrap_global_stmts(Allocator &al,
7979
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
8080
target = return_var_ref;
8181
idx++;
82+
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Logical) {
83+
s.from_str(al, fn_name_s + std::to_string(idx));
84+
var_name = s.c_str(al);
85+
86+
int a_kind = down_cast<ASR::Logical_t>(ASRUtils::expr_type(value))->m_kind;
87+
88+
type = ASRUtils::TYPE(ASR::make_Logical_t(al, loc, a_kind));
89+
return_var = ASR::make_Variable_t(al, loc,
90+
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
91+
ASR::storage_typeType::Default, type,
92+
nullptr, ASR::abiType::BindC,
93+
ASR::Public, ASR::presenceType::Required, false);
94+
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
95+
down_cast<ASR::symbol_t>(return_var)));
96+
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
97+
target = return_var_ref;
98+
idx++;
8299
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Real) {
83100
s.from_str(al, fn_name_s + std::to_string(idx));
84101
var_name = s.c_str(al);

src/lpython/python_evaluator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
208208
result.type = EvalResult::complex8;
209209
result.c64.re = r.real();
210210
result.c64.im = r.imag();
211+
} else if (return_type == "logical") {
212+
bool r = e->execfn<bool>(run_fn);
213+
result.type = EvalResult::boolean;
214+
result.b = r;
211215
} else if (return_type == "void") {
212216
e->execfn<void>(run_fn);
213217
result.type = EvalResult::statement;

src/lpython/python_evaluator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class PythonCompiler
4949
real8,
5050
complex4,
5151
complex8,
52+
boolean,
5253
string,
5354
statement,
5455
none
@@ -58,6 +59,7 @@ class PythonCompiler
5859
int64_t i64;
5960
uint32_t u32;
6061
uint64_t u64;
62+
bool b;
6163
float f32;
6264
double f64;
6365
char *str;

src/lpython/tests/test_llvm.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,77 @@ TEST_CASE("PythonCompiler u16 declaration") {
12921292
CHECK(r.result.u32 == 45);
12931293
}
12941294

1295+
TEST_CASE("PythonCompiler boolean expressions") {
1296+
CompilerOptions cu;
1297+
cu.po.disable_main = true;
1298+
cu.emit_debug_line_column = false;
1299+
cu.generate_object_code = false;
1300+
cu.interactive = true;
1301+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
1302+
PythonCompiler e(cu);
1303+
LCompilers::Result<PythonCompiler::EvalResult>
1304+
1305+
r = e.evaluate2("True");
1306+
CHECK(r.ok);
1307+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1308+
CHECK(r.result.b);
1309+
1310+
r = e.evaluate2("False");
1311+
CHECK(r.ok);
1312+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1313+
CHECK(!r.result.b);
1314+
1315+
r = e.evaluate2("False or True");
1316+
CHECK(r.ok);
1317+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1318+
CHECK(r.result.b);
1319+
1320+
r = e.evaluate2("False and True");
1321+
CHECK(r.ok);
1322+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1323+
CHECK(!r.result.b);
1324+
}
1325+
1326+
TEST_CASE("PythonCompiler boolean declaration") {
1327+
CompilerOptions cu;
1328+
cu.po.disable_main = true;
1329+
cu.emit_debug_line_column = false;
1330+
cu.generate_object_code = false;
1331+
cu.interactive = true;
1332+
cu.po.runtime_library_dir = LCompilers::LPython::get_runtime_library_dir();
1333+
PythonCompiler e(cu);
1334+
LCompilers::Result<PythonCompiler::EvalResult>
1335+
1336+
r = e.evaluate2("t: bool");
1337+
CHECK(r.ok);
1338+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
1339+
r = e.evaluate2("t = True");
1340+
CHECK(r.ok);
1341+
CHECK(r.result.type == PythonCompiler::EvalResult::statement);
1342+
r = e.evaluate2("t");
1343+
CHECK(r.ok);
1344+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1345+
CHECK(r.result.b);
1346+
1347+
r = e.evaluate2("f: bool = False");
1348+
CHECK(r.ok);
1349+
CHECK(r.result.type == PythonCompiler::EvalResult::none);
1350+
r = e.evaluate2("f");
1351+
CHECK(r.ok);
1352+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1353+
CHECK(!r.result.b);
1354+
1355+
r = e.evaluate2("t or f");
1356+
CHECK(r.ok);
1357+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1358+
CHECK(r.result.b);
1359+
1360+
r = e.evaluate2("t and f");
1361+
CHECK(r.ok);
1362+
CHECK(r.result.type == PythonCompiler::EvalResult::boolean);
1363+
CHECK(!r.result.b);
1364+
}
1365+
12951366
TEST_CASE("PythonCompiler string 1") {
12961367
CompilerOptions cu;
12971368
cu.po.disable_main = true;

0 commit comments

Comments
 (0)