Ejemplo n.º 1
0
 public SelectorExpr(Expr x, Ident sel, TokenPos pos)
 {
     X        = x;
     Selector = sel;
     DotPos   = pos;
     BuildRelation();
 }
Ejemplo n.º 2
0
        Expr ParseNewExpr()
        {
            var oppos = CurrTokenPos;

            Expect(TokenType.New);
            Expect(TokenType.LParen);

            Ident pkgName = null;
            Ident className;

            Ident nameA = ParseIdent();
            Ident nameB;

            if (CurrTokenType == TokenType.Dot)
            {
                Next();
                nameB     = ParseIdent();
                className = nameB;
                pkgName   = nameA;
            }
            else
            {
                className = nameA;
            }

            Expect(TokenType.RParen);


            return(new NewExpr(className, pkgName, oppos));
        }
Ejemplo n.º 3
0
        public DelegateDeclare(Ident name, FuncType ft)
        {
            Name     = name;
            TypeInfo = ft;

            BuildRelation();
        }
Ejemplo n.º 4
0
        public NewExpr(Ident className, Ident pkgName, TokenPos newpos)
        {
            NewPos      = newpos;
            ClassName   = className;
            PackageName = pkgName;

            BuildRelation();
        }
Ejemplo n.º 5
0
        public ConstDeclareStmt(Ident name, Expr value, TokenPos defpos)
        {
            Name     = name;
            ConstPos = defpos;
            Value    = value;

            BuildRelation();
        }
Ejemplo n.º 6
0
        public ClassDeclare(Ident name, Ident parentName, Scope s, List <Ident> member, TokenPos classpos)
        {
            Name       = name;
            ParentName = parentName;
            ClassPos   = classpos;
            Member     = member;
            ScopeInfo  = s;

            BuildRelation();
        }
Ejemplo n.º 7
0
        public ForRangeStmt(Ident key, Ident value, Expr x, TokenPos inpos, TokenPos defpos, Scope s, BlockStmt body)
        {
            Key       = key;
            Value     = value;
            X         = x;
            InPos     = inpos;
            Pos       = defpos;
            ScopeInfo = s;
            Body      = body;

            BuildRelation();
        }
Ejemplo n.º 8
0
        Ident DelcareIteratorVar( )
        {
            var iter = new Ident(new Token(Pos, null, "@Iterator"));

            iter.BaseScope        = ScopeInfo;
            iter.Symbol           = new Symbol();
            iter.Symbol.Name      = iter.Name;
            iter.Symbol.Decl      = this;
            iter.Symbol.DefinePos = Pos;
            iter.Symbol.Usage     = SymbolUsage.Variable;
            ScopeInfo.Insert(iter.Symbol);

            return(iter);
        }
Ejemplo n.º 9
0
        Stmt ParseClassDecl( )
        {
            List <Ident> memberVar = new List <Ident>();

            var defpos = CurrTokenPos;

            Expect(TokenType.Class);

            var className = ParseIdent();

            var scope = ScopeMgr.OpenClassScope(className.Name, defpos);


            Ident parentName = null;
            var   colonPos   = CurrTokenPos;

            if (CurrTokenType == TokenType.Colon)
            {
                Next();

                parentName = ParseIdent();
            }

            Expect(TokenType.LBrace);


            var decl = new ClassDeclare(className, parentName, scope, memberVar, defpos);

            if (parentName != null)
            {
                decl.ColonPos = colonPos;
            }

            className.Symbol = ScopeManager.Declare(decl, ScopeMgr.PackageScope, className.Name, className.DefinePos, SymbolUsage.Class);

            while (CurrTokenType != TokenType.RBrace)
            {
                var param = ParseIdent();
                memberVar.Add(param);

                ScopeManager.Declare(param, scope, param.Name, param.DefinePos, SymbolUsage.Member);
            }

            ScopeMgr.CloseScope();

            Next();

            return(decl);
        }
Ejemplo n.º 10
0
        void ResolveSelectorElement(Expr x, Ident sel, TokenPos dotpos)
        {
            var xident = x as Ident;

            if (xident == null)
            {
                return;
            }

            if (xident.Symbol == null)
            {
                throw new CompileException(string.Format("{0} not defined", xident.Name), dotpos);
            }

            switch (xident.Symbol.Usage)
            {
            // 包.函数名
            case SymbolUsage.Package:
            {
                var pkg = Exe.GetPackageByName(xident.Name);
                if (pkg == null)
                {
                    throw new CompileException("package not found: " + xident.Name, dotpos);
                }

                // 包必须有一个顶级作用域
                if (pkg.TopScope == null)
                {
                    throw new CompileException("package should have a scope: " + xident.Name, dotpos);
                }


                ScopeMgr.Resolve(sel, pkg.TopScope);
            }
            break;

            // 实例.函数名
            case SymbolUsage.Variable:
            case SymbolUsage.Parameter:
            case SymbolUsage.SelfParameter:
            {
            }
            break;

            default:
                throw new CompileException("unknown usage", dotpos);
            }
        }
Ejemplo n.º 11
0
        void AddRefUpvalueInNode(Scope baseScope, Scope regScope, Ident var)
        {
            Scope s = baseScope.Outter;

            int level = 0;

            while (regScope != s)
            {
                var fl = Scope2FuncLitNode(s, Parent);
                if (fl != null)
                {
                    // 给这一层增加一个引用变量
                    fl.UpValues.Add(var);
                }

                s = s.Outter;
                level--;
            }
        }
Ejemplo n.º 12
0
        Stmt ParseFuncDecl()
        {
            var funcPos = CurrTokenPos;

            Expect(TokenType.Func);

            var scope = ScopeMgr.OpenScope(ScopeType.Function, funcPos);

            Ident funcName;
            Ident className = null;

            var NameA = ParseIdent();

            Scope funcAtScope;

            if (CurrTokenType == TokenType.Dot)
            {
                Next();

                funcName = ParseIdent();

                className = NameA;

                funcAtScope = ScopeMgr.GetClassScope(className.Name);

                // 主定义文件还未出现, 所以暂时创建空的scope
                if (funcAtScope == null)
                {
                    funcAtScope = ScopeMgr.OpenClassScope(className.Name, funcPos);
                    ScopeMgr.CloseScope();
                }
            }
            else
            {
                funcAtScope = ScopeMgr.PackageScope;
                funcName    = NameA;
            }


            var paramlist = ParseParameters(scope, className != null);

            if (CurrTokenType == TokenType.LBrace)
            {
                var decl = new FuncDeclare(funcName, new FuncType(funcPos, paramlist, scope));
                decl.ClassName = className;

                funcName.Symbol = ScopeManager.Declare(decl, funcAtScope, funcName.Name, funcName.DefinePos, SymbolUsage.Func);


                decl.Body = ParseBody(scope);

                decl.BuildRelation();



                return(decl);
            }
            else
            {
                // 声明已经结束
                ScopeMgr.CloseScope();

                // 函数前置声明
                var decl = new DelegateDeclare(funcName, new FuncType(funcPos, paramlist, scope));

                funcName.Symbol = ScopeManager.Declare(decl, ScopeMgr.PackageScope, funcName.Name, funcName.DefinePos, SymbolUsage.Delegate);

                return(decl);
            }
        }
Ejemplo n.º 13
0
        public FuncDeclare(Ident name, FuncType ft)
        {
            Name = name;

            TypeInfo = ft;
        }
Ejemplo n.º 14
0
 public UpvaluePair(Ident id)
 {
     _id = id;
 }