about summary refs log tree commit diff
path: root/pkg/lang/compiler/scope_chain.go
blob: 3a3b819f74b2fa5390933c2a889c9137ed775d8c (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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package compiler

import (
	"fmt"
	"jinx/pkg/lang/vm/code"
)

type ScopeID int

type ScopeChain struct {
	nameToSymbol map[string]SymbolID
	scopes       []Scope
}

func NewScopeChain() ScopeChain {
	scopes := make([]Scope, 1)
	scopes[0] = NewFunctionScope(0, "") // Top-most scope is a function scope, so it can have sub-units

	return ScopeChain{
		nameToSymbol: make(map[string]SymbolID),
		scopes:       scopes,
	}
}

func (sc *ScopeChain) CurrentScopeID() ScopeID {
	return ScopeID(len(sc.scopes) - 1)
}

func (sc *ScopeChain) IsRootScope() bool {
	return sc.CurrentScopeID() == ScopeID(0)
}

func (sc *ScopeChain) Current() *Scope {
	return &sc.scopes[sc.CurrentScopeID()]
}

func (sc *ScopeChain) CurrentFunction() *Scope {
	// TODO: Probably should make this lookup constant by making a seperate array of function scopes
	for i := len(sc.scopes) - 1; i >= 0; i++ {
		if sc.scopes[i].kind == ScopeKindFunction {
			return &sc.scopes[i]
		}
	}

	panic("top scope should always be a function scope")
}

func (sc *ScopeChain) Enter() {
	sc.scopes = append(sc.scopes, NewNormalScope())
}

func (sc *ScopeChain) EnterFunction(unit code.Marker) {
	id := sc.CurrentScopeID() + 1
	sc.scopes = append(sc.scopes, NewFunctionScope(id, unit))
}

func (sc *ScopeChain) Exit() {
	if sc.CurrentScopeID() == 0 {
		return
	}

	sc.scopes[sc.CurrentScopeID()] = Scope{}
	sc.scopes = sc.scopes[:sc.CurrentScopeID()]
}

func (sc *ScopeChain) Declare(name string) (int, bool) {
	// Check whether the symbol is already declared in any of the scopes.
	if _, ok := sc.nameToSymbol[name]; ok {
		return 0, false
	}

	current := sc.Current()
	indexInScope := len(current.variableSymbols)

	symbolID := SymbolID{
		symbolKind:   SymbolKindVariable,
		scopeID:      sc.CurrentScopeID(),
		indexInScope: indexInScope,
	}

	// Declare the symbol in the current scope.
	current.variableSymbols = append(current.variableSymbols, Symbol[SymbolVariable]{
		name: name,
		data: SymbolVariable{
			localIndex: indexInScope,
		},
	})

	sc.nameToSymbol[name] = symbolID

	return indexInScope, true
}

func (sc *ScopeChain) DeclareFunction(name string) (code.Marker, bool) {
	if _, ok := sc.nameToSymbol[name]; ok {
		return "", false
	}

	current := sc.Current()
	index := len(current.functionSymbols)

	symbolID := SymbolID{
		symbolKind:   SymbolKindFunction,
		scopeID:      sc.CurrentScopeID(),
		indexInScope: index,
	}

	unitName := sc.CreateFunctionSubUnit(name)

	current.functionSymbols = append(current.functionSymbols, Symbol[SymbolFunction]{
		name: name,
		data: SymbolFunction{
			marker: unitName,
		},
	})

	sc.nameToSymbol[name] = symbolID

	return unitName, true
}

func (sc *ScopeChain) DeclareAnonymous() int {
	current := sc.Current()
	index := len(current.variableSymbols)

	current.variableSymbols = append(current.variableSymbols, Symbol[SymbolVariable]{
		name: "", // An anonymous symbol has no name.
		data: SymbolVariable{
			localIndex: index,
		},
	})

	return index
}

func (sc *ScopeChain) DeclareTemporary() int {
	return len(sc.Current().variableSymbols) // :)
}

func (sc *ScopeChain) CreateAnonymousFunctionSubUnit() code.Marker {
	fnScope := sc.CurrentFunction()
	data := fnScope.data.(ScopeFunction)

	index := data.subUnitCount
	data.subUnitCount++

	fnScope.data = data

	return sc.CreateFunctionSubUnit(fmt.Sprintf("anon_%d", index))
}

func (sc *ScopeChain) CreateFunctionSubUnit(subUnitName string) code.Marker {
	fnScope := sc.CurrentFunction()
	data := fnScope.data.(ScopeFunction)

	name := data.unit
	if name == "" {
		name = code.Marker(subUnitName)
	} else {
		name = name.SubUnit(subUnitName)
	}

	return name
}

func (sc *ScopeChain) Lookup(name string) (SymbolID, bool) {
	if id, ok := sc.nameToSymbol[name]; ok {
		return id, true
	}

	return SymbolID{}, false
}

func (sc *ScopeChain) GetVariable(id SymbolID) Symbol[SymbolVariable] {
	if id.symbolKind != SymbolKindVariable {
		panic("incorrect symbol id kind given")
	}

	return sc.scopes[id.scopeID].variableSymbols[id.indexInScope]
}

func (sc *ScopeChain) GetFunction(id SymbolID) Symbol[SymbolFunction] {
	if id.symbolKind != SymbolKindFunction {
		panic("incorrect symbol id kind given")
	}

	return sc.scopes[id.scopeID].functionSymbols[id.indexInScope]
}

type SymbolID struct {
	symbolKind   SymbolKind
	scopeID      ScopeID
	indexInScope int
}

type ScopeKind int

const (
	ScopeKindNormal ScopeKind = iota
	ScopeKindFunction
	ScopeKindLoop
)

type Scope struct {
	variableSymbols []Symbol[SymbolVariable]
	functionSymbols []Symbol[SymbolFunction]

	kind ScopeKind
	data any
}

func NewNormalScope() Scope {
	return Scope{
		variableSymbols: make([]Symbol[SymbolVariable], 0),
		functionSymbols: make([]Symbol[SymbolFunction], 0),
		kind:            ScopeKindNormal,
		data:            ScopeNormal{},
	}
}

func NewFunctionScope(id ScopeID, unit code.Marker) Scope {
	return Scope{
		variableSymbols: make([]Symbol[SymbolVariable], 0),
		functionSymbols: make([]Symbol[SymbolFunction], 0),
		kind:            ScopeKindFunction,
		data: ScopeFunction{
			id:           id,
			unit:         unit,
			subUnitCount: 0,
		},
	}
}

type ScopeNormal struct{}

type ScopeFunction struct {
	id           ScopeID
	unit         code.Marker
	subUnitCount int
}

func (sf ScopeFunction) ID() ScopeID {
	return sf.id
}

func (sf ScopeFunction) IsRootScope() bool {
	return sf.ID() == ScopeID(0)
}

type ScopeLoop struct {
	breakMarker    code.Marker
	continueMarker code.Marker
}