fix(checker): avoid raising on unknown variable
when an unknown variable is referenced, avoid raising an error and only report it with a diagnostic, returning `UnknownType` instead
This commit is contained in:
@@ -110,7 +110,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
return self._local_variables[name]
|
return self._local_variables[name]
|
||||||
return self.types.get_type(name)
|
return self.types.get_type(name)
|
||||||
|
|
||||||
def get_variable(self, name: str) -> Type:
|
def get_variable(self, location: Location, name: str) -> Type:
|
||||||
"""Get the type of a variable
|
"""Get the type of a variable
|
||||||
|
|
||||||
This function will first look into the current predicate's parameters if
|
This function will first look into the current predicate's parameters if
|
||||||
@@ -118,11 +118,9 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
The the variable is looked up in the preamble (i.e. global environment)
|
The the variable is looked up in the preamble (i.e. global environment)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
location (Location): the location of the variable reference
|
||||||
name (str): the name of the variable
|
name (str): the name of the variable
|
||||||
|
|
||||||
Raises:
|
|
||||||
NameError: if the variable cannot be found
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Type: the type of the variable
|
Type: the type of the variable
|
||||||
"""
|
"""
|
||||||
@@ -136,7 +134,8 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
if global_ is not None:
|
if global_ is not None:
|
||||||
return global_
|
return global_
|
||||||
|
|
||||||
raise NameError(f"Unknown variable '{name}'")
|
self.reporter.error(location, f"Unknown variable '{name}'")
|
||||||
|
return UnknownType()
|
||||||
|
|
||||||
def resolve(self, stmts: list[m.Stmt]):
|
def resolve(self, stmts: list[m.Stmt]):
|
||||||
"""Process a sequence of statements
|
"""Process a sequence of statements
|
||||||
@@ -350,7 +349,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
return member
|
return member
|
||||||
|
|
||||||
def visit_variable_expr(self, expr: m.VariableExpr) -> Type:
|
def visit_variable_expr(self, expr: m.VariableExpr) -> Type:
|
||||||
return self.get_variable(expr.name.lexeme)
|
return self.get_variable(expr.location, expr.name.lexeme)
|
||||||
|
|
||||||
def visit_grouping_expr(self, expr: m.GroupingExpr) -> Type:
|
def visit_grouping_expr(self, expr: m.GroupingExpr) -> Type:
|
||||||
return expr.expr.accept(self)
|
return expr.expr.accept(self)
|
||||||
@@ -370,7 +369,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
|||||||
return UnknownType()
|
return UnknownType()
|
||||||
|
|
||||||
def visit_wildcard_expr(self, expr: m.WildcardExpr) -> Type:
|
def visit_wildcard_expr(self, expr: m.WildcardExpr) -> Type:
|
||||||
return self.get_variable("_")
|
return self.get_variable(expr.location, "_")
|
||||||
|
|
||||||
def visit_named_type(self, type: m.NamedType) -> Type:
|
def visit_named_type(self, type: m.NamedType) -> Type:
|
||||||
name: str = type.name.lexeme
|
name: str = type.name.lexeme
|
||||||
|
|||||||
Reference in New Issue
Block a user