Skip to content

Commit

Permalink
wip: interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Valeev committed Aug 3, 2023
1 parent d3608cb commit 1c31bd2
Show file tree
Hide file tree
Showing 17 changed files with 1,316 additions and 1,626 deletions.
4 changes: 2 additions & 2 deletions cmd/interpreter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ interfaces {
components {
Main(enter) (exit) {
node {
nodes {
abs IReader
concrete io.Reader()
concrete = io.Reader()
}
net {
in.start -> abs.in.v
Expand Down
4 changes: 2 additions & 2 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Interpreter struct {
}

type Parser interface {
Parse(context.Context, []byte) (map[string]shared.HLFile, error)
Parse(context.Context, []byte) (map[string]shared.File, error)
}

type LowLvlGenerator interface {
Generate(context.Context, map[string]shared.HLFile) (shared.LowLvlProgram, error)
Generate(context.Context, map[string]shared.File) (shared.LowLvlProgram, error)
}

type Transformer interface {
Expand Down
10 changes: 1 addition & 9 deletions internal/interpreter/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,11 @@ func (t transformer) Transform(ctx context.Context, ll shared.LowLvlProgram) (ru
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,
Selectors: rcvr.Selectors,
},
})
}
Expand Down
38 changes: 19 additions & 19 deletions internal/llrgen/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ func (h Helper) MainComponent(nodes map[string]shared.Node, net []shared.Connect
}
}

func (h Helper) NodeWithStaticPorts(
node shared.Node,
ports map[shared.RelPortAddr]shared.EntityRef,
) shared.Node {
return shared.Node{
Ref: node.Ref,
TypeArgs: node.TypeArgs,
ComponentDI: node.ComponentDI,
}
}
// func (h Helper) NodeWithStaticPorts(
// node shared.Node,
// ports map[shared.RelPortAddr]shared.EntityRef,
// ) shared.Node {
// return shared.Node{
// Ref: node.Ref,
// TypeArgs: node.TypeArgs,
// ComponentDI: node.ComponentDI,
// }
// }

func (h Helper) NodeInstance(pkg, entity string, args ...ts.Expr) shared.Node {
return shared.Node{
Expand All @@ -73,11 +73,11 @@ func (h Helper) InstanceWithDI(pkg, entity string, di map[string]shared.Node, ar

/* --- MESSAGES --- */

func (h Helper) MsgEntity(exported bool, v shared.MsgValue) shared.Entity {
func (h Helper) MsgEntity(exported bool, v shared.ConstValue) shared.Entity {
return shared.Entity{
Exported: exported,
Kind: shared.MsgEntity,
Msg: shared.HLMsg{
Kind: shared.ConstEntity,
Const: shared.Const{
Value: v,
},
}
Expand All @@ -86,15 +86,15 @@ func (h Helper) MsgEntity(exported bool, v shared.MsgValue) shared.Entity {
func (h Helper) MsgWithRefEntity(exported bool, ref *shared.EntityRef) shared.Entity {
return shared.Entity{
Exported: exported,
Kind: shared.MsgEntity,
Msg: shared.HLMsg{
Kind: shared.ConstEntity,
Const: shared.Const{
Ref: ref,
},
}
}

func (h Helper) IntMsgValue(v int) shared.MsgValue {
return shared.MsgValue{
func (h Helper) IntMsgValue(v int) shared.ConstValue {
return shared.ConstValue{
TypeExpr: h.Inst("int"),
Int: v,
}
Expand All @@ -107,8 +107,8 @@ func (h Helper) IntMsg(exported bool, v int) shared.Entity {
)
}

func (h Helper) IntVecMsgEntity(exported bool, vv []shared.HLMsg) shared.Entity {
return h.MsgEntity(exported, shared.MsgValue{
func (h Helper) IntVecMsgEntity(exported bool, vv []shared.Const) shared.Entity {
return h.MsgEntity(exported, shared.ConstValue{
TypeExpr: h.Inst("vec", h.Inst("int")),
Vec: vv,
})
Expand Down
58 changes: 24 additions & 34 deletions internal/llrgen/llrgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func New() Generator {

var ErrNoPkgs = errors.New("no packages")

func (g Generator) Generate(ctx context.Context, prog map[string]shared.HLFile) (shared.LowLvlProgram, error) {
func (g Generator) Generate(ctx context.Context, prog map[string]shared.File) (shared.LowLvlProgram, error) {
if len(prog) == 0 {
return shared.LowLvlProgram{}, ErrNoPkgs
}
Expand All @@ -27,8 +27,8 @@ func (g Generator) Generate(ctx context.Context, prog map[string]shared.HLFile)
path: "main",
entityRef: ref,
io: ioContext{
in: map[shared.RelPortAddr]inportSlotContext{
{Name: "start"}: {},
in: map[shared.PortAddr]inportSlotContext{
{Port: "start"}: {},
},
out: map[string]uint8{
"exit": 1, // runtime is the only one who will read (once) message (code) from this outport
Expand Down Expand Up @@ -58,7 +58,7 @@ type (
}
// ioContext describes how many port slots must be created and how many incoming connections inports have
ioContext struct {
in map[shared.RelPortAddr]inportSlotContext
in map[shared.PortAddr]inportSlotContext
out map[string]uint8 // name -> slots used by parent count
}
inportSlotContext struct {
Expand All @@ -78,7 +78,7 @@ var (
func (g Generator) processNode(
ctx context.Context,
nodeCtx nodeContext,
pkgs map[string]shared.HLFile,
pkgs map[string]shared.File,
result shared.LowLvlProgram,
) error {
entity, err := g.lookupEntity(pkgs, nodeCtx.entityRef)
Expand Down Expand Up @@ -175,7 +175,7 @@ func (Generator) getFuncRoutine(
}

type portSlotsCount struct {
in map[shared.RelPortAddr]inportSlotContext // inportSlotContext will be empty
in map[shared.PortAddr]inportSlotContext // inportSlotContext will be empty
out map[string]uint8
}

Expand All @@ -187,27 +187,27 @@ type handleNetworkResult struct {
// handleNetwork inserts ir connections into the given result
// and returns information about how many slots of each port is actually used in network.
func (g Generator) handleNetwork(
pkgs map[string]shared.HLFile,
pkgs map[string]shared.File,
net []shared.Connection, // pass only net
nodeCtx nodeContext,
result shared.LowLvlProgram,
) (handleNetworkResult, error) {
slotsUsage := map[string]portSlotsCount{}
inPortsSlotsSet := map[shared.ConnPortAddr]bool{}
inPortsSlotsSet := map[shared.PortAddr]bool{}
giverSpecEls := make([]giverSpecEl, 0, len(net))

for _, conn := range net {
senderPortAddr := conn.SenderSide.PortAddr

if _, ok := slotsUsage[senderPortAddr.Node]; !ok {
slotsUsage[senderPortAddr.Node] = portSlotsCount{
in: map[shared.RelPortAddr]inportSlotContext{},
in: map[shared.PortAddr]inportSlotContext{},
out: map[string]uint8{},
}
}

// we assume every sender is unique so we won't increment same port addr twice
slotsUsage[senderPortAddr.Node].out[senderPortAddr.Name]++
slotsUsage[senderPortAddr.Node].out[senderPortAddr.Port]++

handleSenderResult, err := g.handleSenderSide(pkgs, nodeCtx.path, conn.SenderSide, result)
if err != nil {
Expand All @@ -225,7 +225,7 @@ func (g Generator) handleNetwork(

// we can have same receiver for different senders and we don't want to count it twice
if !inPortsSlotsSet[receiverSide.PortAddr] {
slotsUsage[senderPortAddr.Node].in[receiverSide.PortAddr.RelPortAddr] = inportSlotContext{}
slotsUsage[senderPortAddr.Node].in[receiverSide.PortAddr] = inportSlotContext{}
}
}

Expand All @@ -251,7 +251,7 @@ func (Generator) handleInPortsCreation(nodeCtx nodeContext, result shared.LowLvl
for addr := range nodeCtx.io.in {
addr := shared.LLPortAddr{
Path: nodeCtx.path + "/" + "in",
Name: addr.Name,
Name: addr.Port,
Idx: addr.Idx,
}
result.Ports[addr] = 0
Expand Down Expand Up @@ -292,7 +292,7 @@ func (Generator) handleOutPortsCreation(
return runtimeFuncOutportAddrs
}

func (Generator) lookupEntity(pkgs map[string]shared.HLFile, ref shared.EntityRef) (shared.Entity, error) {
func (Generator) lookupEntity(pkgs map[string]shared.File, ref shared.EntityRef) (shared.Entity, error) {
pkg, ok := pkgs[ref.Pkg]
if !ok {
return shared.Entity{}, fmt.Errorf("%w: %v", ErrPkgNotFound, ref.Pkg)
Expand Down Expand Up @@ -320,29 +320,24 @@ type giverSpecEl struct {
// If not, then it acts just like a mapReceiverPortSide without any side-effects.
// Otherwise it first builds the message, then inserts it into result, then returns params for giver creation.
func (g Generator) handleSenderSide(
pkgs map[string]shared.HLFile,
pkgs map[string]shared.File,
nodeCtxPath string,
side shared.SenderConnectionSide,
result shared.LowLvlProgram,
) (handleSenderSideResult, error) {
if side.MsgRef == nil {
irConnSide := g.portAddr(nodeCtxPath, side.PortConnectionSide, "out")
return handleSenderSideResult{irConnSide: irConnSide}, nil
}
// if side.ConstRef == nil {
// irConnSide := g.portAddr(nodeCtxPath, side.ConnectionSide, "out")
// return handleSenderSideResult{irConnSide: irConnSide}, nil
// }

msgName := nodeCtxPath + "/" + side.MsgRef.Pkg + "." + side.MsgRef.Name
msgName := nodeCtxPath + "/" + side.ConstRef.Pkg + "." + side.ConstRef.Name

giverOutport := shared.LLPortAddr{
Path: nodeCtxPath,
Name: side.MsgRef.Pkg + "." + side.MsgRef.Name,
Name: side.ConstRef.Pkg + "." + side.ConstRef.Name,
}
result.Ports[giverOutport] = 0

selectors := make([]shared.LLSelector, 0, len(side.Selectors))
for _, selector := range side.Selectors {
selectors = append(selectors, shared.LLSelector(selector))
}

return handleSenderSideResult{
irConnSide: giverOutport,
giverParams: &giverSpecEl{
Expand All @@ -353,22 +348,17 @@ func (g Generator) handleSenderSide(
}

// mapReceiverPortSide maps compiler connection side to ir connection side 1-1 just making the port addr's path absolute
func (g Generator) mapReceiverPortSide(nodeCtxPath string, side shared.PortConnectionSide, pathPostfix string) shared.LLReceiverConnectionSide {
selectors := make([]shared.LLSelector, 0, len(side.Selectors))
for _, selector := range side.Selectors {
selectors = append(selectors, shared.LLSelector(selector))
}

func (g Generator) mapReceiverPortSide(nodeCtxPath string, side shared.ReceiverConnectionSide, pathPostfix string) shared.LLReceiverConnectionSide {
return shared.LLReceiverConnectionSide{
PortAddr: g.portAddr(nodeCtxPath, side, pathPostfix),
Selectors: selectors,
Selectors: side.Selectors,
}
}

func (Generator) portAddr(nodeCtxPath string, side shared.PortConnectionSide, pathPostfix string) shared.LLPortAddr {
func (Generator) portAddr(nodeCtxPath string, side shared.ReceiverConnectionSide, pathPostfix string) shared.LLPortAddr {
addr := shared.LLPortAddr{
Path: nodeCtxPath + "/" + side.PortAddr.Node,
Name: side.PortAddr.Name,
Name: side.PortAddr.Port,
Idx: side.PortAddr.Idx,
}

Expand Down
4 changes: 3 additions & 1 deletion internal/parser/generated/neva.interp

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions internal/parser/generated/neva_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/parser/generated/neva_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1c31bd2

Please sign in to comment.