Pass through wildcard when have match in incmp
This commit is contained in:
23
vm/runner.go
23
vm/runner.go
@@ -60,7 +60,11 @@ func(vm *Vm) Run(b []byte, ctx context.Context) ([]byte, error) {
|
||||
log.Printf("terminate set! bailing!")
|
||||
return []byte{}, nil
|
||||
}
|
||||
vm.st.ResetBaseFlags()
|
||||
//vm.st.ResetBaseFlags()
|
||||
_, err = vm.st.ResetFlag(state.FLAG_TERMINATE)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = vm.st.SetFlag(state.FLAG_DIRTY)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -271,13 +275,14 @@ func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
|
||||
}
|
||||
|
||||
// RunIncmp executes the INCMP opcode
|
||||
// TODO: create state transition table and simplify flow
|
||||
func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
|
||||
sym, target, b, err := ParseInCmp(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
|
||||
change, err := vm.st.SetFlag(state.FLAG_READIN)
|
||||
reading, err := vm.st.GetFlag(state.FLAG_READIN)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -286,7 +291,7 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
|
||||
panic(err)
|
||||
}
|
||||
if have {
|
||||
if change {
|
||||
if !reading {
|
||||
_, err = vm.st.ResetFlag(state.FLAG_INMATCH)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -295,18 +300,24 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
|
||||
log.Printf("ignoring input %s, already have match", sym)
|
||||
return b, nil
|
||||
}
|
||||
} else {
|
||||
_, err = vm.st.SetFlag(state.FLAG_READIN)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
input, err := vm.st.GetInput()
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
log.Printf("sym is %s", sym)
|
||||
if sym == "*" {
|
||||
log.Printf("testing sym %s input %s", sym, input)
|
||||
|
||||
if !have && sym == "*" {
|
||||
log.Printf("input wildcard match ('%s'), target '%s'", input, target)
|
||||
} else {
|
||||
if sym != string(input) {
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
log.Printf("input match for '%s', target '%s'", input, target)
|
||||
}
|
||||
|
||||
|
||||
@@ -426,7 +426,6 @@ func TestRunReturn(t *testing.T) {
|
||||
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)
|
||||
@@ -434,6 +433,7 @@ func TestRunReturn(t *testing.T) {
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
st.SetInput([]byte("0"))
|
||||
b, err = vm.Run(b, ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -522,3 +522,57 @@ func TestInputBranch(t *testing.T) {
|
||||
t.Fatalf("expected 'one', got %s", location)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInputIgnore(t *testing.T) {
|
||||
st := state.NewState(5)
|
||||
rs := TestResource{}
|
||||
ca := cache.NewCache()
|
||||
vm := NewVm(&st, &rs, ca, nil)
|
||||
|
||||
var err error
|
||||
|
||||
st.Down("root")
|
||||
|
||||
b := NewLine(nil, INCMP, []string{"foo", "one"}, nil, nil)
|
||||
b = NewLine(b, INCMP, []string{"bar", "two"}, nil, nil)
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
st.SetInput([]byte("foo"))
|
||||
b, err = vm.Run(b, ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
location, _ := st.Where()
|
||||
if location != "one" {
|
||||
t.Fatalf("expected 'one', got %s", location)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInputIgnoreWildcard(t *testing.T) {
|
||||
st := state.NewState(5)
|
||||
rs := TestResource{}
|
||||
ca := cache.NewCache()
|
||||
vm := NewVm(&st, &rs, ca, nil)
|
||||
|
||||
var err error
|
||||
|
||||
st.Down("root")
|
||||
|
||||
b := NewLine(nil, INCMP, []string{"foo", "one"}, nil, nil)
|
||||
b = NewLine(b, INCMP, []string{"*", "two"}, nil, nil)
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
st.SetInput([]byte("foo"))
|
||||
b, err = vm.Run(b, ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
location, _ := st.Where()
|
||||
if location != "one" {
|
||||
t.Fatalf("expected 'one', got %s", location)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user