-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
StringVal
to hugr-py (#1818)
Closes #1817 Enables strings in Guppy, see CQCL/guppylang#695
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |