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); }
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); } }