mirror of
https://github.com/hanzo-fi/numscript.git
synced 2026-10-30 20:54:52 +00:00
128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
package numscript
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzo-fi/numscript/internal/interpreter"
|
|
"github.com/hanzo-fi/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
|
|
BalanceQueryItem = interpreter.BalanceQueryItem
|
|
MetadataQuery = interpreter.MetadataQuery
|
|
AccountBalance = interpreter.AccountBalance
|
|
Balances = interpreter.Balances
|
|
BalanceRow = interpreter.BalanceRow
|
|
|
|
// Input account metadata (opaque, string-valued) read via meta()
|
|
AccountMetadataRow = interpreter.AccountMetadataRow
|
|
AccountsMetadata = interpreter.AccountsMetadata
|
|
|
|
// The account metadata set during the execution (tagged, typed values)
|
|
SetAccountMetadataRow = interpreter.SetAccountMetadataRow
|
|
SetAccountsMetadata = interpreter.SetAccountsMetadata
|
|
|
|
// The transaction metadata, set by set_tx_meta()
|
|
Metadata = interpreter.Metadata
|
|
|
|
Store = interpreter.Store
|
|
|
|
StaticStore = interpreter.StaticStore
|
|
|
|
Value = interpreter.Value
|
|
|
|
InterpreterError = interpreter.InterpreterError
|
|
MissingFundsErr = interpreter.MissingFundsErr
|
|
|
|
ResolvedDependencies = interpreter.ResolvedDependencies
|
|
AccountDependency = interpreter.AccountDependency
|
|
MetaDependency = interpreter.MetaDependency
|
|
)
|
|
|
|
var ErrScalingNotSupported = interpreter.ErrScalingNotSupported
|
|
|
|
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 len(p.parseResult.Errors) != 0 {
|
|
return ExecutionResult{}, p.parseResult.Errors[0]
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// ResolveDependencies statically determines which accounts and metadata the
|
|
// script reads and writes, resolving account/asset/key expressions against the
|
|
// given vars and store.
|
|
func (p ParseResult) ResolveDependencies(ctx context.Context, vars VariablesMap, store Store) (ResolvedDependencies, error) {
|
|
if len(p.parseResult.Errors) != 0 {
|
|
return ResolvedDependencies{}, p.parseResult.Errors[0]
|
|
}
|
|
|
|
return interpreter.ResolveDependencies(ctx, store, vars, p.parseResult.Value)
|
|
}
|
|
|
|
func (p ParseResult) GetSource() string {
|
|
return p.parseResult.Source
|
|
}
|