Commit 7f08b35a authored by Tom Niget's avatar Tom Niget

Add object type

parent f1dd801a
from typing import Self, TypeVar, Generic, Protocol from typing import Self, TypeVar, Generic, Protocol
class object:
def __eq__(self, other: Self) -> bool: ...
def __ne__(self, other: Self) -> bool: ...
class int: class int:
def __add__(self, other: Self) -> Self: ... def __add__(self, other: Self) -> Self: ...
...@@ -11,6 +15,8 @@ class int: ...@@ -11,6 +15,8 @@ class int:
def __neg__(self) -> Self: ... def __neg__(self) -> Self: ...
def __init__(self, x: str) -> None: ... def __init__(self, x: str) -> None: ...
def __lt__(self, other: Self) -> bool: ...
def __gt__(self, other: Self) -> bool: ...
assert int.__add__ assert int.__add__
...@@ -64,6 +70,7 @@ class list(Generic[U]): ...@@ -64,6 +70,7 @@ class list(Generic[U]):
def __iter__(self) -> Iterator[U]: ... def __iter__(self) -> Iterator[U]: ...
def __len__(self) -> int: ... def __len__(self) -> int: ...
def append(self, value: U) -> None: ... def append(self, value: U) -> None: ...
def __contains__(self, item: U) -> bool: ...
assert [1, 2].__iter__() assert [1, 2].__iter__()
assert list[int].__iter__ assert list[int].__iter__
...@@ -82,6 +89,8 @@ assert [1, 2, 3][1] ...@@ -82,6 +89,8 @@ assert [1, 2, 3][1]
def iter(x: Iterable[U]) -> Iterator[U]: def iter(x: Iterable[U]) -> Iterator[U]:
... ...
assert iter
def next(it: Iterator[U], default: None) -> U: def next(it: Iterator[U], default: None) -> U:
... ...
# what happens with multiple functions # what happens with multiple functions
......
...@@ -5,7 +5,7 @@ from transpiler.phases.typing.scope import VarKind, VarDecl, ScopeKind, Scope ...@@ -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.stdlib import PRELUDE, StdlibVisitor
from transpiler.phases.typing.types import TY_TYPE, TY_INT, TY_STR, TY_BOOL, TY_COMPLEX, TY_NONE, FunctionType, \ 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, \ 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({ PRELUDE.vars.update({
# "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT), # "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT),
...@@ -34,7 +34,8 @@ PRELUDE.vars.update({ ...@@ -34,7 +34,8 @@ PRELUDE.vars.update({
"Future": VarDecl(VarKind.LOCAL, TypeType(Future)), "Future": VarDecl(VarKind.LOCAL, TypeType(Future)),
"Iterator": VarDecl(VarKind.LOCAL, TypeType(PyIterator)), "Iterator": VarDecl(VarKind.LOCAL, TypeType(PyIterator)),
"tuple": VarDecl(VarKind.LOCAL, TypeType(TupleType)), "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" typon_std = Path(__file__).parent.parent.parent.parent / "stdlib"
......
...@@ -157,7 +157,7 @@ class ScoperExprVisitor(ScoperVisitor): ...@@ -157,7 +157,7 @@ class ScoperExprVisitor(ScoperVisitor):
ltype = self.visit(node.value) ltype = self.visit(node.value)
return self.visit_getattr(ltype, node.attr) 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 bound = True
if isinstance(ltype, TypeType): if isinstance(ltype, TypeType):
ltype = ltype.type_object ltype = ltype.type_object
...@@ -179,6 +179,13 @@ class ScoperExprVisitor(ScoperVisitor): ...@@ -179,6 +179,13 @@ class ScoperExprVisitor(ScoperVisitor):
else: else:
return meth return meth
from transpiler.phases.typing.exceptions import MissingAttributeError 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) raise MissingAttributeError(ltype, name)
def visit_List(self, node: ast.List) -> BaseType: def visit_List(self, node: ast.List) -> BaseType:
......
...@@ -8,17 +8,38 @@ from typing import Dict, Optional, List, ClassVar, Callable ...@@ -8,17 +8,38 @@ from typing import Dict, Optional, List, ClassVar, Callable
from transpiler.utils import highlight from transpiler.utils import highlight
def get_default_parents():
if obj := globals().get("TY_OBJECT"):
return [obj]
return []
@dataclass(eq=False) @dataclass(eq=False)
class BaseType(ABC): class BaseType(ABC):
members: Dict[str, "BaseType"] = field(default_factory=dict, init=False) members: Dict[str, "BaseType"] = field(default_factory=dict, init=False)
methods: Dict[str, "FunctionType"] = 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) typevars: List["TypeVariable"] = field(default_factory=list, init=False)
def get_parents(self) -> List["BaseType"]: def get_parents(self) -> List["BaseType"]:
return self.parents 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": def resolve(self) -> "BaseType":
return self return self
...@@ -343,6 +364,7 @@ class TypeType(TypeOperator): ...@@ -343,6 +364,7 @@ class TypeType(TypeOperator):
self.args[0] = value self.args[0] = value
TY_OBJECT = TypeOperator.make_type("object")
TY_SELF = TypeOperator.make_type("Self") TY_SELF = TypeOperator.make_type("Self")
def self_gen_sub(this, typevars, _): def self_gen_sub(this, typevars, _):
if this is not None: if this is not None:
...@@ -350,9 +372,6 @@ def self_gen_sub(this, typevars, _): ...@@ -350,9 +372,6 @@ def self_gen_sub(this, typevars, _):
return TY_SELF return TY_SELF
TY_SELF.gen_sub = self_gen_sub TY_SELF.gen_sub = self_gen_sub
TY_BOOL = TypeOperator.make_type("bool") 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_TYPE = TypeOperator.make_type("type")
TY_INT = TypeOperator.make_type("int") TY_INT = TypeOperator.make_type("int")
TY_FLOAT = TypeOperator.make_type("float") 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