Skip to content

Commit

Permalink
refactor: ir: go -> proto
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 17, 2023
1 parent d5657dc commit aae648d
Show file tree
Hide file tree
Showing 13 changed files with 1,038 additions and 176 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
.PHONY: antlr
antlr:
@cd internal/parser && \
antlr4 -Dlanguage=Go -no-visitor -package parsing ./neva.g4 -o generated
antlr4 -Dlanguage=Go -no-visitor -package parsing ./neva.g4 -o generated

# generate go sdk from ir proto
.PHONY: irproto
irproto:
@protoc --go_out=pkg/ir ./api/ir.proto
63 changes: 63 additions & 0 deletions api/ir.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
syntax = "proto3";

package ir;

option go_package = "pkg/ir";

message LLProgram {
repeated PortInfo ports = 1;
repeated LLConnection net = 2;
repeated LLFunc funcs = 3;
}

message PortInfo {
LLPortAddr portAddr = 1;
uint32 bufSize = 2;
}

message LLPortAddr {
string path = 1;
string port = 2;
uint32 idx = 3;
}

message LLConnection {
LLPortAddr senderSide = 1;
repeated LLReceiverConnectionSide receiverSides = 2;
}

message LLReceiverConnectionSide {
LLPortAddr portAddr = 1;
repeated string selectors = 2;
}

message LLFunc {
LLFuncRef ref = 1;
LLFuncIO io = 2;
LLMsg params = 3;
}

message LLFuncRef {
string pkg = 1;
string name = 2;
}

message LLFuncIO {
repeated LLPortAddr in = 1;
repeated LLPortAddr out = 2;
}

message LLMsg {
LLMsgType type = 1;
bool bool = 2;
int64 int = 3;
double float = 4;
string str = 5;
}

enum LLMsgType {
LLBoolMsg = 0;
LLIntMsg = 1;
LLFloatMsg = 2;
LLStrMsg = 3;
}
28 changes: 2 additions & 26 deletions cmd/interpreter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,9 @@ import (
"golang.org/x/exp/slog"
)

var prog = `
use {
std io
}
types {
MyInt int
MyFloat float
MyStr str
MyBool bool
}
interfaces {
IReader(path string) (i int, e err)
IWriter(path) (i int, anything)
}
components {
Main(enter) (exit) {
net {
in.enter -> out.exit
}
}
}
`

func main() {
prog, err := os.ReadFile(os.Args[1])

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

connector, err := runtime.NewDefaultConnector(runtime.DefaultInterceptor{})
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/stretchr/testify v1.8.1
)

require google.golang.org/protobuf v1.31.0 // indirect

require (
github.com/antlr4-go/antlr/v4 v4.13.0
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -40,7 +42,11 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
12 changes: 5 additions & 7 deletions internal/compiler/highlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ type Msg struct {
}

type MsgValue struct {
TypeExpr ts.Expr // type of the message
Bool bool // only for messages with `bool` type
Int int // only for messages with `int` type
Float float64 // only for messages with `float` type
Str string // only for messages with `str` type
Vec []Msg // only for types with `vec` type
Map map[string]Msg // only for types with `map` type
TypeExpr ts.Expr
Bool bool
Int int
Float float64
Str string
}

type IO struct {
Expand Down
35 changes: 18 additions & 17 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import (

"github.com/nevalang/neva/internal/runtime"
"github.com/nevalang/neva/internal/shared"
ir "github.com/nevalang/neva/pkg/ir/api"
"github.com/nevalang/neva/pkg/tools"
)

type Interpreter struct {
parser Parser
irgen LowLvlGenerator
transformer Transformer
runtime Runtime
parser SourceCodeParser
irgen IRGenerator
rtgen RuntimeProgramGenerator
runtime Runtime
}

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

type LowLvlGenerator interface {
Generate(context.Context, map[string]shared.File) (shared.LLProgram, error)
type IRGenerator interface {
Generate(context.Context, map[string]shared.File) (*ir.LLProgram, error)
}

type Transformer interface {
Transform(context.Context, shared.LLProgram) (runtime.Program, error)
type RuntimeProgramGenerator interface {
Transform(context.Context, *ir.LLProgram) (runtime.Program, error)
}

type Runtime interface {
Expand All @@ -42,7 +43,7 @@ func (i Interpreter) Interpret(ctx context.Context, bb []byte) (int, error) {
return 0, err
}

rprog, err := i.transformer.Transform(ctx, ll)
rprog, err := i.rtgen.Transform(ctx, ll)
if err != nil {
return 0, err
}
Expand All @@ -56,16 +57,16 @@ func (i Interpreter) Interpret(ctx context.Context, bb []byte) (int, error) {
}

func MustNew(
parser Parser,
irgen LowLvlGenerator,
transformer Transformer,
parser SourceCodeParser,
irgen IRGenerator,
transformer RuntimeProgramGenerator,
runtime Runtime,
) Interpreter {
tools.NilPanic(parser, irgen, transformer, runtime)
return Interpreter{
parser: parser,
irgen: irgen,
transformer: transformer,
runtime: runtime,
parser: parser,
irgen: irgen,
rtgen: transformer,
runtime: runtime,
}
}
40 changes: 20 additions & 20 deletions internal/interpreter/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import (
"context"

"github.com/nevalang/neva/internal/runtime"
"github.com/nevalang/neva/internal/shared"
ir "github.com/nevalang/neva/pkg/ir/api"
)

type transformer struct{}

func (t transformer) Transform(ctx context.Context, ll shared.LLProgram) (runtime.Program, error) {
func (t transformer) Transform(ctx context.Context, ll *ir.LLProgram) (runtime.Program, error) {
rPorts := make(runtime.Ports, len(ll.Ports))
for addr, buf := range ll.Ports {
for _, portInfo := range ll.Ports {
rPorts[runtime.PortAddr{
Path: addr.Path,
Port: addr.Port,
Idx: addr.Idx,
}] = make(chan runtime.Msg, buf)
Path: portInfo.PortAddr.Path,
Port: portInfo.PortAddr.Port,
Idx: uint8(portInfo.PortAddr.Idx),
}] = make(chan runtime.Msg, portInfo.BufSize)
}

rConns := make([]runtime.Connection, len(ll.Net))
for _, conn := range ll.Net {
senderAddr := runtime.PortAddr{
Path: conn.SenderSide.Path,
Port: conn.SenderSide.Port,
Idx: conn.SenderSide.Idx,
Idx: uint8(conn.SenderSide.Idx),
}

senderPort, ok := rPorts[senderAddr]
Expand All @@ -44,7 +44,7 @@ func (t transformer) Transform(ctx context.Context, ll shared.LLProgram) (runtim
receiverPortAddr := runtime.PortAddr{
Path: rcvr.PortAddr.Path,
Port: rcvr.PortAddr.Port,
Idx: rcvr.PortAddr.Idx,
Idx: uint8(rcvr.PortAddr.Idx),
}

receiverPort, ok := rPorts[receiverPortAddr]
Expand All @@ -69,22 +69,22 @@ func (t transformer) Transform(ctx context.Context, ll shared.LLProgram) (runtim

rFuncs := make([]runtime.FuncRoutine, len(ll.Funcs))
for _, f := range ll.Funcs {
rIOIn := make(map[string][]chan runtime.Msg, len(f.IO.In))
for _, addr := range f.IO.In {
rIOIn := make(map[string][]chan runtime.Msg, len(f.Io.In))
for _, addr := range f.Io.In {
rPort := rPorts[runtime.PortAddr{
Path: addr.Path,
Port: addr.Port,
Idx: addr.Idx,
Idx: uint8(addr.Idx),
}]
rIOIn[addr.Port] = append(rIOIn[addr.Port], rPort)
}

rIOOut := make(map[string][]chan runtime.Msg, len(f.IO.Out))
for _, addr := range f.IO.Out {
rIOOut := make(map[string][]chan runtime.Msg, len(f.Io.Out))
for _, addr := range f.Io.Out {
rPort := rPorts[runtime.PortAddr{
Path: addr.Path,
Port: addr.Port,
Idx: addr.Idx,
Idx: uint8(addr.Idx),
}]
rIOOut[addr.Port] = append(rIOOut[addr.Port], rPort)
}
Expand All @@ -111,17 +111,17 @@ func (t transformer) Transform(ctx context.Context, ll shared.LLProgram) (runtim
}, nil
}

func (t transformer) msg(msg shared.LLMsg) runtime.Msg {
func (t transformer) msg(msg *ir.LLMsg) runtime.Msg {
var rMsg runtime.Msg

switch msg.Type {
case shared.LLBoolMsg:
case ir.LLMsgType_LLBoolMsg:
rMsg = runtime.NewBoolMsg(msg.Bool)
case shared.LLIntMsg:
case ir.LLMsgType_LLIntMsg:
rMsg = runtime.NewIntMsg(msg.Int)
case shared.LLFloatMsg:
case ir.LLMsgType_LLFloatMsg:
rMsg = runtime.NewFloatMsg(msg.Float)
case shared.LLStrMsg:
case ir.LLMsgType_LLStrMsg:
rMsg = runtime.NewStrMsg(msg.Str)
default:
panic("unknown message type")
Expand Down
Loading

0 comments on commit aae648d

Please sign in to comment.