-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
226 lines (175 loc) · 4.71 KB
/
init.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
package parser
import (
"github.com/alecthomas/participle/v2/lexer"
"github.com/peter-mount/go-script/errors"
"github.com/peter-mount/go-script/script"
)
type Initialiser interface {
Scan(s *script.Script) error
Expression(op *script.Expression) error
Statements(op *script.Statements) error
Statement(op *script.Statement) error
}
func NewInitialiser() Initialiser {
return &initialiser{}
}
type initialiser struct {
state initState
}
// initState holds various state during the init Scan
type initState struct {
inLoop bool // true when parsing within a loop statement
}
func (p *defaultParser) init(s *script.Script) (*script.Script, error) {
err := p.includeTopDec(s, s)
if err != nil {
return nil, err
}
init := NewInitialiser()
err = init.Scan(s)
if err != nil {
return nil, errors.Error(s.Pos, err)
}
return s, nil
}
func (p *initialiser) Scan(s *script.Script) error {
for _, f := range s.FunDec {
if err := p.funcDec(f); err != nil {
return errors.Error(s.Pos, err)
}
}
return nil
}
// funcDec initialises a function declaration.
//
// Here we save the current state and set the state to a blank slate.
// The previous state is restored when we exit
func (p *initialiser) funcDec(op *script.FuncDec) error {
old := p.state
defer func() { p.state = old }()
p.state = initState{}
return errors.Error(op.Pos, p.Statements(op.FunBody))
}
// Expression initialises an expression.
func (p *initialiser) Expression(op *script.Expression) error {
// Currently do nothing, just here to make the API usable if we ever need to include it
return nil
}
func (p *initialiser) Statements(op *script.Statements) error {
if op == nil {
return nil
}
for i, s := range op.Statements {
if i > 0 {
op.Statements[i-1].Next = s
}
if err := p.Statement(s); err != nil {
return errors.Error(s.Pos, err)
}
}
return nil
}
func (p *initialiser) Statement(op *script.Statement) error {
if op == nil {
return nil
}
var err error
switch {
case op.Block != nil:
err = p.Statements(op.Block)
case op.IfStmt != nil:
err = p.initIf(op.IfStmt)
case op.For != nil:
err = p.initFor(op.For)
case op.ForRange != nil:
err = p.initForRange(op.ForRange)
case op.Repeat != nil:
err = p.initRepeat(op.Repeat)
case op.Switch != nil:
err = p.initSwitch(op.Switch)
case op.Try != nil:
err = p.initTry(op.Try)
case op.While != nil:
err = p.initWhile(op.While)
case op.Break:
// break is only valid within a loop so this will force
// an error if it's found outside of one
if !p.state.inLoop {
err = errors.Errorf(op.Pos, "break not allowed here")
}
case op.Continue:
// continue is only valid within a loop so this will force
// an error if it's found outside of one
if !p.state.inLoop {
err = errors.Errorf(op.Pos, "continue not allowed here")
}
}
return errors.Error(op.Pos, err)
}
func (p *initialiser) initIf(op *script.If) error {
err := p.Statement(op.Body)
if err == nil {
err = p.Statement(op.Else)
}
return errors.Error(op.Pos, err)
}
func (p *initialiser) initSwitch(op *script.Switch) error {
var err error
for _, c := range op.Case {
err = p.Statement(c.Statement)
if err != nil {
break
}
}
if err == nil {
err = p.Statement(op.Default)
}
return errors.Error(op.Pos, err)
}
func (p *initialiser) initDoWhile(op *script.DoWhile) error {
return p.initLoop(op.Pos, op.Body)
}
func (p *initialiser) initRepeat(op *script.Repeat) error {
return p.initLoop(op.Pos, op.Body)
}
func (p *initialiser) initWhile(op *script.While) error {
return p.initLoop(op.Pos, op.Body)
}
func (p *initialiser) initFor(op *script.For) error {
return p.initLoop(op.Pos, op.Body)
}
func (p *initialiser) initForRange(op *script.ForRange) error {
return p.initLoop(op.Pos, op.Body)
}
// initLoop handles all loop statements.
//
// It sets inLoop to true to mark that break/continue are now valid
// and then initialises the statement.
//
// inLoop is restored to its previous value afterwards
func (p *initialiser) initLoop(pos lexer.Position, body *script.Statement) error {
old := p.state
defer func() { p.state = old }()
p.state.inLoop = true
err := p.Statement(body)
return errors.Error(pos, err)
}
func (p *initialiser) initTry(op *script.Try) error {
// try-resources ensure only assignments and enforce declare mode
// as those variables can only be accessed from within the body
if op.Init != nil {
for _, init := range op.Init.Resources {
if init.Right != nil && init.Right.Right != nil {
init.Right.Declare = true
}
}
}
err := p.Statement(op.Body)
if err == nil && op.Catch != nil {
err = p.Statement(op.Catch.Statement)
}
if err == nil && op.Finally != nil {
err = p.Statement(op.Finally.Statement)
}
return errors.Error(op.Pos, err)
}