Skip to content

Commit

Permalink
fix: a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 20, 2023
1 parent d5b6add commit 888ac73
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
22 changes: 13 additions & 9 deletions internal/interpreter/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (t transformer) Transform(ctx context.Context, irprog *ir.Program) (runtime
})
}

rFuncs := make([]runtime.FuncRoutine, len(irprog.Funcs))
rFuncs := make([]runtime.FuncRoutine, 0, len(irprog.Funcs))
for _, f := range irprog.Funcs {
rIOIn := make(map[string][]chan runtime.Msg, len(f.Io.Inports))
for _, addr := range f.Io.Inports {
Expand All @@ -90,19 +90,23 @@ func (t transformer) Transform(ctx context.Context, irprog *ir.Program) (runtime
rIOOut[addr.Port] = append(rIOOut[addr.Port], rPort)
}

rMsg, err := t.msg(f.Params)
if err != nil {
return runtime.Program{}, err
}

rFuncs = append(rFuncs, runtime.FuncRoutine{
rFunc := runtime.FuncRoutine{
Ref: f.Ref,
IO: runtime.FuncIO{
In: rIOIn,
Out: rIOOut,
},
MetaMsg: rMsg,
})
}

if f.Params != nil {
rMsg, err := t.msg(f.Params)
if err != nil {
return runtime.Program{}, err
}
rFunc.MetaMsg = rMsg
}

rFuncs = append(rFuncs, rFunc)
}

return runtime.Program{
Expand Down
33 changes: 24 additions & 9 deletions internal/irgen/irgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,12 @@ func (g Generator) insertConnectionsAndReturnPortsUsage(

nodesIOUsage[senderPortAddr.Node].out[senderPortAddr.Port]++

senderSide := ir.PortAddr{
Path: nodeCtx.path + "/" + conn.SenderSide.PortAddr.Node,
Port: conn.SenderSide.PortAddr.Port,
Idx: uint32(conn.SenderSide.PortAddr.Idx),
}
senderSide := g.mapSenderSide(nodeCtx, conn)

receiverSidesIR := make([]*ir.ReceiverConnectionSide, 0, len(conn.ReceiverSides))
for _, receiverSide := range conn.ReceiverSides {
receiverSideIR := g.mapReceiverSide(nodeCtx.path, receiverSide)
receiverSidesIR = append(receiverSidesIR, &receiverSideIR)
receiverSidesIR = append(receiverSidesIR, receiverSideIR)

if _, ok := nodesIOUsage[receiverSide.PortAddr.Node]; !ok { // same receiver can be used by multiple senders so we only add it once
nodesIOUsage[receiverSide.PortAddr.Node] = nodeIOUsage{
Expand All @@ -196,14 +192,28 @@ func (g Generator) insertConnectionsAndReturnPortsUsage(
}

result.Connections = append(result.Connections, &ir.Connection{
SenderSide: &senderSide,
SenderSide: senderSide,
ReceiverSides: receiverSidesIR,
})
}

return nodesIOUsage, nil
}

func (Generator) mapSenderSide(nodeCtx nodeContext, conn src.Connection) *ir.PortAddr {
senderSide := &ir.PortAddr{
Path: nodeCtx.path + "/" + conn.SenderSide.PortAddr.Node,
Port: conn.SenderSide.PortAddr.Port,
Idx: uint32(conn.SenderSide.PortAddr.Idx),
}
switch conn.SenderSide.PortAddr.Node {
case "const", "in": // 1) 'const.out' is redundant 2) 'in' is actually sender but we don't want to have 'in.out' addresses
return senderSide
}
senderSide.Path += "/out"
return senderSide
}

func (Generator) insertAndReturnInports(
nodeCtx nodeContext,
result *ir.Program,
Expand Down Expand Up @@ -277,13 +287,18 @@ type handleSenderSideResult struct {
}

// mapReceiverSide maps compiler connection side to ir connection side 1-1 just making the port addr's path absolute
func (g Generator) mapReceiverSide(nodeCtxPath string, side src.ReceiverConnectionSide) ir.ReceiverConnectionSide {
return ir.ReceiverConnectionSide{
func (g Generator) mapReceiverSide(nodeCtxPath string, side src.ReceiverConnectionSide) *ir.ReceiverConnectionSide {
result := &ir.ReceiverConnectionSide{
PortAddr: &ir.PortAddr{
Path: nodeCtxPath + "/" + side.PortAddr.Node,
Port: side.PortAddr.Port,
Idx: uint32(side.PortAddr.Idx),
},
Selectors: side.Selectors,
}
if side.PortAddr.Node == "out" { // 'out' node is actually receiver but we don't want to have 'out.in' addresses
return result
}
result.PortAddr.Path += "/in"
return result
}
6 changes: 3 additions & 3 deletions internal/runtime/func_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

var (
ErrRepo = errors.New("repo")
ErrFunc = errors.New("func")
ErrFuncNotFound = errors.New("func not found")
ErrFunc = errors.New("func")
)

const CtxMsgKey = "msg"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (d DefaultFuncRunner) Run(ctx context.Context, funcRoutines []FuncRoutine)

constructor, ok := d.repo[routine.Ref]
if !ok {
return fmt.Errorf("%w: %v", ErrRepo, routine.Ref)
return fmt.Errorf("%w: %v", ErrFuncNotFound, routine.Ref)
}

fun, err := constructor(ctx, routine.IO)
Expand Down
15 changes: 8 additions & 7 deletions internal/runtime/funcs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

func Print(ctx context.Context, io runtime.FuncIO) (func(), error) {
in, err := io.In.Port("v")
vin, err := io.In.Port("v")
if err != nil {
return nil, err
}
out, err := io.Out.Port("v")
vout, err := io.Out.Port("v")
if err != nil {
return nil, err
}
Expand All @@ -22,9 +22,9 @@ func Print(ctx context.Context, io runtime.FuncIO) (func(), error) {
select {
case <-ctx.Done():
return
case v := <-in:
case v := <-vin:
fmt.Println(v.String())
out <- v
vout <- v
}
}
}, nil
Expand All @@ -47,6 +47,7 @@ func Lock(ctx context.Context, io runtime.FuncIO) (func(), error) {
for {
select {
case <-ctx.Done():
fmt.Println(ctx.Err())
return
default:
<-sig
Expand Down Expand Up @@ -81,8 +82,8 @@ func Const(ctx context.Context, io runtime.FuncIO) (func(), error) {

func Repo() map[string]runtime.Func {
return map[string]runtime.Func{
"print": Print,
"lock": Lock,
"const": Const,
"Print": Print,
"Lock": Lock,
"Const": Const,
}
}
2 changes: 1 addition & 1 deletion internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r Runtime) Run(ctx context.Context, prog Program) (code int, err error) {

var exitCode int64
go func() {
exitCode = (<-exitPort).Int()
exitCode, ok = (<-exitPort).Int()
cancel()
}()

Expand Down

0 comments on commit 888ac73

Please sign in to comment.