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);
        }
示例#3
0
 public override void PostWalk(ExpressionWithAnnotation node)
 {
 }
示例#4
0
 // ExpressionWithAnnotation
 public override bool Walk(ExpressionWithAnnotation node)
 {
     return(false);
 }
示例#5
0
 // ExpressionWithAnnotation
 public override bool Walk(ExpressionWithAnnotation node)
 {
     return(Location >= node.StartIndex && Location <= node.EndIndex);
 }
示例#6
0
 public virtual void PostWalk(ExpressionWithAnnotation node)
 {
 }
示例#7
0
 // ExpressionWithAnnotation
 public virtual bool Walk(ExpressionWithAnnotation node)
 {
     return(true);
 }
示例#8
0
 public virtual void PostWalk(ExpressionWithAnnotation node) { }
示例#9
0
 // ExpressionWithAnnotation
 public virtual bool Walk(ExpressionWithAnnotation node) { return true; }
示例#10
0
 // ExpressionWithAnnotation
 public override bool Walk(ExpressionWithAnnotation node) { return Location >= node.StartIndex && Location <= node.EndIndex; }
示例#11
0
 public override void PostWalk(ExpressionWithAnnotation node) { }
示例#12
0
 // ExpressionWithAnnotation
 public override bool Walk(ExpressionWithAnnotation node) { return false; }