Rename elif in python

I recently played around a bit with the addition of some optional operator names in python, and this worked fine until I got to the if statement, and added the optional name for else and elif:

if_stmt = 'if' test ':' suite ('elif' test ':'suite)* ['else' ':' suite] |  'wenn' test ':' suite ('andernfalls' test ':'suite)* ['sonst' ':' suite]

then it compiled without errors, but when I ran the test with wenn and andernfalls, and the interpreter threw the error:

SystemError: unexpected token in 'if' statement: andernfalls 

And every addition I made did a great job. So why can't I add another and elif and how could I do this?

I am changing the most recent python 2.7 code from the python website.

EDIT My test code:

x = 1
y = 2
wenn x > y:
    print 1
andernfalls x < y:
    print 2
sonst:
    print 3

The file I added the line at the top, it was a grammar file in the grammar directory of the python source code.

+4
source share
1

else elif AST:

static stmt_ty
ast_for_if_stmt(struct compiling *c, const node *n)
{
    ...
    /* 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') {
        ...
    }
    else if (s[2] == 'i') {
        ...
        if (TYPE(CHILD(n, (n_elif + 1))) == NAME
            && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
            ...

ast_for_if_stmt Python/ast.c, .

+6

Source: https://habr.com/ru/post/1684330/


All Articles