Skip to content

Commit

Permalink
implemented oneof in destination
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 20, 2025
1 parent d134aad commit 452d1cb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,27 @@ func (s *programState) receiveFrom(destination parser.Destination, amount *big.I
// passing "remainingAmount" directly breaks the code
return handler(destination.Remaining, remainingAmountCopy)

case *parser.DestinationOneof:
err := s.checkFeatureFlag(ExperimentalOneofFeatureFlag)
if err != nil {
return err
}
for _, destinationClause := range destination.Clauses {
cap, err := evaluateExprAs(s, destinationClause.Cap, expectMonetaryOfAsset(s.CurrentAsset))
if err != nil {
return err
}

// if the clause cap is >= the amount we're trying to receive, only go through this branch
switch cap.Cmp(amount) {
case 0, 1:
return s.receiveFromKeptOrDest(destinationClause.To, amount)
}

// otherwise try next clause (keep looping)
}
return s.receiveFromKeptOrDest(destination.Remaining, amount)

default:
utils.NonExhaustiveMatchPanic[any](destination)
return nil
Expand Down
77 changes: 77 additions & 0 deletions internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3666,3 +3666,80 @@ func TestOneofSingleton(t *testing.T) {
}
testWithFeatureFlag(t, tc, machine.ExperimentalOneofFeatureFlag)
}

func TestOneofDestinationFirstClause(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `
send [GEM 10] (
source = @world
destination = oneof {
max [GEM 99999] to @a
remaining to @b
}
)
`)
tc.expected = CaseResult{
Postings: []Posting{
{
Asset: "GEM",
Amount: big.NewInt(10),
Source: "world",
Destination: "a",
},
},
Error: nil,
}
testWithFeatureFlag(t, tc, machine.ExperimentalOneofFeatureFlag)
}

func TestOneofDestinationSecondClause(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `
send [GEM 10] (
source = @world
destination = oneof {
max [GEM 9] to @a
max [GEM 10] to @b
remaining to @rem
}
)
`)
tc.expected = CaseResult{
Postings: []Posting{
{
Asset: "GEM",
Amount: big.NewInt(10),
Source: "world",
Destination: "b",
},
},
Error: nil,
}
testWithFeatureFlag(t, tc, machine.ExperimentalOneofFeatureFlag)
}

func TestOneofDestinationRemainingClause(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `
send [GEM 100] (
source = @world
destination = oneof {
max [GEM 9] to @a
max [GEM 10] to @b
remaining to @rem
}
)
`)
tc.expected = CaseResult{
Postings: []Posting{
{
Asset: "GEM",
Amount: big.NewInt(100),
Source: "world",
Destination: "rem",
},
},
Error: nil,
}
testWithFeatureFlag(t, tc, machine.ExperimentalOneofFeatureFlag)
}

0 comments on commit 452d1cb

Please sign in to comment.