pyne/datatypes.py

56 lines
1.3 KiB
Python
Raw Normal View History

2023-04-08 20:07:28 +00:00
from dataclasses import dataclass
from enum import auto, Enum
from typing import Any
import re
2023-04-08 20:07:28 +00:00
class Datatype(Enum):
NONE = auto()
NULL = auto()
COMMENT = auto()
2023-04-08 20:07:28 +00:00
STRING = auto()
INTEGER = auto()
FLOAT = auto()
COMPLEX = auto()
2023-04-08 20:07:28 +00:00
2023-04-09 15:04:56 +00:00
@dataclass
class Data:
type: Datatype
value: Any
2023-04-08 20:07:28 +00:00
@dataclass
class Variable:
name: str
type: Datatype
2023-04-09 15:04:56 +00:00
value: Data
def __setattr__(self, name, value):
if name == "value" and isinstance(value, Data):
if self.type == value.type:
object.__setattr__(self, name, value)
else:
raise TypeError(
2023-04-09 15:04:56 +00:00
"Type inequality; cannot assign value of "
f"type {value.type} to variable of type {self.type}"
)
else:
object.__setattr__(self, name, value)
NONE_TYPE = Variable(None, Datatype.NONE, None)
STR_MATCH_BEGIN = re.compile(r"^\"")
STR_MATCH_END = re.compile(r"\"$")
VAR_ASSIGN_TOKEN = re.compile(r"[A-Za-z]+ ?= ?")
VAR_MATCH = {
re.compile(r"null"): Datatype.NULL,
re.compile(r"\/\/.+"): Datatype.COMMENT,
re.compile(r"\".+\""): Datatype.STRING,
re.compile(r"-?\d+\.?\d{0,}?\+\d+\.?\d{0,}?i"): Datatype.COMPLEX,
re.compile(r"-?\d+\.\d{0,}"): Datatype.FLOAT,
re.compile(r"-?\d+"): Datatype.INTEGER,
}