forked from google/go-jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.go
227 lines (194 loc) · 5.43 KB
/
evaluator.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
/*
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package jsonnet
import (
"fmt"
"github.com/google/go-jsonnet/ast"
)
// evaluator is a convenience wrapper for interpreter
// Most importantly it keeps the context for traces and handles details
// of error handling.
type evaluator struct {
i *interpreter
trace *TraceElement
}
func makeEvaluator(i *interpreter, trace *TraceElement) *evaluator {
return &evaluator{i: i, trace: trace}
}
func (e *evaluator) inNewContext(trace *TraceElement) *evaluator {
return makeEvaluator(e.i, trace)
}
func (e *evaluator) evaluate(ph potentialValue) (value, error) {
return ph.getValue(e.i, e.trace)
}
func (e *evaluator) evaluateTailCall(ph potentialValue, tc tailCallStatus) (value, error) {
if tc == tailCall {
e.i.stack.tailCallTrimStack()
}
return ph.getValue(e.i, e.trace)
}
func (e *evaluator) Error(s string) error {
err := makeRuntimeError(s, e.i.getCurrentStackTrace(e.trace))
return err
}
func (e *evaluator) typeErrorSpecific(bad value, good value) error {
return e.Error(
fmt.Sprintf("Unexpected type %v, expected %v", bad.getType().name, good.getType().name),
)
}
func (e *evaluator) typeErrorGeneral(bad value) error {
return e.Error(
fmt.Sprintf("Unexpected type %v", bad.getType().name),
)
}
func (e *evaluator) getNumber(val value) (*valueNumber, error) {
switch v := val.(type) {
case *valueNumber:
return v, nil
default:
return nil, e.typeErrorSpecific(val, &valueNumber{})
}
}
func (e *evaluator) evaluateNumber(pv potentialValue) (*valueNumber, error) {
v, err := e.evaluate(pv)
if err != nil {
return nil, err
}
return e.getNumber(v)
}
func (e *evaluator) getInt(val value) (int, error) {
num, err := e.getNumber(val)
if err != nil {
return 0, err
}
// We conservatively convert ot int32, so that it can be machine-sized int
// on any machine. And it's used only for indexing anyway.
intNum := int(int32(num.value))
if float64(intNum) != num.value {
return 0, e.Error(fmt.Sprintf("Expected an integer, but got %v", num.value))
}
return intNum, nil
}
func (e *evaluator) evaluateInt(pv potentialValue) (int, error) {
v, err := e.evaluate(pv)
if err != nil {
return 0, err
}
return e.getInt(v)
}
func (e *evaluator) getInt64(val value) (int64, error) {
num, err := e.getNumber(val)
if err != nil {
return 0, err
}
intNum := int64(num.value)
if float64(intNum) != num.value {
return 0, e.Error(fmt.Sprintf("Expected an integer, but got %v", num.value))
}
return intNum, nil
}
func (e *evaluator) evaluateInt64(pv potentialValue) (int64, error) {
v, err := e.evaluate(pv)
if err != nil {
return 0, err
}
return e.getInt64(v)
}
func (e *evaluator) getString(val value) (*valueString, error) {
switch v := val.(type) {
case *valueString:
return v, nil
default:
return nil, e.typeErrorSpecific(val, &valueString{})
}
}
func (e *evaluator) evaluateString(pv potentialValue) (*valueString, error) {
v, err := e.evaluate(pv)
if err != nil {
return nil, err
}
return e.getString(v)
}
func (e *evaluator) getBoolean(val value) (*valueBoolean, error) {
switch v := val.(type) {
case *valueBoolean:
return v, nil
default:
return nil, e.typeErrorSpecific(val, &valueBoolean{})
}
}
func (e *evaluator) evaluateBoolean(pv potentialValue) (*valueBoolean, error) {
v, err := e.evaluate(pv)
if err != nil {
return nil, err
}
return e.getBoolean(v)
}
func (e *evaluator) getArray(val value) (*valueArray, error) {
switch v := val.(type) {
case *valueArray:
return v, nil
default:
return nil, e.typeErrorSpecific(val, &valueArray{})
}
}
func (e *evaluator) evaluateArray(pv potentialValue) (*valueArray, error) {
v, err := e.evaluate(pv)
if err != nil {
return nil, err
}
return e.getArray(v)
}
func (e *evaluator) getFunction(val value) (*valueFunction, error) {
switch v := val.(type) {
case *valueFunction:
return v, nil
default:
return nil, e.typeErrorSpecific(val, &valueFunction{})
}
}
func (e *evaluator) evaluateFunction(pv potentialValue) (*valueFunction, error) {
v, err := e.evaluate(pv)
if err != nil {
return nil, err
}
return e.getFunction(v)
}
func (e *evaluator) getObject(val value) (valueObject, error) {
switch v := val.(type) {
case valueObject:
return v, nil
default:
return nil, e.typeErrorSpecific(val, &valueSimpleObject{})
}
}
func (e *evaluator) evaluateObject(pv potentialValue) (valueObject, error) {
v, err := e.evaluate(pv)
if err != nil {
return nil, err
}
return e.getObject(v)
}
func (e *evaluator) evalInCurrentContext(a ast.Node, tc tailCallStatus) (value, error) {
return e.i.evaluate(a, tc)
}
func (e *evaluator) evalInCleanEnv(env *environment, ast ast.Node, trimmable bool) (value, error) {
return e.i.EvalInCleanEnv(e.trace, env, ast, trimmable)
}
func (e *evaluator) lookUpVar(ident ast.Identifier) potentialValue {
th := e.i.stack.lookUpVar(ident)
if th == nil {
panic(fmt.Sprintf("RUNTIME: Unknown variable: %v (we should have caught this statically)", ident))
}
return th
}