Skip to content

Commit

Permalink
refactor: moved files
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Dec 6, 2024
1 parent 0096794 commit 83db9ae
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 157 deletions.
131 changes: 131 additions & 0 deletions internal/interpreter/function_expressions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package interpreter

import (
"math/big"

"github.com/formancehq/numscript/internal/parser"
)

func meta(
s *programState,
rng parser.Range,
args []Value,
) (string, InterpreterError) {
// TODO more precise location
p := NewArgsParser(args)
account := parseArg(p, rng, expectAccount)
key := parseArg(p, rng, expectString)
err := p.parse()
if err != nil {
return "", err
}

meta, fetchMetaErr := s.Store.GetAccountsMetadata(s.ctx, MetadataQuery{
*account: []string{*key},
})
if fetchMetaErr != nil {
return "", QueryMetadataError{WrappedError: fetchMetaErr}
}
s.CachedAccountsMeta = meta

// body
accountMeta := s.CachedAccountsMeta[*account]
value, ok := accountMeta[*key]

if !ok {
return "", MetadataNotFound{Account: *account, Key: *key, Range: rng}
}

return value, nil
}

func balance(
s *programState,
r parser.Range,
args []Value,
) (*Monetary, InterpreterError) {
// TODO more precise args range location
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
asset := parseArg(p, r, expectAsset)
err := p.parse()
if err != nil {
return nil, err
}

// body

balance, err := getBalance(s, *account, *asset)
if err != nil {
return nil, err
}

if balance.Cmp(big.NewInt(0)) == -1 {
return nil, NegativeBalanceError{
Account: *account,
Amount: *balance,
}
}

balanceCopy := new(big.Int).Set(balance)

m := Monetary{
Asset: Asset(*asset),
Amount: MonetaryInt(*balanceCopy),
}
return &m, nil
}

func overdraft(
s *programState,
r parser.Range,
args []Value,
) (*Monetary, InterpreterError) {
if !s.OverdraftFunctionFeatureFlag {
return nil, ExperimentalFeature{FlagName: ExperimentalOverdraftFunctionFeatureFlag}
}

// TODO more precise args range location
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
asset := parseArg(p, r, expectAsset)
err := p.parse()
if err != nil {
return nil, err
}

balance_, err := getBalance(s, *account, *asset)
if err != nil {
return nil, err
}

balanceIsPositive := balance_.Cmp(big.NewInt(0)) == 1
if balanceIsPositive {
return &Monetary{
Amount: NewMonetaryInt(0),
Asset: Asset(*asset),
}, nil
}

overdraft := new(big.Int).Neg(balance_)
return &Monetary{
Amount: MonetaryInt(*overdraft),
Asset: Asset(*asset),
}, nil
}

// Utility function to get the balance
func getBalance(
s *programState,
account string,
asset string,
) (*big.Int, InterpreterError) {
s.batchQuery(account, asset)
fetchBalanceErr := s.runBalancesQuery()
if fetchBalanceErr != nil {
return nil, QueryBalanceError{WrappedError: fetchBalanceErr}
}
balance := s.getCachedBalance(account, asset)
return balance, nil

}
35 changes: 35 additions & 0 deletions internal/interpreter/function_statements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package interpreter

import "github.com/formancehq/numscript/internal/parser"

func setTxMeta(st *programState, r parser.Range, args []Value) InterpreterError {
p := NewArgsParser(args)
key := parseArg(p, r, expectString)
meta := parseArg(p, r, expectAnything)
err := p.parse()
if err != nil {
return err
}

st.TxMeta[*key] = *meta
return nil
}

func setAccountMeta(st *programState, r parser.Range, args []Value) InterpreterError {
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
key := parseArg(p, r, expectString)
meta := parseArg(p, r, expectAnything)
err := p.parse()
if err != nil {
return err
}

accountMeta := defaultMapGet(st.SetAccountsMeta, *account, func() AccountMetadata {
return AccountMetadata{}
})

accountMeta[*key] = (*meta).String()

return nil
}
157 changes: 0 additions & 157 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,163 +761,6 @@ func (s *programState) makeAllotment(monetary *big.Int, items []parser.Allotment
return parts, nil
}

// Builtins
func meta(
s *programState,
rng parser.Range,
args []Value,
) (string, InterpreterError) {
// TODO more precise location
p := NewArgsParser(args)
account := parseArg(p, rng, expectAccount)
key := parseArg(p, rng, expectString)
err := p.parse()
if err != nil {
return "", err
}

meta, fetchMetaErr := s.Store.GetAccountsMetadata(s.ctx, MetadataQuery{
*account: []string{*key},
})
if fetchMetaErr != nil {
return "", QueryMetadataError{WrappedError: fetchMetaErr}
}
s.CachedAccountsMeta = meta

// body
accountMeta := s.CachedAccountsMeta[*account]
value, ok := accountMeta[*key]

if !ok {
return "", MetadataNotFound{Account: *account, Key: *key, Range: rng}
}

return value, nil
}

// Utility function to get the balance
func getBalance(
s *programState,
account string,
asset string,
) (*big.Int, InterpreterError) {
s.batchQuery(account, asset)
fetchBalanceErr := s.runBalancesQuery()
if fetchBalanceErr != nil {
return nil, QueryBalanceError{WrappedError: fetchBalanceErr}
}
balance := s.getCachedBalance(account, asset)
return balance, nil

}

func balance(
s *programState,
r parser.Range,
args []Value,
) (*Monetary, InterpreterError) {
// TODO more precise args range location
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
asset := parseArg(p, r, expectAsset)
err := p.parse()
if err != nil {
return nil, err
}

// body

balance, err := getBalance(s, *account, *asset)
if err != nil {
return nil, err
}

if balance.Cmp(big.NewInt(0)) == -1 {
return nil, NegativeBalanceError{
Account: *account,
Amount: *balance,
}
}

balanceCopy := new(big.Int).Set(balance)

m := Monetary{
Asset: Asset(*asset),
Amount: MonetaryInt(*balanceCopy),
}
return &m, nil
}

func overdraft(
s *programState,
r parser.Range,
args []Value,
) (*Monetary, InterpreterError) {
if !s.OverdraftFunctionFeatureFlag {
return nil, ExperimentalFeature{FlagName: ExperimentalOverdraftFunctionFeatureFlag}
}

// TODO more precise args range location
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
asset := parseArg(p, r, expectAsset)
err := p.parse()
if err != nil {
return nil, err
}

balance_, err := getBalance(s, *account, *asset)
if err != nil {
return nil, err
}

balanceIsPositive := balance_.Cmp(big.NewInt(0)) == 1
if balanceIsPositive {
return &Monetary{
Amount: NewMonetaryInt(0),
Asset: Asset(*asset),
}, nil
}

overdraft := new(big.Int).Neg(balance_)
return &Monetary{
Amount: MonetaryInt(*overdraft),
Asset: Asset(*asset),
}, nil
}

func setTxMeta(st *programState, r parser.Range, args []Value) InterpreterError {
p := NewArgsParser(args)
key := parseArg(p, r, expectString)
meta := parseArg(p, r, expectAnything)
err := p.parse()
if err != nil {
return err
}

st.TxMeta[*key] = *meta
return nil
}

func setAccountMeta(st *programState, r parser.Range, args []Value) InterpreterError {
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
key := parseArg(p, r, expectString)
meta := parseArg(p, r, expectAnything)
err := p.parse()
if err != nil {
return err
}

accountMeta := defaultMapGet(st.SetAccountsMeta, *account, func() AccountMetadata {
return AccountMetadata{}
})

accountMeta[*key] = (*meta).String()

return nil
}

func (st *programState) evaluateSentAmt(sentValue parser.SentValue) (*string, *big.Int, InterpreterError) {
switch sentValue := sentValue.(type) {
case *parser.SentValueAll:
Expand Down

0 comments on commit 83db9ae

Please sign in to comment.