Improve support for functions #1
This commit is contained in:
0
smnp/type/__init__.py
Normal file
0
smnp/type/__init__.py
Normal file
23
smnp/type/model.py
Normal file
23
smnp/type/model.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from enum import Enum
|
||||
|
||||
from smnp.error.runtime import RuntimeException
|
||||
from smnp.note.model import Note
|
||||
|
||||
|
||||
class Type(Enum):
|
||||
INTEGER = (int, lambda x: str(x))
|
||||
STRING = (str, lambda x: x)
|
||||
LIST = (list, lambda x: f"({', '.join([e.stringify() for e in x])})")
|
||||
PERCENT = (float, lambda x: f"{int(x * 100)}%")
|
||||
NOTE = (Note, lambda x: x.note.name)
|
||||
VOID = (None, lambda x: _failStringify(x))
|
||||
|
||||
def stringify(self, element):
|
||||
return self.value[1](element)
|
||||
|
||||
|
||||
def _failStringify(obj):
|
||||
raise RuntimeException(None, f"Not able to interpret '{obj.type.name()}'")
|
||||
|
||||
|
||||
|
||||
17
smnp/type/value.py
Normal file
17
smnp/type/value.py
Normal file
@@ -0,0 +1,17 @@
|
||||
class Value:
|
||||
def __init__(self, objectType, value):
|
||||
self.value = value
|
||||
|
||||
if type(value) == objectType.value[0]:
|
||||
self.type = objectType
|
||||
else:
|
||||
raise RuntimeError(f"Invalid type '{objectType.name}' for value '{value}'")
|
||||
|
||||
def stringify(self):
|
||||
return self.type.stringify(self.value)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.type.name}({self.stringify()})"
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
Reference in New Issue
Block a user