Skip to content

Commit

Permalink
feat: remove selectors from senders and add interpreter transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Valeev committed Jul 29, 2023
1 parent 93f503a commit aef0351
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
83 changes: 83 additions & 0 deletions internal/interpreter/transformer.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 3 additions & 3 deletions internal/runtime/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
23 changes: 16 additions & 7 deletions internal/runtime/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}

Expand Down
6 changes: 3 additions & 3 deletions internal/shared/lowlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit aef0351

Please sign in to comment.