diff --git a/internal/interpreter/transformer.go b/internal/interpreter/transformer.go new file mode 100644 index 00000000..2de2be7c --- /dev/null +++ b/internal/interpreter/transformer.go @@ -0,0 +1,83 @@ +package interpreter + +import ( + "context" + + "github.com/nevalang/neva/internal/runtime" + "github.com/nevalang/neva/internal/shared" +) + +type transformer struct{} + +func (t transformer) Transform(ctx context.Context, ll shared.LowLvlProgram) (runtime.Program, error) { + rPorts := make(runtime.Ports, len(ll.Ports)) + for addr, buf := range ll.Ports { + rPorts[runtime.PortAddr{ + Path: addr.Path, + Name: addr.Name, + Idx: addr.Idx, + }] = make(chan runtime.Msg, buf) + } + + rConns := make([]runtime.Connection, len(ll.Net)) + for _, conn := range ll.Net { + senderAddr := runtime.PortAddr{ + Path: conn.SenderSide.Path, + Name: conn.SenderSide.Name, + Idx: conn.SenderSide.Idx, + } + + senderPort, ok := rPorts[senderAddr] + if !ok { + panic("!ok") + } + + rSenderConnSide := runtime.SenderConnectionSide{ + Port: senderPort, + SenderConnectionSideMeta: runtime.SenderConnectionSideMeta{ + PortAddr: senderAddr, + }, + } + + rReceivers := make([]runtime.ReceiverConnectionSide, len(conn.ReceiverSides)) + for _, rcvr := range conn.ReceiverSides { + receiverPortAddr := runtime.PortAddr{ + Path: rcvr.PortAddr.Path, + Name: rcvr.PortAddr.Name, + Idx: rcvr.PortAddr.Idx, + } + + receiverPort, ok := rPorts[receiverPortAddr] + if !ok { + panic("!ok") + } + + selectors := make([]runtime.Selector, len(rcvr.Selectors)) + for _, sel := range rcvr.Selectors { + selectors = append(selectors, runtime.Selector{ + RecField: sel.RecField, + ArrIdx: sel.ArrIdx, + }) + } + + rReceivers = append(rReceivers, runtime.ReceiverConnectionSide{ + Port: receiverPort, + Meta: runtime.ReceiverConnectionSideMeta{ + PortAddr: receiverPortAddr, + Selectors: selectors, + }, + }) + } + + rConns = append(rConns, runtime.Connection{ + Sender: rSenderConnSide, + Receivers: rReceivers, + }) + } + + return runtime.Program{ + Ports: rPorts, + Connections: rConns, + Funcs: []runtime.FuncRoutine{}, + }, nil +} diff --git a/internal/runtime/connector.go b/internal/runtime/connector.go index 6e64320e..7f12f446 100644 --- a/internal/runtime/connector.go +++ b/internal/runtime/connector.go @@ -24,9 +24,9 @@ func NewConnector(interceptor Interceptor) Connector { } type Interceptor interface { - AfterSending(from ConnectionSideMeta, msg Msg) Msg - BeforeReceiving(from, to ConnectionSideMeta, msg Msg) Msg - AfterReceiving(from, to ConnectionSideMeta, msg Msg) + AfterSending(from SenderConnectionSideMeta, msg Msg) Msg + BeforeReceiving(from, to SenderConnectionSideMeta, msg Msg) Msg + AfterReceiving(from, to SenderConnectionSideMeta, msg Msg) } func (c DefaultConnector) Connect(ctx context.Context, conns []Connection) error { // pass ports map here? diff --git a/internal/runtime/program.go b/internal/runtime/program.go index d158b551..7b9f9bb1 100644 --- a/internal/runtime/program.go +++ b/internal/runtime/program.go @@ -27,12 +27,12 @@ func (p PortAddr) String() string { type Ports map[PortAddr]chan Msg type Connection struct { - Sender ConnectionSide - Receivers []ConnectionSide + Sender SenderConnectionSide + Receivers []ReceiverConnectionSide } func (c Connection) String() string { - s := c.Sender.Meta.String() + "->" + s := c.Sender.PortAddr.String() + "->" for i := range c.Receivers { s += c.Receivers[i].Meta.String() if i < len(c.Receivers)-1 { @@ -42,17 +42,26 @@ func (c Connection) String() string { return s } -type ConnectionSide struct { +type SenderConnectionSide struct { Port chan Msg - Meta ConnectionSideMeta + SenderConnectionSideMeta } -type ConnectionSideMeta struct { +type ReceiverConnectionSide struct { + Port chan Msg + Meta ReceiverConnectionSideMeta +} + +type SenderConnectionSideMeta struct { + PortAddr PortAddr +} + +type ReceiverConnectionSideMeta struct { PortAddr PortAddr Selectors []Selector } -func (c ConnectionSideMeta) String() string { +func (c ReceiverConnectionSideMeta) String() string { return c.PortAddr.String() } diff --git a/internal/shared/lowlevel.go b/internal/shared/lowlevel.go index c4080d43..7dd1a096 100644 --- a/internal/shared/lowlevel.go +++ b/internal/shared/lowlevel.go @@ -13,11 +13,11 @@ type LLPortAddr struct { } type LLConnection struct { - SenderSide LLConnectionSide - ReceiverSides []LLConnectionSide + SenderSide LLPortAddr + ReceiverSides []LLReceiverConnectionSide } -type LLConnectionSide struct { +type LLReceiverConnectionSide struct { PortAddr LLPortAddr Selectors []LLSelector }