@@ -1506,6 +1506,152 @@ TEST_CASE("PythonCompiler lists") {
15061506 CHECK (e.aggregate_type_to_string (r.result ) == " [\" lfortran\" , \" lpython\" , \" lc\" ]" );
15071507}
15081508
1509+ TEST_CASE (" PythonCompiler underscore 1" ) {
1510+ CompilerOptions cu;
1511+ cu.po .disable_main = true ;
1512+ cu.emit_debug_line_column = false ;
1513+ cu.generate_object_code = false ;
1514+ cu.interactive = true ;
1515+ cu.po .runtime_library_dir = LCompilers::LPython::get_runtime_library_dir ();
1516+ PythonCompiler e (cu);
1517+ LCompilers::Result<PythonCompiler::EvalResult>
1518+
1519+ r = e.evaluate2 (" 2" );
1520+ CHECK (r.ok );
1521+ CHECK (r.result .type == PythonCompiler::EvalResult::integer4);
1522+ CHECK (r.result .i32 == 2 );
1523+
1524+ r = e.evaluate2 (" _" );
1525+ CHECK (r.ok );
1526+ CHECK (r.result .type == PythonCompiler::EvalResult::integer4);
1527+ CHECK (r.result .i32 == 2 );
1528+
1529+ r = e.evaluate2 (" _ + 4" );
1530+ CHECK (r.ok );
1531+ CHECK (r.result .type == PythonCompiler::EvalResult::integer4);
1532+ CHECK (r.result .i32 == 6 );
1533+
1534+ r = e.evaluate2 (" _ * 2" );
1535+ CHECK (r.ok );
1536+ CHECK (r.result .type == PythonCompiler::EvalResult::integer4);
1537+ CHECK (r.result .i32 == 12 );
1538+ }
1539+
1540+ TEST_CASE (" PythonCompiler underscore 2" ) {
1541+ CompilerOptions cu;
1542+ cu.po .disable_main = true ;
1543+ cu.emit_debug_line_column = false ;
1544+ cu.generate_object_code = false ;
1545+ cu.interactive = true ;
1546+ cu.po .runtime_library_dir = LCompilers::LPython::get_runtime_library_dir ();
1547+ PythonCompiler e (cu);
1548+ LCompilers::Result<PythonCompiler::EvalResult>
1549+
1550+ r = e.evaluate2 (" 2" );
1551+ CHECK (r.ok );
1552+ CHECK (r.result .type == PythonCompiler::EvalResult::integer4);
1553+ CHECK (r.result .i32 == 2 );
1554+ r = e.evaluate2 (" _" );
1555+ CHECK (r.ok );
1556+ CHECK (r.result .type == PythonCompiler::EvalResult::integer4);
1557+ CHECK (r.result .i32 == 2 );
1558+
1559+ r = e.evaluate2 (" 2.5" );
1560+ CHECK (r.ok );
1561+ CHECK (r.result .type == PythonCompiler::EvalResult::real8);
1562+ CHECK (r.result .f64 == 2.5 );
1563+ r = e.evaluate2 (" _" );
1564+ CHECK (r.ok );
1565+ CHECK (r.result .type == PythonCompiler::EvalResult::real8);
1566+ CHECK (r.result .f64 == 2.5 );
1567+
1568+ r = e.evaluate2 (" \" lpython\" " );
1569+ CHECK (r.ok );
1570+ CHECK (r.result .type == PythonCompiler::EvalResult::string);
1571+ CHECK (std::strcmp (r.result .str , " lpython" ) == 0 );
1572+ r = e.evaluate2 (" _" );
1573+ CHECK (r.ok );
1574+ CHECK (r.result .type == PythonCompiler::EvalResult::string);
1575+ CHECK (std::strcmp (r.result .str , " lpython" ) == 0 );
1576+
1577+ r = e.evaluate2 (" [1, 2, 3]" );
1578+ CHECK (r.ok );
1579+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1580+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1581+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3]" );
1582+ r = e.evaluate2 (" _" );
1583+ CHECK (r.ok );
1584+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1585+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1586+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3]" );
1587+ }
1588+
1589+ TEST_CASE (" PythonCompiler underscore 3" ) {
1590+ CompilerOptions cu;
1591+ cu.po .disable_main = true ;
1592+ cu.emit_debug_line_column = false ;
1593+ cu.generate_object_code = false ;
1594+ cu.interactive = true ;
1595+ cu.po .runtime_library_dir = LCompilers::LPython::get_runtime_library_dir ();
1596+ PythonCompiler e (cu);
1597+ LCompilers::Result<PythonCompiler::EvalResult>
1598+
1599+ r = e.evaluate2 (" [1, 2, 3]" );
1600+ CHECK (r.ok );
1601+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1602+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1603+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3]" );
1604+
1605+ r = e.evaluate2 (" _ + [1, 2, 3]" );
1606+ CHECK (r.ok );
1607+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1608+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1609+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3, 1, 2, 3]" );
1610+
1611+ r = e.evaluate2 (R"(
1612+ _.append(5)
1613+ x: list[i32] = _
1614+ x
1615+ )" );
1616+ CHECK (r.ok );
1617+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1618+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1619+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3, 1, 2, 3, 5]" );
1620+ }
1621+
1622+ TEST_CASE (" PythonCompiler underscore 4" ) {
1623+ CompilerOptions cu;
1624+ cu.po .disable_main = true ;
1625+ cu.emit_debug_line_column = false ;
1626+ cu.generate_object_code = false ;
1627+ cu.interactive = true ;
1628+ cu.po .runtime_library_dir = LCompilers::LPython::get_runtime_library_dir ();
1629+ PythonCompiler e (cu);
1630+ LCompilers::Result<PythonCompiler::EvalResult>
1631+
1632+ r = e.evaluate2 (" [1, 2, 3]" );
1633+ CHECK (r.ok );
1634+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1635+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1636+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3]" );
1637+
1638+ r = e.evaluate2 (" _" );
1639+ CHECK (r.ok );
1640+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1641+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1642+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3]" );
1643+
1644+ r = e.evaluate2 (" f: bool = False" );
1645+ CHECK (r.ok );
1646+ CHECK (r.result .type == PythonCompiler::EvalResult::none);
1647+
1648+ r = e.evaluate2 (" _" );
1649+ CHECK (r.ok );
1650+ CHECK (r.result .type == PythonCompiler::EvalResult::struct_type);
1651+ CHECK (LCompilers::ASRUtils::get_type_code (r.result .structure .ttype ) == " list[i32]" );
1652+ CHECK (e.aggregate_type_to_string (r.result ) == " [1, 2, 3]" );
1653+ }
1654+
15091655TEST_CASE (" PythonCompiler asr verify 1" ) {
15101656 CompilerOptions cu;
15111657 cu.po .disable_main = true ;
0 commit comments