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