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

Problem when declaring variables with same name in different scopes (e.g. two if/else branches) #21

Open
stklik opened this issue Jul 25, 2019 · 1 comment

Comments

@stklik
Copy link
Member

stklik commented Jul 25, 2019

The following code causes issues in the Z3-translator, because the variable light is declared in both* branches. To solve, we have to declare the variable outside.

In fact, the current implementation forbids any two variables with the same name.

@update(state = state, target = light_Out)
def update_light_out(self, dt):
    # FIX: write light = 0 here
    if(self.time_Local.value%1440 > 21*60):
        light = 0
    elif(self.time_Local.value%1440 > 16*60):
        light = 20000*(21*60-self.time_Local.value)//60 #On veut 20000*un nombre flottant entre 0 et 5
    elif(self.time_Local.value%1440 > 12*60):
       light = 100000
    elif(self.time_Local.value%1440 > 7*60):
        light = 20000*(abs(self.time_Local.value - 7*60))//60  #On veut 20000*un nombre flottant entre 0 et 5
    else:
       light = 0
        
    return light

Solution: The current solution is to declare a variable in an outside scope (e.g. light = 0 on line 3). Thus, the original variable will be reassigned, instead of declared again.

Potential issues: This doesn't work if you want to have the same variable name for values of different type (e.g. one light of type REAL and one of type string).

  • The AST treats elif ...: as else: if ... :
@stklik stklik changed the title Declaration of variable with same name in two branches Problem when declaring variables with same name in different scopes (e.g. two if/else branches) Jul 25, 2019
@stklik
Copy link
Member Author

stklik commented Jul 25, 2019

Original Bugreport (email from @gmarthe):

Dans le fichier SmartHome, la cellule ElectricPart affiche des erreurs incompréhensible
(On a vérifié ensemble et il n'est écrit nulle part sun.light, donc la premiere erreur de la cellule n'a pas lieu d’être)

Notebooks.zip

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

No branches or pull requests

1 participant