Skip to content

Commit

Permalink
feat: start->enter
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 16, 2023
1 parent f3451b2 commit d5657dc
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 57 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# generate parser from antlr grammar
.PHONY: antlr
antlr:
@cd internal/parser && \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

> On shore abandoned, kissed by wave, he stood, of mighty thoughts the slave.
Neva is a general purpose [dataflow](https://en.wikipedia.org/wiki/Dataflow_programming) ([flow-based](https://en.wikipedia.org/wiki/Flow-based_programming)) programming language with static [structural](https://en.wikipedia.org/wiki/Structural_type_system) typing and [implicit parallelism](https://en.wikipedia.org/wiki/Implicit_parallelism) that compiles to machine code.
Neva is a general purpose [dataflow](https://en.wikipedia.org/wiki/Dataflow_programming) ([flow-based](https://en.wikipedia.org/wiki/Flow-based_programming)) programming language with static [structural](https://en.wikipedia.org/wiki/Structural_type_system) typing and [implicit parallelism](https://en.wikipedia.org/wiki/Implicit_parallelism).

Oh, and a [visual programming](https://en.wikipedia.org/wiki/Visual_programming_language) finally done right!

---

[FAQ](./docs/faq.md)
[FAQ](./docs/faq.md)
4 changes: 2 additions & 2 deletions cmd/interpreter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ interfaces {
}
components {
Main(start) (exit) {
Main(enter) (exit) {
net {
in.start -> out.exit
in.enter -> out.exit
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/0_do_nothing.neva
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
components {
Main(start) (exit) {
Main(enter) (exit) {
net {
in.start -> out.exit
in.enter -> out.exit
}
}
}
6 changes: 3 additions & 3 deletions examples/1_hello_world.neva
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const {
}

components {
Main(start) (exit) {
Main(enter) (exit) {
nodes {
lock = std.Lock()
print = std.Print()
}
net {
in.start -> lock.sig
in.enter -> lock.sig
const.msg -> lock.v
lock.v -> print.v[0]
lock.v -> print.v
print.v -> out.exit
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/antlr4-go/antlr/v4 v4.13.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
golang.org/x/sync v0.3.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
2 changes: 1 addition & 1 deletion internal/irgen/irgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g Generator) Generate(ctx context.Context, pkgs map[string]shared.File) (s
entityRef: shared.EntityRef{Pkg: "main", Name: "Main"},
ioUsage: nodeIOUsage{
in: map[repPortAddr]struct{}{
{Port: "start"}: {},
{Port: "enter"}: {},
},
out: map[string]uint8{
"exit": 1,
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/tests/happypath/25_comp.neva
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ components {
concrete = io.Reader()
}
net {
in.start -> abs.in.v
in.enter -> abs.in.v
abs.out.v -> {
concrete.in.v
}
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/tests/happypath/26_mixed.neva
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ components {
concrete = io.Reader()
}
net {
in.start -> abs.in.v
in.enter -> abs.in.v
abs.out.v -> {
concrete.in.v
}
Expand Down
18 changes: 7 additions & 11 deletions internal/runtime/func_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"
"fmt"

"golang.org/x/sync/errgroup"
)

var (
Expand All @@ -29,8 +27,6 @@ func NewDefaultFuncRunner(repo map[FuncRef]Func) (DefaultFuncRunner, error) {
}

func (d DefaultFuncRunner) Run(ctx context.Context, funcRoutines []FuncRoutine) error {
g, gctx := errgroup.WithContext(ctx)

for i := range funcRoutines {
funcRoutine := funcRoutines[i]

Expand All @@ -43,13 +39,13 @@ func (d DefaultFuncRunner) Run(ctx context.Context, funcRoutines []FuncRoutine)
return fmt.Errorf("%w: %v", ErrRepo, funcRoutine.Ref)
}

g.Go(func() error {
if err := f(gctx, funcRoutine.IO); err != nil {
return fmt.Errorf("%w: %v", errors.Join(ErrFunc, err), funcRoutine.Ref)
}
return nil
})
cb, err := f(funcRoutine.IO)
if err != nil {
return fmt.Errorf("%w: %v", errors.Join(ErrFunc, err), funcRoutine.Ref)
}

go cb(context.WithValue(ctx, "msg", funcRoutine.Msg))
}

return g.Wait()
return nil
}
26 changes: 0 additions & 26 deletions internal/runtime/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,3 @@ func (f FuncPorts) Port(name string) (chan Msg, error) {

return slots[0], nil
}

// ArrPort returns slots associated with the given name.
// It only returns error if port is not found. It doesn't care about slots count.
func (i FuncPorts) ArrPort(name string) ([]chan Msg, error) {
slots, ok := i[name]
if !ok {
return nil, fmt.Errorf("")
}

return slots, nil
}

// Slot returns slot number idx of the port named by given name.
// It assumes it's an array port at leas as big as idx and returns error otherwise.
func (i FuncPorts) Slot(name string, idx uint8) (chan Msg, error) {
port, ok := i[name]
if !ok {
return nil, fmt.Errorf("")
}

if len(port) < int(idx) {
return nil, fmt.Errorf("")
}

return port[idx], nil
}
10 changes: 5 additions & 5 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ type (
FuncRunner interface {
Run(context.Context, []FuncRoutine) error
}
Func func(context.Context, FuncIO) error
Func func(FuncIO) (func(context.Context), error)
)

var (
ErrStartPortNotFound = errors.New("start port not found")
ErrStartPortNotFound = errors.New("enter port not found")
ErrExitPortNotFound = errors.New("exit port not found")
ErrConnector = errors.New("connector")
ErrRoutineRunner = errors.New("routine runner")
)

func (r Runtime) Run(ctx context.Context, prog Program) (code int, err error) {
// FirstByName is not how this supposed to be working! There could be more "start" and "exit" ports!
startPort := prog.Ports[PortAddr{Path: "main/in", Port: "start"}]
// FirstByName is not how this supposed to be working! There could be more "enter" and "exit" ports!
startPort := prog.Ports[PortAddr{Path: "main/in", Port: "enter"}]
if startPort == nil {
return 0, ErrStartPortNotFound
}

exitPort := prog.Ports[PortAddr{Path: "main/in", Port: "start"}]
exitPort := prog.Ports[PortAddr{Path: "main/in", Port: "enter"}]
if exitPort == nil {
return 0, ErrExitPortNotFound
}
Expand Down
70 changes: 69 additions & 1 deletion internal/runtime/std/std.go
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
package std
package std

import (
"context"
"fmt"

"github.com/nevalang/neva/internal/runtime"
)

func Print(io runtime.FuncIO) (func(context.Context), error) {
ch, err := io.In.Port("v")
if err != nil {
return nil, err
}
return func(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case v := <-ch:
fmt.Println(v.String())
}
}
}, nil
}

func Lock(io runtime.FuncIO) (func(context.Context), error) {
vin, err := io.In.Port("v")
if err != nil {
return nil, err
}
sig, err := io.In.Port("sig")
if err != nil {
return nil, err
}
vout, err := io.Out.Port("v")
if err != nil {
return nil, err
}
return func(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
<-sig
vout <- <-vin
}
}
}, nil
}

func Const(io runtime.FuncIO) (func(context.Context), error) {
vout, err := io.Out.Port("v")
if err != nil {
return nil, err
}
return func(ctx context.Context) {
msg := ctx.Value("msg").(runtime.Msg)
for {
select {
case <-ctx.Done():
return
default:
vout <- msg
}
}
}, nil
}

0 comments on commit d5657dc

Please sign in to comment.