Skip to content

Commit

Permalink
impl inorder dest
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jul 30, 2024
1 parent f2a9f33 commit 99ee840
Show file tree
Hide file tree
Showing 3 changed files with 394 additions and 28 deletions.
106 changes: 79 additions & 27 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"numscript/analysis"
"numscript/parser"
"numscript/utils"
"strconv"
"strings"
)
Expand Down Expand Up @@ -111,6 +112,13 @@ func (s *programState) handleOrigin(type_ string, fnCall parser.FnCall) (Value,

return parsed, nil

case "balance":
monetary, err := balance(s, args)
if err != nil {
return nil, err
}
return *monetary, nil

default:
panic("TODO handle fn call: " + fnCall.Caller.Name)
}
Expand Down Expand Up @@ -481,37 +489,54 @@ func (s *programState) receiveFrom(destination parser.Destination, monetary Mone

return *receivedTotal

// case *parser.DestinationInorder:
// sentTotal := big.NewInt(0)
// for _, source := range source.Sources {
// var sendingMonetary big.Int
// sendingMonetary.Sub((*big.Int)(&monetary.Amount), sentTotal)
// sentAmt := s.trySending(source, Monetary{
// Amount: MonetaryInt(sendingMonetary),
// Asset: monetary.Asset,
// })
// sentTotal.Add(sentTotal, &sentAmt)
// }
// return *sentTotal

// receivedTotal := big.NewInt(0)
// for _, destination := range d.Destinations {
// receivedTotal += destination.receive(monetary-receivedTotal, ctx)
// // if receivedTotal >= monetary {
// // break
// // }
// }

// return receivedTotal

// case *parser.SourceCapped:
// case *parser.SourceOverdraft:
// case *parser.VariableLiteral:
case *parser.DestinationInorder:
receivedTotal := big.NewInt(0)

// TODO make this prettier
handler := func(keptOrDest parser.KeptOrDestination, capLit parser.Literal) {
switch destinationTarget := keptOrDest.(type) {
case *parser.DestinationKept:
panic("TODO handle destination kept")
case *parser.DestinationTo:
var amountToReceive *big.Int
if capLit == nil {
amountToReceive = (*big.Int)(&monetary.Amount)
} else {
cap, err := evaluateLitExpecting(s, capLit, expectMonetary)
if err != nil {
// TODo properly handle err
panic(err)
}

amountToReceive = utils.MinBigInt((*big.Int)(&cap.Amount), (*big.Int)(&monetary.Amount))
}

var remainingAmount big.Int
remainingAmount.Sub(amountToReceive, receivedTotal)
remainingMonetary := Monetary{
Amount: MonetaryInt(remainingAmount),
Asset: monetary.Asset,
}
// receivedTotal += destination.receive(monetary-receivedTotal, ctx)
received := s.receiveFrom(destinationTarget.Destination, remainingMonetary)
receivedTotal.Add(receivedTotal, &received)

default:
panic("TODO handle")
}
}

for _, destinationClause := range destination.Clauses {
handler(destinationClause.To, destinationClause.Cap)
// TODO should I break if all the amount has been received?
}
handler(destination.Remaining, nil)
return *receivedTotal

default:
panic("TODO handle clause")

}

}

func (s *programState) makeAllotment(monetary int64, items []parser.AllotmentValue) []int64 {
Expand Down Expand Up @@ -609,6 +634,33 @@ func meta(
return value, nil
}

func balance(
s *programState,
args []Value,
) (*Monetary, error) {
if len(args) < 2 {
panic("TODO handle type error in balance")
}

account, err := expectAccount(args[0])
if err != nil {
return nil, err
}

asset, err := expectAsset(args[1])
if err != nil {
return nil, err
}

// body
balance := s.getBalance(string(*account), string(*asset))
m := Monetary{
Asset: Asset(*asset),
Amount: MonetaryInt(*balance),
}
return &m, nil
}

func setTxMeta(st *programState, args []Value) error {
if len(args) != 2 {
// TODO err
Expand Down
Loading

0 comments on commit 99ee840

Please sign in to comment.