cinap_lenrekadd hg and python4bb9e2215 years agoHistory
3292 lines · 89.5 KB · C
RawDownload
		return NULL; 	ch = CHILD(n, 2);	if (TYPE(ch) == testlist)	    expr2 = ast_for_testlist(c, ch);	else	    expr2 = ast_for_expr(c, ch);        if (!expr2)            return NULL;         newoperator = ast_for_augassign(CHILD(n, 1));        if (!newoperator)            return NULL; 	return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);    }    else {	int i;	asdl_seq *targets;	node *value;        expr_ty expression; 	/* a normal assignment */	REQ(CHILD(n, 1), EQUAL);	targets = asdl_seq_new(NCH(n) / 2, c->c_arena);	if (!targets)	    return NULL;	for (i = 0; i < NCH(n) - 2; i += 2) {	    expr_ty e;	    node *ch = CHILD(n, i);	    if (TYPE(ch) == yield_expr) {		ast_error(ch, "assignment to yield expression not possible");		return NULL;	    }	    e = ast_for_testlist(c, ch); 	    /* set context to assign */	    if (!e) 	      return NULL; 	    if (!set_context(e, Store, CHILD(n, i)))	      return NULL; 	    asdl_seq_SET(targets, i / 2, e);	}	value = CHILD(n, NCH(n) - 1);	if (TYPE(value) == testlist)	    expression = ast_for_testlist(c, value);	else	    expression = ast_for_expr(c, value);	if (!expression)	    return NULL;	return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);    }} static stmt_tyast_for_print_stmt(struct compiling *c, const node *n){    /* print_stmt: 'print' ( [ test (',' test)* [','] ]                             | '>>' test [ (',' test)+ [','] ] )     */    expr_ty dest = NULL, expression;    asdl_seq *seq;    bool nl;    int i, j, start = 1;     REQ(n, print_stmt);    if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {	dest = ast_for_expr(c, CHILD(n, 2));        if (!dest)            return NULL;	    start = 4;    }    seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);    if (!seq)    	return NULL;    for (i = start, j = 0; i < NCH(n); i += 2, ++j) {        expression = ast_for_expr(c, CHILD(n, i));        if (!expression)            return NULL;    	asdl_seq_SET(seq, j, expression);    }    nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;    return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);} static asdl_seq *ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context){    asdl_seq *seq;    int i;    expr_ty e;     REQ(n, exprlist);     seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);    if (!seq)	return NULL;    for (i = 0; i < NCH(n); i += 2) {	e = ast_for_expr(c, CHILD(n, i));	if (!e)	    return NULL;	asdl_seq_SET(seq, i / 2, e);	if (context && !set_context(e, context, CHILD(n, i)))	    return NULL;    }    return seq;} static stmt_tyast_for_del_stmt(struct compiling *c, const node *n){    asdl_seq *expr_list;        /* del_stmt: 'del' exprlist */    REQ(n, del_stmt);     expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);    if (!expr_list)        return NULL;    return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);} static stmt_tyast_for_flow_stmt(struct compiling *c, const node *n){    /*      flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt                 | yield_stmt      break_stmt: 'break'      continue_stmt: 'continue'      return_stmt: 'return' [testlist]      yield_stmt: yield_expr      yield_expr: 'yield' testlist      raise_stmt: 'raise' [test [',' test [',' test]]]    */    node *ch;     REQ(n, flow_stmt);    ch = CHILD(n, 0);    switch (TYPE(ch)) {        case break_stmt:            return Break(LINENO(n), n->n_col_offset, c->c_arena);        case continue_stmt:            return Continue(LINENO(n), n->n_col_offset, c->c_arena);        case yield_stmt: { /* will reduce to yield_expr */	    expr_ty exp = ast_for_expr(c, CHILD(ch, 0));	    if (!exp)		return NULL;            return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);        }        case return_stmt:            if (NCH(ch) == 1)                return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);            else {                expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));                if (!expression)                    return NULL;                return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);            }        case raise_stmt:            if (NCH(ch) == 1)                return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);            else if (NCH(ch) == 2) {                expr_ty expression = ast_for_expr(c, CHILD(ch, 1));                if (!expression)                    return NULL;                return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);            }            else if (NCH(ch) == 4) {                expr_ty expr1, expr2;                 expr1 = ast_for_expr(c, CHILD(ch, 1));                if (!expr1)                    return NULL;                expr2 = ast_for_expr(c, CHILD(ch, 3));                if (!expr2)                    return NULL;                 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);            }            else if (NCH(ch) == 6) {                expr_ty expr1, expr2, expr3;                 expr1 = ast_for_expr(c, CHILD(ch, 1));                if (!expr1)                    return NULL;                expr2 = ast_for_expr(c, CHILD(ch, 3));                if (!expr2)                    return NULL;                expr3 = ast_for_expr(c, CHILD(ch, 5));                if (!expr3)                    return NULL;                                    return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);            }        default:            PyErr_Format(PyExc_SystemError,                         "unexpected flow_stmt: %d", TYPE(ch));            return NULL;    }     PyErr_SetString(PyExc_SystemError, "unhandled flow statement");    return NULL;} static alias_tyalias_for_import_name(struct compiling *c, const node *n){    /*      import_as_name: NAME ['as' NAME]      dotted_as_name: dotted_name ['as' NAME]      dotted_name: NAME ('.' NAME)*    */    PyObject *str;  loop:    switch (TYPE(n)) {        case import_as_name:            str = NULL;            if (NCH(n) == 3) {                if (strcmp(STR(CHILD(n, 1)), "as") != 0) {                    ast_error(n, "must use 'as' in import");                    return NULL;                }                str = NEW_IDENTIFIER(CHILD(n, 2));            }            return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);        case dotted_as_name:            if (NCH(n) == 1) {                n = CHILD(n, 0);                goto loop;            }            else {                alias_ty a = alias_for_import_name(c, CHILD(n, 0));                if (!a)                    return NULL;                if (strcmp(STR(CHILD(n, 1)), "as") != 0) {                    ast_error(n, "must use 'as' in import");                    return NULL;                }                assert(!a->asname);                a->asname = NEW_IDENTIFIER(CHILD(n, 2));                return a;            }            break;        case dotted_name:            if (NCH(n) == 1)                return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);            else {                /* Create a string of the form "a.b.c" */                int i;                size_t len;                char *s;                 len = 0;                for (i = 0; i < NCH(n); i += 2)                    /* length of string plus one for the dot */                    len += strlen(STR(CHILD(n, i))) + 1;                len--; /* the last name doesn't have a dot */                str = PyString_FromStringAndSize(NULL, len);                if (!str)                    return NULL;                s = PyString_AS_STRING(str);                if (!s)                    return NULL;                for (i = 0; i < NCH(n); i += 2) {                    char *sch = STR(CHILD(n, i));                    strcpy(s, STR(CHILD(n, i)));                    s += strlen(sch);                    *s++ = '.';                }                --s;                *s = '\0';                PyString_InternInPlace(&str);		PyArena_AddPyObject(c->c_arena, str);                return alias(str, NULL, c->c_arena);            }            break;        case STAR:	    str = PyString_InternFromString("*");	    PyArena_AddPyObject(c->c_arena, str);            return alias(str, NULL, c->c_arena);        default:            PyErr_Format(PyExc_SystemError,                         "unexpected import name: %d", TYPE(n));            return NULL;    }     PyErr_SetString(PyExc_SystemError, "unhandled import name condition");    return NULL;} static stmt_tyast_for_import_stmt(struct compiling *c, const node *n){    /*      import_stmt: import_name | import_from      import_name: 'import' dotted_as_names      import_from: 'from' ('.'* dotted_name | '.') 'import'                          ('*' | '(' import_as_names ')' | import_as_names)    */    int lineno;    int col_offset;    int i;    asdl_seq *aliases;     REQ(n, import_stmt);    lineno = LINENO(n);    col_offset = n->n_col_offset;    n = CHILD(n, 0);    if (TYPE(n) == import_name) {        n = CHILD(n, 1);	REQ(n, dotted_as_names);	aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);	if (!aliases)		return NULL;	for (i = 0; i < NCH(n); i += 2) {            alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));            if (!import_alias)                return NULL;	    asdl_seq_SET(aliases, i / 2, import_alias);        }	return Import(aliases, lineno, col_offset, c->c_arena);    }    else if (TYPE(n) == import_from) {        int n_children;	int idx, ndots = 0;	alias_ty mod = NULL;	identifier modname;	       /* Count the number of dots (for relative imports) and check for the          optional module name */	for (idx = 1; idx < NCH(n); idx++) {	    if (TYPE(CHILD(n, idx)) == dotted_name) {	    	mod = alias_for_import_name(c, CHILD(n, idx));	    	idx++;	    	break;	    } else if (TYPE(CHILD(n, idx)) != DOT) {	        break;	    }	    ndots++;	}	idx++; /* skip over the 'import' keyword */        switch (TYPE(CHILD(n, idx))) {        case STAR:            /* from ... import * */	    n = CHILD(n, idx);	    n_children = 1;	    if (ndots) {	        ast_error(n, "'import *' not allowed with 'from .'");	        return NULL;	    }	    break;	case LPAR:	    /* from ... import (x, y, z) */	    n = CHILD(n, idx + 1);	    n_children = NCH(n);	    break;	case import_as_names:	    /* from ... import x, y, z */	    n = CHILD(n, idx);	    n_children = NCH(n);            if (n_children % 2 == 0) {                ast_error(n, "trailing comma not allowed without"                             " surrounding parentheses");                return NULL;            }	    break;	default:	    ast_error(n, "Unexpected node-type in from-import");	    return NULL;	} 	aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);	if (!aliases)            return NULL;         /* handle "from ... import *" special b/c there's no children */        if (TYPE(n) == STAR) {            alias_ty import_alias = alias_for_import_name(c, n);            if (!import_alias)                return NULL;	        asdl_seq_SET(aliases, 0, import_alias);        }        else {    	    for (i = 0; i < NCH(n); i += 2) {                alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));                if (!import_alias)                    return NULL;	            asdl_seq_SET(aliases, i / 2, import_alias);            }        }        if (mod != NULL)            modname = mod->name;        else            modname = new_identifier("", c->c_arena);        return ImportFrom(modname, aliases, ndots, lineno, col_offset,                          c->c_arena);    }    PyErr_Format(PyExc_SystemError,                 "unknown import statement: starts with command '%s'",                 STR(CHILD(n, 0)));    return NULL;} static stmt_tyast_for_global_stmt(struct compiling *c, const node *n){    /* global_stmt: 'global' NAME (',' NAME)* */    identifier name;    asdl_seq *s;    int i;     REQ(n, global_stmt);    s = asdl_seq_new(NCH(n) / 2, c->c_arena);    if (!s)    	return NULL;    for (i = 1; i < NCH(n); i += 2) {	name = NEW_IDENTIFIER(CHILD(n, i));	if (!name)	    return NULL;	asdl_seq_SET(s, i / 2, name);    }    return Global(s, LINENO(n), n->n_col_offset, c->c_arena);} static stmt_tyast_for_exec_stmt(struct compiling *c, const node *n){    expr_ty expr1, globals = NULL, locals = NULL;    int n_children = NCH(n);    if (n_children != 2 && n_children != 4 && n_children != 6) {        PyErr_Format(PyExc_SystemError,                     "poorly formed 'exec' statement: %d parts to statement",                     n_children);        return NULL;    }     /* exec_stmt: 'exec' expr ['in' test [',' test]] */    REQ(n, exec_stmt);    expr1 = ast_for_expr(c, CHILD(n, 1));    if (!expr1)        return NULL;    if (n_children >= 4) {        globals = ast_for_expr(c, CHILD(n, 3));        if (!globals)            return NULL;    }    if (n_children == 6) {        locals = ast_for_expr(c, CHILD(n, 5));        if (!locals)            return NULL;    }     return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset, c->c_arena);} static stmt_tyast_for_assert_stmt(struct compiling *c, const node *n){    /* assert_stmt: 'assert' test [',' test] */    REQ(n, assert_stmt);    if (NCH(n) == 2) {        expr_ty expression = ast_for_expr(c, CHILD(n, 1));        if (!expression)            return NULL;	return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);    }    else if (NCH(n) == 4) {        expr_ty expr1, expr2;         expr1 = ast_for_expr(c, CHILD(n, 1));        if (!expr1)            return NULL;        expr2 = ast_for_expr(c, CHILD(n, 3));        if (!expr2)            return NULL;            	return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);    }    PyErr_Format(PyExc_SystemError,                 "improper number of parts to 'assert' statement: %d",                 NCH(n));    return NULL;} static asdl_seq *ast_for_suite(struct compiling *c, const node *n){    /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */    asdl_seq *seq;    stmt_ty s;    int i, total, num, end, pos = 0;    node *ch;     REQ(n, suite);     total = num_stmts(n);

Lines 2001–2500 of 3292. Previous lines