public IMember CreateInstance(string typeName, IArgumentSet args)
        {
            var types = args.Values <IPythonType>();

            if (types.Count != args.Arguments.Count)
            {
                throw new ArgumentException(@"Generic type instance construction arguments must be all of IPythonType", nameof(args));
            }
            var specific = CreateSpecificType(types);

            return(specific == null
                ? DeclaringModule.Interpreter.UnknownType
                : specific.CreateInstance(typeName));
        }
        public static IMember Iterator(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan)
        {
            var args = argSet.Values <IMember>();

            if (args.Count > 0)
            {
                if (args[0] is IPythonCollection seq)
                {
                    return(seq.GetIterator());
                }
                var t = args[0].GetPythonType();
                if (t.IsBuiltin && t.Name == "str")
                {
                    return(new PythonTypeIterator(BuiltinTypeId.StrIterator, BuiltinTypeId.Str, module.Interpreter));
                }
            }
            return(null);
        }
        public static IMember Super(IPythonModule declaringModule, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan)
        {
            var args = argSet.Values <IMember>();

            if (args.Count == 0)
            {
                //Zero argument form only works inside a class definition
                foreach (var s in argSet.Eval.CurrentScope.EnumerateTowardsGlobal.Where(s => s.Node is ClassDefinition))
                {
                    var classType = s.Variables["__class__"].GetPythonType <IPythonClassType>();
                    return(PythonSuperType.Create(classType)?.CreateInstance(argSet));
                }
                return(null);
            }

            // If multiple arguments first argument is required
            var firstCls = args.FirstOrDefault().GetPythonType <IPythonClassType>();

            if (firstCls == null)
            {
                return(null);
            }

            // second argument optional
            bool isUnbound = args.Count == 1;

            if (isUnbound)
            {
                return(PythonSuperType.Create(firstCls)?.CreateInstance(argSet));
            }

            var secondCls = args[1].GetPythonType <IPythonClassType>();

            if (secondCls?.Equals(firstCls) == true ||
                secondCls?.IsSubClassOf(firstCls) == true)
            {
                // We walk the mro of the second parameter looking for the first
                return(PythonSuperType.Create(secondCls, typeToFind: firstCls)?.CreateInstance(argSet));
            }

            return(null);
        }
        public static IMember Next(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan)
        {
            var args = argSet.Values <IMember>();

            return(args.Count > 0 && args[0] is IPythonIterator it ? it.Next : null);
        }
        public static IMember Identity(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan)
        {
            var args = argSet.Values <IMember>();

            return(args.Count > 0 ? args.FirstOrDefault(a => !a.IsUnknown()) ?? args[0] : null);
        }
        public static IMember CollectionItem(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan)
        {
            var args = argSet.Values <IMember>();

            return(args.Count > 0 && args[0] is PythonCollection c?c.Contents.FirstOrDefault() : null);
        }
 public IMember GetReturnValue(LocationInfo callLocation, IArgumentSet args)
 {
     if (!_fromAnnotation)
     {
         // First try supplied specialization callback.
         var rt = _returnValueProvider?.Invoke(_declaringModule, this, callLocation, args.Values <IMember>());
         if (!rt.IsUnknown())
         {
             return(rt);
         }
     }
     return(StaticReturnValue);
 }
        public static IMember TypeInfo(IPythonModule module, IPythonFunctionOverload overload, LocationInfo location, IArgumentSet argSet)
        {
            var args = argSet.Values <IMember>();

            return(args.Count > 0 ? args[0].GetPythonType() : module.Interpreter.GetBuiltinType(BuiltinTypeId.Type));
        }
Esempio n. 9
0
 public override IMember CreateInstance(IArgumentSet args)
 => CreateNamedTuple(args.Values <IMember>(), DeclaringModule, default);