public IMember GetValueFromSet(SetExpression expression)
        {
            var contents = new List <IMember>();

            foreach (var item in expression.Items)
            {
                var value = GetValueFromExpression(item) ?? UnknownType;
                contents.Add(value);
            }
            return(PythonCollectionType.CreateSet(Interpreter, GetLoc(expression), contents));
        }
示例#2
0
        public IMember GetValueFromSet(SetExpression expression)
        {
            var contents = new List <IMember>();

            foreach (var item in expression.Items.Take(MaxCollectionSize))
            {
                var value = GetValueFromExpression(item) ?? UnknownType;
                contents.Add(value);
            }
            return(PythonCollectionType.CreateSet(Module, contents, exact: expression.Items.Count <= MaxCollectionSize));
        }
        public async Task <IMember> GetValueFromSetAsync(SetExpression expression, CancellationToken cancellationToken = default)
        {
            var contents = new List <IMember>();

            foreach (var item in expression.Items)
            {
                var value = await GetValueFromExpressionAsync(item, cancellationToken) ?? UnknownType;

                contents.Add(value);
            }
            return(PythonCollectionType.CreateSet(Interpreter, GetLoc(expression), contents));
        }
示例#4
0
        public IMember GetValueFromComprehension(Comprehension node)
        {
            var oldVariables = CurrentScope.Variables.OfType <Variable>().ToDictionary(k => k.Name, v => v);

            try {
                ProcessComprehension(node);

                // TODO: Evaluate comprehensions to produce exact contents, if possible.
                switch (node)
                {
                case ListComprehension lc:
                    var v1 = GetValueFromExpression(lc.Item) ?? UnknownType;
                    return(PythonCollectionType.CreateList(Module, new[] { v1 }));

                case SetComprehension sc:
                    var v2 = GetValueFromExpression(sc.Item) ?? UnknownType;
                    return(PythonCollectionType.CreateSet(Module, new[] { v2 }));

                case DictionaryComprehension dc:
                    var k = GetValueFromExpression(dc.Key) ?? UnknownType;
                    var v = GetValueFromExpression(dc.Value) ?? UnknownType;
                    return(new PythonDictionary(new PythonDictionaryType(Interpreter.ModuleResolution.BuiltinsModule), new Dictionary <IMember, IMember> {
                        { k, v }
                    }));
                }

                return(UnknownType);
            } finally {
                // Remove temporary variables since this is assignment and the right hand
                // side comprehension does not leak internal variables into the scope.
                var newVariables = CurrentScope.Variables.ToDictionary(k => k.Name, v => v);
                var variables    = (VariableCollection)CurrentScope.Variables;
                foreach (var kvp in newVariables)
                {
                    if (!oldVariables.ContainsKey(kvp.Key))
                    {
                        variables.RemoveVariable(kvp.Key);
                    }
                }
            }
        }