forked from google/mtail
/
symtab.go
114 lines (101 loc) · 3.36 KB
/
symtab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package vm
import (
"bytes"
"fmt"
)
type SymbolKind int
// SymbolKind enumerates the kinds of symbols found in the program text.
const (
VarSymbol SymbolKind = iota // Variables
CaprefSymbol // Capture group references
DecoSymbol // Decorators
PatternSymbol // Named pattern constants
)
func (k SymbolKind) String() string {
switch k {
case VarSymbol:
return "variable"
case CaprefSymbol:
return "capture group reference"
case DecoSymbol:
return "decorator"
case PatternSymbol:
return "named pattern constant"
default:
panic("unexpected symbolkind")
}
}
// Symbol describes a named program object.
type Symbol struct {
Name string // identifier name
Kind SymbolKind // kind of program object
Type Type // object's type
Pos *position // Source file position of definition
Binding interface{} // binding to storage allocated in runtime
Addr int // Address offset in another structure, object specific
Used bool // Optional marker that this symbol is used after declaration.
}
// NewSymbol creates a record of a given symbol kind, named name, found at loc
func NewSymbol(name string, kind SymbolKind, pos *position) (sym *Symbol) {
return &Symbol{name, kind, Undef, pos, nil, 0, false}
}
// Scope maintains a record of the identifiers declared in the current program
// scope, and a link to the parent scope.
type Scope struct {
Parent *Scope
Symbols map[string]*Symbol
}
// NewScope creates a new scope within the parent scope
func NewScope(parent *Scope) *Scope {
return &Scope{parent, make(map[string]*Symbol)}
}
// Insert attempts to insert a symbol into the scope. If the scope already
// contains an object alt with the same name, the scope is unchanged and the
// function returns alt. Otherwise the symbol is inserted, and returns nil.
func (s *Scope) Insert(sym *Symbol) (alt *Symbol) {
if alt = s.Symbols[sym.Name]; alt == nil {
s.Symbols[sym.Name] = sym
}
return
}
// InsertAlias attempts to insert a duplicate name for an existing symbol into
// the scope. If the scope already contains an object alt with the alias, the
// scope is unchanged and the function returns alt. Otherwise, the symbol is
// inserted and the function returns nil.
func (s *Scope) InsertAlias(sym *Symbol, alias string) (alt *Symbol) {
if alt := s.Symbols[alias]; alt == nil {
s.Symbols[alias] = sym
}
return
}
// Lookup returns the symbol with the given name if it is found in this or any
// parent scope, otherwise nil.
func (s *Scope) Lookup(name string, kind SymbolKind) *Symbol {
for scope := s; scope != nil; scope = scope.Parent {
if sym := scope.Symbols[name]; sym != nil && sym.Kind == kind {
return sym
}
}
return nil
}
// String prints the current scope and all parents to a string, recursing up to
// the root scope. This method is only used for debugging.
func (s *Scope) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "scope %p {", s)
if s != nil {
fmt.Fprintln(&buf)
if len(s.Symbols) > 0 {
for name, sym := range s.Symbols {
fmt.Fprintf(&buf, "\t%q: %s %q %v\n", name, sym.Kind, sym.Name, sym.Used)
}
}
if s.Parent != nil {
fmt.Fprintf(&buf, "%s", s.Parent.String())
}
}
fmt.Fprintf(&buf, "}\n")
return buf.String()
}