From ce14ae8e1968913260864062a8fdaf720a736ad7 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 02:19:55 +0100 Subject: [PATCH 01/16] basic implementation of length --- internal/runtime/funcs/length.go | 35 ++++++++++++++++++++++++++++++ internal/runtime/funcs/registry.go | 8 ++++--- std/builtin/base.neva | 9 +++++++- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 internal/runtime/funcs/length.go diff --git a/internal/runtime/funcs/length.go b/internal/runtime/funcs/length.go new file mode 100644 index 00000000..7efdda08 --- /dev/null +++ b/internal/runtime/funcs/length.go @@ -0,0 +1,35 @@ +package funcs + +import ( + "context" + "github.com/nevalang/neva/internal/runtime" +) + +type length struct{} + +func (p length) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { + dataIn, err := io.In.Port("data") + if err != nil { + return nil, err + } + + resOut, err := io.Out.Port("res") + if err != nil { + return nil, err + } + + return func(ctx context.Context) { + for { + select { + case <-ctx.Done(): + return + case data := <-dataIn: + select { + case <-ctx.Done(): + return + case resOut <- runtime.NewIntMsg(int64(len(data.List()))): + } + } + } + }, nil +} diff --git a/internal/runtime/funcs/registry.go b/internal/runtime/funcs/registry.go index 558376cc..0bac8bfd 100644 --- a/internal/runtime/funcs/registry.go +++ b/internal/runtime/funcs/registry.go @@ -19,9 +19,9 @@ func CreatorRegistry() map[string]runtime.FuncCreator { // logic "int_eq": intEq{}, // math - "int_adder": intAdder{}, - "int_subtractor": intSubtractor{}, - "int_multiplier": intMultiplier{}, + "int_adder": intAdder{}, + "int_subtractor": intSubtractor{}, + "int_multiplier": intMultiplier{}, "int_decrementor": intDecrementor{}, // io "line_scanner": lineScanner{}, @@ -31,5 +31,7 @@ func CreatorRegistry() map[string]runtime.FuncCreator { "int_parser": intParser{}, // regexp "regexp_submatcher": regexpSubmatcher{}, + //array + "length": length{}, } } diff --git a/std/builtin/base.neva b/std/builtin/base.neva index b5fa03ba..0e65fe29 100644 --- a/std/builtin/base.neva +++ b/std/builtin/base.neva @@ -49,4 +49,11 @@ type EqElse struct { } #extern(int int_eq, float float_eq, string string_eq) -pub component Eq(a T, b T) (then T, else EqElse) \ No newline at end of file +pub component Eq(a T, b T) (then T, else EqElse) + +// Array +#extern(length) +pub component Length(data T) (res int) + + + From ed74335542aa333390b7760d27e7bbe2e5375514 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 02:21:18 +0100 Subject: [PATCH 02/16] Added an example of length --- examples/13_array_length/main.neva | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 examples/13_array_length/main.neva diff --git a/examples/13_array_length/main.neva b/examples/13_array_length/main.neva new file mode 100644 index 00000000..8632b570 --- /dev/null +++ b/examples/13_array_length/main.neva @@ -0,0 +1,12 @@ +const n any = [4,123] +component Main(start any) (stop any) { + nodes { + printer Printer + len Length + } + net { + :start -> ($n -> len:data) + len:res -> printer:data + printer:sig -> :stop + } +} \ No newline at end of file From 2c7989d0393236e40bb74a926ff59c1baa45cf55 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 02:42:15 +0100 Subject: [PATCH 03/16] Added testing --- e2e/run_tests_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/e2e/run_tests_test.go b/e2e/run_tests_test.go index d3c8d17f..1d034690 100644 --- a/e2e/run_tests_test.go +++ b/e2e/run_tests_test.go @@ -99,3 +99,22 @@ func TestOrderDependendWithArrInport(t *testing.T) { require.Equal(t, 0, cmd.ProcessState.ExitCode()) } } + +func TestLength(t *testing.T) { + err := os.Chdir("./tests/arr_length") + require.NoError(t, err) + + defer os.Chdir(wd) + + cmd := exec.Command("neva", "run", "main") + + out, err := cmd.CombinedOutput() + require.NoError(t, err) + require.Equal( + t, + "2\n", + string(out), + ) + + require.Equal(t, 0, cmd.ProcessState.ExitCode()) +} From ff166637ea50b88e962619e1fab2969550ed9e89 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 02:46:16 +0100 Subject: [PATCH 04/16] changed comment to List --- std/builtin/base.neva | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/builtin/base.neva b/std/builtin/base.neva index 0e65fe29..3c291712 100644 --- a/std/builtin/base.neva +++ b/std/builtin/base.neva @@ -51,7 +51,7 @@ type EqElse struct { #extern(int int_eq, float float_eq, string string_eq) pub component Eq(a T, b T) (then T, else EqElse) -// Array +// List #extern(length) pub component Length(data T) (res int) From 112ea85a60ddc1dfe9bbf69317c3bf53ec438822 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 02:48:34 +0100 Subject: [PATCH 05/16] Added e2e tests --- e2e/tests/list_length/main/main.neva | 12 ++++++++++++ e2e/tests/list_length/neva.yml | 1 + 2 files changed, 13 insertions(+) create mode 100644 e2e/tests/list_length/main/main.neva create mode 100644 e2e/tests/list_length/neva.yml diff --git a/e2e/tests/list_length/main/main.neva b/e2e/tests/list_length/main/main.neva new file mode 100644 index 00000000..8632b570 --- /dev/null +++ b/e2e/tests/list_length/main/main.neva @@ -0,0 +1,12 @@ +const n any = [4,123] +component Main(start any) (stop any) { + nodes { + printer Printer + len Length + } + net { + :start -> ($n -> len:data) + len:res -> printer:data + printer:sig -> :stop + } +} \ No newline at end of file diff --git a/e2e/tests/list_length/neva.yml b/e2e/tests/list_length/neva.yml new file mode 100644 index 00000000..2bec1757 --- /dev/null +++ b/e2e/tests/list_length/neva.yml @@ -0,0 +1 @@ +neva: 0.10.4 \ No newline at end of file From 58bb9c802746fe3a648a071d67b82bbb13a61b73 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 02:48:53 +0100 Subject: [PATCH 06/16] Changed from arr_length to list_length --- e2e/run_tests_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/run_tests_test.go b/e2e/run_tests_test.go index 1d034690..b13d1b68 100644 --- a/e2e/run_tests_test.go +++ b/e2e/run_tests_test.go @@ -101,7 +101,7 @@ func TestOrderDependendWithArrInport(t *testing.T) { } func TestLength(t *testing.T) { - err := os.Chdir("./tests/arr_length") + err := os.Chdir("./tests/list_length") require.NoError(t, err) defer os.Chdir(wd) From 2802326967cd330671f6e5be28ad9e73e4880318 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 09:51:02 +0100 Subject: [PATCH 07/16] rename from length to listlen --- internal/runtime/funcs/length.go | 4 ++-- internal/runtime/funcs/registry.go | 2 +- std/builtin/base.neva | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/runtime/funcs/length.go b/internal/runtime/funcs/length.go index 7efdda08..cb77d4ea 100644 --- a/internal/runtime/funcs/length.go +++ b/internal/runtime/funcs/length.go @@ -5,9 +5,9 @@ import ( "github.com/nevalang/neva/internal/runtime" ) -type length struct{} +type listlen struct{} -func (p length) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { +func (p listlen) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) { dataIn, err := io.In.Port("data") if err != nil { return nil, err diff --git a/internal/runtime/funcs/registry.go b/internal/runtime/funcs/registry.go index 0bac8bfd..ec535673 100644 --- a/internal/runtime/funcs/registry.go +++ b/internal/runtime/funcs/registry.go @@ -32,6 +32,6 @@ func CreatorRegistry() map[string]runtime.FuncCreator { // regexp "regexp_submatcher": regexpSubmatcher{}, //array - "length": length{}, + "listlen": listlen{}, } } diff --git a/std/builtin/base.neva b/std/builtin/base.neva index 3c291712..2acaf6a1 100644 --- a/std/builtin/base.neva +++ b/std/builtin/base.neva @@ -52,8 +52,8 @@ type EqElse struct { pub component Eq(a T, b T) (then T, else EqElse) // List -#extern(length) -pub component Length(data T) (res int) +#extern(list list_len, map map_len) +pub component Length | map>(data T) (res int) From d6619470d89db532d13333e129c476d0f8ba5bf2 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 09:51:33 +0100 Subject: [PATCH 08/16] rename to Len to match Go --- std/builtin/base.neva | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/builtin/base.neva b/std/builtin/base.neva index 2acaf6a1..e94baf21 100644 --- a/std/builtin/base.neva +++ b/std/builtin/base.neva @@ -53,7 +53,7 @@ pub component Eq(a T, b T) (then T, else EqElse) // List #extern(list list_len, map map_len) -pub component Length | map>(data T) (res int) +pub component Len | map>(data T) (res int) From 4f03f58d1bc0ced74bd1ef9b75fb52bdb5b20103 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 09:53:40 +0100 Subject: [PATCH 09/16] change comment --- std/builtin/base.neva | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/builtin/base.neva b/std/builtin/base.neva index e94baf21..41959d98 100644 --- a/std/builtin/base.neva +++ b/std/builtin/base.neva @@ -51,7 +51,7 @@ type EqElse struct { #extern(int int_eq, float float_eq, string string_eq) pub component Eq(a T, b T) (then T, else EqElse) -// List +// List / Map operations #extern(list list_len, map map_len) pub component Len | map>(data T) (res int) From 25a35b9666cd21f7a05dc880511a5461eff6cd77 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 09:53:52 +0100 Subject: [PATCH 10/16] rename from listlen to list_len --- internal/runtime/funcs/registry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/runtime/funcs/registry.go b/internal/runtime/funcs/registry.go index ec535673..b56f3a77 100644 --- a/internal/runtime/funcs/registry.go +++ b/internal/runtime/funcs/registry.go @@ -32,6 +32,6 @@ func CreatorRegistry() map[string]runtime.FuncCreator { // regexp "regexp_submatcher": regexpSubmatcher{}, //array - "listlen": listlen{}, + "list_len": listlen{}, } } From 4b3d72b2b81c4b7283d181537a6631e335b8feb0 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 09:57:07 +0100 Subject: [PATCH 11/16] rename pointless test --- e2e/run_tests_test.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/e2e/run_tests_test.go b/e2e/run_tests_test.go index b13d1b68..d3c8d17f 100644 --- a/e2e/run_tests_test.go +++ b/e2e/run_tests_test.go @@ -99,22 +99,3 @@ func TestOrderDependendWithArrInport(t *testing.T) { require.Equal(t, 0, cmd.ProcessState.ExitCode()) } } - -func TestLength(t *testing.T) { - err := os.Chdir("./tests/list_length") - require.NoError(t, err) - - defer os.Chdir(wd) - - cmd := exec.Command("neva", "run", "main") - - out, err := cmd.CombinedOutput() - require.NoError(t, err) - require.Equal( - t, - "2\n", - string(out), - ) - - require.Equal(t, 0, cmd.ProcessState.ExitCode()) -} From 01e885dfc40d53934903110604feec7c9593009f Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 09:59:24 +0100 Subject: [PATCH 12/16] updated example --- examples/{13_array_length => 13_list_length}/main.neva | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename examples/{13_array_length => 13_list_length}/main.neva (60%) diff --git a/examples/13_array_length/main.neva b/examples/13_list_length/main.neva similarity index 60% rename from examples/13_array_length/main.neva rename to examples/13_list_length/main.neva index 8632b570..73497edc 100644 --- a/examples/13_array_length/main.neva +++ b/examples/13_list_length/main.neva @@ -1,12 +1,12 @@ -const n any = [4,123] +const n list = [4,123,22,1121,215] component Main(start any) (stop any) { nodes { printer Printer - len Length + len Len> } net { :start -> ($n -> len:data) - len:res -> printer:data + len:res -> printer:data printer:sig -> :stop } } \ No newline at end of file From b4907a4cd33254d5131ee3754dd104f712d5931f Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 10:01:04 +0100 Subject: [PATCH 13/16] updated test --- e2e/run_examples_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/e2e/run_examples_test.go b/e2e/run_examples_test.go index 943331ae..ec9d88f4 100644 --- a/e2e/run_examples_test.go +++ b/e2e/run_examples_test.go @@ -50,3 +50,23 @@ func TestEcho(t *testing.T) { require.Equal(t, 0, cmd.ProcessState.ExitCode()) } + +func TestLength(t *testing.T) { + err := os.Chdir("../examples") + require.NoError(t, err) + + defer os.Chdir(wd) + + cmd := exec.Command("neva", "run", "13_list_length") + + out, err := cmd.CombinedOutput() + require.NoError(t, err) + + require.Equal( + t, + "5\n", + strings.TrimSpace(string(out)), + ) + + require.Equal(t, 0, cmd.ProcessState.ExitCode()) +} From 6217b69b4126af9f305dbc40a3bcac5518142674 Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 10:02:08 +0100 Subject: [PATCH 14/16] changed comment from array to list --- internal/runtime/funcs/registry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/runtime/funcs/registry.go b/internal/runtime/funcs/registry.go index b56f3a77..19baf6e9 100644 --- a/internal/runtime/funcs/registry.go +++ b/internal/runtime/funcs/registry.go @@ -31,7 +31,7 @@ func CreatorRegistry() map[string]runtime.FuncCreator { "int_parser": intParser{}, // regexp "regexp_submatcher": regexpSubmatcher{}, - //array + //list "list_len": listlen{}, } } From 85b6c6b54b1abea14ef62c6a232c135321dba71f Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 13:35:20 +0100 Subject: [PATCH 15/16] fixed e2e test --- e2e/run_examples_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/run_examples_test.go b/e2e/run_examples_test.go index ec9d88f4..34607126 100644 --- a/e2e/run_examples_test.go +++ b/e2e/run_examples_test.go @@ -65,7 +65,7 @@ func TestLength(t *testing.T) { require.Equal( t, "5\n", - strings.TrimSpace(string(out)), + string(out), ) require.Equal(t, 0, cmd.ProcessState.ExitCode()) From 8d0d95b3f5b51c34e661aa051fb4a5b196a16ffc Mon Sep 17 00:00:00 2001 From: dorian3343 Date: Wed, 13 Mar 2024 13:37:32 +0100 Subject: [PATCH 16/16] remove old test --- e2e/tests/list_length/main/main.neva | 12 ------------ e2e/tests/list_length/neva.yml | 1 - 2 files changed, 13 deletions(-) delete mode 100644 e2e/tests/list_length/main/main.neva delete mode 100644 e2e/tests/list_length/neva.yml diff --git a/e2e/tests/list_length/main/main.neva b/e2e/tests/list_length/main/main.neva deleted file mode 100644 index 8632b570..00000000 --- a/e2e/tests/list_length/main/main.neva +++ /dev/null @@ -1,12 +0,0 @@ -const n any = [4,123] -component Main(start any) (stop any) { - nodes { - printer Printer - len Length - } - net { - :start -> ($n -> len:data) - len:res -> printer:data - printer:sig -> :stop - } -} \ No newline at end of file diff --git a/e2e/tests/list_length/neva.yml b/e2e/tests/list_length/neva.yml deleted file mode 100644 index 2bec1757..00000000 --- a/e2e/tests/list_length/neva.yml +++ /dev/null @@ -1 +0,0 @@ -neva: 0.10.4 \ No newline at end of file