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

Function Compile

compiler/compiler.go:25–82  ·  view source on GitHub ↗
(tree *parser.Tree, config *conf.Config)

Source from the content-addressed store, hash-verified

23)
24
25func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err error) {
26 defer func() {
27 if r := recover(); r != nil {
28 err = fmt.Errorf("%v\n%s", r, debug.Stack())
29 }
30 }()
31
32 c := &compiler{
33 config: config,
34 locations: make([]file.Location, 0),
35 constantsIndex: make(map[any]int),
36 functionsIndex: make(map[string]int),
37 debugInfo: make(map[string]string),
38 }
39
40 if config != nil {
41 c.ntCache = &c.config.NtCache
42 } else {
43 c.ntCache = new(Cache)
44 }
45
46 c.compile(tree.Node)
47
48 if c.config != nil {
49 switch c.config.Expect {
50 case reflect.Int:
51 c.emit(OpCast, 0)
52 case reflect.Int64:
53 c.emit(OpCast, 1)
54 case reflect.Float64:
55 c.emit(OpCast, 2)
56 case reflect.Bool:
57 c.emit(OpCast, 3)
58 }
59 if c.config.Optimize {
60 c.optimize()
61 }
62 }
63
64 var span *Span
65 if len(c.spans) > 0 {
66 span = c.spans[0]
67 }
68
69 program = NewProgram(
70 tree.Source,
71 tree.Node,
72 c.locations,
73 c.variables,
74 c.constants,
75 c.bytecode,
76 c.arguments,
77 c.functions,
78 c.debugInfo,
79 span,
80 )
81 return
82}

Callers 15

CompileFunction · 0.92
EvalFunction · 0.92
TestTimeFunction · 0.92
TestRun_ReuseVMFunction · 0.92
TestRun_CastFunction · 0.92
TestRun_HelpersFunction · 0.92
TestRun_MethodWithErrorFunction · 0.92
TestRun_FastMethodsFunction · 0.92
TestRun_TaggedFieldNameFunction · 0.92
TestVM_CallNFunction · 0.92

Calls 5

compileMethod · 0.95
emitMethod · 0.95
optimizeMethod · 0.95
NewProgramFunction · 0.85
ErrorfMethod · 0.65

Tested by 13

TestTimeFunction · 0.74
TestRun_ReuseVMFunction · 0.74
TestRun_CastFunction · 0.74
TestRun_HelpersFunction · 0.74
TestRun_MethodWithErrorFunction · 0.74
TestRun_FastMethodsFunction · 0.74
TestRun_TaggedFieldNameFunction · 0.74
TestVM_CallNFunction · 0.74
TestVM_MemoryBudgetFunction · 0.74
TestDebuggerFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…