commit 89deda1268d9a91108beae0c76694bb2ae098f27
parent 084b23babd4a7a131e601dee7813525946717aa1
Author: lash <dev@holbrook.no>
Date: Mon, 10 Apr 2023 07:54:52 +0100
Use correct target, ctrl regex for incmp
Diffstat:
6 files changed, 53 insertions(+), 21 deletions(-)
diff --git a/go/engine/loop.go b/go/engine/loop.go
@@ -5,11 +5,11 @@ import (
"bytes"
"context"
"fmt"
- "os"
+ "io"
"strings"
)
-func Loop(startSym string, en *Engine, ctx context.Context) error {
+func Loop(en *Engine, startSym string, ctx context.Context, reader io.Reader, writer io.Writer) error {
err := en.Init(startSym, ctx)
if err != nil {
return fmt.Errorf("cannot init: %v\n", err)
@@ -20,9 +20,9 @@ func Loop(startSym string, en *Engine, ctx context.Context) error {
fmt.Println(b.String())
running := true
+ bufReader := bufio.NewReader(reader)
for running {
- reader := bufio.NewReader(os.Stdin)
- in, err := reader.ReadString('\n')
+ in, err := bufReader.ReadString('\n')
if err != nil {
return fmt.Errorf("cannot read input: %v\n", err)
}
@@ -33,7 +33,9 @@ func Loop(startSym string, en *Engine, ctx context.Context) error {
}
b := bytes.NewBuffer(nil)
en.WriteResult(b)
- fmt.Println(b.String())
+ //fmt.Println(b.String())
+ writer.Write(b.Bytes())
+ writer.Write([]byte{0x0a})
}
return nil
}
diff --git a/go/resource/fs.go b/go/resource/fs.go
@@ -44,7 +44,7 @@ func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
return fn, nil
}
_, err := fs.getFuncNoCtx(sym)
- if err != nil {
+ if err == nil {
return nil, fmt.Errorf("unknown sym: %s", sym)
}
return fs.getFunc, nil
diff --git a/go/state/state.go b/go/state/state.go
@@ -266,6 +266,7 @@ func(st *State) Up() (string, error) {
if len(st.execPath) > 0 {
sym = st.execPath[len(st.execPath)-1]
}
+ log.Printf("execpath %v", st.execPath)
st.sizeIdx = 0
return sym, nil
}
diff --git a/go/vm/input.go b/go/vm/input.go
@@ -11,10 +11,10 @@ import (
var (
inputRegexStr = "^[a-zA-Z0-9].*$"
inputRegex = regexp.MustCompile(inputRegexStr)
- ctrlRegexStr = "^[<>_]$"
- ctrlRegex = regexp.MustCompile(inputRegexStr)
+ ctrlRegexStr = "^[><_]$"
+ ctrlRegex = regexp.MustCompile(ctrlRegexStr)
symRegexStr = "^[a-zA-Z0-9][a-zA-Z0-9_]+$"
- symRegex = regexp.MustCompile(inputRegexStr)
+ symRegex = regexp.MustCompile(symRegexStr)
)
diff --git a/go/vm/runner.go b/go/vm/runner.go
@@ -255,16 +255,19 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
return b, nil
}
- log.Printf("input match for '%s'", input)
+ log.Printf("input match for '%s', target '%s'", input, target)
_, err = vm.st.SetFlag(state.FLAG_INMATCH)
- sym, _, err = applyTarget([]byte(target), vm.st, ctx)
+ target, _, err = applyTarget([]byte(target), vm.st, ctx)
+ if err != nil {
+ return b, err
+ }
code, err := vm.rs.GetCode(target)
if err != nil {
return b, err
}
- log.Printf("loaded additional code: %x", code)
+ log.Printf("loaded additional code for target '%s': %x", target, code)
b = append(b, code...)
return b, err
}
diff --git a/go/vm/runner_test.go b/go/vm/runner_test.go
@@ -185,7 +185,6 @@ func TestRunReload(t *testing.T) {
t.Fatal(err)
}
r, err := vm.Render()
-// r, err := pg.Val("dyn")
if err != nil {
t.Fatal(err)
}
@@ -199,14 +198,6 @@ func TestRunReload(t *testing.T) {
if err != nil {
t.Fatal(err)
}
-// r, err = pg.Val("dyn")
-// if err != nil {
-// t.Fatal(err)
-// }
-// log.Printf("dun now %s", r)
-// if r != "baz" {
-// t.Fatalf("expected result 'baz', got %v", r)
-// }
}
func TestHalt(t *testing.T) {
@@ -373,3 +364,38 @@ func TestRunMenuBrowse(t *testing.T) {
}
}
+func TestRunReturn(t *testing.T) {
+ st := state.NewState(5)
+ rs := TestResource{}
+ ca := cache.NewCache()
+ vm := NewVm(&st, &rs, ca, nil)
+
+ var err error
+
+ st.Down("root")
+ st.SetInput([]byte("0"))
+ b := NewLine(nil, INCMP, []string{"0", "bar"}, nil, nil)
+ b = NewLine(b, HALT, nil, nil, nil)
+ b = NewLine(b, INCMP, []string{"1", "_"}, nil, nil)
+ b = NewLine(b, HALT, nil, nil, nil)
+
+ ctx := context.TODO()
+
+ b, err = vm.Run(b, ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ location, _ := st.Where()
+ if location != "bar" {
+ t.Fatalf("expected location 'bar', got '%s'", location)
+ }
+ st.SetInput([]byte("1"))
+ b, err = vm.Run(b, ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ location, _ = st.Where()
+ if location != "root" {
+ t.Fatalf("expected location 'foo', got '%s'", location)
+ }
+}