@deprecated("instead of `If(cond, ...)`, use `with m.If(cond): ...`")
def __init__(self, cond, *stmts):
cond = Value.cast(cond)
- if len(cond) != 1:
+ if not cond.considered_bool():
cond = cond.bool()
super().__init__(cond, {("1",): ast.Statement.cast(stmts)})
@deprecated("instead of `.Elif(cond, ...)`, use `with m.Elif(cond): ...`")
def Elif(self, cond, *stmts):
cond = Value.cast(cond)
- if len(cond) != 1:
+ if not cond.considered_bool():
cond = cond.bool()
self.cases = OrderedDict((("-" + k,), v) for (k,), v in self.cases.items())
self.cases[("1" + "-" * len(self.test),)] = ast.Statement.cast(stmts)
def __Assign__(self, rhs, *, src_loc_at=0):
return _InternalAssign(self, rhs, src_loc_at=src_loc_at)
+ def considered_bool(self):
+ return len(self) == 1
+
def __bool__(self):
raise TypeError("Attempted to convert nMigen value to Python boolean")
tests, cases = [], OrderedDict()
for if_test, if_case in zip(if_tests + [None], if_bodies):
if if_test is not None:
- if_test = Value.cast(if_test)
- if len(if_test) != 1:
- if_test = if_test.bool()
+ # check if the if_test is not considered boolean, and only
+ # append a converted version if it is not. this defers the
+ # Value.cast (which would lose type if done mandatorially).
+ # ast._InternalSwitch does a (second) Value.cast anyway
+ _if_test = Value.cast(if_test)
+ if not _if_test.considered_bool():
+ if_test = _if_test.bool()
tests.append(if_test)
if if_test is not None: