Skip to content

Commit 955876f

Browse files
committed
Refactored some more of the global symbol table code
1 parent 419adbe commit 955876f

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

java2go.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,9 @@ or to fix crashes with the symbol handling`,
113113
continue
114114
}
115115

116-
symbols := symbol.ParseSymbols(file.Ast, file.Source)
117-
118-
files[index].Symbols = symbols
119-
120-
if _, exist := symbol.GlobalScope.Packages[symbols.Package]; !exist {
121-
symbol.GlobalScope.Packages[symbols.Package] = &symbol.PackageScope{Files: make(map[string]*symbol.FileScope)}
122-
}
123-
124-
symbol.GlobalScope.Packages[symbols.Package].AddSymbolsFromFile(symbols)
116+
symbols := files[index].ParseSymbols()
117+
// Add the symbols to the global symbol table
118+
symbol.AddSymbolsToPackage(symbols)
125119
}
126120

127121
// Go back through the symbol tables and fill in anything that could not be resolved

parsing/source_file.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func (file *SourceFile) ParseAST() error {
3232
return nil
3333
}
3434

35-
func (file *SourceFile) ParseSymbols() {
36-
file.Symbols = symbol.ParseSymbols(file.Ast, file.Source)
35+
func (file *SourceFile) ParseSymbols() *symbol.FileScope {
36+
symbols := symbol.ParseSymbols(file.Ast, file.Source)
37+
file.Symbols = symbols
38+
return symbols
3739
}

symbol/globals.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package symbol
22

33
var (
4-
// The global symbol table
4+
// GlobalScope represents the global symbol table, and contains a mapping
5+
// between the package's path, and its symbols
6+
//
7+
// Example:
8+
// "net.java.math" -> Symbols { Vectors, Cos }
59
GlobalScope = &GlobalSymbols{Packages: make(map[string]*PackageScope)}
610
)
711

12+
// AddSymbolsToPackage adds a given file's symbols to the global package scope
13+
func AddSymbolsToPackage(symbols *FileScope) {
14+
if _, exist := GlobalScope.Packages[symbols.Package]; !exist {
15+
GlobalScope.Packages[symbols.Package] = NewPackageScope()
16+
}
17+
GlobalScope.Packages[symbols.Package].Files[symbols.BaseClass.Class.Name] = symbols
18+
}
19+
820
// A GlobalSymbols represents a global view of all the packages in the parsed source
921
type GlobalSymbols struct {
1022
// Every package's path associatedd with its definition

symbol/package_scope.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ type PackageScope struct {
66
Files map[string]*FileScope
77
}
88

9+
func NewPackageScope() *PackageScope {
10+
return &PackageScope{
11+
Files: make(map[string]*FileScope),
12+
}
13+
}
14+
915
func (ps *PackageScope) ExcludeFile(excludedFileName string) *PackageScope {
1016
newScope := &PackageScope{Files: make(map[string]*FileScope)}
1117
for fileName, fileScope := range ps.Files {

0 commit comments

Comments
 (0)