@@ -287,6 +287,7 @@ static int com_init PROTO((struct compiling *, char *));
287287static void com_free PROTO ((struct compiling * ) );
288288static void com_done PROTO ((struct compiling * ) );
289289static void com_node PROTO ((struct compiling * , struct _node * ) );
290+ static void com_factor PROTO ((struct compiling * , struct _node * ) );
290291static void com_addbyte PROTO ((struct compiling * , int ) );
291292static void com_addint PROTO ((struct compiling * , int ) );
292293static void com_addoparg PROTO ((struct compiling * , int , int ) );
@@ -563,8 +564,16 @@ parsenumber(s)
563564 extern double atof PROTO ((const char * ) );
564565 char * end ;
565566 long x ;
567+ #ifndef WITHOUT_COMPLEX
568+ complex c ;
569+ int imflag ;
570+ #endif
571+
566572 errno = 0 ;
567573 end = s + strlen (s ) - 1 ;
574+ #ifndef WITHOUT_COMPLEX
575+ imflag = * end == 'i' || * end == 'I' || * end == 'j' || * end == 'J' ;
576+ #endif
568577 if (* end == 'l' || * end == 'L' )
569578 return long_scan (s , 0 );
570579 if (s [0 ] == '0' )
@@ -580,7 +589,15 @@ parsenumber(s)
580589 return newintobject (x );
581590 }
582591 /* XXX Huge floats may silently fail */
583- return newfloatobject (atof (s ));
592+ #ifndef WITHOUT_COMPLEX
593+ if (imflag ) {
594+ c .real = 0. ;
595+ c .imag = atof (s );
596+ return newcomplexobject (c );
597+ }
598+ else
599+ #endif
600+ return newfloatobject (atof (s ));
584601}
585602
586603static object *
@@ -810,15 +827,23 @@ com_apply_subscript(c, n)
810827 node * n ;
811828{
812829 REQ (n , subscript );
813- if (NCH (n ) == 1 && TYPE (CHILD (n , 0 )) != COLON ) {
814- /* It's a single subscript */
815- com_node (c , CHILD (n , 0 ));
816- com_addbyte (c , BINARY_SUBSCR );
817- }
818- else {
830+ if (TYPE (CHILD (n , 0 )) == COLON || (NCH (n ) > 1 && TYPE (CHILD (n , 1 )) == COLON )) {
819831 /* It's a slice: [expr] ':' [expr] */
820832 com_slice (c , n , SLICE );
821833 }
834+ else {
835+ /* It's a list of subscripts */
836+ if (NCH (n ) == 1 )
837+ com_node (c , CHILD (n , 0 ));
838+ else {
839+ int i ;
840+ int len = (NCH (n )+ 1 )/2 ;
841+ for (i = 0 ; i < NCH (n ); i += 2 )
842+ com_node (c , CHILD (n , i ));
843+ com_addoparg (c , BUILD_TUPLE , len );
844+ }
845+ com_addbyte (c , BINARY_SUBSCR );
846+ }
822847}
823848
824849static int
@@ -921,6 +946,25 @@ com_apply_trailer(c, n)
921946 }
922947}
923948
949+ static void
950+ com_power (c , n )
951+ struct compiling * c ;
952+ node * n ;
953+ {
954+ int i ;
955+ REQ (n , power );
956+ com_atom (c , CHILD (n , 0 ));
957+ for (i = 1 ; i < NCH (n ); i ++ ) {
958+ if (TYPE (CHILD (n , i )) == DOUBLESTAR ) {
959+ com_factor (c , CHILD (n , i + 1 ));
960+ com_addbyte (c , BINARY_POWER );
961+ break ;
962+ }
963+ else
964+ com_apply_trailer (c , CHILD (n , i ));
965+ }
966+ }
967+
924968static void
925969com_factor (c , n )
926970 struct compiling * c ;
@@ -941,9 +985,7 @@ com_factor(c, n)
941985 com_addbyte (c , UNARY_INVERT );
942986 }
943987 else {
944- com_atom (c , CHILD (n , 0 ));
945- for (i = 1 ; i < NCH (n ); i ++ )
946- com_apply_trailer (c , CHILD (n , i ));
988+ com_power (c , CHILD (n , 0 ));
947989 }
948990}
949991
@@ -1338,7 +1380,15 @@ com_assign_subscript(c, n, assigning)
13381380 node * n ;
13391381 int assigning ;
13401382{
1341- com_node (c , n );
1383+ if (NCH (n ) == 1 )
1384+ com_node (c , CHILD (n , 0 ));
1385+ else {
1386+ int i ;
1387+ int len = (NCH (n )+ 1 )/2 ;
1388+ for (i = 0 ; i < NCH (n ); i += 2 )
1389+ com_node (c , CHILD (n , i ));
1390+ com_addoparg (c , BUILD_TUPLE , len );
1391+ }
13421392 com_addbyte (c , assigning ? STORE_SUBSCR : DELETE_SUBSCR );
13431393}
13441394
@@ -1359,11 +1409,11 @@ com_assign_trailer(c, n, assigning)
13591409 break ;
13601410 case LSQB : /* '[' subscript ']' */
13611411 n = CHILD (n , 1 );
1362- REQ (n , subscript ); /* subscript: expr | [expr] ':' [expr] */
1363- if (NCH (n ) > 1 || TYPE (CHILD (n , 0 )) == COLON )
1412+ REQ (n , subscript ); /* subscript: expr (',' expr)* | [expr] ':' [expr] */
1413+ if (TYPE ( CHILD ( n , 0 )) == COLON || ( NCH (n ) > 1 && TYPE (CHILD (n , 1 )) == COLON ) )
13641414 com_assign_slice (c , n , assigning );
13651415 else
1366- com_assign_subscript (c , CHILD ( n , 0 ) , assigning );
1416+ com_assign_subscript (c , n , assigning );
13671417 break ;
13681418 default :
13691419 err_setstr (SystemError , "unknown trailer type" );
@@ -1438,6 +1488,7 @@ com_assign(c, n, assigning)
14381488 case shift_expr :
14391489 case arith_expr :
14401490 case term :
1491+ case factor :
14411492 if (NCH (n ) > 1 ) {
14421493 err_setstr (SyntaxError ,
14431494 "can't assign to operator" );
@@ -1447,17 +1498,24 @@ com_assign(c, n, assigning)
14471498 n = CHILD (n , 0 );
14481499 break ;
14491500
1450- case factor : /* ('+'|'-'|'~') factor | atom trailer* */
1451- if (TYPE (CHILD (n , 0 )) != atom ) { /* '+'|'-'|'~' */
1501+ case power : /* atom trailer* ('**' power)* */
1502+ /* ('+'|'-'|'~') factor | atom trailer* */
1503+ if (TYPE (CHILD (n , 0 )) != atom ) {
14521504 err_setstr (SyntaxError ,
14531505 "can't assign to operator" );
14541506 c -> c_errors ++ ;
14551507 return ;
14561508 }
1457- if (NCH (n ) > 1 ) { /* trailer present */
1509+ if (NCH (n ) > 1 ) { /* trailer or exponent present */
14581510 int i ;
14591511 com_node (c , CHILD (n , 0 ));
14601512 for (i = 1 ; i + 1 < NCH (n ); i ++ ) {
1513+ if (TYPE (CHILD (n , i )) == DOUBLESTAR ) {
1514+ err_setstr (SyntaxError ,
1515+ "can't assign to operator" );
1516+ c -> c_errors ++ ;
1517+ return ;
1518+ }
14611519 com_apply_trailer (c , CHILD (n , i ));
14621520 } /* NB i is still alive */
14631521 com_assign_trailer (c ,
@@ -2059,6 +2117,7 @@ get_docstring(n)
20592117 case arith_expr :
20602118 case term :
20612119 case factor :
2120+ case power :
20622121 if (NCH (n ) == 1 )
20632122 return get_docstring (CHILD (n , 0 ));
20642123 break ;
@@ -2136,7 +2195,7 @@ com_argdefs(c, n)
21362195 ndefs = 0 ;
21372196 for (i = 0 ; i < nch ; i ++ ) {
21382197 int t ;
2139- if (TYPE (CHILD (n , i )) == STAR )
2198+ if (TYPE (CHILD (n , i )) == STAR || TYPE ( CHILD ( n , i )) == DOUBLESTAR )
21402199 break ;
21412200 nargs ++ ;
21422201 i ++ ;
@@ -2373,6 +2432,9 @@ com_node(c, n)
23732432 case factor :
23742433 com_factor (c , n );
23752434 break ;
2435+ case power :
2436+ com_power (c , n );
2437+ break ;
23762438 case atom :
23772439 com_atom (c , n );
23782440 break ;
@@ -2431,7 +2493,7 @@ com_arglist(c, n)
24312493 node * ch = CHILD (n , i );
24322494 node * fp ;
24332495 char * name ;
2434- if (TYPE (ch ) == STAR )
2496+ if (TYPE (ch ) == STAR || TYPE ( ch ) == DOUBLESTAR )
24352497 break ;
24362498 REQ (ch , fpdef ); /* fpdef: NAME | '(' fplist ')' */
24372499 fp = CHILD (ch , 0 );
@@ -2455,22 +2517,28 @@ com_arglist(c, n)
24552517 if (i < nch ) {
24562518 node * ch ;
24572519 ch = CHILD (n , i );
2458- REQ (ch , STAR );
2459- ch = CHILD (n , i + 1 );
2460- if (TYPE (ch ) == NAME ) {
2461- c -> c_flags |= CO_VARARGS ;
2462- i += 3 ;
2463- com_newlocal (c , STR (ch ));
2520+ if (TYPE (ch ) != DOUBLESTAR ) {
2521+ REQ (ch , STAR );
2522+ ch = CHILD (n , i + 1 );
2523+ if (TYPE (ch ) == NAME ) {
2524+ c -> c_flags |= CO_VARARGS ;
2525+ i += 3 ;
2526+ com_newlocal (c , STR (ch ));
2527+ }
24642528 }
24652529 }
24662530 /* Handle **keywords */
24672531 if (i < nch ) {
24682532 node * ch ;
24692533 ch = CHILD (n , i );
2470- REQ (ch , STAR );
2471- ch = CHILD (n , i + 1 );
2472- REQ (ch , STAR );
2473- ch = CHILD (n , i + 2 );
2534+ if (TYPE (ch ) != DOUBLESTAR ) {
2535+ REQ (ch , STAR );
2536+ ch = CHILD (n , i + 1 );
2537+ REQ (ch , STAR );
2538+ ch = CHILD (n , i + 2 );
2539+ }
2540+ else
2541+ ch = CHILD (n , i + 1 );
24742542 REQ (ch , NAME );
24752543 c -> c_flags |= CO_VARKEYWORDS ;
24762544 com_newlocal (c , STR (ch ));
@@ -2482,7 +2550,7 @@ com_arglist(c, n)
24822550 for (i = 0 ; i < nch ; i ++ ) {
24832551 node * ch = CHILD (n , i );
24842552 node * fp ;
2485- if (TYPE (ch ) == STAR )
2553+ if (TYPE (ch ) == STAR || TYPE ( ch ) == DOUBLESTAR )
24862554 break ;
24872555 REQ (ch , fpdef ); /* fpdef: NAME | '(' fplist ')' */
24882556 fp = CHILD (ch , 0 );
0 commit comments