Esempio n. 1
0
        List <ImportStmt> ParseImportStmt( )
        {
            var importList = new List <ImportStmt>();

            while (CurrTokenType == TokenType.Import &&
                   CurrTokenType != TokenType.EOF)
            {
                var defpos = CurrTokenPos;
                Expect(TokenType.Import);


                var tk = Expect(TokenType.QuotedString);

                var pkgName = new BasicLit(tk.Value, (TokenType)tk.MatcherID, tk.Pos);

                var n = new ImportStmt(pkgName, defpos);
                importList.Add(n);

                ScopeManager.Declare(n, ScopeMgr.PackageScope, tk.Value, defpos, SymbolUsage.Package);

                // 如果包存在, 就不会在定义
                var pkg = Exe.GetPackageByName(tk.Value);
                if (pkg == null)
                {
                    Compiler.Import(_exe, _loader, tk.Value, tk.Value, ImportMode.Directory);
                }
            }

            return(importList);
        }
Esempio n. 2
0
        Stmt ParseVarDecl()
        {
            var defpos = CurrTokenPos;

            Expect(TokenType.Var);

            var idents = ParseIdentList();

            foreach (var i in idents)
            {
                ScopeManager.Declare(i, ScopeMgr.TopScope, i.Name, i.DefinePos, SymbolUsage.Variable);
            }

            List <Expr> values = new List <Expr>();

            var assignPos = CurrTokenPos;

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

                values = ParseRHSList();

                var lhs = new List <Expr>();
                foreach (var id in idents)
                {
                    lhs.Add(id);
                }

                return(new AssignStmt(lhs, values, assignPos, TokenType.Assign));
            }

            return(new VarDeclareStmt(idents, defpos));
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        Stmt ParseConstDecl()
        {
            var defpos = CurrTokenPos;

            Expect(TokenType.Const);

            var ident = ParseIdent();

            ScopeManager.Declare(ident, ScopeMgr.TopScope, ident.Name, ident.DefinePos, SymbolUsage.Constant);

            List <Expr> values = new List <Expr>();

            var assignPos = CurrTokenPos;

            Expect(TokenType.Assign);

            var value = ParseExpr(false);

            return(new ConstDeclareStmt(ident, value, defpos));
        }
Esempio n. 5
0
        List <Ident> ParseParameters(Scope s, bool isMethod)
        {
            Expect(TokenType.LParen);

            List <Ident> p = new List <Ident>();


            if (CurrTokenType != TokenType.RParen)
            {
                while (true)
                {
                    var param = ParseIdent();
                    p.Add(param);


                    SymbolUsage usage;
                    if (isMethod && p.Count == 1)
                    {
                        usage = SymbolUsage.SelfParameter;
                    }
                    else
                    {
                        usage = SymbolUsage.Parameter;
                    }


                    ScopeManager.Declare(param, s, param.Name, param.DefinePos, usage);

                    if (CurrTokenType != TokenType.Comma)
                    {
                        break;
                    }

                    Next();
                }
            }

            Expect(TokenType.RParen);

            return(p);
        }
Esempio n. 6
0
        Stmt ParseForStmt()
        {
            var defPos = CurrTokenPos;

            Expect(TokenType.For);

            var forscope = ScopeMgr.OpenScope(ScopeType.For, defPos);

            Stmt s1 = null;
            Stmt s2 = null;
            Stmt s3 = null;

            // 可能是foreach
            if (CurrTokenType != TokenType.SemiColon)
            {
                var idents = ParseIdentList();

                foreach (var i in idents)
                {
                    ScopeManager.Declare(i, ScopeMgr.TopScope, i.Name, i.DefinePos, SymbolUsage.Variable);
                }

                var opPos = CurrTokenPos;

                switch (CurrTokenType)
                {
                // for numeral
                case TokenType.Assign:
                {
                    Next();

                    var values = ParseRHSList();

                    var lhs = new List <Expr>();
                    foreach (var id in idents)
                    {
                        lhs.Add(id);
                    }

                    s1 = new AssignStmt(lhs, values, opPos, TokenType.Assign);
                }
                break;

                // for k, v in x {}
                case TokenType.In:
                {
                    Next();

                    var x = ParseRHS();

                    var body = ParseBlockStmt();

                    ScopeMgr.CloseScope();

                    if (idents.Count != 2)
                    {
                        throw new CompileException("Expect key & value in for-range", CurrTokenPos);
                    }

                    return(new ForRangeStmt(idents[0], idents[1], x, opPos, defPos, forscope, body));
                }

                default:
                    throw new CompileException("Expect '=' or 'in' in for statement", CurrTokenPos);
                }
            }

            // s1和s2之间的分号
            Expect(TokenType.SemiColon);

            if (CurrTokenType != TokenType.SemiColon)
            {
                s2 = ParseSimpleStmt();
            }

            // s2和s3之间的分号
            Expect(TokenType.SemiColon);

            if (CurrTokenType != TokenType.LBrace)
            {
                s3 = ParseSimpleStmt();
            }

            var body2 = ParseBlockStmt();

            ScopeMgr.CloseScope();

            Expr condition = null;

            if (s2 != null)
            {
                condition = (s2 as ExprStmt).X[0];
            }

            return(new ForStmt(s1, condition, s3, defPos, forscope, body2));
        }
Esempio n. 7
0
 public Parser(Executable exe, ContentLoader loader, ScopeManager scopemgr)
 {
     _exe      = exe;
     _loader   = loader;
     _scopemgr = scopemgr;
 }
Esempio n. 8
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);
            }
        }
Esempio n. 9
0
 internal Package(string name, bool native)
 {
     _name         = name;
     _native       = native;
     _scopeManager = new ScopeManager();
 }
Esempio n. 10
0
 public Package( )
 {
     _scopeManager = new ScopeManager();
 }