Skip to content

Commit

Permalink
Merge pull request #649 from nevalang/std-update
Browse files Browse the repository at this point in the history
Std update
  • Loading branch information
emil14 committed May 22, 2024
2 parents 882af32 + dedc24f commit bb93b4b
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 11 deletions.
22 changes: 22 additions & 0 deletions e2e/compare_ints_lt_equal/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
cmd := exec.Command("neva", "run", "main")

out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Equal(
t,
"false\n",
string(out),
)

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
8 changes: 8 additions & 0 deletions e2e/compare_ints_lt_equal/main/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
component Main(start) (stop) {
nodes { Println, Lt<int>}
:start -> [
(20 -> lt:compared),
(20 -> lt:actual)
]
lt:res -> println -> :stop
}
1 change: 1 addition & 0 deletions e2e/compare_ints_lt_equal/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
neva: 0.23.0
8 changes: 4 additions & 4 deletions examples/compare_values/main.neva
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
component Main(start) (stop) {
nodes { Println, Eq<string>,If,Not }
nodes { Println, Eq<int>,If}
:start -> [
('neva is cool' -> eq:expected),
('neva' -> eq:actual)
(2 -> eq:expected),
(2 -> eq:actual)
]
eq:res -> not -> if
eq:res -> if

if:then -> ('They match' -> println -> :stop)
if:else -> ('They do not match' -> println -> :stop)
Expand Down
6 changes: 6 additions & 0 deletions examples/get_args/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {os,lists }

component Main(start) (stop) {
nodes { os.Args, lists.For{Println}}
:start -> (args -> for -> :stop)
}
31 changes: 31 additions & 0 deletions internal/runtime/funcs/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package funcs

import (
"context"
"github.com/nevalang/neva/internal/runtime"
"os"
)

type args struct{}

func (a args) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
outport, err := io.Out.Port("data")
if err != nil {
return nil, err
}

return func(ctx context.Context) {
var lst_args []runtime.Msg
for i := 0; i < len(os.Args); i++ {
lst_args = append(lst_args, runtime.NewStrMsg(os.Args[i]))
}
for {
select {

case <-ctx.Done():
return
case outport <- runtime.NewListMsg(lst_args...):
}
}
}, nil
}
51 changes: 51 additions & 0 deletions internal/runtime/funcs/float_is_lesser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package funcs

import (
"context"
"github.com/nevalang/neva/internal/runtime"
)

type floatIsLesser struct{}

func (p floatIsLesser) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
actualIn, err := io.In.Port("actual")
if err != nil {
return nil, err
}
comparedIn, err := io.In.Port("compared")
if err != nil {
return nil, err
}

resOut, err := io.Out.Port("res")
if err != nil {
return nil, err
}

return func(ctx context.Context) {
var (
val1 runtime.Msg
val2 runtime.Msg
)

for {
select {
case <-ctx.Done():
return
case val1 = <-actualIn:
}

select {
case <-ctx.Done():
return
case val2 = <-comparedIn:
}

select {
case <-ctx.Done():
return
case resOut <- runtime.NewBoolMsg(val1.Int() < val2.Int()):
}
}
}, nil
}
51 changes: 51 additions & 0 deletions internal/runtime/funcs/int_is_lesser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package funcs

import (
"context"
"github.com/nevalang/neva/internal/runtime"
)

type intIsLesser struct{}

func (p intIsLesser) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
actualIn, err := io.In.Port("actual")
if err != nil {
return nil, err
}
comparedIn, err := io.In.Port("compared")
if err != nil {
return nil, err
}

resOut, err := io.Out.Port("res")
if err != nil {
return nil, err
}

return func(ctx context.Context) {
var (
val1 runtime.Msg
val2 runtime.Msg
)

for {
select {
case <-ctx.Done():
return
case val1 = <-actualIn:
}

select {
case <-ctx.Done():
return
case val2 = <-comparedIn:
}

select {
case <-ctx.Done():
return
case resOut <- runtime.NewBoolMsg(val1.Int() < val2.Int()):
}
}
}, nil
}
4 changes: 4 additions & 0 deletions internal/runtime/funcs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func CreatorRegistry() map[string]runtime.FuncCreator {
"and": and{},
"or": or{},
"int_is_greater": intIsGreater{},
"int_is_lesser": intIsLesser{},
"string_is_greater": strIsGreater{},
"string_is_lesser": strIsLesser{},
"float_is_greater": floatIsGreater{},
"float_is_lesser": floatIsLesser{},
"int_is_even": intIsEven{},

// streamers
Expand Down Expand Up @@ -73,6 +76,7 @@ func CreatorRegistry() map[string]runtime.FuncCreator {

// io
"scanln": scanln{},
"args": args{},
"println": println{},
"printf": printf{},

Expand Down
51 changes: 51 additions & 0 deletions internal/runtime/funcs/string_is_lesser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package funcs

import (
"context"
"github.com/nevalang/neva/internal/runtime"
)

type strIsLesser struct{}

func (p strIsLesser) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
actualIn, err := io.In.Port("actual")
if err != nil {
return nil, err
}
comparedIn, err := io.In.Port("compared")
if err != nil {
return nil, err
}

resOut, err := io.Out.Port("res")
if err != nil {
return nil, err
}

return func(ctx context.Context) {
var (
val1 runtime.Msg
val2 runtime.Msg
)

for {
select {
case <-ctx.Done():
return
case val1 = <-actualIn:
}

select {
case <-ctx.Done():
return
case val2 = <-comparedIn:
}

select {
case <-ctx.Done():
return
case resOut <- runtime.NewBoolMsg(val1.Str() < val2.Str()):
}
}
}, nil
}
8 changes: 2 additions & 6 deletions std/builtin/logic.neva
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,5 @@ pub component Or(A bool, B bool) (res bool)
#extern(int int_is_greater, float float_is_greater, string string_is_greater)
pub component Gt<T int | float | string>(actual T, compared T) (res bool)

pub component Lt<T int | float | string>(actual T, compared T) (res bool){
nodes{Gt<T>, Not}
:actual -> gt:actual
:compared -> gt:compared
gt -> not -> :res
}
#extern(int int_is_lesser, float float_is_lesser, string string_is_lesser)
pub component Lt<T int | float | string>(actual T, compared T) (res bool)
3 changes: 2 additions & 1 deletion std/io/io.neva
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#extern(scanln)
pub component Scanln(sig any) (data string)
pub component Scanln(sig any) (data string)

2 changes: 2 additions & 0 deletions std/os/os.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#extern(args)
pub component Args() (data list<string>)

0 comments on commit bb93b4b

Please sign in to comment.