diff --git a/Makefile b/Makefile index fb4dd2f2..39f3b036 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # build neva cli for host OS and put to the PATH .PHONY: install install: - @go build -ldflags="-s -w" cmd/cli/*.go && \ + @go build -ldflags="-s -w" ./cmd/cli && \ rm -rf /usr/local/bin/neva && \ mv cli /usr/local/bin/neva diff --git a/examples/10_regex_submatch/main.neva b/examples/10_regex_submatch/main.neva new file mode 100644 index 00000000..3d769b7e --- /dev/null +++ b/examples/10_regex_submatch/main.neva @@ -0,0 +1,17 @@ +import { regexp } + +const { regex string = 'a(x*)b(y|z)c' } + +component Main(start) (stop) { + nodes { + Printer + + #bind(regex) + regexp.Submatcher + } + net { + :start -> ('-axxxbyc-' -> submatcher:data) + submatcher:res -> printer:data + printer:sig -> :stop + } +} \ No newline at end of file diff --git a/internal/runtime/funcs/regexp_submatcher.go b/internal/runtime/funcs/regexp_submatcher.go new file mode 100644 index 00000000..c42531f4 --- /dev/null +++ b/internal/runtime/funcs/regexp_submatcher.go @@ -0,0 +1,50 @@ +package funcs + +import ( + "context" + "regexp" + + "github.com/nevalang/neva/internal/runtime" +) + +type regexpSubmatcher struct{} + +func (r regexpSubmatcher) Create(io runtime.FuncIO, cfgMsg runtime.Msg) (func(ctx context.Context), error) { + regex, err := regexp.Compile(cfgMsg.Str()) + if err != nil { + return nil, err + } + + 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 dataMsg := <-dataIn: + resOut <- wrap( + regex.FindStringSubmatch( + dataMsg.String(), + ), + ) + } + } + }, nil +} + +func wrap(ss []string) runtime.Msg { + msgs := make([]runtime.Msg, 0, len(ss)) + for _, s := range ss { + msgs = append(msgs, runtime.NewStrMsg(s)) + } + return runtime.NewListMsg(msgs...) +} diff --git a/internal/runtime/funcs/registry.go b/internal/runtime/funcs/registry.go index 7534e368..78deb1db 100644 --- a/internal/runtime/funcs/registry.go +++ b/internal/runtime/funcs/registry.go @@ -22,6 +22,7 @@ func CreatorRegistry() map[string]runtime.FuncCreator { "line_printer": linePrinter{}, // math "int_adder": intAdder{}, - // parsing + // regexp + "regexp_submatcher": regexpSubmatcher{}, } } diff --git a/pkg/version.go b/pkg/version.go index cfcd922b..b490dd76 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -1,4 +1,4 @@ package pkg // Version is the current version of the language and stdlib -var Version = "0.9.0" //nolint:gochecknoglobals +var Version = "0.10.0" //nolint:gochecknoglobals diff --git a/std/regexp/regexp.neva b/std/regexp/regexp.neva new file mode 100644 index 00000000..fc562184 --- /dev/null +++ b/std/regexp/regexp.neva @@ -0,0 +1,3 @@ +// bind regexp string +#extern(regexp_submatcher) +pub component Submatcher(data string) (res list) diff --git a/std/time/time.neva b/std/time/time.neva deleted file mode 100644 index 5dc37a2c..00000000 --- a/std/time/time.neva +++ /dev/null @@ -1,10 +0,0 @@ -pub type Time struct { - wall int - ext int -} - -#extern(time_now) -pub component Now(sig any) (data Time) - -#extern(time_stringer) -pub component Stringer(data Time) (res string) \ No newline at end of file