diff options
Diffstat (limited to 'lib9p/protogen/c9util.py')
-rw-r--r-- | lib9p/protogen/c9util.py | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/lib9p/protogen/c9util.py b/lib9p/protogen/c9util.py index e7ad999..85fd47b 100644 --- a/lib9p/protogen/c9util.py +++ b/lib9p/protogen/c9util.py @@ -3,6 +3,7 @@ # Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> # SPDX-License-Identifier: AGPL-3.0-or-later +import re import typing import idl @@ -90,20 +91,30 @@ def typename(typ: idl.Type, parent: idl.StructMember | None = None) -> str: raise ValueError(f"not a type: {typ.__class__.__name__}") -def idl_expr(expr: idl.Expr, lookup_sym: typing.Callable[[str], str]) -> str: +def idl_expr( + expr: idl.Expr, lookup_sym: typing.Callable[[str], str], bitwidth: int = 0 +) -> str: ret: list[str] = [] for tok in expr.tokens: match tok: case idl.ExprOp(): ret.append(tok.op) case idl.ExprLit(): - ret.append(str(tok.val)) - case idl.ExprSym(symname="s32_max"): - ret.append("INT32_MAX") - case idl.ExprSym(symname="s64_max"): - ret.append("INT64_MAX") + if bitwidth: + ret.append(f"{tok.val:#0{bitwidth}b}") + else: + ret.append(str(tok.val)) case idl.ExprSym(): - ret.append(lookup_sym(tok.symname)) + if m := re.fullmatch(r"^u(8|16|32|64)_max$", tok.symname): + ret.append(f"UINT{m.group(1)}_MAX") + elif m := re.fullmatch(r"^s(8|16|32|64)_max$", tok.symname): + ret.append(f"INT{m.group(1)}_MAX") + else: + ret.append(lookup_sym(tok.symname)) + case idl.ExprOff(): + ret.append(lookup_sym("&" + tok.membname)) + case idl.ExprNum(): + ret.append(Ident(add_prefix(f"{tok.numname}_".upper(), tok.valname))) case _: assert False return " ".join(ret) |