示例#1
0
 /// <summary>
 /// Bind a name to this scope, including destructuring assignment.
 /// </summary>
 public void Bind(Analyzer analyzer, Exp target, DataType rvalue, BindingKind kind)
 {
     if (target is Identifier id)
     {
         this.Bind(analyzer, id, rvalue, kind);
     }
     else if (target is PyTuple tup)
     {
         this.Bind(analyzer, tup.values, rvalue, kind);
     }
     else if (target is PyList list)
     {
         this.Bind(analyzer, list.elts, rvalue, kind);
     }
     else if (target is AttributeAccess attr)
     {
         DataType targetType = TransformExp(analyzer, attr.Expression, this);
         setAttr(analyzer, attr, rvalue, targetType);
     }
     else if (target is ArrayRef sub)
     {
         DataType valueType = TransformExp(analyzer, sub.array, this);
         var      xform     = new TypeTransformer(this, analyzer);
         TransformExprs(analyzer, sub.subs, this);
         if (valueType is ListType t)
         {
             t.setElementType(UnionType.Union(t.eltType, rvalue));
         }
     }
     else if (target != null)
     {
         analyzer.putProblem(target, "invalid location for assignment");
     }
 }
示例#2
0
        public static void TransformExprs(Analyzer analyzer, List <Slice> exprs, State s)
        {
            var x = new TypeTransformer(s, analyzer);

            foreach (var e in exprs)
            {
                e.Accept(x);
            }
        }
示例#3
0
        public static void ApplyConstructor(Analyzer analyzer, InstanceType i, Application call, List <DataType> args)
        {
            var initFunc = i.Table.LookupAttributeType("__init__") as FunType;

            if (initFunc != null && initFunc.Definition != null)
            {
                ((FunType)initFunc).SelfType = i;
                TypeTransformer.Apply(analyzer, (FunType)initFunc, args, null, null, null, call);
                ((FunType)initFunc).SelfType = null;
            }
        }
示例#4
0
        public void ApplyUncalled()
        {
            IProgress progress = new Progress(this, uncalled.Count, 50, this.HasOption("quiet"));

            while (uncalled.Count != 0)
            {
                var uncalledDup = uncalled.ToList();
                foreach (FunType cl in uncalledDup)
                {
                    progress.Tick();
                    TypeTransformer.Apply(this, cl, null, null, null, null, null);
                }
            }
        }
示例#5
0
文件: Analyzer.cs 项目: yuhan0/pytocs
        public DataType LoadFile(string path)
        {
            path = FileSystem.GetFullPath(path);

            if (!FileSystem.FileExists(path))
            {
                return(null);
            }

            ModuleType module = GetCachedModule(path);

            if (module != null)
            {
                return(module);
            }

            // detect circular import
            if (InImportStack(path))
            {
                return(null);
            }

            // set new CWD and save the old one on stack
            string oldcwd = cwd;

            setCWD(FileSystem.GetDirectoryName(path));

            PushImportStack(path);
            loadingProgress.Tick();
            Debug.Print($"*** Path {path}");
            var      ast  = GetAstForFile(path);
            DataType type = null;

            if (ast == null)
            {
                failedToParse.Add(path);
            }
            else
            {
                loadedFiles.Add(path);
                type = new TypeTransformer(ModuleTable, this).VisitModule(ast);
            }
            PopImportStack(path);

            // restore old CWD
            setCWD(oldcwd);
            return(type);
        }
示例#6
0
        private DataType parseAndResolve(string file)
        {
            loadingProgress.Tick();
            Module ast = getAstForFile(file);

            if (ast == null)
            {
                failedToParse.Add(file);
                return(null);
            }
            else
            {
                DataType type = new TypeTransformer(ModuleTable, this).VisitModule(ast);
                loadedFiles.Add(file);
                return(type);
            }
        }
示例#7
0
        /// <summary>
        /// Bind a name to this scope, including destructuring assignment.
        /// </summary>
        public void Bind(Analyzer analyzer, Exp target, DataType rvalue, BindingKind kind)
        {
            if (target is Identifier)
            {
                this.Bind(analyzer, (Identifier)target, rvalue, kind);
            }
            else if (target is PyTuple)
            {
                this.Bind(analyzer, ((PyTuple)target).values, rvalue, kind);
            }
            else if (target is PyList)
            {
                this.Bind(analyzer, ((PyList)target).elts, rvalue, kind);
            }
            else if (target is AttributeAccess)
            {
                var      attr       = (AttributeAccess)target;
                DataType targetType = transformExpr(analyzer, attr.Expression, this);
                setAttr(analyzer, attr, rvalue, targetType);
            }
            else if (target is ArrayRef)
            {
                ArrayRef sub = (ArrayRef)target;

                DataType valueType = transformExpr(analyzer, sub.array, this);
                var      xform     = new TypeTransformer(this, analyzer);
                transformExprs(analyzer, sub.subs, this);
                if (valueType is ListType)
                {
                    ListType t = (ListType)valueType;
                    t.setElementType(UnionType.Union(t.eltType, rvalue));
                }
            }
            else if (target != null)
            {
                analyzer.putProblem(target, "invalid location for assignment");
            }
        }