/
resolver.go
239 lines (204 loc) · 4.91 KB
/
resolver.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
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
package interpreter
import (
"errors"
"fmt"
"github.com/levpaul/glocks/internal/parser"
)
const MAX_SCOPES = 255
type FunctionType int
const (
FT_NONE FunctionType = iota
FT_FUNCTION
)
type Scope map[string]bool
type Resolver struct {
i *Interpreter
scopes []Scope
currentFunction FunctionType
}
func (r *Resolver) VisitClassDeclaration(c *parser.ClassDeclaration) error {
//TODO implement me
panic("implement me")
}
func (r *Resolver) resolve(node parser.Node) error {
return node.Accept(r)
}
func (r *Resolver) resolveNodes(nodes []parser.Node) error {
for _, node := range nodes {
if err := r.resolve(node); err != nil {
return err
}
}
return nil
}
func (r *Resolver) beginScope() error {
if len(r.scopes) > MAX_SCOPES {
return errors.New("Refused to create new scope, at the maximum supported!")
}
r.scopes = append([]Scope{{}}, r.scopes...)
return nil
}
func (r *Resolver) endScope() error {
if len(r.scopes) < 1 {
return errors.New("Failed to end scope - there are no active scopes!")
}
r.scopes = r.scopes[1:]
return nil
}
func (r *Resolver) declare(name string) {
if len(r.scopes) == 0 {
return
}
r.scopes[0][name] = false
}
func (r *Resolver) define(name string) {
if len(r.scopes) == 0 {
return
}
r.scopes[0][name] = true
}
// resolveLocal walks through the scopes stack, from narrowest to widest to find the 'distance' to resolution
func (r *Resolver) resolveLocal(node parser.Node, name string) {
// This is different to the book as Java indexes Stacks with 0 being the bottom of the stack
// whereas here I'm using the zero index as the top of the stack
for i, scope := range r.scopes {
if _, exists := scope[name]; exists {
r.i.resolve(node, i)
return
}
}
return
}
func (r *Resolver) resolveFunction(f *parser.FunctionDeclaration, ft FunctionType) error {
enclosingFunction := r.currentFunction
r.currentFunction = ft
defer func() { r.currentFunction = enclosingFunction }()
if err := r.beginScope(); err != nil {
return err
}
for _, p := range f.Params {
r.declare(p)
r.define(p)
}
if err := r.resolveNodes(f.Body); err != nil {
return err
}
return r.endScope()
}
// ============================================================
// VISITOR METHODS
func (r *Resolver) VisitIfStmt(i *parser.IfStmt) error {
err := r.resolve(i.Expression)
if err != nil {
return err
}
r.resolve(i.Statement)
if err != nil {
return err
}
if i.ElseStatement != nil {
return r.resolve(i.ElseStatement)
}
return nil
}
func (r *Resolver) VisitBlock(b *parser.Block) error {
err := r.beginScope()
if err != nil {
return err
}
err = r.resolveNodes(b.Statements)
if err != nil {
return err
}
return r.endScope()
}
func (r *Resolver) VisitBinary(b *parser.Binary) error {
err := r.resolve(b.Left)
if err != nil {
return err
}
return r.resolve(b.Right)
}
func (r *Resolver) VisitGrouping(g *parser.Grouping) error {
return r.resolve(g.Expression)
}
func (r *Resolver) VisitLiteral(l *parser.Literal) error {
return nil
}
func (r *Resolver) VisitUnary(u *parser.Unary) error {
return r.resolve(u.Right)
}
func (r *Resolver) VisitVariable(v *parser.Variable) error {
if len(r.scopes) > 0 {
if defined, exists := r.scopes[0][v.TokenName]; exists && !defined {
return fmt.Errorf("can't read local variable '%s' in its own initializer", v.TokenName)
}
}
r.resolveLocal(v, v.TokenName)
return nil
}
func (r *Resolver) VisitPrintStmt(p *parser.PrintStmt) error {
return r.resolve(p.Arg)
}
func (r *Resolver) VisitVarStmt(v *parser.VarStmt) error {
if len(r.scopes) > 0 {
if _, exists := r.scopes[0][v.Name]; exists {
return fmt.Errorf("already exists a variable with name='%s' in scope", v.Name)
}
}
r.declare(v.Name)
if v.Initializer != nil {
err := r.resolve(v.Initializer)
if err != nil {
return err
}
}
r.define(v.Name)
return nil
}
func (r *Resolver) VisitAssignment(v *parser.Assignment) error {
err := r.resolve(v.Value)
if err != nil {
return err
}
r.resolveLocal(v, v.TokenName)
return nil
}
func (r *Resolver) VisitLogicalConjunction(v *parser.LogicalConjuction) error {
err := r.resolve(v.Left)
if err != nil {
return err
}
return r.resolve(v.Right)
}
func (r *Resolver) VisitWhileStmt(w *parser.WhileStmt) error {
err := r.resolve(w.Expression)
if err != nil {
return err
}
return r.resolve(w.Body)
}
func (r *Resolver) VisitCallExpr(f *parser.CallExpr) error {
err := r.resolve(f.Callee)
if err != nil {
return err
}
for _, arg := range f.Args {
err = r.resolve(arg)
if err != nil {
return err
}
}
return nil
}
func (r *Resolver) VisitFunctionDeclaration(f *parser.FunctionDeclaration) error {
r.declare(f.Name)
r.define(f.Name)
return r.resolveFunction(f, FT_FUNCTION)
}
func (r *Resolver) VisitReturnStmt(rs *parser.ReturnStmt) error {
if r.currentFunction == FT_NONE {
return errors.New("detected return statement from global scope - not allowed")
}
return r.resolve(rs.Expression)
}