Skip to content

Commit

Permalink
implemented allotted destination
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jul 29, 2024
1 parent d7b55c2 commit b966ba4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
39 changes: 31 additions & 8 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,40 @@ func (s *programState) receiveFrom(destination parser.Destination, monetary Mone
return s.receiveFromAccount(string(account), monetary)

case *parser.DestinationAllotment:
var receivedTotal int64

var allotments []Allotment[parser.DestinationAllotmentItem]
receivedTotal := big.NewInt(0)
var allotments []big.Rat
for _, item := range destination.Items {
switch allotment := item.Allotment.(type) {
case *parser.RatioLiteral:
rat := big.NewRat(int64(allotment.Numerator), int64(allotment.Denominator))
// TODO runtime error when rat > 1?
allotments = append(allotments, *rat)
case *parser.RemainingAllotment:
panic("TODO remaining")
case *parser.VariableLiteral:
panic("TODO var allotment")
}
}

allot := makeAllotment(monetaryAmount.Int64(), allotments)
for i, a := range destination.Allotments {
receivedTotal += a.Value.receive(allot[i], ctx)
for i, allotmentItem := range destination.Items {
allot_ := allot[i]

switch allotmentItem := allotmentItem.To.(type) {
case *parser.DestinationTo:
dest := allotmentItem.Destination
receivedMon := monetary
receivedMon.Amount = NewMonetaryInt(allot_)
received := s.receiveFrom(dest, receivedMon)
receivedTotal.Add(receivedTotal, &received)

case *parser.DestinationKept:
panic("TODO handle kept destination")
}

}
return receivedTotal

return *receivedTotal

// case *parser.DestinationInorder:
// sentTotal := big.NewInt(0)
Expand Down Expand Up @@ -326,14 +349,14 @@ func (s *programState) receiveFrom(destination parser.Destination, monetary Mone

}

func makeAllotment[T interface{}](monetary int64, allotments []Allotment[T]) []int64 {
func makeAllotment(monetary int64, allotments []big.Rat) []int64 {
parts := make([]int64, len(allotments))

var totalAllocated int64

for i, allot := range allotments {
var product big.Rat
product.Mul(&allot.Ratio, big.NewRat(monetary, 1))
product.Mul(&allot, big.NewRat(monetary, 1))

floored := product.Num().Int64() / product.Denom().Int64()

Expand Down
5 changes: 0 additions & 5 deletions interpreter/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import (
"slices"
)

type Allotment[T interface{}] struct {
Ratio big.Rat
Value T
}

type Posting struct {
Source string `json:"source"`
Destination string `json:"destination"`
Expand Down

0 comments on commit b966ba4

Please sign in to comment.