refactor: reuse CallDispatcher
This commit is contained in:
@@ -74,6 +74,9 @@ class CallDispatcher(Generic[E]):
|
|||||||
self.reporter: FileReporter = reporter
|
self.reporter: FileReporter = reporter
|
||||||
self.logger: logging.Logger = logging.getLogger("CallDispatcher")
|
self.logger: logging.Logger = logging.getLogger("CallDispatcher")
|
||||||
|
|
||||||
|
def set_reporter(self, reporter: FileReporter):
|
||||||
|
self.reporter = reporter
|
||||||
|
|
||||||
def get_result(
|
def get_result(
|
||||||
self,
|
self,
|
||||||
location: Location,
|
location: Location,
|
||||||
|
|||||||
@@ -62,8 +62,11 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
def __init__(self, types: TypesRegistry, reporter: Reporter) -> None:
|
def __init__(self, types: TypesRegistry, reporter: Reporter) -> None:
|
||||||
self.logger: logging.Logger = logging.getLogger("MidasTyper")
|
self.logger: logging.Logger = logging.getLogger("MidasTyper")
|
||||||
self.reporter: FileReporter = reporter.for_file(None)
|
self.reporter: FileReporter = reporter.for_file(None)
|
||||||
|
|
||||||
self.types: TypesRegistry = types
|
self.types: TypesRegistry = types
|
||||||
|
self.dispatcher: CallDispatcher[m.Expr] = CallDispatcher[m.Expr](
|
||||||
|
self.types, self.reporter
|
||||||
|
)
|
||||||
|
|
||||||
self._local_variables: dict[str, TypeVar] = {}
|
self._local_variables: dict[str, TypeVar] = {}
|
||||||
|
|
||||||
self._predicate_params: dict[str, Type] = {}
|
self._predicate_params: dict[str, Type] = {}
|
||||||
@@ -78,8 +81,14 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
|
|
||||||
self._preamble: Environment = Preamble(self.types)
|
self._preamble: Environment = Preamble(self.types)
|
||||||
|
|
||||||
|
def set_reporter(self, reporter: FileReporter):
|
||||||
|
self.reporter = reporter
|
||||||
|
self.dispatcher.set_reporter(reporter)
|
||||||
|
|
||||||
def process(self, source: str, path: Optional[str]):
|
def process(self, source: str, path: Optional[str]):
|
||||||
self.reporter = self.reporter.for_file(path)
|
reporter: FileReporter = self.reporter.for_file(path)
|
||||||
|
self.set_reporter(reporter)
|
||||||
|
|
||||||
lexer: MidasLexer = MidasLexer(source)
|
lexer: MidasLexer = MidasLexer(source)
|
||||||
tokens: list[Token] = lexer.process()
|
tokens: list[Token] = lexer.process()
|
||||||
parser: MidasParser = MidasParser(tokens)
|
parser: MidasParser = MidasParser(tokens)
|
||||||
@@ -254,8 +263,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
)
|
)
|
||||||
return UnknownType()
|
return UnknownType()
|
||||||
|
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=location,
|
location=location,
|
||||||
callee=operation,
|
callee=operation,
|
||||||
positional=[(right_expr, right)],
|
positional=[(right_expr, right)],
|
||||||
@@ -281,8 +289,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
)
|
)
|
||||||
return UnknownType()
|
return UnknownType()
|
||||||
|
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=expr.location,
|
location=expr.location,
|
||||||
callee=operation,
|
callee=operation,
|
||||||
positional=[],
|
positional=[],
|
||||||
@@ -298,8 +305,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
keywords: dict[str, tuple[m.Expr, Type]] = {
|
keywords: dict[str, tuple[m.Expr, Type]] = {
|
||||||
name: (arg, self.type_of(arg)) for name, arg in expr.keywords.items()
|
name: (arg, self.type_of(arg)) for name, arg in expr.keywords.items()
|
||||||
}
|
}
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=expr.location,
|
location=expr.location,
|
||||||
callee=callee,
|
callee=callee,
|
||||||
positional=positional,
|
positional=positional,
|
||||||
|
|||||||
@@ -84,9 +84,17 @@ class PythonTyper(
|
|||||||
self.locals: dict[p.Expr, int] = {}
|
self.locals: dict[p.Expr, int] = {}
|
||||||
self.judgements: list[tuple[p.Expr, Type]] = []
|
self.judgements: list[tuple[p.Expr, Type]] = []
|
||||||
self.evaluated_casts: list[p.CastExpr] = []
|
self.evaluated_casts: list[p.CastExpr] = []
|
||||||
|
self.dispatcher: CallDispatcher[p.Expr] = CallDispatcher[p.Expr](
|
||||||
|
self.types, self.reporter
|
||||||
|
)
|
||||||
|
|
||||||
|
def set_reporter(self, reporter: FileReporter):
|
||||||
|
self.reporter = reporter
|
||||||
|
self.dispatcher.set_reporter(self.reporter)
|
||||||
|
|
||||||
def process(self, source: str, path: Optional[str]) -> TypedAST:
|
def process(self, source: str, path: Optional[str]) -> TypedAST:
|
||||||
self.reporter = self.reporter.for_file(path)
|
reporter: FileReporter = self.reporter.for_file(path)
|
||||||
|
self.set_reporter(reporter)
|
||||||
|
|
||||||
tree: ast.Module = ast.parse(source, filename=path or "<unknown>")
|
tree: ast.Module = ast.parse(source, filename=path or "<unknown>")
|
||||||
parser = PythonParser()
|
parser = PythonParser()
|
||||||
@@ -221,8 +229,7 @@ class PythonTyper(
|
|||||||
if method is None:
|
if method is None:
|
||||||
raise UndefinedMethodException
|
raise UndefinedMethodException
|
||||||
|
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=location,
|
location=location,
|
||||||
callee=method,
|
callee=method,
|
||||||
positional=positional,
|
positional=positional,
|
||||||
@@ -572,8 +579,7 @@ class PythonTyper(
|
|||||||
)
|
)
|
||||||
|
|
||||||
callee: Type = self.type_of(expr.callee)
|
callee: Type = self.type_of(expr.callee)
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=expr.location,
|
location=expr.location,
|
||||||
callee=callee,
|
callee=callee,
|
||||||
positional=positional,
|
positional=positional,
|
||||||
@@ -742,8 +748,7 @@ class PythonTyper(
|
|||||||
return UnknownType()
|
return UnknownType()
|
||||||
|
|
||||||
index: Type = self.type_of(expr.index)
|
index: Type = self.type_of(expr.index)
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=expr.location,
|
location=expr.location,
|
||||||
callee=operation,
|
callee=operation,
|
||||||
positional=[(expr.index, index)],
|
positional=[(expr.index, index)],
|
||||||
@@ -808,8 +813,7 @@ class PythonTyper(
|
|||||||
|
|
||||||
index: p.Expr = p.LiteralExpr(location=expr.location, value=0)
|
index: p.Expr = p.LiteralExpr(location=expr.location, value=0)
|
||||||
index_type: Type = self.compute_type(index)
|
index_type: Type = self.compute_type(index)
|
||||||
dispatcher = CallDispatcher(self.types, self.reporter)
|
result: CallResult = self.dispatcher.get_result(
|
||||||
result: CallResult = dispatcher.get_result(
|
|
||||||
location=expr.location,
|
location=expr.location,
|
||||||
callee=getitem,
|
callee=getitem,
|
||||||
positional=[(index, index_type)],
|
positional=[(index, index_type)],
|
||||||
|
|||||||
Reference in New Issue
Block a user