示例#1
0
文件: Import.cs 项目: Natsu13/Pyr2
        public Import(Token whatimpot, Block _block, Interpreter inter, string _as = null)
        {
            __block  = _block;
            this._as = _as;
            var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            this.import = whatimpot;
            symbolTable = inter.SymbolTable;

            assingBlock = _block;
            var module = GetModule();

            if (!(_block.SymbolTable.Get(module) is Error))
            {
                _ihaveit = _block.SymbolTable.Get(module);
            }

            if (Interpreter.Imports.ContainsKey(module))
            {
                _ihaveit = Interpreter.Imports[module];
            }
            else
            {
                Interpreter.Imports.Add(module, this);
            }

            if (inter != null && _ihaveit == null && inter.parent != null)
            {
                var find = inter.parent.MainBlock.SymbolTable.Get(module);
                if (!(find is Error))
                {
                    _ihaveit = find;
                }
            }

            if (inter != null && _ihaveit == null && inter.parent != null)
            {
                var mod = inter.parent.FindImport(module);
                if (mod)
                {
                    _ihaveit = inter.parent.GetImport(module);
                }
            }

            string path = whatimpot.Value.Replace('.', '\\');

            if (File.Exists(dir + "\\" + Program.projectFolder + @"\" + path + ".p"))
            {
                found = true;
                if (_ihaveit == null)
                {
                    //Console.WriteLine("[ADD]"+inter.File + "\tAdding " + GetName() + ", module: " + GetModule());
                    _code               = File.ReadAllText(dir + "\\" + Program.projectFolder + @"\" + path + ".p");
                    interpret           = new Interpreter(_code, "" + path + ".p", inter, symbolTable: symbolTable);
                    interpret.isConsole = inter.isConsole;
                    block               = (Block)interpret.Interpret();
                    block.import        = this;
                    if (_as == null)
                    {
                        Block beforeblock = null;
                        Block ___block    = _block;
                        foreach (var part in GetName().Split('.').Take(GetName().Split('.').Count()))
                        {
                            if (part == "")
                            {
                                continue;
                            }
                            var find = ___block.SymbolTable.Get(part);
                            if (!(find is Error))
                            {
                                ___block    = find.assingBlock;
                                beforeblock = ___block;
                            }
                            else
                            {
                                //Block b = new Block(_block.Interpret);
                                Block b = block;
                                b.BlockParent = ___block;
                                Class c = new Class(new Token(Token.Type.ID, part), b, null)
                                {
                                    isForImport = true
                                };
                                c.assignTo = part;
                                c.block.SymbolTable.isForImport = true;
                                beforeblock?.SymbolTable.Add(part, c, true);
                                if (beforeblock != ___block)
                                {
                                    ___block.SymbolTable.Add(part, c);
                                    ___block.children.Add(c);
                                    c.parent = ___block;
                                }
                                beforeblock = c.block;
                                ___block    = b;
                            }
                        }
                        if (___block.SymbolTable.Get(module) is Error)
                        {
                            ___block.SymbolTable.Add(module, this, true);
                            //block.SymbolTable.Copy(whatimpot.Value.Split('.').Last(), _as);
                        }
                    }
                    else
                    {
                        _block.SymbolTable.Add(_as, this);
                        block.SymbolTable.Copy(whatimpot.Value.Split('.').Last(), _as);
                        this._as = string.Join(".", whatimpot.Value.Split('.').Take(whatimpot.Value.Split('.').Length - 1));
                    }
                }
                else
                {
                    //Console.WriteLine("[HAS]"+inter.File + "\tAdding " + GetName() + ", module: " + GetModule());
                    if (_as == null)
                    {
                        //_block.SymbolTable.Get("Array.Clear");
                        if (_block.SymbolTable.Get(GetName()) is Error)
                        {
                            if (_ihaveit.assingBlock?.BlockParent.import == null || _ihaveit is Import)
                            {
                                _block.SymbolTable.Add(GetName(), _ihaveit);
                            }
                            else
                            {
                                _block.SymbolTable.Add(GetName(), _ihaveit.assingBlock?.BlockParent.import);
                            }
                        }
                    }
                    else
                    {
                        _block.SymbolTable.Add(_as, _ihaveit.assingBlock.BlockParent.import);
                        //Block block = new Block(interpret);
                        //block.SymbolTable.Copy(whatimpot.Value.Split('.').Last(), _as);
                        this._as = string.Join(".", whatimpot.Value.Split('.').Take(whatimpot.Value.Split('.').Length - 1));
                    }
                }
            }
        }