Use correct target, ctrl regex for incmp

This commit is contained in:
lash 2023-04-10 07:54:52 +01:00
parent 084b23babd
commit 89deda1268
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 53 additions and 21 deletions

View File

@ -5,11 +5,11 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"os" "io"
"strings" "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) err := en.Init(startSym, ctx)
if err != nil { if err != nil {
return fmt.Errorf("cannot init: %v\n", err) 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()) fmt.Println(b.String())
running := true running := true
bufReader := bufio.NewReader(reader)
for running { for running {
reader := bufio.NewReader(os.Stdin) in, err := bufReader.ReadString('\n')
in, err := reader.ReadString('\n')
if err != nil { if err != nil {
return fmt.Errorf("cannot read input: %v\n", err) 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) b := bytes.NewBuffer(nil)
en.WriteResult(b) en.WriteResult(b)
fmt.Println(b.String()) //fmt.Println(b.String())
writer.Write(b.Bytes())
writer.Write([]byte{0x0a})
} }
return nil return nil
} }

View File

@ -44,7 +44,7 @@ func(fs FsResource) FuncFor(sym string) (EntryFunc, error) {
return fn, nil return fn, nil
} }
_, err := fs.getFuncNoCtx(sym) _, err := fs.getFuncNoCtx(sym)
if err != nil { if err == nil {
return nil, fmt.Errorf("unknown sym: %s", sym) return nil, fmt.Errorf("unknown sym: %s", sym)
} }
return fs.getFunc, nil return fs.getFunc, nil

View File

@ -266,6 +266,7 @@ func(st *State) Up() (string, error) {
if len(st.execPath) > 0 { if len(st.execPath) > 0 {
sym = st.execPath[len(st.execPath)-1] sym = st.execPath[len(st.execPath)-1]
} }
log.Printf("execpath %v", st.execPath)
st.sizeIdx = 0 st.sizeIdx = 0
return sym, nil return sym, nil
} }

View File

@ -11,10 +11,10 @@ import (
var ( var (
inputRegexStr = "^[a-zA-Z0-9].*$" inputRegexStr = "^[a-zA-Z0-9].*$"
inputRegex = regexp.MustCompile(inputRegexStr) inputRegex = regexp.MustCompile(inputRegexStr)
ctrlRegexStr = "^[<>_]$" ctrlRegexStr = "^[><_]$"
ctrlRegex = regexp.MustCompile(inputRegexStr) ctrlRegex = regexp.MustCompile(ctrlRegexStr)
symRegexStr = "^[a-zA-Z0-9][a-zA-Z0-9_]+$" symRegexStr = "^[a-zA-Z0-9][a-zA-Z0-9_]+$"
symRegex = regexp.MustCompile(inputRegexStr) symRegex = regexp.MustCompile(symRegexStr)
) )

View File

@ -255,16 +255,19 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
return b, nil 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) _, 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) code, err := vm.rs.GetCode(target)
if err != nil { if err != nil {
return b, err 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...) b = append(b, code...)
return b, err return b, err
} }

View File

@ -185,7 +185,6 @@ func TestRunReload(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
r, err := vm.Render() r, err := vm.Render()
// r, err := pg.Val("dyn")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -199,14 +198,6 @@ func TestRunReload(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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) { 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)
}
}