|
30 | 30 | 'Increment', 'Return', 'While', 'ListMajor', 'ParallelIteration', |
31 | 31 | 'ParallelBlock', 'Dereference', 'Lambda', 'SyncSpot', 'Pragma', |
32 | 32 | 'DummyExpr', 'BlankLine', 'ParallelTree', 'BusyWait', 'UsingNamespace', |
33 | | - 'Using', 'CallableBody', 'Transfer', 'EmptyList'] |
| 33 | + 'Using', 'CallableBody', 'Transfer', 'EmptyList', 'Switch'] |
34 | 34 |
|
35 | 35 | # First-class IET nodes |
36 | 36 |
|
@@ -406,6 +406,11 @@ def output(self): |
406 | 406 | """The Symbol/Indexed this Expression writes to.""" |
407 | 407 | return self.expr.lhs |
408 | 408 |
|
| 409 | + @property |
| 410 | + def rhs(self): |
| 411 | + """The right-hand side of the underlying expression.""" |
| 412 | + return self.expr.rhs |
| 413 | + |
409 | 414 | @cached_property |
410 | 415 | def reads(self): |
411 | 416 | """The Functions read by the Expression.""" |
@@ -892,6 +897,50 @@ def __repr__(self): |
892 | 897 | return "<[%s] ? [%s]" % (ccode(self.condition), repr(self.then_body)) |
893 | 898 |
|
894 | 899 |
|
| 900 | +class Switch(DoIf): |
| 901 | + |
| 902 | + """ |
| 903 | + A node to express switch-case blocks. |
| 904 | +
|
| 905 | + Parameters |
| 906 | + ---------- |
| 907 | + condition : expr-like |
| 908 | + The switch condition. |
| 909 | + cases : expr-like or list of expr-like |
| 910 | + One or more case conditions; there must be one case per node in `nodes`, |
| 911 | + plus an optional default case. |
| 912 | + nodes : Node or list of Node |
| 913 | + One or more Case nodes. |
| 914 | + default : Node, optional |
| 915 | + The default case of the switch, if any. |
| 916 | + """ |
| 917 | + |
| 918 | + _traversable = ['nodes', 'default'] |
| 919 | + |
| 920 | + def __init__(self, condition, cases, nodes, default=None): |
| 921 | + super().__init__(condition) |
| 922 | + |
| 923 | + self.cases = as_tuple(cases) |
| 924 | + self.nodes = as_tuple(nodes) |
| 925 | + self.default = default |
| 926 | + |
| 927 | + assert len(self.cases) == len(self.nodes) |
| 928 | + |
| 929 | + def __repr__(self): |
| 930 | + return "f<Switch {ccode(self.condition)}; {self.ncases} cases>" |
| 931 | + |
| 932 | + @property |
| 933 | + def ncases(self): |
| 934 | + return len(self.cases) + int(self.default is not None) |
| 935 | + |
| 936 | + @property |
| 937 | + def as_mapper(self): |
| 938 | + retval = dict(zip(self.cases, self.nodes)) |
| 939 | + if self.default: |
| 940 | + retval['default'] = self.default |
| 941 | + return retval |
| 942 | + |
| 943 | + |
895 | 944 | # Second level IET nodes |
896 | 945 |
|
897 | 946 | class TimedList(List): |
|
0 commit comments