Skip to content

Commit

Permalink
feat: added string_to_account function
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Dec 6, 2024
1 parent 83db9ae commit be83e34
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
6 changes: 6 additions & 0 deletions internal/analysis/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const FnSetAccountMeta = "set_account_meta"
const FnVarOriginMeta = "meta"
const FnVarOriginBalance = "balance"
const FnVarOriginOverdraft = "overdraft"
const FnVarOriginStringToAccount = "string_to_account"

var Builtins = map[string]FnCallResolution{
FnSetTxMeta: StatementFnCallResolution{
Expand All @@ -81,6 +82,11 @@ var Builtins = map[string]FnCallResolution{
Return: TypeMonetary,
Docs: "get absolute amount of the overdraft of an account. Returns zero if balance is not negative",
},
FnVarOriginStringToAccount: VarOriginFnCallResolution{
Params: []string{TypeString},
Return: TypeAccount,
Docs: "cast a string to an account",
},
}

type Diagnostic struct {
Expand Down
20 changes: 20 additions & 0 deletions internal/interpreter/function_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import (
"github.com/formancehq/numscript/internal/parser"
)

func stringToAccount(
s *programState,
rng parser.Range,
args []Value,
) (Value, InterpreterError) {
if !s.StringToAccountFunctionFeatureFlag {
return nil, ExperimentalFeature{FlagName: ExperimentalStringToAccountFunctionFeatureFlag}
}

// TODO more precise location
p := NewArgsParser(args)
accountStr := parseArg(p, rng, expectString)
err := p.parse()
if err != nil {
return nil, err
}

return AccountAddress(*accountStr), nil
}

func meta(
s *programState,
rng parser.Range,
Expand Down
15 changes: 13 additions & 2 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func (s *programState) handleOrigin(type_ string, fnCall parser.FnCall) (Value,
}
return *monetary, nil

case analysis.FnVarOriginStringToAccount:
return stringToAccount(s, fnCall.Range, args)

default:
return nil, UnboundFunctionErr{Name: fnCall.Caller.Name}
}
Expand Down Expand Up @@ -179,7 +182,10 @@ func (s *programState) parseVars(varDeclrs []parser.VarDeclaration, rawVars map[

type FeatureFlag = string

const ExperimentalOverdraftFunctionFeatureFlag FeatureFlag = "experimental-overdraft-function"
const (
ExperimentalOverdraftFunctionFeatureFlag FeatureFlag = "experimental-overdraft-function"
ExperimentalStringToAccountFunctionFeatureFlag FeatureFlag = "experimental-string-to-account-function"
)

func RunProgram(
ctx context.Context,
Expand All @@ -204,6 +210,10 @@ func RunProgram(
st.OverdraftFunctionFeatureFlag = true
}

if _, ok := featureFlags[ExperimentalStringToAccountFunctionFeatureFlag]; ok {
st.StringToAccountFunctionFeatureFlag = true
}

err := st.parseVars(program.Vars, vars)
if err != nil {
return nil, err
Expand Down Expand Up @@ -261,7 +271,8 @@ type programState struct {

CurrentBalanceQuery BalanceQuery

OverdraftFunctionFeatureFlag bool
OverdraftFunctionFeatureFlag bool
StringToAccountFunctionFeatureFlag bool
}

func (st *programState) pushSender(name string, monetary *big.Int) {
Expand Down
28 changes: 28 additions & 0 deletions internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3560,3 +3560,31 @@ func TestPlusStringOverload(t *testing.T) {
}
test(t, tc)
}

func TestToAccount(t *testing.T) {
script := `
vars {
account $acc = string_to_account("account_name")
}
send [COIN 10] (
source = @world
destination = $acc
)
`

tc := NewTestCase()
tc.compile(t, script)

tc.expected = CaseResult{
Postings: []Posting{
{
Source: "world",
Destination: "account_name",
Amount: big.NewInt(10),
Asset: "COIN",
},
},
}
testWithFeatureFlag(t, tc, machine.ExperimentalStringToAccountFunctionFeatureFlag)
}

0 comments on commit be83e34

Please sign in to comment.