Skip to content

Commit

Permalink
feat: Add StringVal to hugr-py (#1818)
Browse files Browse the repository at this point in the history
Closes #1817

Enables strings in Guppy, see CQCL/guppylang#695
  • Loading branch information
tatiana-s authored Dec 19, 2024
1 parent 1f841ae commit b05a419
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
34 changes: 34 additions & 0 deletions hugr-py/src/hugr/std/prelude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""HUGR prelude values."""

from __future__ import annotations

from dataclasses import dataclass

from hugr import val
from hugr.std import _load_extension

PRELUDE_EXTENSION = _load_extension("prelude")

STRING_T_DEF = PRELUDE_EXTENSION.types["string"]

STRING_T = STRING_T_DEF.instantiate([])


@dataclass
class StringVal(val.ExtensionValue):
"""Custom value for a string."""

v: str

def to_value(self) -> val.Extension:
name = "ConstString"
payload = {"value": self.v}
return val.Extension(
name,
typ=STRING_T,
val=payload,
extensions=[PRELUDE_EXTENSION.name],
)

def __str__(self) -> str:
return f"{self.v}"
8 changes: 8 additions & 0 deletions hugr-py/tests/test_prelude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from hugr.std.prelude import STRING_T, StringVal


def test_string_val():
ext_val = StringVal("test").to_value()
assert ext_val.name == "ConstString"
assert ext_val.typ == STRING_T
assert ext_val.val == {"value": "test"}

0 comments on commit b05a419

Please sign in to comment.