Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
Value.py
Go to the documentation of this file.
1"""
2phasor.Value
3=============
4Runtime value types stored in the constant pool.
5"""
6
7from __future__ import annotations
8
9from dataclasses import dataclass
10from enum import IntEnum
11from typing import Union
12
13
14class ValueType(IntEnum):
15 """Type of a constant-pool :class:`Value`."""
16 Null = 0
17 Bool = 1
18 Int = 2
19 Float = 3
20 String = 4
21
22_Payload = Union[None, bool, int, float, str]
23
24
25@dataclass
26class Value:
27 """
28 A typed constant-pool entry.
29
30 Use the class-method constructors rather than setting ``type`` /
31 ``_data`` directly::
32
33 Value.null()
34 Value.from_bool(True)
35 Value.from_int(42)
36 Value.from_float(3.14)
37 Value.from_string("hello")
38 """
39
40 type: ValueType
41 _data: _Payload = None
42
43 @classmethod
44 def null(cls) -> "Value":
45 """Return a new Null-typed value."""
46 return cls(ValueType.Null, None)
47
48 @classmethod
49 def from_bool(cls, v: bool) -> "Value":
50 """Return a new Bool-typed value wrapping *v*."""
51 return cls(ValueType.Bool, bool(v))
52
53 @classmethod
54 def from_int(cls, v: int) -> "Value":
55 """Return a new Int-typed value wrapping *v*."""
56 return cls(ValueType.Int, int(v))
57
58 @classmethod
59 def from_float(cls, v: float) -> "Value":
60 """Return a new Float-typed value wrapping *v*."""
61 return cls(ValueType.Float, float(v))
62
63 @classmethod
64 def from_string(cls, v: str) -> "Value":
65 """Return a new String-typed value wrapping *v*."""
66 return cls(ValueType.String, str(v))
67
68 def as_bool(self) -> bool:
69 """Return the payload as a Python ``bool``, raising ``TypeError`` if the type is not Bool."""
70 if self.type != ValueType.Bool:
71 raise TypeError(f"Value is {self.type.name}, not Bool")
72 return bool(self._data)
73
74 def as_int(self) -> int:
75 """Return the payload as a Python ``int``, raising ``TypeError`` if the type is not Int."""
76 if self.type != ValueType.Int:
77 raise TypeError(f"Value is {self.type.name}, not Int")
78 return int(self._data) # type: ignore[arg-type]
79
80 def as_float(self) -> float:
81 """Return the payload as a Python ``float``, raising ``TypeError`` if the type is not Float."""
82 if self.type != ValueType.Float:
83 raise TypeError(f"Value is {self.type.name}, not Float")
84 return float(self._data) # type: ignore[arg-type]
85
86 def as_string(self) -> str:
87 """Return the payload as a Python ``str``, raising ``TypeError`` if the type is not String."""
88 if self.type != ValueType.String:
89 raise TypeError(f"Value is {self.type.name}, not String")
90 return str(self._data)
91
92 def __repr__(self) -> str:
93 """Return a concise debug representation showing the type and payload."""
94 if self.type == ValueType.Null:
95 return "Value(null)"
96 return f"Value({self.type.name}, {self._data!r})"
97
98 def __eq__(self, other: object) -> bool:
99 """Return ``True`` if *other* is a :class:`Value` with identical type and payload."""
100 if not isinstance(other, Value):
101 return NotImplemented
102 return self.type == other.type and self._data == other._data
bool __eq__(self, object other)
Definition Value.py:98
str as_string(self)
Definition Value.py:86
_Payload type
Definition Value.py:94
"Value" from_int(cls, int v)
Definition Value.py:54
bool as_bool(self)
Definition Value.py:68
_Payload _data
Definition Value.py:41
int as_int(self)
Definition Value.py:74
float as_float(self)
Definition Value.py:80
"Value" null(cls)
Definition Value.py:44
"Value" from_bool(cls, bool v)
Definition Value.py:49
str __repr__(self)
Definition Value.py:92
"Value" from_string(cls, str v)
Definition Value.py:64
"Value" from_float(cls, float v)
Definition Value.py:59