@@ -1439,6 +1439,99 @@ ast_for_binop(struct compiling *c, const node *n)
14391439 return result ;
14401440}
14411441
1442+ static expr_ty
1443+ ast_for_trailer (struct compiling * c , const node * n , expr_ty left_expr )
1444+ {
1445+ /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1446+ expr_ty e ;
1447+ REQ (n , trailer );
1448+ if (TYPE (CHILD (n , 0 )) == LPAR ) {
1449+ if (NCH (n ) == 2 )
1450+ e = Call (left_expr , NULL , NULL , NULL , NULL , LINENO (n ));
1451+ else
1452+ e = ast_for_call (c , CHILD (n , 1 ), left_expr );
1453+ }
1454+ else if (TYPE (CHILD (n , 0 )) == LSQB ) {
1455+ REQ (CHILD (n , 2 ), RSQB );
1456+ n = CHILD (n , 1 );
1457+ if (NCH (n ) <= 2 ) {
1458+ slice_ty slc = ast_for_slice (c , CHILD (n , 0 ));
1459+ if (!slc )
1460+ return NULL ;
1461+ e = Subscript (left_expr , slc , Load , LINENO (n ));
1462+ if (!e ) {
1463+ free_slice (slc );
1464+ return NULL ;
1465+ }
1466+ }
1467+ else {
1468+ int j ;
1469+ slice_ty slc ;
1470+ asdl_seq * slices = asdl_seq_new ((NCH (n ) + 1 ) / 2 );
1471+ if (!slices )
1472+ return NULL ;
1473+ for (j = 0 ; j < NCH (n ); j += 2 ) {
1474+ slc = ast_for_slice (c , CHILD (n , j ));
1475+ if (!slc ) {
1476+ asdl_seq_free (slices );
1477+ return NULL ;
1478+ }
1479+ asdl_seq_SET (slices , j / 2 , slc );
1480+ }
1481+ e = Subscript (left_expr , ExtSlice (slices ), Load , LINENO (n ));
1482+ if (!e ) {
1483+ asdl_seq_free (slices );
1484+ return NULL ;
1485+ }
1486+ }
1487+ }
1488+ else {
1489+ assert (TYPE (CHILD (n , 0 )) == DOT );
1490+ e = Attribute (left_expr , NEW_IDENTIFIER (CHILD (n , 1 )), Load , LINENO (n ));
1491+ }
1492+ return e ;
1493+ }
1494+
1495+ static expr_ty
1496+ ast_for_power (struct compiling * c , const node * n )
1497+ {
1498+ /* power: atom trailer* ('**' factor)*
1499+ */
1500+ int i ;
1501+ expr_ty e , tmp ;
1502+ REQ (n , power );
1503+ e = ast_for_atom (c , CHILD (n , 0 ));
1504+ if (!e )
1505+ return NULL ;
1506+ if (NCH (n ) == 1 )
1507+ return e ;
1508+ for (i = 1 ; i < NCH (n ); i ++ ) {
1509+ node * ch = CHILD (n , i );
1510+ if (TYPE (ch ) != trailer )
1511+ break ;
1512+ tmp = ast_for_trailer (c , ch , e );
1513+ if (!tmp ) {
1514+ free_expr (e );
1515+ return NULL ;
1516+ }
1517+ e = tmp ;
1518+ }
1519+ if (TYPE (CHILD (n , NCH (n ) - 1 )) == factor ) {
1520+ expr_ty f = ast_for_expr (c , CHILD (n , NCH (n ) - 1 ));
1521+ if (!f ) {
1522+ free_expr (e );
1523+ return NULL ;
1524+ }
1525+ tmp = BinOp (e , Pow , f , LINENO (n ));
1526+ if (!tmp ) {
1527+ free_expr (e );
1528+ return NULL ;
1529+ }
1530+ e = tmp ;
1531+ }
1532+ return e ;
1533+ }
1534+
14421535/* Do not name a variable 'expr'! Will cause a compile error.
14431536*/
14441537
@@ -1587,97 +1680,8 @@ ast_for_expr(struct compiling *c, const node *n)
15871680 }
15881681 break ;
15891682 }
1590- case power : {
1591- expr_ty e = ast_for_atom (c , CHILD (n , 0 ));
1592- if (!e )
1593- return NULL ;
1594- if (NCH (n ) == 1 )
1595- return e ;
1596- /* power: atom trailer* ('**' factor)*
1597- trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1598-
1599- XXX What about atom trailer trailer ** factor?
1600- */
1601- for (i = 1 ; i < NCH (n ); i ++ ) {
1602- expr_ty new = e ;
1603- node * ch = CHILD (n , i );
1604- if (ch -> n_str && strcmp (ch -> n_str , "**" ) == 0 )
1605- break ;
1606- if (TYPE (CHILD (ch , 0 )) == LPAR ) {
1607- if (NCH (ch ) == 2 )
1608- new = Call (new , NULL , NULL , NULL , NULL , LINENO (ch ));
1609- else
1610- new = ast_for_call (c , CHILD (ch , 1 ), new );
1611-
1612- if (!new ) {
1613- free_expr (e );
1614- return NULL ;
1615- }
1616- }
1617- else if (TYPE (CHILD (ch , 0 )) == LSQB ) {
1618- REQ (CHILD (ch , 2 ), RSQB );
1619- ch = CHILD (ch , 1 );
1620- if (NCH (ch ) <= 2 ) {
1621- slice_ty slc = ast_for_slice (c , CHILD (ch , 0 ));
1622- if (!slc ) {
1623- free_expr (e );
1624- return NULL ;
1625- }
1626-
1627- new = Subscript (e , slc , Load , LINENO (ch ));
1628- if (!new ) {
1629- free_expr (e );
1630- free_slice (slc );
1631- return NULL ;
1632- }
1633- }
1634- else {
1635- int j ;
1636- slice_ty slc ;
1637- asdl_seq * slices = asdl_seq_new ((NCH (ch ) + 1 ) / 2 );
1638- if (!slices ) {
1639- free_expr (e );
1640- return NULL ;
1641- }
1642-
1643- for (j = 0 ; j < NCH (ch ); j += 2 ) {
1644- slc = ast_for_slice (c , CHILD (ch , j ));
1645- if (!slc ) {
1646- free_expr (e );
1647- asdl_seq_free (slices );
1648- return NULL ;
1649- }
1650- asdl_seq_SET (slices , j / 2 , slc );
1651- }
1652- new = Subscript (e , ExtSlice (slices ), Load , LINENO (ch ));
1653- if (!new ) {
1654- free_expr (e );
1655- asdl_seq_free (slices );
1656- return NULL ;
1657- }
1658- }
1659- }
1660- else {
1661- assert (TYPE (CHILD (ch , 0 )) == DOT );
1662- new = Attribute (e , NEW_IDENTIFIER (CHILD (ch , 1 )), Load ,
1663- LINENO (ch ));
1664- if (!new ) {
1665- free_expr (e );
1666- return NULL ;
1667- }
1668- }
1669- e = new ;
1670- }
1671- if (TYPE (CHILD (n , NCH (n ) - 1 )) == factor ) {
1672- expr_ty f = ast_for_expr (c , CHILD (n , NCH (n ) - 1 ));
1673- if (!f ) {
1674- free_expr (e );
1675- return NULL ;
1676- }
1677- return BinOp (e , Pow , f , LINENO (n ));
1678- }
1679- return e ;
1680- }
1683+ case power :
1684+ return ast_for_power (c , n );
16811685 default :
16821686 abort ();
16831687 PyErr_Format (PyExc_Exception , "unhandled expr: %d" , TYPE (n ));
0 commit comments