Commit 208daf4e authored by Tom Niget's avatar Tom Niget

Add object type

parent c5aae2f9
from typing import Self, TypeVar, Generic, Protocol
class object:
def __eq__(self, other: Self) -> bool: ...
def __ne__(self, other: Self) -> bool: ...
class int:
def __add__(self, other: Self) -> Self: ...
......@@ -11,6 +15,8 @@ class int:
def __neg__(self) -> Self: ...
def __init__(self, x: str) -> None: ...
def __lt__(self, other: Self) -> bool: ...
def __gt__(self, other: Self) -> bool: ...
assert int.__add__
......@@ -64,6 +70,7 @@ class list(Generic[U]):
def __iter__(self) -> Iterator[U]: ...
def __len__(self) -> int: ...
def append(self, value: U) -> None: ...
def __contains__(self, item: U) -> bool: ...
assert [1, 2].__iter__()
assert list[int].__iter__
......@@ -82,6 +89,8 @@ assert [1, 2, 3][1]
def iter(x: Iterable[U]) -> Iterator[U]:
...
assert iter
def next(it: Iterator[U], default: None) -> U:
...
# what happens with multiple functions
......
......@@ -5,7 +5,7 @@ from transpiler.phases.typing.scope import VarKind, VarDecl, ScopeKind, Scope
from transpiler.phases.typing.stdlib import PRELUDE, StdlibVisitor
from transpiler.phases.typing.types import TY_TYPE, TY_INT, TY_STR, TY_BOOL, TY_COMPLEX, TY_NONE, FunctionType, \
TypeVariable, CppType, PyList, TypeType, Forked, Task, Future, PyIterator, TupleType, TypeOperator, BaseType, \
ModuleType, TY_BYTES, TY_FLOAT, PyDict, TY_SLICE
ModuleType, TY_BYTES, TY_FLOAT, PyDict, TY_SLICE, TY_OBJECT
PRELUDE.vars.update({
# "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT),
......@@ -34,7 +34,8 @@ PRELUDE.vars.update({
"Future": VarDecl(VarKind.LOCAL, TypeType(Future)),
"Iterator": VarDecl(VarKind.LOCAL, TypeType(PyIterator)),
"tuple": VarDecl(VarKind.LOCAL, TypeType(TupleType)),
"slice": VarDecl(VarKind.LOCAL, TypeType(TY_SLICE))
"slice": VarDecl(VarKind.LOCAL, TypeType(TY_SLICE)),
"object": VarDecl(VarKind.LOCAL, TypeType(TY_OBJECT)),
})
typon_std = Path(__file__).parent.parent.parent.parent / "stdlib"
......
......@@ -157,7 +157,7 @@ class ScoperExprVisitor(ScoperVisitor):
ltype = self.visit(node.value)
return self.visit_getattr(ltype, node.attr)
def visit_getattr(self, ltype: BaseType, name: str):
def visit_getattr(self, ltype: BaseType, name: str) -> BaseType:
bound = True
if isinstance(ltype, TypeType):
ltype = ltype.type_object
......@@ -179,6 +179,13 @@ class ScoperExprVisitor(ScoperVisitor):
else:
return meth
from transpiler.phases.typing.exceptions import MissingAttributeError
parents = ltype.iter_hierarchy_recursive()
next(parents)
for p in parents:
try:
return self.visit_getattr(p, name)
except MissingAttributeError as e:
pass
raise MissingAttributeError(ltype, name)
def visit_List(self, node: ast.List) -> BaseType:
......
......@@ -8,17 +8,38 @@ from typing import Dict, Optional, List, ClassVar, Callable
from transpiler.utils import highlight
def get_default_parents():
if obj := globals().get("TY_OBJECT"):
return [obj]
return []
@dataclass(eq=False)
class BaseType(ABC):
members: Dict[str, "BaseType"] = field(default_factory=dict, init=False)
methods: Dict[str, "FunctionType"] = field(default_factory=dict, init=False)
parents: List["BaseType"] = field(default_factory=list, init=False)
parents: List["BaseType"] = field(default_factory=get_default_parents, init=False)
typevars: List["TypeVariable"] = field(default_factory=list, init=False)
def get_parents(self) -> List["BaseType"]:
return self.parents
def iter_hierarchy_recursive(self) -> typing.Iterator["BaseType"]:
cache = set()
from queue import Queue
queue = Queue()
queue.put(self)
while not queue.empty():
cur = queue.get()
yield cur
if cur in cache:
continue
cache.add(cur)
if cur == TY_OBJECT:
continue
for p in cur.get_parents():
queue.put(p)
def resolve(self) -> "BaseType":
return self
......@@ -343,6 +364,7 @@ class TypeType(TypeOperator):
self.args[0] = value
TY_OBJECT = TypeOperator.make_type("object")
TY_SELF = TypeOperator.make_type("Self")
def self_gen_sub(this, typevars, _):
if this is not None:
......@@ -350,9 +372,6 @@ def self_gen_sub(this, typevars, _):
return TY_SELF
TY_SELF.gen_sub = self_gen_sub
TY_BOOL = TypeOperator.make_type("bool")
DEFAULT_EQ = FunctionType([TY_SELF, TY_SELF], TY_BOOL)
TY_BOOL._add_default_eq()
TY_TYPE = TypeOperator.make_type("type")
TY_INT = TypeOperator.make_type("int")
TY_FLOAT = TypeOperator.make_type("float")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment