Snippets
Created by
Василий Шередеко
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | import abc
import pytest
from typing import Sequence
class Context:
def __init__(self):
self.function_type = None
self.array_type = None
self.boolean_type = None
self.void_type = None
self.void_type = VoidType(self, location='builtin.orx')
self.boolean_type = BooleanType(self, location='builtin.orx')
func_args = VariadicType(self, 'Args', location="builtin.orx")
func_r = GenericType(self, 'R', location="builtin.orx")
self.function_type = FunctionType(self, [func_args], func_r, generic_parameters=[func_args, func_r],
location='builtin.orx')
array_t = GenericType(self, 'T', location="builtin.orx")
self.array_type = ArrayType(self, array_t, generic_parameters=[array_t], location="builtin.orx")
class Type(metaclass=abc.ABCMeta):
def __init__(self, context: Context, *, name: str, location):
self.__context = context
self.__name = name
self.__location = location
self.generator = None
self.generic_parameters = []
self.generic_arguments = []
@property
def context(self) -> Context:
return self.__context
@property
def name(self) -> str:
return self.__name
@property
def generic_name(self) -> str:
if self.generic_arguments:
arguments = ", ".join(map(str, (arg for arg in self.generic_arguments)))
elif self.generic_parameters:
arguments = ", ".join(map(str, (arg for arg in self.generic_parameters)))
else:
arguments = None
if arguments:
return f"{self.name}[{arguments}]"
return self.name
@property
def location(self):
return self.__location
@property
def is_generic(self) -> bool:
if self.generic_parameters:
return True
if any(arg.is_generic for arg in self.generic_arguments):
return True
return False
def __str__(self):
return self.generic_name
class GenericParameter:
@property
def is_generic(self) -> bool:
return True
class GenericType(GenericParameter, Type):
def __init__(self, context: Context, name, *, location):
super().__init__(context, name=name, location=location)
class VariadicType(GenericParameter, Type):
def __init__(self, context: Context, name, *, location):
super().__init__(context, name=name, location=location)
def __str__(self):
return f"{self.name}..."
class LazyMetaclass(abc.ABCMeta):
def __new__(metacls, name, bases, namespace, *, storage):
cls = super().__new__(metacls, name, bases, namespace)
cls.__lazy_storage = storage
return cls
def __init__(cls, name, bases, namespace, *, storage):
super().__init__(name, bases, namespace)
def __call__(cls, context: Context, *args, **kwargs):
storage_name = cls.__lazy_storage
storage_type = getattr(context, storage_name, None)
if storage_type:
return storage_type
storage_type = type.__call__(cls, context, *args, **kwargs)
setattr(context, storage_name, storage_type)
return storage_type
class GenericMetaclass(abc.ABCMeta):
def __new__(metacls, name, bases, namespace, *, storage):
cls = super().__new__(metacls, name, bases, namespace)
cls.__lazy_storage = storage
return cls
def __init__(cls, name, bases, namespace, *, storage):
super().__init__(name, bases, namespace)
def __call__(cls, context: Context, *args, generator=None, generic_parameters=None, generic_arguments=None,
**kwargs):
storage_name = cls.__lazy_storage
generic_type = getattr(context, storage_name, None)
if not args and not kwargs:
return generic_type
if generic_type and not generator:
return generic_type.instantiate(*args, **kwargs)
function_type = type.__call__(cls, context, *args, **kwargs)
function_type.generator = generator
function_type.generic_parameters = generic_parameters
function_type.generic_arguments = generic_arguments
return function_type
class BooleanType(Type, metaclass=LazyMetaclass, storage='boolean_type'):
def __init__(self, context: Context, *, location=None):
super().__init__(context, name="Boolean", location=location)
def __str__(self):
return "bool"
class VoidType(Type, metaclass=LazyMetaclass, storage='void_type'):
def __init__(self, context: Context, *, location=None):
super().__init__(context, name="Void", location=location)
def __str__(self):
return "void"
class FunctionType(Type, metaclass=GenericMetaclass, storage='function_type'):
def __init__(self, context: Context, parameters: Sequence[Type] = None, result_type: Type = None, *, location=None):
assert result_type, "Function type is required result type"
assert parameters, "Function type is required parameters"
super().__init__(context, name="Function", location=location)
self.parameters = tuple(parameters)
self.result_type = result_type
def __str__(self):
parameters = ", ".join(str(param) for param in self.parameters)
return f"({parameters}) -> {self.result_type}"
def instantiate(self, parameters, result_type):
kwargs = dict(
location=self.location,
generator=self,
generic_arguments=parameters + [result_type]
)
return FunctionType(self.context, parameters, result_type, **kwargs)
class ArrayType(Type, metaclass=GenericMetaclass, storage='array_type'):
def __init__(self, context: Context, element_type: Type = None, *, location=None):
assert element_type, "Function type is required element type"
super().__init__(context, name="Array", location=location)
self.element_type = element_type
def __str__(self):
return f"{self.element_type}*"
def instantiate(self, element_type):
kwargs = dict(
location=self.location,
generator=self,
generic_arguments=[element_type]
)
return ArrayType(self.context, element_type, **kwargs)
def test_aaa():
context = Context()
function_generic = FunctionType(context)
function_type = FunctionType(context, [BooleanType(context), BooleanType(context)], VoidType(context))
array_generic = ArrayType(context)
array_type = ArrayType(context, BooleanType(context))
assert FunctionType(context) is FunctionType(context)
assert BooleanType(context) is BooleanType(context)
assert VoidType(context) is VoidType(context)
assert context.function_type == function_generic
assert function_generic != function_type
assert context.array_type == array_generic
assert array_generic != array_type
assert function_generic.generator is None
assert function_generic.is_generic
assert not function_type.is_generic
assert function_type.generator == function_generic
assert array_generic.generator is None
assert array_generic.is_generic
assert not array_type.is_generic
assert array_type.generator == array_generic
def dump(type: Type):
print("----------------------------")
print(f"type: {type}")
print(f"name: {type.name}")
print(f"generator: {type.generator}")
print(f"is generic: {type.is_generic}")
print(f"generic name: {type.generic_name}")
dump(VoidType(context))
dump(BooleanType(context))
dump(FunctionType(context))
dump(ArrayType(context))
dump(function_type)
dump(array_type)
if __name__ == '__main__':
pytest.main()
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.