refactor(checker): rename methods

improve a couple methods names, namely evaluate → type_of and evaluate_block → process_block
This commit is contained in:
2026-06-03 13:03:41 +02:00
parent 9a934fabfd
commit 822a74acce

View File

@@ -74,7 +74,7 @@ class Checker(
message=message, message=message,
) )
def evaluate(self, expr: p.Expr) -> Type: def type_of(self, expr: p.Expr) -> Type:
"""Evaluate the type of an expression """Evaluate the type of an expression
Args: Args:
@@ -85,7 +85,7 @@ class Checker(
""" """
return expr.accept(self) return expr.accept(self)
def evaluate_block(self, block: list[p.Stmt], env: Environment) -> bool: def process_block(self, block: list[p.Stmt], env: Environment) -> bool:
"""Evaluate a sequence of statements """Evaluate a sequence of statements
Args: Args:
@@ -181,7 +181,7 @@ class Checker(
self.logger.debug(f"Midas operations: {self.ctx._operations}") self.logger.debug(f"Midas operations: {self.ctx._operations}")
def visit_expression_stmt(self, stmt: p.ExpressionStmt) -> None: def visit_expression_stmt(self, stmt: p.ExpressionStmt) -> None:
self.evaluate(stmt.expr) self.type_of(stmt.expr)
def visit_function(self, stmt: p.Function) -> None: def visit_function(self, stmt: p.Function) -> None:
env: Environment = Environment(self.env) env: Environment = Environment(self.env)
@@ -237,7 +237,7 @@ class Checker(
) )
self.env.define(stmt.name, inside_function) self.env.define(stmt.name, inside_function)
returned: bool = self.evaluate_block(stmt.body, env) returned: bool = self.process_block(stmt.body, env)
inferred_return: Type = UnknownType() inferred_return: Type = UnknownType()
if not returned: if not returned:
env.return_types.append(UnitType()) env.return_types.append(UnitType())
@@ -278,7 +278,7 @@ class Checker(
self.env.define(stmt.name, type) self.env.define(stmt.name, type)
def visit_assign_stmt(self, stmt: p.AssignStmt) -> None: def visit_assign_stmt(self, stmt: p.AssignStmt) -> None:
value: Type = self.evaluate(stmt.value) value: Type = self.type_of(stmt.value)
for target in stmt.targets: for target in stmt.targets:
if not isinstance(target, p.VariableExpr): if not isinstance(target, p.VariableExpr):
self.logger.warning(f"Unsupported assignment to {target}") self.logger.warning(f"Unsupported assignment to {target}")
@@ -317,8 +317,8 @@ class Checker(
) )
env: Environment = Environment(self.env) env: Environment = Environment(self.env)
body_returned: bool = self.evaluate_block(stmt.body, env) body_returned: bool = self.process_block(stmt.body, env)
else_returned: bool = self.evaluate_block(stmt.orelse, env) else_returned: bool = self.process_block(stmt.orelse, env)
self.env.return_types.extend(env.return_types) self.env.return_types.extend(env.return_types)
if body_returned and else_returned: if body_returned and else_returned:
raise ReturnException() raise ReturnException()
@@ -329,8 +329,8 @@ class Checker(
self.logger.warning(f"Unsupported operator {expr.operator}") self.logger.warning(f"Unsupported operator {expr.operator}")
self.warning(expr.location, f"Unsupported operator {expr.operator}") self.warning(expr.location, f"Unsupported operator {expr.operator}")
return UnknownType() return UnknownType()
left: Type = self.evaluate(expr.left) left: Type = self.type_of(expr.left)
right: Type = self.evaluate(expr.right) right: Type = self.type_of(expr.right)
result: Optional[Type] = self.ctx.get_operation_result(left, method, right) result: Optional[Type] = self.ctx.get_operation_result(left, method, right)
if result is None: if result is None:
@@ -347,8 +347,8 @@ class Checker(
self.logger.warning(f"Unsupported operator {expr.operator}") self.logger.warning(f"Unsupported operator {expr.operator}")
self.warning(expr.location, f"Unsupported operator {expr.operator}") self.warning(expr.location, f"Unsupported operator {expr.operator}")
return UnknownType() return UnknownType()
left: Type = self.evaluate(expr.left) left: Type = self.type_of(expr.left)
right: Type = self.evaluate(expr.right) right: Type = self.type_of(expr.right)
result: Optional[Type] = self.ctx.get_operation_result(left, method, right) result: Optional[Type] = self.ctx.get_operation_result(left, method, right)
if result is None: if result is None:
@@ -365,7 +365,7 @@ class Checker(
if path := self.parse_midas_import(expr): if path := self.parse_midas_import(expr):
self.import_midas(path) self.import_midas(path)
return UnknownType() return UnknownType()
callee: Type = self.evaluate(expr.callee) callee: Type = self.type_of(expr.callee)
if not isinstance(callee, Function): if not isinstance(callee, Function):
self.error(expr.callee.location, "Callee is not a function") self.error(expr.callee.location, "Callee is not a function")
return UnknownType() return UnknownType()
@@ -460,10 +460,10 @@ class Checker(
list[MappedArgument]: the list of mapped arguments list[MappedArgument]: the list of mapped arguments
""" """
positional: list[tuple[p.Expr, Type]] = [ positional: list[tuple[p.Expr, Type]] = [
(arg, self.evaluate(arg)) for arg in call.arguments (arg, self.type_of(arg)) for arg in call.arguments
] ]
keywords: dict[str, tuple[p.Expr, Type]] = { keywords: dict[str, tuple[p.Expr, Type]] = {
name: (arg, self.evaluate(arg)) for name, arg in call.keywords.items() name: (arg, self.type_of(arg)) for name, arg in call.keywords.items()
} }
set_args: set[str] = set() set_args: set[str] = set()