Add catch implementation
This commit is contained in:
parent
324fd98cc3
commit
22d37de779
@ -126,6 +126,11 @@ func(st *State) FlagBitSize() uint32 {
|
|||||||
return st.bitSize
|
return st.bitSize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FlagBitSize reports the amount of bits available in the bit field index.
|
||||||
|
func(st *State) FlagByteSize() uint8 {
|
||||||
|
return uint8(len(st.Flags))
|
||||||
|
}
|
||||||
|
|
||||||
// GetIndex scans a byte slice in same order as in storage, and returns the index of the first set bit.
|
// GetIndex scans a byte slice in same order as in storage, and returns the index of the first set bit.
|
||||||
//
|
//
|
||||||
// If the given byte slice is too small for the bit field bitsize, the check will terminate at end-of-data without error.
|
// If the given byte slice is too small for the bit field bitsize, the check will terminate at end-of-data without error.
|
||||||
|
23
go/vm/vm.go
23
go/vm/vm.go
@ -120,13 +120,22 @@ func RunCatch(instruction []byte, st state.State, rs resource.Resource, ctx cont
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return st, instruction, err
|
return st, instruction, err
|
||||||
}
|
}
|
||||||
r, err := rs.Get(head)
|
// TODO: perhaps check against the registered byte size
|
||||||
if err != nil {
|
//l := st.FlagByteSize()
|
||||||
return st, instruction, err
|
bitFieldSize := tail[0]
|
||||||
}
|
bitField := tail[1:1+bitFieldSize]
|
||||||
_ = tail
|
tail = tail[1+bitFieldSize:]
|
||||||
st.Add(head, r, 0)
|
if st.GetIndex(bitField) {
|
||||||
return st, []byte{}, nil
|
log.Printf("catch at flag %v, moving to %v", bitField, head)
|
||||||
|
// r, err := rs.Get(head)
|
||||||
|
// if err != nil {
|
||||||
|
// return st, instruction, err
|
||||||
|
// }
|
||||||
|
//st.Add(head, r, 0)
|
||||||
|
st.Down(head)
|
||||||
|
tail = []byte{}
|
||||||
|
}
|
||||||
|
return st, tail, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunMap executes the CROAK opcode
|
// RunMap executes the CROAK opcode
|
||||||
|
@ -51,6 +51,7 @@ func (r *TestResource) Get(sym string) (string, error) {
|
|||||||
case "three":
|
case "three":
|
||||||
return "{{.one}} inky pinky {{.three}} blinky clyde {{.two}}", nil
|
return "{{.one}} inky pinky {{.three}} blinky clyde {{.two}}", nil
|
||||||
}
|
}
|
||||||
|
panic(fmt.Sprintf("unknown symbol %s", sym))
|
||||||
return "", fmt.Errorf("unknown symbol %s", sym)
|
return "", fmt.Errorf("unknown symbol %s", sym)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +90,7 @@ func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
|
|||||||
func TestRun(t *testing.T) {
|
func TestRun(t *testing.T) {
|
||||||
st := state.NewState(5)
|
st := state.NewState(5)
|
||||||
rs := TestResource{}
|
rs := TestResource{}
|
||||||
b := []byte{0x00, 0x01, 0x03}
|
b := []byte{0x00, MOVE, 0x03}
|
||||||
b = append(b, []byte("foo")...)
|
b = append(b, []byte("foo")...)
|
||||||
r, _, err := Run(b, st, &rs, context.TODO())
|
r, _, err := Run(b, st, &rs, context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -339,3 +340,34 @@ func TestRunMoveAndBack(t *testing.T) {
|
|||||||
t.Errorf("expected where-string 'bar', got %v", loc)
|
t.Errorf("expected where-string 'bar', got %v", loc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCatchAndBack(t *testing.T) {
|
||||||
|
st := state.NewState(5)
|
||||||
|
rs := TestResource{}
|
||||||
|
rt := router.NewRouter()
|
||||||
|
rt.Add("foo", "bar")
|
||||||
|
b := NewLine([]byte{}, LOAD, []string{"one"}, nil, []uint8{0})
|
||||||
|
b = NewLine(b, CATCH, []string{"bar"}, []byte{0x04}, nil)
|
||||||
|
b = NewLine(b, MOVE, []string{"foo"}, nil, nil)
|
||||||
|
st, _, err := Run(b, st, &rs, context.TODO())
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
r := st.Where()
|
||||||
|
if r != "foo" {
|
||||||
|
t.Errorf("expected where-symbol 'foo', got %v", r)
|
||||||
|
}
|
||||||
|
|
||||||
|
st.SetFlag(2)
|
||||||
|
b = NewLine([]byte{}, LOAD, []string{"two"}, nil, []uint8{0})
|
||||||
|
b = NewLine(b, CATCH, []string{"bar"}, []byte{0x04}, nil)
|
||||||
|
b = NewLine(b, MOVE, []string{"foo"}, nil, nil)
|
||||||
|
st, _, err = Run(b, st, &rs, context.TODO())
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
r = st.Where()
|
||||||
|
if r != "bar" {
|
||||||
|
t.Errorf("expected where-symbol 'bar', got %v", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user