Repository navigation
Expand file tree
/
Copy pathruntime.go
More file actions
284 lines (244 loc) · 7.39 KB
/
Copy pathruntime.go
File metadata and controls
284 lines (244 loc) · 7.39 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package runtime
import (
"context"
"encoding/json"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
bctypes "github.com/influxdata/flux/bytecode/types"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/libflux/go/libflux"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
// Default contains the preregistered packages and builtin values
// required to execute a flux script.
var Default = &runtime{}
// runtime contains the flux runtime for interpreting and
// executing queries.
type runtime struct {
astPkgs map[string]*ast.Package
pkgs map[string]*semantic.Package
builtins map[string]map[string]values.Value
finalized bool
}
func (r *runtime) Parse(flux string) (flux.ASTHandle, error) {
return Parse(flux)
}
func (r *runtime) JSONToHandle(json []byte) (flux.ASTHandle, error) {
return libflux.ParseJSON(json)
}
func (r *runtime) MergePackages(dst, src flux.ASTHandle) error {
return MergePackages(dst, src)
}
func (r *runtime) IsPreludePackage(pkg string) bool {
for _, p := range prelude {
if p == pkg {
return true
}
}
return false
}
func (r *runtime) LookupBuiltinType(pkg, name string) (semantic.MonoType, error) {
return LookupBuiltinType(pkg, name)
}
func (r *runtime) RegisterPackage(pkg *ast.Package) error {
if r.finalized {
return errors.New(codes.Internal, "already finalized, cannot register builtin package")
}
if r.astPkgs == nil {
r.astPkgs = make(map[string]*ast.Package)
}
if _, ok := r.astPkgs[pkg.Path]; ok {
return errors.Newf(codes.Internal, "duplicate builtin package %q", pkg.Path)
}
if ast.Check(pkg) > 0 {
err := ast.GetError(pkg)
return errors.Wrapf(err, codes.Inherit, "failed to parse builtin package %q", pkg.Path)
}
r.astPkgs[pkg.Path] = pkg
return nil
}
func (r *runtime) RegisterPackageValue(pkgpath, name string, value values.Value) error {
return r.registerPackageValue(pkgpath, name, value, false)
}
func (r *runtime) ReplacePackageValue(pkgpath, name string, value values.Value) error {
return r.registerPackageValue(pkgpath, name, value, true)
}
func (r *runtime) registerPackageValue(pkgpath, name string, value values.Value, replace bool) error {
if r.finalized {
return errors.Newf(codes.Internal, "already finalized, cannot register builtin package value")
}
if r.builtins == nil {
r.builtins = make(map[string]map[string]values.Value)
}
pkg, ok := r.builtins[pkgpath]
if !ok {
pkg = make(map[string]values.Value)
r.builtins[pkgpath] = pkg
}
if _, ok := pkg[name]; ok && !replace {
return errors.Newf(codes.Internal, "duplicate builtin package value %q %q", pkgpath, name)
} else if !ok && replace {
return errors.Newf(codes.Internal, "missing builtin package value %q %q", pkgpath, name)
}
pkg[name] = value
return nil
}
func (r *runtime) Prelude() values.Scope {
if !r.finalized {
panic("builtins not finalized")
}
importer := r.Stdlib()
scope, err := r.newScopeFor("main", importer)
if err != nil {
panic(err)
}
return scope
}
func (r *runtime) Eval(ctx context.Context, astPkg flux.ASTHandle, es interpreter.ExecOptsConfig, opts ...flux.ScopeMutator) ([]interpreter.SideEffect, values.Scope, error) {
semPkg, err := AnalyzePackage(astPkg)
if err != nil {
return nil, nil, err
}
// Construct the initial scope for this package.
importer := &importer{r: r}
scope, err := r.newScopeFor("main", importer)
if err != nil {
return nil, nil, err
}
// Mutate the scope with any additional options.
for _, opt := range opts {
opt(r, scope)
}
// Execute the interpreter over the package.
itrp := interpreter.NewInterpreter(nil, es)
sideEffects, err := itrp.Eval(ctx, semPkg, scope, importer)
if err != nil {
return nil, nil, err
}
return sideEffects, scope, nil
}
func (r *runtime) Synthesis(ctx context.Context, astPkg flux.ASTHandle, es interpreter.ExecOptsConfig, opts ...flux.ScopeMutator) ([]bctypes.OpCode, values.Scope, error) {
semPkg, err := AnalyzePackage(astPkg)
if err != nil {
return nil, nil, err
}
// Construct the initial scope for this package.
importer := &synth_importer{r: r}
scope, err := r.newScopeFor("main", importer)
if err != nil {
return nil, nil, err
}
// Mutate the scope with any additional options.
for _, opt := range opts {
opt(r, scope)
}
// Execute the interpreter over the package.
itrp := interpreter.NewInterpreter(nil, es)
err = itrp.Synthesis(ctx, semPkg, scope, importer)
if err != nil {
return nil, nil, err
}
return itrp.Code(), scope, nil
}
// newScopeFor constructs a new scope for the given package using the
// passed in importer.
func (r *runtime) newScopeFor(pkgpath string, imp interpreter.Importer) (values.Scope, error) {
// Construct the prelude scope from the prelude paths.
// If we are importing part of the prelude, we do not
// include it as part of the prelude and will stop
// including values as soon as we hit the prelude.
// This allows us to import all previous paths when loading
// the prelude, but avoid a circular import.
preludeScope := values.NewScope()
for _, path := range prelude {
if path == pkgpath {
break
}
p, err := imp.ImportPackageObject(path)
if err != nil {
return nil, err
}
p.Range(preludeScope.Set)
}
// Build an object with the initial set of identifiers
// from the known builtin values.
object := values.NewObjectWithValues(r.builtins[pkgpath])
scope := values.NewNestedScope(preludeScope, object)
return scope, nil
}
func (r *runtime) Stdlib() interpreter.Importer {
if !r.finalized {
panic("builtins not finalized")
}
return &importer{r: r}
}
func (r *runtime) compilePackages() error {
pkgs := make(map[string]*semantic.Package)
for _, pkg := range r.astPkgs {
bs, err := json.Marshal(pkg)
if err != nil {
return err
}
hdl, err := r.JSONToHandle(bs)
if err != nil {
return err
}
root, err := AnalyzePackage(hdl)
if err != nil {
return err
}
pkgs[pkg.Path] = root
}
r.pkgs = pkgs
r.astPkgs = nil
return nil
}
func (r *runtime) Finalize() error {
if r.finalized {
return errors.New(codes.Internal, "already finalized")
}
r.finalized = true
if err := r.compilePackages(); err != nil {
return err
}
for path, pkg := range r.builtins {
semPkg, ok := r.pkgs[path]
if !ok {
return errors.Newf(codes.Internal, "missing semantic package %s", path)
}
if err := validatePackageBuiltins(pkg, semPkg); err != nil {
return err
}
}
return nil
}
// validatePackageBuiltins ensures that all package builtins have both an AST builtin statement and a registered value.
func validatePackageBuiltins(pkg map[string]values.Value, semPkg *semantic.Package) error {
builtinStmts := make(map[string]*semantic.BuiltinStatement)
semantic.Walk(semantic.CreateVisitor(func(n semantic.Node) {
if bs, ok := n.(*semantic.BuiltinStatement); ok {
builtinStmts[bs.ID.Name] = bs
}
}), semPkg)
missing := make([]string, 0, len(builtinStmts))
extra := make([]string, 0, len(builtinStmts))
for n := range builtinStmts {
if _, ok := pkg[n]; !ok {
missing = append(missing, n)
continue
}
// TODO(nathanielc): Ensure that the value's type matches the type expression
}
for k := range pkg {
if _, ok := builtinStmts[k]; !ok {
extra = append(extra, k)
}
}
if len(missing) > 0 || len(extra) > 0 {
return errors.Newf(codes.Internal, "missing builtin values %v, extra builtin values %v", missing, extra)
}
return nil
}