Skip to content

Commit ae8ce28

Browse files
authored
Merge pull request #618 from namannimmo10/logical_binop
Add test for binop on booleans
2 parents 0a7865e + faedbf7 commit ae8ce28

File tree

6 files changed

+48
-31
lines changed

6 files changed

+48
-31
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ RUN(NAME test_max_min LABELS cpython llvm)
167167
RUN(NAME test_integer_bitnot LABELS cpython llvm)
168168
RUN(NAME test_unary_minus LABELS cpython llvm)
169169
RUN(NAME test_unary_plus LABELS cpython llvm)
170+
RUN(NAME test_bool_binop LABELS cpython llvm)
170171
RUN(NAME test_issue_518 LABELS cpython llvm)
171172
RUN(NAME structs_01 LABELS cpython llvm)
172173

integration_tests/test_bool_binop.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from ltypes import i32, f64
2+
3+
def f():
4+
i: i32
5+
i = True + True
6+
assert i == 2
7+
8+
i = True - False
9+
assert i == 1
10+
11+
i = False * True
12+
assert i == 0
13+
14+
i = True // True
15+
assert i == 1
16+
17+
i = False // True
18+
assert i == 0
19+
20+
i = True ** True
21+
assert i == 1
22+
23+
f: f64
24+
b1: bool = False
25+
b2: bool = True
26+
f = b1/b2
27+
assert f == 0.0
28+
29+
f()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,6 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
12241224
);
12251225
throw SemanticAbort();
12261226
}
1227-
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
1228-
value = nullptr;
12291227

12301228
if (ASRUtils::is_integer(*dest_type)) {
12311229

@@ -1295,35 +1293,6 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
12951293

12961294
tmp = ASR::make_ComplexBinOp_t(al, loc, left, op, right, dest_type, value);
12971295

1298-
} else if (ASRUtils::is_logical(*dest_type)) {
1299-
1300-
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
1301-
int8_t left_value = ASR::down_cast<ASR::LogicalConstant_t>(
1302-
ASRUtils::expr_value(left))->m_value;
1303-
int8_t right_value = ASR::down_cast<ASR::LogicalConstant_t>(
1304-
ASRUtils::expr_value(right))->m_value;
1305-
int8_t result;
1306-
switch (op) {
1307-
case (ASR::binopType::Add): { result = left_value + right_value; break; }
1308-
case (ASR::binopType::Sub): { result = left_value - right_value; break; }
1309-
case (ASR::binopType::Mul): { result = left_value * right_value; break; }
1310-
case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; }
1311-
case (ASR::binopType::Div): { } // TODO: Handle division of logicals
1312-
default: { LFORTRAN_ASSERT(false); } // should never happen
1313-
}
1314-
value = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
1315-
al, loc, result, int_type));
1316-
}
1317-
1318-
// True + True -> (1 + 1)
1319-
left = ASR::down_cast<ASR::expr_t>(ASR::make_Cast_t(
1320-
al, loc, left, ASR::cast_kindType::LogicalToInteger, int_type, nullptr));
1321-
right = ASR::down_cast<ASR::expr_t>(ASR::make_Cast_t(
1322-
al, loc, right, ASR::cast_kindType::LogicalToInteger, int_type, nullptr));
1323-
1324-
// now that Logical is casted to Integer, do the binary operation on it.
1325-
tmp = ASR::make_IntegerBinOp_t(al, loc, left, op, right, int_type, value);
1326-
13271296
}
13281297

13291298
if (overloaded != nullptr) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_bool_binop-f856ef0",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/../integration_tests/test_bool_binop.py",
5+
"infile_hash": "6f578959884e05dde74f07328d3f32cb7cdf13349cc49c1e7d4d8972",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "asr-test_bool_binop-f856ef0.stdout",
9+
"stdout_hash": "da20c06dbc29523b9010516d6c1fc79748f7a5bab368b3d74c648f3a",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(TranslationUnit (SymbolTable 1 {_lpython_floordiv@__lpython_overloaded_2___lpython_floordiv: (ExternalSymbol 1 _lpython_floordiv@__lpython_overloaded_2___lpython_floordiv 4 __lpython_overloaded_2___lpython_floordiv lpython_builtin [] __lpython_overloaded_2___lpython_floordiv Public), _lpython_main_program: (Subroutine (SymbolTable 100 {}) _lpython_main_program [] [(SubroutineCall 1 f () [] ())] Source Public Implementation () .false. .false.), f: (Subroutine (SymbolTable 2 {_lpython_floordiv: (ExternalSymbol 2 _lpython_floordiv 4 _lpython_floordiv lpython_builtin [] _lpython_floordiv Private), b1: (Variable 2 b1 Local (LogicalConstant .false. (Logical 4 [])) (LogicalConstant .false. (Logical 4 [])) Default (Logical 4 []) Source Public Required .false.), b2: (Variable 2 b2 Local (LogicalConstant .true. (Logical 4 [])) (LogicalConstant .true. (Logical 4 [])) Default (Logical 4 []) Source Public Required .false.), f: (Variable 2 f Local () () Default (Real 8 []) Source Public Required .false.), i: (Variable 2 i Local () () Default (Integer 4 []) Source Public Required .false.)}) f [] [(= (Var 2 i) (IntegerBinOp (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) Add (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) (Integer 4 []) ()) ()) (Assert (Compare (Var 2 i) Eq (IntegerConstant 2 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 i) (IntegerBinOp (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) Sub (Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) (Integer 4 []) ()) ()) (Assert (Compare (Var 2 i) Eq (IntegerConstant 1 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 i) (IntegerBinOp (Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) Mul (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) (Integer 4 []) ()) ()) (Assert (Compare (Var 2 i) Eq (IntegerConstant 0 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 i) (FunctionCall 1 _lpython_floordiv@__lpython_overloaded_2___lpython_floordiv 2 _lpython_floordiv [((Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ())) ((Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()))] (Integer 4 []) () ()) ()) (Assert (Compare (Var 2 i) Eq (IntegerConstant 1 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 i) (FunctionCall 1 _lpython_floordiv@__lpython_overloaded_2___lpython_floordiv 2 _lpython_floordiv [((Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) ())) ((Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()))] (Integer 4 []) () ()) ()) (Assert (Compare (Var 2 i) Eq (IntegerConstant 0 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 i) (IntegerBinOp (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) Pow (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) ()) (Integer 4 []) ()) ()) (Assert (Compare (Var 2 i) Eq (IntegerConstant 1 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 f) (RealBinOp (Cast (Cast (Var 2 b1) LogicalToInteger (Integer 4 []) ()) IntegerToReal (Real 8 []) ()) Div (Cast (Cast (Var 2 b2) LogicalToInteger (Integer 4 []) ()) IntegerToReal (Real 8 []) ()) (Real 8 []) ()) ()) (Assert (Compare (Var 2 f) Eq (RealConstant 0.00000000000000000e+00 (Real 8 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 99 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ asr = true
270270
llvm = true
271271
cpp = true
272272

273+
[[test]]
274+
filename = "../integration_tests/test_bool_binop.py"
275+
asr = true
276+
273277
[[test]]
274278
filename = "../integration_tests/test_issue_518.py"
275279
llvm = true

0 commit comments

Comments
 (0)