fix!: remove union type
This commit is contained in:
@@ -111,10 +111,6 @@ class ConstraintType:
|
|||||||
constraint: Expr
|
constraint: Expr
|
||||||
|
|
||||||
|
|
||||||
class UnionType:
|
|
||||||
types: list[Type]
|
|
||||||
|
|
||||||
|
|
||||||
class ComplexType:
|
class ComplexType:
|
||||||
properties: list[PropertyStmt]
|
properties: list[PropertyStmt]
|
||||||
|
|
||||||
|
|||||||
@@ -228,9 +228,6 @@ class Type(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def visit_constraint_type(self, type: ConstraintType) -> T: ...
|
def visit_constraint_type(self, type: ConstraintType) -> T: ...
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def visit_union_type(self, type: UnionType) -> T: ...
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def visit_complex_type(self, type: ComplexType) -> T: ...
|
def visit_complex_type(self, type: ComplexType) -> T: ...
|
||||||
|
|
||||||
@@ -261,14 +258,6 @@ class ConstraintType(Type):
|
|||||||
return visitor.visit_constraint_type(self)
|
return visitor.visit_constraint_type(self)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class UnionType(Type):
|
|
||||||
types: list[Type]
|
|
||||||
|
|
||||||
def accept(self, visitor: Type.Visitor[T]) -> T:
|
|
||||||
return visitor.visit_union_type(self)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class ComplexType(Type):
|
class ComplexType(Type):
|
||||||
properties: list[PropertyStmt]
|
properties: list[PropertyStmt]
|
||||||
|
|||||||
@@ -252,17 +252,6 @@ class MidasAstPrinter(
|
|||||||
with self._child_level(single=True):
|
with self._child_level(single=True):
|
||||||
type.constraint.accept(self)
|
type.constraint.accept(self)
|
||||||
|
|
||||||
def visit_union_type(self, type: m.UnionType) -> None:
|
|
||||||
self._write_line("UnionType")
|
|
||||||
with self._child_level():
|
|
||||||
self._write_line("types", last=True)
|
|
||||||
with self._child_level():
|
|
||||||
for i, type_ in enumerate(type.types):
|
|
||||||
self._idx = i
|
|
||||||
if i == len(type.types) - 1:
|
|
||||||
self._mark_last()
|
|
||||||
type_.accept(self)
|
|
||||||
|
|
||||||
def visit_complex_type(self, type: m.ComplexType) -> None:
|
def visit_complex_type(self, type: m.ComplexType) -> None:
|
||||||
self._write_line("ComplexType")
|
self._write_line("ComplexType")
|
||||||
with self._child_level():
|
with self._child_level():
|
||||||
@@ -379,10 +368,6 @@ class MidasPrinter(m.Expr.Visitor[str], m.Stmt.Visitor[str], m.Type.Visitor[str]
|
|||||||
res += " where " + type.constraint.accept(self)
|
res += " where " + type.constraint.accept(self)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def visit_union_type(self, type: m.UnionType) -> str:
|
|
||||||
types: list[str] = [type_.accept(self) for type_ in type.types]
|
|
||||||
return " | ".join(types)
|
|
||||||
|
|
||||||
def visit_complex_type(self, type: m.ComplexType) -> str:
|
def visit_complex_type(self, type: m.ComplexType) -> str:
|
||||||
res: str = "{\n"
|
res: str = "{\n"
|
||||||
self.level += 1
|
self.level += 1
|
||||||
|
|||||||
@@ -44,11 +44,4 @@ class ComplexType:
|
|||||||
properties: dict[str, Type]
|
properties: dict[str, Type]
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
Type = BaseType | AliasType | UnknownType | UnitType | Function | ComplexType
|
||||||
class UnionType:
|
|
||||||
alternatives: list[Type]
|
|
||||||
|
|
||||||
|
|
||||||
Type = (
|
|
||||||
BaseType | AliasType | UnknownType | UnitType | Function | ComplexType | UnionType
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -294,11 +294,6 @@ class MidasHighlighter(
|
|||||||
type.type.accept(self)
|
type.type.accept(self)
|
||||||
type.constraint.accept(self)
|
type.constraint.accept(self)
|
||||||
|
|
||||||
def visit_union_type(self, type: m.UnionType) -> None:
|
|
||||||
self.wrap(type, "union-type")
|
|
||||||
for type_ in type.types:
|
|
||||||
type_.accept(self)
|
|
||||||
|
|
||||||
def visit_complex_type(self, type: m.ComplexType) -> None:
|
def visit_complex_type(self, type: m.ComplexType) -> None:
|
||||||
self.wrap(type, "complex-type")
|
self.wrap(type, "complex-type")
|
||||||
for prop in type.properties:
|
for prop in type.properties:
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ span {
|
|||||||
&.named-type,
|
&.named-type,
|
||||||
&.generic-type,
|
&.generic-type,
|
||||||
&.constraint-type,
|
&.constraint-type,
|
||||||
&.union-type,
|
|
||||||
&.complex-type {
|
&.complex-type {
|
||||||
--col: 150, 150, 150;
|
--col: 150, 150, 150;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ class MidasLexer(Lexer):
|
|||||||
self.add_token(TokenType.LEFT_BRACE)
|
self.add_token(TokenType.LEFT_BRACE)
|
||||||
case "}":
|
case "}":
|
||||||
self.add_token(TokenType.RIGHT_BRACE)
|
self.add_token(TokenType.RIGHT_BRACE)
|
||||||
case "|":
|
|
||||||
self.add_token(TokenType.PIPE)
|
|
||||||
case "<":
|
case "<":
|
||||||
self.add_token(
|
self.add_token(
|
||||||
TokenType.LESS_EQUAL if self.match("=") else TokenType.LESS
|
TokenType.LESS_EQUAL if self.match("=") else TokenType.LESS
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ class TokenType(Enum):
|
|||||||
AND = auto()
|
AND = auto()
|
||||||
QMARK = auto()
|
QMARK = auto()
|
||||||
DOT = auto()
|
DOT = auto()
|
||||||
PIPE = auto()
|
|
||||||
|
|
||||||
# Operators
|
# Operators
|
||||||
# PLUS = auto()
|
# PLUS = auto()
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ from midas.ast.midas import (
|
|||||||
Type,
|
Type,
|
||||||
TypeStmt,
|
TypeStmt,
|
||||||
UnaryExpr,
|
UnaryExpr,
|
||||||
UnionType,
|
|
||||||
VariableExpr,
|
VariableExpr,
|
||||||
WildcardExpr,
|
WildcardExpr,
|
||||||
)
|
)
|
||||||
@@ -161,18 +160,7 @@ class MidasParser(Parser):
|
|||||||
Returns:
|
Returns:
|
||||||
TypeExpr: the parsed type expression
|
TypeExpr: the parsed type expression
|
||||||
"""
|
"""
|
||||||
return self.union_type()
|
return self.constraint_type()
|
||||||
|
|
||||||
def union_type(self) -> Type:
|
|
||||||
types: list[Type] = [self.constraint_type()]
|
|
||||||
while self.match(TokenType.PIPE):
|
|
||||||
types.append(self.constraint_type())
|
|
||||||
if len(types) == 1:
|
|
||||||
return types[0]
|
|
||||||
return UnionType(
|
|
||||||
location=Location.span(types[0].location, types[-1].location),
|
|
||||||
types=types,
|
|
||||||
)
|
|
||||||
|
|
||||||
def constraint_type(self) -> Type:
|
def constraint_type(self) -> Type:
|
||||||
type: Type = self.base_type()
|
type: Type = self.base_type()
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import midas.ast.midas as m
|
|||||||
from midas.checker.types import (
|
from midas.checker.types import (
|
||||||
AliasType,
|
AliasType,
|
||||||
Type,
|
Type,
|
||||||
UnionType,
|
|
||||||
UnknownType,
|
UnknownType,
|
||||||
)
|
)
|
||||||
from midas.resolver.builtin import define_builtins
|
from midas.resolver.builtin import define_builtins
|
||||||
@@ -157,10 +156,6 @@ class MidasResolver(m.Stmt.Visitor[None], m.Expr.Visitor[None], m.Type.Visitor[T
|
|||||||
# TODO
|
# TODO
|
||||||
return UnknownType()
|
return UnknownType()
|
||||||
|
|
||||||
def visit_union_type(self, type: m.UnionType) -> Type:
|
|
||||||
types: list[Type] = [type_.accept(self) for type_ in type.types]
|
|
||||||
return UnionType(alternatives=types)
|
|
||||||
|
|
||||||
def visit_complex_type(self, type: m.ComplexType) -> Type:
|
def visit_complex_type(self, type: m.ComplexType) -> Type:
|
||||||
for prop in type.properties:
|
for prop in type.properties:
|
||||||
prop.accept(self)
|
prop.accept(self)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ from midas.ast.midas import (
|
|||||||
Type,
|
Type,
|
||||||
TypeStmt,
|
TypeStmt,
|
||||||
UnaryExpr,
|
UnaryExpr,
|
||||||
UnionType,
|
|
||||||
VariableExpr,
|
VariableExpr,
|
||||||
WildcardExpr,
|
WildcardExpr,
|
||||||
)
|
)
|
||||||
@@ -161,12 +160,6 @@ class MidasAstJsonSerializer(
|
|||||||
"constraint": type.constraint.accept(self),
|
"constraint": type.constraint.accept(self),
|
||||||
}
|
}
|
||||||
|
|
||||||
def visit_union_type(self, type: UnionType) -> dict:
|
|
||||||
return {
|
|
||||||
"_type": "UnionType",
|
|
||||||
"types": self._serialize_list(type.types),
|
|
||||||
}
|
|
||||||
|
|
||||||
def visit_complex_type(self, type: ComplexType) -> dict:
|
def visit_complex_type(self, type: ComplexType) -> dict:
|
||||||
return {
|
return {
|
||||||
"_type": "ComplexType",
|
"_type": "ComplexType",
|
||||||
|
|||||||
Reference in New Issue
Block a user