Skip to content

Commit

Permalink
Merge pull request #580 from nevalang/fp
Browse files Browse the repository at this point in the history
# Stdlib

- `x` package was removed from `std` module
- new `strconv` package was added
- `x.ParseNum` was moved to `strconv.ParseNum`
- `x.Scanln` was moved to `io.Scanln`
- `Reduce` (decorator that allows stream processors like `Add` accept array-inport slots) was renamed to `ReducePort`
- new `strings` package?
- `Split` and `Join` components were moved to `strings` package, they are now `strings.Split` and `strings.Join` respectfully
- `Index` signature was updated to work with both lists and strings. Usage now looks like `Index<list<T>>` or `Index<string>`

## New `List` Component

You can now use `List<T>` to turn `stream<T>` into `list<T>`.

There's an [issue](#583) (actually discussion) about the naming of "convertors" in stdlib - we have (and will have more of that) components that turn streams into collections/ports and vice-versa. We need good naming for that. Current naming might change.

# Other

- compiler output was a little bit imprived
- a few bug-fixes in parser
- `runtime/funcs` package was refactored, some runtime functions were renamed to have more specific names
  • Loading branch information
emil14 committed May 3, 2024
2 parents a5fb1ca + 45347e4 commit c38e610
Show file tree
Hide file tree
Showing 38 changed files with 244 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"mode": "auto",
"program": "${workspaceFolder}/cmd/neva",
"cwd": "${workspaceFolder}",
"args": ["run", "e2e/add_nums_verbose/main"]
"args": ["run", "examples/hello_world"]
},
{
"name": "LSP",
Expand Down
15 changes: 9 additions & 6 deletions e2e/add_nums_from_stdin_naive/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { x }
import {
io
strconv
}

component Main(start any) (stop any) {
nodes {
scanner1 x.Scanln
scanner2 x.Scanln
parser1 x.ParseNum<int>
parser2 x.ParseNum<int>
adder Reduce<int> { Add<int> }
scanner1 io.Scanln
scanner2 io.Scanln
parser1 strconv.ParseNum<int>
parser2 strconv.ParseNum<int>
adder ReducePort<int> { Add<int> }
println Println<int>
}
net {
Expand Down
9 changes: 6 additions & 3 deletions e2e/add_nums_from_stdin_with_default_any/main.neva
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { x }
import {
io
strconv
}

component {
Main(start) (stop) {
Expand All @@ -14,7 +17,7 @@ component {
nodes {
reader1 IntReader
reader2 IntReader
adder Reduce<int> { Add<int> }
adder ReducePort<int> { Add<int> }
}
net {
:sig -> reader1:sig
Expand All @@ -26,7 +29,7 @@ component {
}

IntReader(sig any) (num int, err error) {
nodes { x.Scanln, x.ParseNum<int> }
nodes { io.Scanln, strconv.ParseNum<int> }
net {
:sig -> scanln:sig
scanln:data -> parseNum:data
Expand Down
15 changes: 9 additions & 6 deletions e2e/add_nums_from_stdin_with_err_handling/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { x }
import {
io
strconv
}

component Main(start any) (stop any) {
nodes {
scanner1 x.Scanln
scanner2 x.Scanln
parser1 x.ParseNum<int>
parser2 x.ParseNum<int>
adder Reduce<int> { Add<int> }
scanner1 io.Scanln
scanner2 io.Scanln
parser1 strconv.ParseNum<int>
parser2 strconv.ParseNum<int>
adder ReducePort<int> { Add<int> }
println Println<any>
}
net {
Expand Down
9 changes: 6 additions & 3 deletions e2e/add_nums_from_stdin_with_multuple_senders/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { x }
import {
io
strconv
}

component {
Main(start) (stop) {
Expand All @@ -14,7 +17,7 @@ component {
nodes {
reader1 IntReader
reader2 IntReader
adder Reduce<int> { Add<int> }
adder ReducePort<int> { Add<int> }
}
net {
:sig -> reader1:sig
Expand All @@ -26,7 +29,7 @@ component {
}

IntReader(sig any) (num int, err error) {
nodes { x.Scanln, x.ParseNum<int> }
nodes { io.Scanln, strconv.ParseNum<int> }
net {
:sig -> scanln:sig
scanln:data -> parseNum:data
Expand Down
9 changes: 6 additions & 3 deletions e2e/add_nums_from_stdin_with_sub_components/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { x }
import {
io
strconv
}

component {
Main(start any) (stop any) {
Expand All @@ -15,7 +18,7 @@ component {
nodes {
reader1 IntReader
reader2 IntReader
adder Reduce<int> { Add<int> }
adder ReducePort<int> { Add<int> }
}
net {
:sig -> reader1:sig
Expand All @@ -28,7 +31,7 @@ component {
}

IntReader(sig any) (num int, err error) {
nodes { x.Scanln, x.ParseNum<int> }
nodes { io.Scanln, strconv.ParseNum<int> }
net {
:sig -> scanln:sig
scanln:data -> parseNum:data
Expand Down
2 changes: 1 addition & 1 deletion e2e/add_nums_verbose/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component Main(start any) (stop any) {
nodes {
adder Add<int>
sequencer PortSequencer<int>
sequencer StreamPort<int>
println Println<int>
}
net {
Expand Down
2 changes: 1 addition & 1 deletion e2e/add_nums_with_bridge/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
component Main(start any) (stop any) {
nodes {
adder Reduce<int> { Add<int> }
adder ReducePort<int> { Add<int> }
println Println<int>
}
net {
Expand Down
4 changes: 2 additions & 2 deletions e2e/echo_verbose/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { x }
import { io }

component Main(start any) (stop any) {
nodes {
scanner x.Scanln
scanner io.Scanln
println Println<string>
}
net {
Expand Down
2 changes: 1 addition & 1 deletion e2e/multiply_numbers/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component Main(start) (stop) {
nodes {
Println
mul Reduce { Mul<int> }
mul ReducePort { Mul<int> }
}
net {
:start -> [
Expand Down
2 changes: 1 addition & 1 deletion e2e/order_dependend_with_arr_inport/main/main.neva
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component Main(start) (stop) {
nodes {
Println<int>
sub Reduce<int> { Sub<int> }
sub ReducePort<int> { Sub<int> }
}
net {
:start -> [
Expand Down
11 changes: 11 additions & 0 deletions e2e/stream_to_list/main/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TODO move to examples
component Main(start) (stop) {
nodes { Range, List, Println }
net {
:start -> [
(1 -> range:from),
(11 -> range:to)
]
range -> list -> println -> :stop
}
}
1 change: 1 addition & 0 deletions e2e/stream_to_list/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.10.0
2 changes: 1 addition & 1 deletion examples/add_numbers/main.neva
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
component Main(start) (stop) {
nodes {
add Reduce<int> { Add<int> }
add ReducePort<int> { Add<int> }
Println
}
net {
Expand Down
9 changes: 6 additions & 3 deletions examples/add_numbers_from_stdin/main.neva
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { x }
import {
io
strconv
}

component {
Main(start) (stop) {
Expand All @@ -13,7 +16,7 @@ component {
nodes {
readFirstInt ReadIntFromStdin
readSecondInt ReadIntFromStdin
add Reduce<int> { Add<int> }
add ReducePort<int> { Add<int> }
}
net {
:sig -> readFirstInt
Expand All @@ -25,7 +28,7 @@ component {
}

ReadIntFromStdin(sig) (num int, err error) {
nodes { x.Scanln, x.ParseNum<int> }
nodes { io.Scanln, strconv.ParseNum<int> }
net {
:sig -> scanln -> parseNum
parseNum:res -> :num
Expand Down
4 changes: 2 additions & 2 deletions examples/echo/main.neva
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { x }
import { io }

component Main(start) (stop) {
nodes { x.Scanln, Println }
nodes { io.Scanln, Println }
net {
:start -> scanln -> println -> :stop
}
Expand Down
9 changes: 4 additions & 5 deletions examples/list_index/main.neva
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const l list<int> = [1, 1, 5, 112, 69, 420]
const i int = 4
const lst list<int> = [1, 1, 5, 112, 69, 420]

component Main(start) (stop) {
nodes { Index, Println }
nodes { Index<list<int>>, Println }
net {
:start -> [
($l -> index:data),
($i -> index:idx)
($lst -> index:data),
(4 -> index:idx)
]
[index:res, index:err] -> println -> :stop
}
Expand Down
4 changes: 3 additions & 1 deletion examples/split_join_string/main.neva
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { strings }

component Main(start) (stop) {
nodes { Println, Split, Join }
nodes { Println, strings.Split, strings.Join }
net {
:start -> [
('neva' -> split:data),
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/analyzer/component_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (a Analyzer) analyzeConnection(
if err != nil {
return src.Connection{}, compiler.Error{
Location: &scope.Location,
Meta: &conn.Normal.SenderSide.Meta,
Meta: &conn.Meta,
}.Wrap(err)
}
if !isArray {
Expand Down
6 changes: 5 additions & 1 deletion internal/compiler/parser/listener_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ func parsePortAddr(

}

func parseSinglePortAddr(fallbackNode string, expr generated.ISinglePortAddrContext, meta core.Meta) (src.PortAddr, *compiler.Error) {
func parseSinglePortAddr(
fallbackNode string,
expr generated.ISinglePortAddrContext,
meta core.Meta,
) (src.PortAddr, *compiler.Error) {
nodeName := fallbackNode
if n := expr.PortAddrNode(); n != nil {
nodeName = n.GetText()
Expand Down
10 changes: 3 additions & 7 deletions internal/compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/nevalang/neva/internal/compiler"
generated "github.com/nevalang/neva/internal/compiler/parser/generated"
src "github.com/nevalang/neva/internal/compiler/sourcecode"
"github.com/nevalang/neva/internal/compiler/sourcecode/core"
)

type treeShapeListener struct {
Expand Down Expand Up @@ -139,16 +138,13 @@ func (p Parser) parseFile(

if len(lexerErrors.Errors) > 0 {
return src.File{}, &compiler.Error{
Err: errors.Join(lexerErrors.Errors...),
Location: &src.Location{},
Meta: &core.Meta{},
Err: lexerErrors.Errors[0],
}
}

if len(parserErrors.Errors) > 0 {
return src.File{}, &compiler.Error{
Err: errors.Join(parserErrors.Errors...),
Location: &src.Location{},
Meta: &core.Meta{},
Err: parserErrors.Errors[0],
}
}

Expand Down
11 changes: 9 additions & 2 deletions internal/compiler/sourcecode/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sourcecode
import (
"errors"
"fmt"
"path"

"github.com/nevalang/neva/internal/compiler/sourcecode/core"
ts "github.com/nevalang/neva/internal/compiler/sourcecode/typesystem"
Expand Down Expand Up @@ -36,10 +37,16 @@ type Location struct {
}

func (l Location) String() string {
var s string
if l.ModRef.Path == "@" {
return fmt.Sprintf("%v/%v.neva", l.PkgName, l.FileName)
s = l.PkgName
} else {
s = path.Join(l.ModRef.String(), l.PkgName)
}
if l.FileName != "" {
s = path.Join(s, l.FileName+".neva")
}
return fmt.Sprintf("%v/%v/%v.neva", l.ModRef, l.PkgName, l.FileName)
return s
}

func (s Scope) IsTopType(expr ts.Expr) bool {
Expand Down
6 changes: 3 additions & 3 deletions internal/runtime/funcs/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/nevalang/neva/internal/runtime"
)

type field struct{}
type readStructField struct{}

func (s field) Create(io runtime.FuncIO, fieldPathMsg runtime.Msg) (func(ctx context.Context), error) {
func (s readStructField) Create(io runtime.FuncIO, fieldPathMsg runtime.Msg) (func(ctx context.Context), error) {
fieldPath := fieldPathMsg.List()
if len(fieldPath) == 0 {
return nil, errors.New("field path cannot be empty")
Expand Down Expand Up @@ -49,7 +49,7 @@ func (s field) Create(io runtime.FuncIO, fieldPathMsg runtime.Msg) (func(ctx con
}, nil
}

func (field) mapLookup(m runtime.Msg, path []string) runtime.Msg {
func (readStructField) mapLookup(m runtime.Msg, path []string) runtime.Msg {
for len(path) > 0 {
m = m.Map()[path[0]]
path = path[1:]
Expand Down
1 change: 0 additions & 1 deletion internal/runtime/funcs/int_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (intAdd) Create(
continue
}
}

}
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/nevalang/neva/internal/runtime"
)

type list_iter struct{}
type listToStream struct{}

func (c list_iter) Create(
func (c listToStream) Create(
io runtime.FuncIO,
_ runtime.Msg,
) (func(ctx context.Context), error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/runtime/funcs/port_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/nevalang/neva/internal/runtime"
)

type portStreamer struct{}
type arrayPortToStream struct{}

func (portStreamer) Create(
func (arrayPortToStream) Create(
io runtime.FuncIO,
_ runtime.Msg,
) (func(context.Context), error) {
Expand Down
Loading

0 comments on commit c38e610

Please sign in to comment.