public async Task HandleAnnotatedExpressionAsync(ExpressionWithAnnotation expr, IMember value, CancellationToken cancellationToken = default) { if (expr?.Annotation == null) { return; } var variableType = await Eval.GetTypeFromAnnotationAsync(expr.Annotation, cancellationToken); // If value is null, then this is a pure declaration like // x: List[str] // without a value. If value is provided, then this is // x: List[str] = [...] // Check value type for compatibility IMember instance = null; if (value != null) { var valueType = value.GetPythonType(); if (!variableType.IsUnknown() && !valueType.Equals(variableType)) { // TODO: warn incompatible value type. // TODO: verify values. Value may be list() while variable type is List[str]. // Leave it as variable type. } else { instance = value; } } instance = instance ?? variableType?.CreateInstance(variableType.Name, Eval.GetLoc(expr.Expression), ArgumentSet.Empty) ?? Eval.UnknownType; if (expr.Expression is NameExpression ne) { Eval.DeclareVariable(ne.Name, instance, expr.Expression); return; } if (expr.Expression is MemberExpression m) { // self.x : int = 42 var self = Eval.LookupNameInScopes("self", out var scope); var argType = self?.GetPythonType(); if (argType is PythonClassType cls && scope != null) { cls.AddMember(m.Name, instance, true); } } }
public void HandleAnnotatedExpression(ExpressionWithAnnotation expr, IMember value) { if (expr?.Annotation == null) { return; } var variableType = Eval.GetTypeFromAnnotation(expr.Annotation); // If value is null, then this is a pure declaration like // x: List[str] // without a value. If value is provided, then this is // x: List[str] = [...] HandleTypedVariable(variableType, value, expr.Expression); }
public override void PostWalk(ExpressionWithAnnotation node) { }
// ExpressionWithAnnotation public override bool Walk(ExpressionWithAnnotation node) { return(false); }
// ExpressionWithAnnotation public override bool Walk(ExpressionWithAnnotation node) { return(Location >= node.StartIndex && Location <= node.EndIndex); }
public virtual void PostWalk(ExpressionWithAnnotation node) { }
// ExpressionWithAnnotation public virtual bool Walk(ExpressionWithAnnotation node) { return(true); }
// ExpressionWithAnnotation public virtual bool Walk(ExpressionWithAnnotation node) { return true; }
// ExpressionWithAnnotation public override bool Walk(ExpressionWithAnnotation node) { return Location >= node.StartIndex && Location <= node.EndIndex; }
// ExpressionWithAnnotation public override bool Walk(ExpressionWithAnnotation node) { return false; }