Skip to content

Commit

Permalink
feat: Add polymorphism (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-koch authored Jan 9, 2024
1 parent c5be90a commit d0cf104
Show file tree
Hide file tree
Showing 46 changed files with 1,561 additions and 158 deletions.
16 changes: 14 additions & 2 deletions guppy/checker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
FunctionType,
GuppyType,
NoneType,
Subst,
SumType,
TupleType,
)
Expand All @@ -33,7 +34,7 @@ class CallableVariable(ABC, Variable):
@abstractmethod
def check_call(
self, args: list[ast.expr], ty: GuppyType, node: AstNode, ctx: "Context"
) -> ast.expr:
) -> tuple[ast.expr, Subst]:
"""Checks the return type of a function call against a given type."""

@abstractmethod
Expand All @@ -43,6 +44,14 @@ def synthesize_call(
"""Synthesizes the return type of a function call."""


@dataclass
class TypeVarDecl:
"""A declared type variable."""

name: str
linear: bool


class Globals(NamedTuple):
"""Collection of names that are available on module-level.
Expand All @@ -52,6 +61,7 @@ class Globals(NamedTuple):

values: dict[str, Variable]
types: dict[str, type[GuppyType]]
type_vars: dict[str, TypeVarDecl]

@staticmethod
def default() -> "Globals":
Expand All @@ -63,7 +73,7 @@ def default() -> "Globals":
NoneType.name: NoneType,
BoolType.name: BoolType,
}
return Globals({}, tys)
return Globals({}, tys, {})

def get_instance_func(self, ty: GuppyType, name: str) -> CallableVariable | None:
"""Looks up an instance function with a given name for a type.
Expand All @@ -81,11 +91,13 @@ def __or__(self, other: "Globals") -> "Globals":
return Globals(
self.values | other.values,
self.types | other.types,
self.type_vars | other.type_vars,
)

def __ior__(self, other: "Globals") -> "Globals": # noqa: PYI034
self.values.update(other.values)
self.types.update(other.types)
self.type_vars.update(other.type_vars)
return self


Expand Down
Loading

0 comments on commit d0cf104

Please sign in to comment.