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