328 lines
7.3 KiB
Python
328 lines
7.3 KiB
Python
"""
|
|
This file was generated by a script. Any manual changes might be overwritten.
|
|
Please modify gen/python.py instead and run gen/gen.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
from abc import ABC, abstractmethod
|
|
from dataclasses import dataclass
|
|
from typing import Any, Generic, Optional, TypeVar
|
|
|
|
from midas.ast.location import Location
|
|
|
|
T = TypeVar("T")
|
|
|
|
####################
|
|
# Type annotations #
|
|
####################
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class MidasType(ABC):
|
|
location: Location
|
|
|
|
@abstractmethod
|
|
def accept(self, visitor: Visitor[T]) -> T: ...
|
|
|
|
class Visitor(ABC, Generic[T]):
|
|
@abstractmethod
|
|
def visit_base_type(self, node: BaseType) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_constraint_type(self, node: ConstraintType) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_frame_column(self, node: FrameColumn) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_frame_type(self, node: FrameType) -> T: ...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BaseType(MidasType):
|
|
base: str
|
|
param: Optional[MidasType]
|
|
|
|
def accept(self, visitor: MidasType.Visitor[T]) -> T:
|
|
return visitor.visit_base_type(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ConstraintType(MidasType):
|
|
type: MidasType
|
|
constraint: ast.expr
|
|
|
|
def accept(self, visitor: MidasType.Visitor[T]) -> T:
|
|
return visitor.visit_constraint_type(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class FrameColumn(MidasType):
|
|
name: Optional[str]
|
|
type: Optional[MidasType]
|
|
|
|
def accept(self, visitor: MidasType.Visitor[T]) -> T:
|
|
return visitor.visit_frame_column(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class FrameType(MidasType):
|
|
columns: list[FrameColumn]
|
|
|
|
def accept(self, visitor: MidasType.Visitor[T]) -> T:
|
|
return visitor.visit_frame_type(self)
|
|
|
|
|
|
##############
|
|
# 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_expression_stmt(self, stmt: ExpressionStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_function(self, stmt: Function) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_type_assign(self, stmt: TypeAssign) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_assign_stmt(self, stmt: AssignStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_return_stmt(self, stmt: ReturnStmt) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_if_stmt(self, stmt: IfStmt) -> T: ...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExpressionStmt(Stmt):
|
|
expr: Expr
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_expression_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Function(Stmt):
|
|
name: str
|
|
posonlyargs: list[Argument]
|
|
args: list[Argument]
|
|
sink: Optional[Argument]
|
|
kwonlyargs: list[Argument]
|
|
kw_sink: Optional[Argument]
|
|
returns: Optional[MidasType]
|
|
body: list[Stmt]
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class Argument:
|
|
location: Optional[Location] = None
|
|
name: str
|
|
type: Optional[MidasType]
|
|
default: Optional[Expr]
|
|
|
|
@property
|
|
def all_args(self) -> list[Argument]:
|
|
return self.posonlyargs + self.args + self.kwonlyargs
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_function(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TypeAssign(Stmt):
|
|
name: str
|
|
type: MidasType
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_type_assign(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AssignStmt(Stmt):
|
|
targets: list[Expr]
|
|
value: Expr
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_assign_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ReturnStmt(Stmt):
|
|
value: Optional[Expr]
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_return_stmt(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class IfStmt(Stmt):
|
|
test: Expr
|
|
body: list[Stmt]
|
|
orelse: list[Stmt]
|
|
|
|
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
|
return visitor.visit_if_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_binary_expr(self, expr: BinaryExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_compare_expr(self, expr: CompareExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_unary_expr(self, expr: UnaryExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_call_expr(self, expr: CallExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_get_expr(self, expr: GetExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_literal_expr(self, expr: LiteralExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_variable_expr(self, expr: VariableExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_logical_expr(self, expr: LogicalExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_set_expr(self, expr: SetExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_cast_expr(self, expr: CastExpr) -> T: ...
|
|
|
|
@abstractmethod
|
|
def visit_ternary_expr(self, expr: TernaryExpr) -> T: ...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BinaryExpr(Expr):
|
|
left: Expr
|
|
operator: ast.operator
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_binary_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CompareExpr(Expr):
|
|
left: Expr
|
|
operator: ast.cmpop
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_compare_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UnaryExpr(Expr):
|
|
operator: ast.unaryop
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_unary_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CallExpr(Expr):
|
|
callee: Expr
|
|
arguments: list[Expr]
|
|
keywords: dict[str, Expr]
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_call_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GetExpr(Expr):
|
|
object: Expr
|
|
name: str
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_get_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 VariableExpr(Expr):
|
|
name: str
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_variable_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LogicalExpr(Expr):
|
|
left: Expr
|
|
operator: ast.boolop
|
|
right: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_logical_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class SetExpr(Expr):
|
|
object: Expr
|
|
name: str
|
|
value: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_set_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CastExpr(Expr):
|
|
type: MidasType
|
|
expr: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_cast_expr(self)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TernaryExpr(Expr):
|
|
test: Expr
|
|
if_true: Expr
|
|
if_false: Expr
|
|
|
|
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
|
return visitor.visit_ternary_expr(self)
|