Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify stmt and expr #1232

Open
certik opened this issue Oct 27, 2022 · 1 comment
Open

Unify stmt and expr #1232

certik opened this issue Oct 27, 2022 · 1 comment

Comments

@certik
Copy link
Contributor

certik commented Oct 27, 2022

Currently in Python we can have code as follows:

def g():
    print("Something")
    return 3

def f():
    2 + g()
    return 42

f()

Which will print "Something".

This is currently represented in the compiler roughly as:

(= 
                        (Var 3 __lcompilers_dummy) 
                        (FunctionCall 
                            1 g 
                            () 
                            [] 
                            (Integer 4 []) 
                            () 
                            ()
                        ) 
                        ()
                    )

If we only have expressions, then the above will not be an assignment, but just the expression (2+g() or g()) and we will not use the result.

@certik
Copy link
Contributor Author

certik commented Oct 27, 2022

However, one downside is that in expressions like 2+g() suddenly some Expr nodes (the former Stmt nodes) would not be allowed. That is not very consistent. Also, many times we accept an expression, say for dimensions, indexing, or loop begin/end, etc. We would have to add verify() checks for each case that it is not a former Stmt node, and handle the case if it is (or assert, etc.).

Here is an alternative solution. Let's add an Expr(expr expression) node into stmt. This is exactly what Python does at the AST level. Then a list of statements can contain an Expr node, and it tells the backend that one should drop the result. Currently we use the Assignment() node with a dummy variable for that. So an Expr node is an iterative improvement of this idea. It cleans things up, and either it will be good enough, or if not, we can still unify Expr and Stmt later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant