github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/interpreter/interpreter.go (about) 1 package interpreter 2 3 import ( 4 "context" 5 6 "github.com/nevalang/neva/internal/builder" 7 "github.com/nevalang/neva/internal/compiler" 8 "github.com/nevalang/neva/internal/compiler/sourcecode" 9 "github.com/nevalang/neva/internal/runtime" 10 "github.com/nevalang/neva/internal/runtime/adapter" 11 "github.com/nevalang/neva/internal/runtime/funcs" 12 ) 13 14 type Interpreter struct { 15 builder builder.Builder 16 compiler compiler.Compiler 17 runtime runtime.Runtime 18 adapter adapter.Adapter 19 } 20 21 func (i Interpreter) Interpret(ctx context.Context, workdirPath string, mainPkgName string) *compiler.Error { 22 irProg, compilerErr := i.compiler.CompileToIR(workdirPath, mainPkgName) 23 if compilerErr != nil { 24 return compiler.Error{ 25 Location: &sourcecode.Location{ 26 PkgName: mainPkgName, 27 }, 28 }.Wrap(compilerErr) 29 } 30 31 rprog, err := i.adapter.Adapt(irProg) 32 if err != nil { 33 return &compiler.Error{ 34 Err: err, 35 Location: &sourcecode.Location{ 36 PkgName: mainPkgName, 37 }, 38 } 39 } 40 41 if err := i.runtime.Run(ctx, rprog); err != nil { 42 return &compiler.Error{ 43 Err: err, 44 Location: &sourcecode.Location{ 45 PkgName: mainPkgName, 46 }, 47 } 48 } 49 50 return nil 51 } 52 53 func New( 54 builder builder.Builder, 55 compiler compiler.Compiler, 56 isDebug bool, 57 ) Interpreter { 58 var connector runtime.Connector 59 if isDebug { 60 connector = runtime.NewConnector(DebugEventListener{}) 61 } else { 62 connector = runtime.NewDefaultConnector() 63 } 64 return Interpreter{ 65 builder: builder, 66 compiler: compiler, 67 adapter: adapter.NewAdapter(), 68 runtime: runtime.New( 69 connector, 70 runtime.MustNewFuncRunner( 71 funcs.CreatorRegistry(), 72 ), 73 ), 74 } 75 }