@@ -183,7 +183,7 @@ mod_ty
183
183
PyAST_FromNode (const node * n , PyCompilerFlags * flags , const char * filename ,
184
184
PyArena * arena )
185
185
{
186
- int i , j , num ;
186
+ int i , j , k , num ;
187
187
asdl_seq * stmts = NULL ;
188
188
stmt_ty s ;
189
189
node * ch ;
@@ -199,6 +199,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
199
199
}
200
200
c .c_arena = arena ;
201
201
202
+ k = 0 ;
202
203
switch (TYPE (n )) {
203
204
case file_input :
204
205
stmts = asdl_seq_new (num_stmts (n ), arena );
@@ -214,7 +215,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
214
215
s = ast_for_stmt (& c , ch );
215
216
if (!s )
216
217
goto error ;
217
- asdl_seq_APPEND (stmts , s );
218
+ asdl_seq_SET (stmts , k ++ , s );
218
219
}
219
220
else {
220
221
ch = CHILD (ch , 0 );
@@ -223,7 +224,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
223
224
s = ast_for_stmt (& c , CHILD (ch , j * 2 ));
224
225
if (!s )
225
226
goto error ;
226
- asdl_seq_APPEND (stmts , s );
227
+ asdl_seq_SET (stmts , k ++ , s );
227
228
}
228
229
}
229
230
}
@@ -314,15 +315,11 @@ get_operator(const node *n)
314
315
}
315
316
}
316
317
317
- /* Set the context ctx for expr_ty e returning 1 on success, 0 on error .
318
+ /* Set the context ctx for expr_ty e, recursively traversing e .
318
319
319
320
Only sets context for expr kinds that "can appear in assignment context"
320
321
(according to ../Parser/Python.asdl). For other expr kinds, it sets
321
322
an appropriate syntax error and returns false.
322
-
323
- If e is a sequential type, items in sequence will also have their context
324
- set.
325
-
326
323
*/
327
324
328
325
static int
@@ -346,7 +343,7 @@ set_context(expr_ty e, expr_context_ty ctx, const node *n)
346
343
switch (e -> kind ) {
347
344
case Attribute_kind :
348
345
if (ctx == Store &&
349
- !strcmp (PyString_AS_STRING (e -> v .Attribute .attr ), "None" )) {
346
+ !strcmp (PyString_AS_STRING (e -> v .Attribute .attr ), "None" )) {
350
347
return ast_error (n , "assignment to None" );
351
348
}
352
349
e -> v .Attribute .ctx = ctx ;
@@ -416,7 +413,7 @@ set_context(expr_ty e, expr_context_ty ctx, const node *n)
416
413
}
417
414
418
415
/* If the LHS is a list or tuple, we need to set the assignment
419
- context for all the tuple elements.
416
+ context for all the contained elements.
420
417
*/
421
418
if (s ) {
422
419
int i ;
@@ -559,34 +556,31 @@ compiler_complex_args(struct compiling *c, const node *n)
559
556
return NULL ;
560
557
561
558
REQ (n , fplist );
562
-
563
559
for (i = 0 ; i < len ; i ++ ) {
564
560
const node * child = CHILD (CHILD (n , 2 * i ), 0 );
565
561
expr_ty arg ;
566
562
if (TYPE (child ) == NAME ) {
567
- if (!strcmp (STR (child ), "None" )) {
568
- ast_error (child , "assignment to None" );
569
- return NULL ;
570
- }
563
+ if (!strcmp (STR (child ), "None" )) {
564
+ ast_error (child , "assignment to None" );
565
+ return NULL ;
566
+ }
571
567
arg = Name (NEW_IDENTIFIER (child ), Store , LINENO (child ),
572
568
c -> c_arena );
573
- }
574
- else
569
+ }
570
+ else {
575
571
arg = compiler_complex_args (c , CHILD (CHILD (n , 2 * i ), 1 ));
576
- set_context ( arg , Store , n );
572
+ }
577
573
asdl_seq_SET (args , i , arg );
578
574
}
579
575
580
576
result = Tuple (args , Store , LINENO (n ), c -> c_arena );
581
- set_context (result , Store , n );
577
+ if (!set_context (result , Store , n ))
578
+ return NULL ;
582
579
return result ;
583
580
}
584
581
585
- /* Create AST for argument list.
586
582
587
- XXX TO DO:
588
- - check for invalid argument lists like normal after default
589
- */
583
+ /* Create AST for argument list. */
590
584
591
585
static arguments_ty
592
586
ast_for_arguments (struct compiling * c , const node * n )
@@ -595,7 +589,7 @@ ast_for_arguments(struct compiling *c, const node *n)
595
589
varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
596
590
| '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
597
591
*/
598
- int i , n_args = 0 , n_defaults = 0 , found_default = 0 ;
592
+ int i , j , k , n_args = 0 , n_defaults = 0 , found_default = 0 ;
599
593
asdl_seq * args , * defaults ;
600
594
identifier vararg = NULL , kwarg = NULL ;
601
595
node * ch ;
@@ -626,6 +620,8 @@ ast_for_arguments(struct compiling *c, const node *n)
626
620
fplist: fpdef (',' fpdef)* [',']
627
621
*/
628
622
i = 0 ;
623
+ j = 0 ; /* index for defaults */
624
+ k = 0 ; /* index for args */
629
625
while (i < NCH (n )) {
630
626
ch = CHILD (n , i );
631
627
switch (TYPE (ch )) {
@@ -634,7 +630,7 @@ ast_for_arguments(struct compiling *c, const node *n)
634
630
anything other than EQUAL or a comma? */
635
631
/* XXX Should NCH(n) check be made a separate check? */
636
632
if (i + 1 < NCH (n ) && TYPE (CHILD (n , i + 1 )) == EQUAL ) {
637
- asdl_seq_APPEND (defaults ,
633
+ asdl_seq_SET (defaults , j ++ ,
638
634
ast_for_expr (c , CHILD (n , i + 2 )));
639
635
i += 2 ;
640
636
found_default = 1 ;
@@ -644,9 +640,8 @@ ast_for_arguments(struct compiling *c, const node *n)
644
640
"non-default argument follows default argument" );
645
641
goto error ;
646
642
}
647
-
648
643
if (NCH (ch ) == 3 ) {
649
- asdl_seq_APPEND (args ,
644
+ asdl_seq_SET (args , k ++ ,
650
645
compiler_complex_args (c , CHILD (ch , 1 )));
651
646
}
652
647
else if (TYPE (CHILD (ch , 0 )) == NAME ) {
@@ -659,7 +654,7 @@ ast_for_arguments(struct compiling *c, const node *n)
659
654
Param , LINENO (ch ), c -> c_arena );
660
655
if (!name )
661
656
goto error ;
662
- asdl_seq_APPEND (args , name );
657
+ asdl_seq_SET (args , k ++ , name );
663
658
664
659
}
665
660
i += 2 ; /* the name and the comma */
@@ -767,16 +762,15 @@ ast_for_decorators(struct compiling *c, const node *n)
767
762
int i ;
768
763
769
764
REQ (n , decorators );
770
-
771
765
decorator_seq = asdl_seq_new (NCH (n ), c -> c_arena );
772
766
if (!decorator_seq )
773
767
return NULL ;
774
768
775
769
for (i = 0 ; i < NCH (n ); i ++ ) {
776
- d = ast_for_decorator (c , CHILD (n , i ));
777
- if (!d )
778
- return NULL ;
779
- asdl_seq_APPEND (decorator_seq , d );
770
+ d = ast_for_decorator (c , CHILD (n , i ));
771
+ if (!d )
772
+ return NULL ;
773
+ asdl_seq_SET (decorator_seq , i , d );
780
774
}
781
775
return decorator_seq ;
782
776
}
@@ -993,21 +987,20 @@ ast_for_listcomp(struct compiling *c, const node *n)
993
987
return NULL ;
994
988
995
989
for (j = 0 ; j < n_ifs ; j ++ ) {
996
- REQ (ch , list_iter );
997
-
998
- ch = CHILD (ch , 0 );
999
- REQ (ch , list_if );
1000
-
1001
- asdl_seq_APPEND (ifs , ast_for_expr (c , CHILD (ch , 1 )));
1002
- if (NCH (ch ) == 3 )
1003
- ch = CHILD (ch , 2 );
1004
- }
1005
- /* on exit, must guarantee that ch is a list_for */
1006
- if (TYPE (ch ) == list_iter )
1007
- ch = CHILD (ch , 0 );
990
+ REQ (ch , list_iter );
991
+ ch = CHILD (ch , 0 );
992
+ REQ (ch , list_if );
993
+
994
+ asdl_seq_SET (ifs , j , ast_for_expr (c , CHILD (ch , 1 )));
995
+ if (NCH (ch ) == 3 )
996
+ ch = CHILD (ch , 2 );
997
+ }
998
+ /* on exit, must guarantee that ch is a list_for */
999
+ if (TYPE (ch ) == list_iter )
1000
+ ch = CHILD (ch , 0 );
1008
1001
lc -> ifs = ifs ;
1009
- }
1010
- asdl_seq_APPEND (listcomps , lc );
1002
+ }
1003
+ asdl_seq_SET (listcomps , i , lc );
1011
1004
}
1012
1005
1013
1006
return ListComp (elt , listcomps , LINENO (n ), c -> c_arena );
@@ -1075,6 +1068,7 @@ count_gen_ifs(const node *n)
1075
1068
}
1076
1069
}
1077
1070
1071
+ /* TODO(jhylton): Combine with list comprehension code? */
1078
1072
static expr_ty
1079
1073
ast_for_genexp (struct compiling * c , const node * n )
1080
1074
{
@@ -1146,7 +1140,7 @@ ast_for_genexp(struct compiling *c, const node *n)
1146
1140
expression = ast_for_expr (c , CHILD (ch , 1 ));
1147
1141
if (!expression )
1148
1142
return NULL ;
1149
- asdl_seq_APPEND (ifs , expression );
1143
+ asdl_seq_SET (ifs , j , expression );
1150
1144
if (NCH (ch ) == 3 )
1151
1145
ch = CHILD (ch , 2 );
1152
1146
}
@@ -1155,7 +1149,7 @@ ast_for_genexp(struct compiling *c, const node *n)
1155
1149
ch = CHILD (ch , 0 );
1156
1150
ge -> ifs = ifs ;
1157
1151
}
1158
- asdl_seq_APPEND (genexps , ge );
1152
+ asdl_seq_SET (genexps , i , ge );
1159
1153
}
1160
1154
1161
1155
return GeneratorExp (elt , genexps , LINENO (n ), c -> c_arena );
@@ -1948,24 +1942,23 @@ ast_for_print_stmt(struct compiling *c, const node *n)
1948
1942
expr_ty dest = NULL , expression ;
1949
1943
asdl_seq * seq ;
1950
1944
bool nl ;
1951
- int i , start = 1 ;
1945
+ int i , j , start = 1 ;
1952
1946
1953
1947
REQ (n , print_stmt );
1954
1948
if (NCH (n ) >= 2 && TYPE (CHILD (n , 1 )) == RIGHTSHIFT ) {
1955
1949
dest = ast_for_expr (c , CHILD (n , 2 ));
1956
1950
if (!dest )
1957
1951
return NULL ;
1958
- start = 4 ;
1952
+ start = 4 ;
1959
1953
}
1960
1954
seq = asdl_seq_new ((NCH (n ) + 1 - start ) / 2 , c -> c_arena );
1961
1955
if (!seq )
1962
- return NULL ;
1963
- for (i = start ; i < NCH (n ); i += 2 ) {
1956
+ return NULL ;
1957
+ for (i = start , j = 0 ; i < NCH (n ); i += 2 , ++ j ) {
1964
1958
expression = ast_for_expr (c , CHILD (n , i ));
1965
1959
if (!expression )
1966
1960
return NULL ;
1967
-
1968
- asdl_seq_APPEND (seq , expression );
1961
+ asdl_seq_SET (seq , j , expression );
1969
1962
}
1970
1963
nl = (TYPE (CHILD (n , NCH (n ) - 1 )) == COMMA ) ? false : true;
1971
1964
return Print (dest , seq , nl , LINENO (n ), c -> c_arena );
@@ -2252,14 +2245,15 @@ ast_for_import_stmt(struct compiling *c, const node *n)
2252
2245
alias_ty import_alias = alias_for_import_name (c , n );
2253
2246
if (!import_alias )
2254
2247
return NULL ;
2255
- asdl_seq_APPEND (aliases , import_alias );
2248
+ asdl_seq_SET (aliases , 0 , import_alias );
2256
2249
}
2257
-
2258
- for (i = 0 ; i < NCH (n ); i += 2 ) {
2259
- alias_ty import_alias = alias_for_import_name (c , CHILD (n , i ));
2260
- if (!import_alias )
2261
- return NULL ;
2262
- asdl_seq_APPEND (aliases , import_alias );
2250
+ else {
2251
+ for (i = 0 ; i < NCH (n ); i += 2 ) {
2252
+ alias_ty import_alias = alias_for_import_name (c , CHILD (n , i ));
2253
+ if (!import_alias )
2254
+ return NULL ;
2255
+ asdl_seq_SET (aliases , i / 2 , import_alias );
2256
+ }
2263
2257
}
2264
2258
if (mod != NULL )
2265
2259
modname = mod -> name ;
0 commit comments