(tree *parser.Tree, config *conf.Config)
| 23 | ) |
| 24 | |
| 25 | func 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 | } |
searching dependent graphs…