cinap_lenrekadd hg and python4bb9e2215 years agoHistory
3292 lines · 89.5 KB · C
RawDownload
    seq = asdl_seq_new(total, c->c_arena);    if (!seq)    	return NULL;    if (TYPE(CHILD(n, 0)) == simple_stmt) {	n = CHILD(n, 0);	/* simple_stmt always ends with a NEWLINE,	   and may have a trailing SEMI 	*/	end = NCH(n) - 1;	if (TYPE(CHILD(n, end - 1)) == SEMI)	    end--;        /* loop by 2 to skip semi-colons */	for (i = 0; i < end; i += 2) {	    ch = CHILD(n, i);	    s = ast_for_stmt(c, ch);	    if (!s)		return NULL;	    asdl_seq_SET(seq, pos++, s);	}    }    else {	for (i = 2; i < (NCH(n) - 1); i++) {	    ch = CHILD(n, i);	    REQ(ch, stmt);	    num = num_stmts(ch);	    if (num == 1) {		/* small_stmt or compound_stmt with only one child */		s = ast_for_stmt(c, ch);		if (!s)		    return NULL;		asdl_seq_SET(seq, pos++, s);	    }	    else {		int j;		ch = CHILD(ch, 0);		REQ(ch, simple_stmt);		for (j = 0; j < NCH(ch); j += 2) {		    /* statement terminates with a semi-colon ';' */		    if (NCH(CHILD(ch, j)) == 0) {			assert((j + 1) == NCH(ch));			break;		    }		    s = ast_for_stmt(c, CHILD(ch, j));		    if (!s)			return NULL;		    asdl_seq_SET(seq, pos++, s);		}	    }	}    }    assert(pos == seq->size);    return seq;} static stmt_tyast_for_if_stmt(struct compiling *c, const node *n){    /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*       ['else' ':' suite]    */    char *s;     REQ(n, if_stmt);     if (NCH(n) == 4) {        expr_ty expression;        asdl_seq *suite_seq;         expression = ast_for_expr(c, CHILD(n, 1));        if (!expression)            return NULL;        suite_seq = ast_for_suite(c, CHILD(n, 3));         if (!suite_seq)            return NULL;            	return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);    }     s = STR(CHILD(n, 4));    /* s[2], the third character in the string, will be       's' for el_s_e, or       'i' for el_i_f    */    if (s[2] == 's') {        expr_ty expression;        asdl_seq *seq1, *seq2;         expression = ast_for_expr(c, CHILD(n, 1));        if (!expression)            return NULL;        seq1 = ast_for_suite(c, CHILD(n, 3));        if (!seq1)            return NULL;        seq2 = ast_for_suite(c, CHILD(n, 6));        if (!seq2)            return NULL; 	return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);    }    else if (s[2] == 'i') {	int i, n_elif, has_else = 0;	expr_ty expression;	asdl_seq *suite_seq;	asdl_seq *orelse = NULL;	n_elif = NCH(n) - 4;        /* must reference the child n_elif+1 since 'else' token is third,           not fourth, child from the end. */	if (TYPE(CHILD(n, (n_elif + 1))) == NAME	    && STR(CHILD(n, (n_elif + 1)))[2] == 's') {	    has_else = 1;	    n_elif -= 3;	}	n_elif /= 4; 	if (has_else) {            asdl_seq *suite_seq2; 	    orelse = asdl_seq_new(1, c->c_arena);	    if (!orelse)		return NULL;            expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));            if (!expression)                return NULL;            suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));            if (!suite_seq)                return NULL;            suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));            if (!suite_seq2)                return NULL; 	    asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2, 				       LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,                                       c->c_arena));	    /* the just-created orelse handled the last elif */	    n_elif--;	} 	for (i = 0; i < n_elif; i++) {	    int off = 5 + (n_elif - i - 1) * 4;	    asdl_seq *newobj = asdl_seq_new(1, c->c_arena);	    if (!newobj)		return NULL;            expression = ast_for_expr(c, CHILD(n, off));            if (!expression)                return NULL;            suite_seq = ast_for_suite(c, CHILD(n, off + 2));            if (!suite_seq)                return NULL; 	    asdl_seq_SET(newobj, 0,			 If(expression, suite_seq, orelse, 			    LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));	    orelse = newobj;	}	expression = ast_for_expr(c, CHILD(n, 1));	if (!expression)		return NULL;	suite_seq = ast_for_suite(c, CHILD(n, 3));	if (!suite_seq)		return NULL;	return If(expression, suite_seq, orelse,			  LINENO(n), n->n_col_offset, c->c_arena);    }     PyErr_Format(PyExc_SystemError,                 "unexpected token in 'if' statement: %s", s);    return NULL;} static stmt_tyast_for_while_stmt(struct compiling *c, const node *n){    /* while_stmt: 'while' test ':' suite ['else' ':' suite] */    REQ(n, while_stmt);     if (NCH(n) == 4) {        expr_ty expression;        asdl_seq *suite_seq;         expression = ast_for_expr(c, CHILD(n, 1));        if (!expression)            return NULL;        suite_seq = ast_for_suite(c, CHILD(n, 3));        if (!suite_seq)            return NULL;	return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);    }    else if (NCH(n) == 7) {        expr_ty expression;        asdl_seq *seq1, *seq2;         expression = ast_for_expr(c, CHILD(n, 1));        if (!expression)            return NULL;        seq1 = ast_for_suite(c, CHILD(n, 3));        if (!seq1)            return NULL;        seq2 = ast_for_suite(c, CHILD(n, 6));        if (!seq2)            return NULL; 	return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);    }     PyErr_Format(PyExc_SystemError,                 "wrong number of tokens for 'while' statement: %d",                 NCH(n));    return NULL;} static stmt_tyast_for_for_stmt(struct compiling *c, const node *n){    asdl_seq *_target, *seq = NULL, *suite_seq;    expr_ty expression;    expr_ty target;    const node *node_target;    /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */    REQ(n, for_stmt);     if (NCH(n) == 9) {	seq = ast_for_suite(c, CHILD(n, 8));        if (!seq)            return NULL;    }     node_target = CHILD(n, 1);    _target = ast_for_exprlist(c, node_target, Store);    if (!_target)        return NULL;    /* Check the # of children rather than the length of _target, since       for x, in ... has 1 element in _target, but still requires a Tuple. */    if (NCH(node_target) == 1)	target = (expr_ty)asdl_seq_GET(_target, 0);    else	target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);     expression = ast_for_testlist(c, CHILD(n, 3));    if (!expression)        return NULL;    suite_seq = ast_for_suite(c, CHILD(n, 5));    if (!suite_seq)        return NULL;     return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,               c->c_arena);} static excepthandler_tyast_for_except_clause(struct compiling *c, const node *exc, node *body){    /* except_clause: 'except' [test [',' test]] */    REQ(exc, except_clause);    REQ(body, suite);     if (NCH(exc) == 1) {        asdl_seq *suite_seq = ast_for_suite(c, body);        if (!suite_seq)            return NULL; 	return excepthandler(NULL, NULL, suite_seq, LINENO(exc),                             exc->n_col_offset, c->c_arena);    }    else if (NCH(exc) == 2) {        expr_ty expression;        asdl_seq *suite_seq;         expression = ast_for_expr(c, CHILD(exc, 1));        if (!expression)            return NULL;        suite_seq = ast_for_suite(c, body);        if (!suite_seq)            return NULL; 	return excepthandler(expression, NULL, suite_seq, LINENO(exc),                             exc->n_col_offset, c->c_arena);    }    else if (NCH(exc) == 4) {        asdl_seq *suite_seq;        expr_ty expression;	expr_ty e = ast_for_expr(c, CHILD(exc, 3));	if (!e)            return NULL;	if (!set_context(e, Store, CHILD(exc, 3)))            return NULL;        expression = ast_for_expr(c, CHILD(exc, 1));        if (!expression)            return NULL;        suite_seq = ast_for_suite(c, body);        if (!suite_seq)            return NULL; 	return excepthandler(expression, e, suite_seq, LINENO(exc),                             exc->n_col_offset, c->c_arena);    }     PyErr_Format(PyExc_SystemError,                 "wrong number of children for 'except' clause: %d",                 NCH(exc));    return NULL;} static stmt_tyast_for_try_stmt(struct compiling *c, const node *n){    const int nch = NCH(n);    int n_except = (nch - 3)/3;    asdl_seq *body, *orelse = NULL, *finally = NULL;     REQ(n, try_stmt);     body = ast_for_suite(c, CHILD(n, 2));    if (body == NULL)        return NULL;     if (TYPE(CHILD(n, nch - 3)) == NAME) {        if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {            if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {                /* we can assume it's an "else",                   because nch >= 9 for try-else-finally and                   it would otherwise have a type of except_clause */                orelse = ast_for_suite(c, CHILD(n, nch - 4));                if (orelse == NULL)                    return NULL;                n_except--;            }             finally = ast_for_suite(c, CHILD(n, nch - 1));            if (finally == NULL)                return NULL;            n_except--;        }        else {            /* we can assume it's an "else",               otherwise it would have a type of except_clause */            orelse = ast_for_suite(c, CHILD(n, nch - 1));            if (orelse == NULL)                return NULL;            n_except--;        }    }    else if (TYPE(CHILD(n, nch - 3)) != except_clause) {        ast_error(n, "malformed 'try' statement");        return NULL;    }        if (n_except > 0) {	int i;	stmt_ty except_st;        /* process except statements to create a try ... except */        asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);        if (handlers == NULL)            return NULL;         for (i = 0; i < n_except; i++) {            excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),                                                       CHILD(n, 5 + i * 3));            if (!e)                return NULL;            asdl_seq_SET(handlers, i, e);        } 	except_st = TryExcept(body, handlers, orelse, LINENO(n),                              n->n_col_offset, c->c_arena);        if (!finally)	    return except_st;         /* if a 'finally' is present too, we nest the TryExcept within a           TryFinally to emulate try ... except ... finally */	body = asdl_seq_new(1, c->c_arena);	if (body == NULL)	    return NULL;	asdl_seq_SET(body, 0, except_st);    }     /* must be a try ... finally (except clauses are in body, if any exist) */    assert(finally != NULL);    return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);} static expr_tyast_for_with_var(struct compiling *c, const node *n){    REQ(n, with_var);    if (strcmp(STR(CHILD(n, 0)), "as") != 0) {        ast_error(n, "expected \"with [expr] as [var]\"");        return NULL;    }    return ast_for_expr(c, CHILD(n, 1));} /* with_stmt: 'with' test [ with_var ] ':' suite */static stmt_tyast_for_with_stmt(struct compiling *c, const node *n){    expr_ty context_expr, optional_vars = NULL;    int suite_index = 3;    /* skip 'with', test, and ':' */    asdl_seq *suite_seq;     assert(TYPE(n) == with_stmt);    context_expr = ast_for_expr(c, CHILD(n, 1));    if (!context_expr)        return NULL;    if (TYPE(CHILD(n, 2)) == with_var) {        optional_vars = ast_for_with_var(c, CHILD(n, 2));         if (!optional_vars) {            return NULL;        }	if (!set_context(optional_vars, Store, n)) {	    return NULL;	}        suite_index = 4;    }     suite_seq = ast_for_suite(c, CHILD(n, suite_index));    if (!suite_seq) {        return NULL;    }    return With(context_expr, optional_vars, suite_seq, LINENO(n), 		n->n_col_offset, c->c_arena);} static stmt_tyast_for_classdef(struct compiling *c, const node *n){    /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */    asdl_seq *bases, *s;        REQ(n, classdef);     if (!strcmp(STR(CHILD(n, 1)), "None")) {	    ast_error(n, "assignment to None");	    return NULL;    }     if (NCH(n) == 4) {        s = ast_for_suite(c, CHILD(n, 3));        if (!s)            return NULL;	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),                        n->n_col_offset, c->c_arena);    }    /* check for empty base list */    if (TYPE(CHILD(n,3)) == RPAR) {	s = ast_for_suite(c, CHILD(n,5));	if (!s)		return NULL;	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),                        n->n_col_offset, c->c_arena);    }     /* else handle the base class list */    bases = ast_for_class_bases(c, CHILD(n, 3));    if (!bases)        return NULL;     s = ast_for_suite(c, CHILD(n, 6));    if (!s)        return NULL;    return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),                    n->n_col_offset, c->c_arena);} static stmt_tyast_for_stmt(struct compiling *c, const node *n){    if (TYPE(n) == stmt) {	assert(NCH(n) == 1);	n = CHILD(n, 0);    }    if (TYPE(n) == simple_stmt) {	assert(num_stmts(n) == 1);	n = CHILD(n, 0);    }    if (TYPE(n) == small_stmt) {	REQ(n, small_stmt);	n = CHILD(n, 0);	/* small_stmt: expr_stmt | print_stmt  | del_stmt | pass_stmt	             | flow_stmt | import_stmt | global_stmt | exec_stmt                     | assert_stmt	*/	switch (TYPE(n)) {            case expr_stmt:                return ast_for_expr_stmt(c, n);            case print_stmt:                return ast_for_print_stmt(c, n);            case del_stmt:                return ast_for_del_stmt(c, n);            case pass_stmt:                return Pass(LINENO(n), n->n_col_offset, c->c_arena);            case flow_stmt:                return ast_for_flow_stmt(c, n);            case import_stmt:                return ast_for_import_stmt(c, n);            case global_stmt:                return ast_for_global_stmt(c, n);            case exec_stmt:                return ast_for_exec_stmt(c, n);            case assert_stmt:

Lines 2501–3000 of 3292. Previous lines