Skip to content

Commit

Permalink
finishing off first draft of symbol table
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah Becker-Mayer committed Mar 28, 2021
1 parent 0cba23c commit 69927fa
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Compiler/pkg/compiler/symboltable.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (s *SymbolTable) VarCount(kind Kind) uint {
return s.varCount[kind]
}

// Returns the kind of the named identifier in the given scope
func (s *SymbolTable) KindOf(name string) Kind {
var kind Kind
subroutineTable := *s.table[KIND_ARG]
Expand All @@ -119,3 +120,44 @@ func (s *SymbolTable) KindOf(name string) Kind {
// If nothing was found in either table return KIND_NONE
return KIND_NONE
}

// Returns the type of the named identifier in the given scope
func (s *SymbolTable) TypeOf(name string) (string, error) {
var type_ string
subroutineTable := *s.table[KIND_ARG]
classTable := *s.table[KIND_STATIC]

// Check subroutine scope first
type_ = subroutineTable[name].Type
if type_ != "" {
return type_, nil
}

// If nothing was found, check class scope
type_ = classTable[name].Type
if type_ != "" {
return type_, nil
}

return "", fmt.Errorf("identifier %v was not found in any scope", name)
}

// IndexOf returns the index assigned to the named identifier
func (s *SymbolTable) IndexOf(name string) (uint, error) {
subroutineTable := *s.table[KIND_ARG]
classTable := *s.table[KIND_STATIC]

// Check subroutine scope first
entry, ok := subroutineTable[name]
if ok {
return entry.Index, nil
}

// If nothing was found, check class scope
entry, ok = classTable[name]
if ok {
return entry.Index, nil
}

return 0, fmt.Errorf("identifier %v was not found in any scope", name)
}

0 comments on commit 69927fa

Please sign in to comment.