about summary refs log tree commit diff
path: root/pkg/lang/compiler/scope/symbol.go
blob: b87d5aa8186f05b788ff6d08de3cfd9745ae6e93 (plain)
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
package scope

type SymbolID struct {
	symbolKind   SymbolKind
	scopeID      ScopeID
	indexInScope int
}

func (id SymbolID) SymbolKind() SymbolKind {
	return id.symbolKind
}

func (id SymbolID) ScopeID() ScopeID {
	return id.scopeID
}

func (id SymbolID) IndexInScope() int {
	return id.indexInScope
}

type SymbolKind int

const (
	// A variable symbol is bound to a local on the stack.
	SymbolKindVariable SymbolKind = iota
	// An env symbol is bound to a local on the stack, outside of the function's scope.
	// Emitted at lookup time, so the SymbolScope has no array for them.
	SymbolKindEnv SymbolKind = iota
)

func (s SymbolKind) String() string {
	switch s {
	case SymbolKindVariable:
		return "variable"
	case SymbolKindEnv:
		return "env"
	default:
		panic("unknown symbol kind")
	}
}

type Symbol[D SymbolData] struct {
	name string
	data D
}

func (s Symbol[D]) Data() D {
	return s.data
}

type SymbolData interface {
	SymbolVariable | SymbolEnv
}

type SymbolVariable struct {
	localIndex int
}

func (sv SymbolVariable) LocalIndex() int {
	return sv.localIndex
}

type SymbolEnv struct {
	indexInEnv int
}

func (se SymbolEnv) IndexInEnv() int {
	return se.indexInEnv
}