Skip to content
Snippets Groups Projects
Commit 490adaae authored by Frederik Hennig's avatar Frederik Hennig
Browse files

refine printing of integer literals

parent da745ae5
No related branches found
No related tags found
1 merge request!389Refine printing of integer literals
Pipeline #66340 passed
......@@ -483,7 +483,20 @@ class PsIntegerType(PsScalarType, ABC):
if not isinstance(value, np_dtype):
raise PsTypeError(f"Given value {value} is not of required type {np_dtype}")
unsigned_suffix = "" if self.signed else "u"
return f"{value}{unsigned_suffix}"
match self.width:
case w if w < 32:
# Plain integer literals get at least type `int`, which is 32 bit in all relevant cases
# So we need to explicitly cast to smaller types
return f"(({self._c_type_without_const()}) {value}{unsigned_suffix})"
case 32:
# No suffix here - becomes `int`, which is 32 bit
return f"{value}{unsigned_suffix}"
case 64:
# LL suffix: `long long` is the only type guaranteed to be 64 bit wide
return f"{value}{unsigned_suffix}LL"
case _:
assert False, "unreachable code"
def create_constant(self, value: Any) -> Any:
np_type = self.NUMPY_TYPES[self._width]
......@@ -498,9 +511,12 @@ class PsIntegerType(PsScalarType, ABC):
raise PsTypeError(f"Could not interpret {value} as {repr(self)}")
def c_string(self) -> str:
def _c_type_without_const(self) -> str:
prefix = "" if self._signed else "u"
return f"{self._const_string()}{prefix}int{self._width}_t"
return f"{prefix}int{self._width}_t"
def c_string(self) -> str:
return f"{self._const_string()}{self._c_type_without_const()}"
def __repr__(self) -> str:
return f"PsIntegerType( width={self.width}, signed={self.signed}, const={self.const} )"
......
......@@ -54,6 +54,6 @@ def test_literals():
print(code)
assert "const double x = C;" in code
assert "CELLS[0]" in code
assert "CELLS[1]" in code
assert "CELLS[2]" in code
assert "CELLS[0LL]" in code
assert "CELLS[1LL]" in code
assert "CELLS[2LL]" in code
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment