Compile parses and compiles given input expression to bytecode program.
(input string, ops ...Option)
| 227 | |
| 228 | // Compile parses and compiles given input expression to bytecode program. |
| 229 | func 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. |
| 264 | func Run(program *vm.Program, env any) (any, error) { |
searching dependent graphs…