Skip to content

Commit d94a1b0

Browse files
allightcopybara-github
authored andcommitted
Avoid using match in python code
PiperOrigin-RevId: 631280405
1 parent 5cb393f commit d94a1b0

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

xls/jit/jit_wrapper_generator_main.py

+62-49
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from collections.abc import Sequence
2121
import dataclasses
2222
import subprocess
23+
from typing import Optional
2324

2425
from absl import app
2526
from absl import flags
@@ -90,7 +91,7 @@ class XlsParam:
9091
name: str
9192
packed_type: str
9293
unpacked_type: str
93-
specialized_type: str | None
94+
specialized_type: Optional[str]
9495

9596
@property
9697
def value_arg(self):
@@ -135,41 +136,46 @@ def params_and_result(self):
135136

136137

137138
def to_packed(t: type_pb2.TypeProto) -> str:
138-
match t.type_enum:
139-
case type_pb2.TypeProto.BITS:
140-
return f"xls::PackedBitsView<{t.bit_count}>"
141-
case type_pb2.TypeProto.TUPLE:
142-
inner = ", ".join(to_packed(e) for e in t.tuple_elements)
143-
return f"xls::PackedTupleView<{inner}>"
144-
case type_pb2.TypeProto.ARRAY:
145-
return (
146-
f"xls::PackedArrayView<{to_packed(t.array_element)}, {t.array_size}>"
147-
)
148-
case _:
149-
raise app.UsageError(
150-
"Incompatible with argument of type:"
151-
f" {type_pb2.TypeProto.TypeEnum.Name(t.type_enum)}"
152-
)
139+
the_type = t.type_enum
140+
if the_type == type_pb2.TypeProto.BITS:
141+
return f"xls::PackedBitsView<{t.bit_count}>"
142+
elif the_type == type_pb2.TypeProto.TUPLE:
143+
inner = ", ".join(to_packed(e) for e in t.tuple_elements)
144+
return f"xls::PackedTupleView<{inner}>"
145+
elif the_type == type_pb2.TypeProto.ARRAY:
146+
return f"xls::PackedArrayView<{to_packed(t.array_element)}, {t.array_size}>"
147+
raise app.UsageError(
148+
"Incompatible with argument of type:"
149+
f" {type_pb2.TypeProto.TypeEnum.Name(t.type_enum)}"
150+
)
153151

154152

155153
def to_unpacked(t: type_pb2.TypeProto, mutable: bool = False) -> str:
154+
"""Get the unpacked c++ view type.
155+
156+
Args:
157+
t: The xls type
158+
mutable: Whether the type is mutable.
159+
160+
Returns:
161+
the C++ unpacked view type
162+
"""
156163
mutable_str = "Mutable" if mutable else ""
157-
match t.type_enum:
158-
case type_pb2.TypeProto.BITS:
159-
return f"xls::{mutable_str}BitsView<{t.bit_count}>"
160-
case type_pb2.TypeProto.TUPLE:
161-
inner = ", ".join(to_unpacked(e, mutable) for e in t.tuple_elements)
162-
return f"xls::{mutable_str}TupleView<{inner}>"
163-
case type_pb2.TypeProto.ARRAY:
164-
return (
165-
f"xls::{mutable_str}ArrayView<{to_unpacked(t.array_element, mutable)},"
166-
f" {t.array_size}>"
167-
)
168-
case _:
169-
raise app.UsageError(
170-
"Incompatible with argument of type:"
171-
f" {type_pb2.TypeProto.TypeEnum.Name(t.type_enum)}"
172-
)
164+
the_type = t.type_enum
165+
if the_type == type_pb2.TypeProto.BITS:
166+
return f"xls::{mutable_str}BitsView<{t.bit_count}>"
167+
elif the_type == type_pb2.TypeProto.TUPLE:
168+
inner = ", ".join(to_unpacked(e, mutable) for e in t.tuple_elements)
169+
return f"xls::{mutable_str}TupleView<{inner}>"
170+
elif the_type == type_pb2.TypeProto.ARRAY:
171+
return (
172+
f"xls::{mutable_str}ArrayView<{to_unpacked(t.array_element, mutable)},"
173+
f" {t.array_size}>"
174+
)
175+
raise app.UsageError(
176+
"Incompatible with argument of type:"
177+
f" {type_pb2.TypeProto.TypeEnum.Name(t.type_enum)}"
178+
)
173179

174180

175181
def is_floating_point(
@@ -195,23 +201,30 @@ def is_float_tuple(t: type_pb2.TypeProto) -> bool:
195201
return is_floating_point(t, 8, 23)
196202

197203

198-
def to_specialized(t: type_pb2.TypeProto) -> str | None:
199-
match t.type_enum:
200-
case type_pb2.TypeProto.BITS:
201-
if t.bit_count <= 8:
202-
return "uint8_t"
203-
elif t.bit_count <= 16:
204-
return "uint16_t"
205-
elif t.bit_count <= 32:
206-
return "uint32_t"
207-
elif t.bit_count <= 64:
208-
return "uint64_t"
209-
case type_pb2.TypeProto.TUPLE if is_double_tuple(t):
210-
return "double"
211-
case type_pb2.TypeProto.TUPLE if is_float_tuple(t):
212-
return "float"
213-
case _:
214-
return None
204+
def to_specialized(t: type_pb2.TypeProto) -> Optional[str]:
205+
"""Get the specialized c++ type.
206+
207+
Args:
208+
t: The xls type
209+
210+
Returns:
211+
the C++ type
212+
"""
213+
the_type = t.type_enum
214+
if the_type == type_pb2.TypeProto.BITS:
215+
if t.bit_count <= 8:
216+
return "uint8_t"
217+
elif t.bit_count <= 16:
218+
return "uint16_t"
219+
elif t.bit_count <= 32:
220+
return "uint32_t"
221+
elif t.bit_count <= 64:
222+
return "uint64_t"
223+
elif is_double_tuple(t):
224+
return "double"
225+
elif is_float_tuple(t):
226+
return "float"
227+
return None
215228

216229

217230
def to_param(p: ir_interface_pb2.PackageInterfaceProto.NamedValue) -> XlsParam:

0 commit comments

Comments
 (0)