-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumscript.go
100 lines (75 loc) · 2.42 KB
/
numscript.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package numscript
import (
"context"
"github.com/formancehq/numscript/internal/interpreter"
"github.com/formancehq/numscript/internal/parser"
)
// This struct represents a parsed numscript source code
type ParseResult struct {
parseResult parser.ParseResult
}
// Returns a map from a variable's name to its type.
//
// doesn't include variables whose value is already defined within the script
func (p ParseResult) GetNeededVariables() map[string]string {
m := make(map[string]string)
if p.parseResult.Value.Vars == nil {
return m
}
for _, varDecl := range p.parseResult.Value.Vars.Declarations {
if varDecl.Name == nil || varDecl.Origin != nil {
continue
}
m[varDecl.Name.Name] = varDecl.Type.Name
}
return m
}
// func (*ParseResult) GetDiagnostics() []Diagnostic {}
type ParserError = parser.ParserError
func Parse(code string) ParseResult {
return ParseResult{parseResult: parser.Parse(code)}
}
var ParseErrorsToString = parser.ParseErrorsToString
func (p ParseResult) GetParsingErrors() []ParserError {
return p.parseResult.Errors
}
type (
VariablesMap = interpreter.VariablesMap
Posting = interpreter.Posting
ExecutionResult = interpreter.ExecutionResult
// For each account, list of the needed assets
BalanceQuery = interpreter.BalanceQuery
MetadataQuery = interpreter.MetadataQuery
AccountBalance = interpreter.AccountBalance
Balances = interpreter.Balances
AccountMetadata = interpreter.AccountMetadata
// The newly defined account metadata after the execution
AccountsMetadata = interpreter.AccountsMetadata
// The transaction metadata, set by set_tx_meta()
Metadata = interpreter.Metadata
Store = interpreter.Store
StaticStore = interpreter.StaticStore
Value = interpreter.Value
InterpreterError = interpreter.InterpreterError
)
func (p ParseResult) Run(ctx context.Context, vars VariablesMap, store Store) (ExecutionResult, InterpreterError) {
return p.RunWithFeatureFlags(ctx, vars, store, nil)
}
func (p ParseResult) RunWithFeatureFlags(
ctx context.Context,
vars VariablesMap,
store Store,
featureFlags map[string]struct{},
) (ExecutionResult, InterpreterError) {
if featureFlags == nil {
featureFlags = make(map[string]struct{})
}
res, err := interpreter.RunProgram(ctx, p.parseResult.Value, vars, store, featureFlags)
if err != nil {
return ExecutionResult{}, err
}
return *res, nil
}
func (p ParseResult) GetSource() string {
return p.parseResult.Source
}