278 lines
6.1 KiB
Python
278 lines
6.1 KiB
Python
"""
|
|
This file was generated by a script. Any manual changes might be overwritten.
|
|
Please modify gen/midas.py instead and run gen/gen.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
from dataclasses import dataclass
|
|
from typing import Any, Generic, Optional, TypeVar
|
|
|
|
from midas.ast.location import Location
|
|
from midas.lexer.token import Token
|
|
|
|
T = TypeVar("T")
|
|
|
|
##############
|
|
# Statements #
|
|
##############
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class Stmt(ABC):
|
|
location: Location
|
|
|
|
@abstractmethod
|
|
def accept(self, visitor: Visitor[T]) -> T: ...
|
|
|
|
class Visitor(ABC, Generic[T]):
|
|
@abstractmethod
|
|
def visit_type_stmt(self, stmt: TypeStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_property_stmt(self, stmt: PropertyStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_extend_stmt(self, stmt: ExtendStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_op_stmt(self, stmt: OpStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_predicate_stmt(self, stmt: PredicateStmt) -> T: ...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TypeStmt(Stmt):
|
|
name: Token
|
|
params: list[Param]
|
|
type: Type
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class Param:
|
|
location: Location
|
|
name: Token
|
|
bound: Optional[Type]
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_type_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PropertyStmt(Stmt):
|
|
name: Token
|
|
type: Type
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_property_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExtendStmt(Stmt):
|
|
type: Type
|
|
operations: list[OpStmt]
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_extend_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class OpStmt(Stmt):
|
|
name: Token
|
|
operand: Type
|
|
result: Type
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_op_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PredicateStmt(Stmt):
|
|
name: Token
|
|
subject: Token
|
|
type: Type
|
|
condition: Expr
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_predicate_stmt(self)
|
|
|
|
|
|
###############
|
|
# Expressions #
|
|
###############
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class Expr(ABC):
|
|
location: Location
|
|
|
|
@abstractmethod
|
|
def accept(self, visitor: Visitor[T]) -> T: ...
|
|
|
|
class Visitor(ABC, Generic[T]):
|
|
@abstractmethod
|
|
def visit_logical_expr(self, expr: LogicalExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_binary_expr(self, expr: BinaryExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_unary_expr(self, expr: UnaryExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_get_expr(self, expr: GetExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_variable_expr(self, expr: VariableExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_grouping_expr(self, expr: GroupingExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_literal_expr(self, expr: LiteralExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_wildcard_expr(self, expr: WildcardExpr) -> T: ...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LogicalExpr(Expr):
|
|
left: Expr
|
|
operator: Token
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_logical_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BinaryExpr(Expr):
|
|
left: Expr
|
|
operator: Token
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_binary_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UnaryExpr(Expr):
|
|
operator: Token
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_unary_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GetExpr(Expr):
|
|
expr: Expr
|
|
name: Token
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_get_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class VariableExpr(Expr):
|
|
name: Token
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_variable_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GroupingExpr(Expr):
|
|
expr: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_grouping_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LiteralExpr(Expr):
|
|
value: Any
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_literal_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WildcardExpr(Expr):
|
|
token: Token
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_wildcard_expr(self)
|
|
|
|
|
|
#########
|
|
# Types #
|
|
#########
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class Type(ABC):
|
|
location: Location
|
|
|
|
@abstractmethod
|
|
def accept(self, visitor: Visitor[T]) -> T: ...
|
|
|
|
class Visitor(ABC, Generic[T]):
|
|
@abstractmethod
|
|
def visit_named_type(self, type: NamedType) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_generic_type(self, type: GenericType) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_constraint_type(self, type: ConstraintType) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_union_type(self, type: UnionType) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_complex_type(self, type: ComplexType) -> T: ...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class NamedType(Type):
|
|
name: Token
|
|
|
|
def accept(self, visitor: Type.Visitor[T]) -> T:
|
|
return visitor.visit_named_type(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GenericType(Type):
|
|
type: Type
|
|
params: list[Type]
|
|
|
|
def accept(self, visitor: Type.Visitor[T]) -> T:
|
|
return visitor.visit_generic_type(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ConstraintType(Type):
|
|
type: Type
|
|
constraint: Expr
|
|
|
|
def accept(self, visitor: Type.Visitor[T]) -> T:
|
|
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)
|
|
class ComplexType(Type):
|
|
properties: list[PropertyStmt]
|
|
|
|
def accept(self, visitor: Type.Visitor[T]) -> T:
|
|
return visitor.visit_complex_type(self)
|