================
@@ -518,3 +519,110 @@ def representer(dumper, data: "Label"):
     def register_yaml(loader):
         yaml.add_constructor("!label", Label.constructor, loader)
         yaml.add_representer(Label, Label.representer)
+
+
+class Float:
+    """Used to match against float values that may have an approximate range.
+    There are four possible representations for a !float node, with/without a 
list of values and with/without a range:
+    - `!float <value>` - checks for an exact match using floating point 
equality, e.g. !float 10 will match 10.0.
+    - `!float <value>+-<range>` - checks for a match within the given range, 
e.g. !float 10 +- 0.2 will match any
+                                  value in the range [9.8, 10.2].
+    - `!float [<value>...] - checks for exact matches against any of the given 
values, e.g. !float [10, 11, 12] will
+                             match 10.0, 11.0, or 12.0. This is effectively a 
shorthand for using a list of single float
+                             values, e.g. `[!float 10, !float 11, !float 12]`.
+    - `!float {values: [<value>...], range: <range>} - checks for matches 
against any of the given values, each of which
+                                                       will match a range of 
+-<range>. As with normal lists, this is a
+                                                       shorthand, e.g. 
!float{values: [1, 2], range: 0.1} is equivalent
+                                                       to [!float 1 +- 0.1, 
!float 2 +- 0.1].
+    """
+
+    def __init__(self, values, range):
+        try:
+            if isinstance(values, list):
+                values = [float(v) for v in values]
+                range = float(range) if range is not None else None
+            elif isinstance(values, str) and "+-" in values:
+                assert (
+                    range is None
+                ), "Float has both an explicit range and a string-embedded 
range?"
+                values, range = (float(n) for n in values.split("+-", 
maxsplit=1))
+            else:
+                assert range is None, "Explicit range passed with single float 
value?"
+                values = float(values)
+        except ValueError as err:
+            raise DexterNodeError(self, f"!float received non-float value: 
{err}")
+        self.values: Union[float, List[float]] = values
----------------
OCHyams wrote:

Would it be simpler to convert float to list of one float, rather than use a 
union?

https://github.com/llvm/llvm-project/pull/204161
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to