Skip to content

Commit

Permalink
Fix direct assignments to the variables (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
a5kin committed Apr 14, 2019
1 parent b4e128b commit 4ec9b84
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions xentica/core/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def emit(self):
from xentica.core.mixins import BscaDetectorMixin
from xentica.core.exceptions import XenticaException
from xentica.core.expressions import DeferredExpression
from xentica.core.base import CellularAutomaton

__all__ = [
'Constant', 'Variable',
Expand Down Expand Up @@ -109,20 +110,29 @@ def __init__(self, val=None, name="var"):
def var_name(self):
"""Get variable name."""
all_vars = self._holder_frame.f_locals.items()
model = None
bad_names = ("self_var", "obj", "cls")
for k, var in all_vars:
if isinstance(var, CellularAutomaton):
model = var
if isinstance(var, self.__class__):
if hash(self) == hash(var) and k != "self_var":
if hash(self) == hash(var) and k not in bad_names:
return k
if model is not None and self.fallback_name == "var":
for k, var in model.__class__.__dict__.items():
if isinstance(var, self.__class__):
if hash(self) == hash(var) and k not in bad_names:
return k
return self.fallback_name

def declare_once(self):
"""Declare variable and assign initial value to it."""
if not self._declared:
if not self.bsca.is_declared(self):
code = "%s %s = %s;\n" % (
self.var_type, self.var_name, self._init_val
)
self.bsca.append_code(code)
self._declared = True
self.bsca.declare(self)
setattr(self.bsca, self.var_name, self)

def __str__(self):
Expand All @@ -137,6 +147,8 @@ def __get__(self, obj, objtype):
def __set__(self, obj, value):
"""Assign a new value to variable (doesn't work properly now)."""
self.declare_once()
if str(self.var_name) == str(value):
return
code = "%s = %s;\n" % (self.var_name, value)
self.bsca.append_code(code)

Expand Down

0 comments on commit 4ec9b84

Please sign in to comment.