MCPcopy
hub / github.com/expr-lang/expr / Compile

Function Compile

expr.go:229–261  ·  view source on GitHub ↗

Compile parses and compiles given input expression to bytecode program.

(input string, ops ...Option)

Source from the content-addressed store, hash-verified

227
228// Compile parses and compiles given input expression to bytecode program.
229func Compile(input string, ops ...Option) (*vm.Program, error) {
230 config := conf.CreateNew()
231 for _, op := range ops {
232 op(config)
233 }
234 for name := range config.Disabled {
235 delete(config.Builtins, name)
236 }
237 config.Check()
238
239 tree, err := checker.ParseCheck(input, config)
240 if err != nil {
241 return nil, err
242 }
243
244 if config.Optimize {
245 err = optimizer.Optimize(&tree.Node, config)
246 if err != nil {
247 var fileError *file.Error
248 if errors.As(err, &fileError) {
249 return nil, fileError.Bind(tree.Source)
250 }
251 return nil, err
252 }
253 }
254
255 program, err := compiler.Compile(tree, config)
256 if err != nil {
257 return nil, err
258 }
259
260 return program, nil
261}
262
263// Run evaluates given bytecode program.
264func Run(program *vm.Program, env any) (any, error) {

Callers 15

Benchmark_exprFunction · 0.92
Benchmark_expr_reuseVmFunction · 0.92
Benchmark_lenFunction · 0.92
Benchmark_filterFunction · 0.92
Benchmark_filterLenFunction · 0.92
Benchmark_filterFirstFunction · 0.92
Benchmark_filterLastFunction · 0.92
Benchmark_filterMapFunction · 0.92
Benchmark_arrayIndexFunction · 0.92
Benchmark_envStructFunction · 0.92
Benchmark_envMapFunction · 0.92

Calls 6

BindMethod · 0.95
CreateNewFunction · 0.92
ParseCheckFunction · 0.92
OptimizeFunction · 0.92
CompileFunction · 0.92
CheckMethod · 0.65

Tested by 15

Benchmark_exprFunction · 0.74
Benchmark_expr_reuseVmFunction · 0.74
Benchmark_lenFunction · 0.74
Benchmark_filterFunction · 0.74
Benchmark_filterLenFunction · 0.74
Benchmark_filterFirstFunction · 0.74
Benchmark_filterLastFunction · 0.74
Benchmark_filterMapFunction · 0.74
Benchmark_arrayIndexFunction · 0.74
Benchmark_envStructFunction · 0.74
Benchmark_envMapFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…