Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,13 @@ func (s *programState) makeAllotment(monetary *big.Int, items []parser.Allotment
allotments = append(allotments, rat)

case *parser.RemainingAllotment:
if remainingAllotmentIndex != -1 {
return nil, InvalidRemainingAllotment{
Range: allotment.Range,
}
}
remainingAllotmentIndex = i
allotments = append(allotments, new(big.Rat))
// TODO check there are not duplicate remaining clause
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/interpreter/interpreter_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ func (e InvalidAllotmentInSendAll) Error() string {
return "cannot take all balance of an allotment source"
}

type InvalidRemainingAllotment struct {
parser.Range
}

func (e InvalidRemainingAllotment) Error() string {
return "Only one 'remaining' clause is allowed in an allotment expression"
}

type DivideByZero struct {
parser.Range
Numerator *big.Int
Expand Down
18 changes: 18 additions & 0 deletions internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,24 @@ func TestInvalidDestinationAllotmentSum(t *testing.T) {
test(t, tc)
}

func TestRejectsDuplicateRemainingAllotments(t *testing.T) {
tc := NewTestCase()
src := tc.compile(t, `send [COIN 100] (
source = {
remaining from @a
remaining from @b
}
destination = @dest
)`)

tc.expected = CaseResult{
Error: machine.InvalidRemainingAllotment{
Range: parser.RangeOfIndexed(src, "remaining", 1),
},
}
test(t, tc)
}

func TestSourceAllotmentInvalidAmt(t *testing.T) {
tc := NewTestCase()
tc.compile(t, `send [COIN 100] (
Expand Down