go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 00e3a9fb8761cc372e353c712a04558931c24280
parent 6b9a2c868952b4726f78546d9d9bab895b1cd695
Author: lash <dev@holbrook.no>
Date:   Sun, 23 Apr 2023 12:18:00 +0100

Reverse catch mode meaning

Diffstat:
Mexamples/profile/root.vis | 2+-
Mexamples/validate/root.vis | 2+-
Mresource/mem.go | 2++
Mstate/state.go | 19++++++++++---------
Mvm/runner.go | 15+++++++++------
Mvm/runner_test.go | 33++++++++++++++++++++++++++-------
6 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/examples/profile/root.vis b/examples/profile/root.vis @@ -1,5 +1,5 @@ LOAD myname 64 LOAD myemail 64 -CATCH identified 8 0 +CATCH identified 8 1 DOWN entry_name 0 "name" DOWN entry_email 1 "email" diff --git a/examples/validate/root.vis b/examples/validate/root.vis @@ -2,5 +2,5 @@ LOAD verifyinput 0 MAP verifyinput HALT RELOAD verifyinput -CATCH . 8 1 +CATCH . 8 0 MOVE end diff --git a/resource/mem.go b/resource/mem.go @@ -49,6 +49,7 @@ func(mr MemResource) getFunc(sym string) (EntryFunc, error) { } func(mr *MemResource) AddTemplate(sym string, tpl string) { + Logg.Tracef("mem resource added template", "sym", sym, "length", len(tpl)) mr.templates[sym] = tpl } @@ -58,5 +59,6 @@ func(mr *MemResource) AddEntryFunc(sym string, fn EntryFunc) { } func(mr *MemResource) AddBytecode(sym string, code []byte) { + Logg.Tracef("mem resource added bytecode", "sym", sym, "code", code) mr.bytecodes[sym] = code } diff --git a/state/state.go b/state/state.go @@ -146,19 +146,20 @@ func(st *State) FlagByteSize() uint8 { // The flag is specified given its bit index in the bit field. // // If invertMatch is set, a positive result will be returned if the flag is not set. -func(st *State) MatchFlag(sig uint32, invertMatch bool) (bool, error) { +func(st *State) MatchFlag(sig uint32, matchSet bool) (bool, error) { r, err := st.GetFlag(sig) if err != nil { return false, err } - if invertMatch { - if !r { - return true, nil - } - } else if r { - return true, nil - } - return false, nil + return matchSet && r, nil +// if matchSet { +// if !r { +// return false, nil +// } +// } else if r { +// return true, nil +// } +// return false, nil } // GetIndex scans a byte slice in same order as in storage, and returns the index of the first set bit. diff --git a/vm/runner.go b/vm/runner.go @@ -78,9 +78,10 @@ func(vmi *Vm) Reset() { // // On error, the remaining instructions will be returned. State will not be rolled back. func(vm *Vm) Run(ctx context.Context, b []byte) ([]byte, error) { + Logg.Tracef("new vm run") running := true for running { - r, err := vm.st.MatchFlag(state.FLAG_TERMINATE, false) + r, err := vm.st.MatchFlag(state.FLAG_TERMINATE, true) if err != nil { panic(err) } @@ -99,7 +100,9 @@ func(vm *Vm) Run(ctx context.Context, b []byte) ([]byte, error) { panic(err) } if change { - ctx = context.WithValue(ctx, "Language", *vm.st.Language) + if vm.st.Language != nil { + ctx = context.WithValue(ctx, "Language", *vm.st.Language) + } } @@ -180,7 +183,7 @@ func(vm *Vm) runErrCheck(ctx context.Context, b []byte, err error) ([]byte, erro } vm.pg = vm.pg.WithError(err) - v, err := vm.st.MatchFlag(state.FLAG_LOADFAIL, false) + v, err := vm.st.MatchFlag(state.FLAG_LOADFAIL, true) if err != nil { panic(err) } @@ -203,7 +206,7 @@ func(vm *Vm) runDeadCheck(ctx context.Context, b []byte) ([]byte, error) { if len(b) > 0 { return b, nil } - r, err := vm.st.MatchFlag(state.FLAG_READIN, true) + r, err := vm.st.MatchFlag(state.FLAG_READIN, false) if err != nil { panic(err) } @@ -215,7 +218,7 @@ func(vm *Vm) runDeadCheck(ctx context.Context, b []byte) ([]byte, error) { } return b, nil } - r, err = vm.st.MatchFlag(state.FLAG_TERMINATE, false) + r, err = vm.st.MatchFlag(state.FLAG_TERMINATE, true) if err != nil { panic(err) } @@ -558,7 +561,7 @@ func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (str } } - haveLang, err := vm.st.MatchFlag(state.FLAG_LANG, false) + haveLang, err := vm.st.MatchFlag(state.FLAG_LANG, true) if err != nil { panic(err) } diff --git a/vm/runner_test.go b/vm/runner_test.go @@ -43,6 +43,25 @@ func NewTestResource(st *state.State) TestResource { tr.AddEntryFunc("setFlagOne", setFlag) tr.AddEntryFunc("set_lang", set_lang) tr.AddEntryFunc("aiee", uhOh) + + var b []byte + b = NewLine(nil, HALT, nil, nil, nil) + tr.AddBytecode("one", b) + + b = NewLine(nil, MOUT, []string{"0", "repent"}, nil, nil) + b = NewLine(b, HALT, nil, nil, nil) + tr.AddBytecode("_catch", b) + + b = NewLine(nil, MOUT, []string{"0", "repent"}, nil, nil) + b = NewLine(b, HALT, nil, nil, nil) + b = NewLine(b, MOVE, []string{"_"}, nil, nil) + tr.AddBytecode("flagCatch", b) + + b = NewLine(nil, MOUT, []string{"1", "oo"}, nil, nil) + b = NewLine(b, HALT, nil, nil, nil) + tr.AddBytecode("ouf", b) + + tr.AddBytecode("root", tr.RootCode) return tr } @@ -132,7 +151,7 @@ func(r TestResource) getInput(ctx context.Context, sym string, input []byte) (re }, err } -func(r TestResource) GetCode(sym string) ([]byte, error) { +func(r TestResource) getCode(sym string) ([]byte, error) { var b []byte switch sym { case "_catch": @@ -400,6 +419,7 @@ func TestRunMenu(t *testing.T) { ctx := context.TODO() + rs.AddBytecode("foo", []byte{}) b := NewLine(nil, MOVE, []string{"foo"}, nil, nil) b = NewLine(b, MOUT, []string{"0", "one"}, nil, nil) b = NewLine(b, MOUT, []string{"1", "two"}, nil, nil) @@ -435,6 +455,7 @@ func TestRunMenuBrowse(t *testing.T) { ctx := context.TODO() + rs.AddBytecode("foo", []byte{}) b := NewLine(nil, MOVE, []string{"foo"}, nil, nil) b = NewLine(b, MOUT, []string{"0", "one"}, nil, nil) b = NewLine(b, MOUT, []string{"1", "two"}, nil, nil) @@ -538,12 +559,10 @@ func TestInputBranch(t *testing.T) { b := NewLine(nil, LOAD, []string{"setFlagOne"}, []byte{0x00}, nil) b = NewLine(b, RELOAD, []string{"setFlagOne"}, nil, nil) - b = NewLine(b, CATCH, []string{"flagCatch"}, []byte{8}, []uint8{0}) - b = NewLine(b, CATCH, []string{"one"}, []byte{9}, []uint8{0}) + b = NewLine(b, CATCH, []string{"flagCatch"}, []byte{8}, []uint8{1}) + b = NewLine(b, CATCH, []string{"one"}, []byte{9}, []uint8{1}) rs.RootCode = b - - //b = NewLine(b, RELOAD, []string{"setFlagOne"}, nil, nil) - //b = NewLine(b, CATCH, []string{"flagCatch"}, []byte{8}, []uint8{0}) + rs.AddBytecode("root", rs.RootCode) ctx := context.TODO() @@ -637,7 +656,7 @@ func TestCatchCleanMenu(t *testing.T) { b = NewLine(b, MOUT, []string{"2", "two"}, nil, nil) b = NewLine(b, HALT, nil, nil, nil) b = NewLine(b, INCMP, []string{"1", "foo"}, nil, nil) - b = NewLine(b, CATCH, []string{"ouf"}, []byte{0x08}, []uint8{0x01}) + b = NewLine(b, CATCH, []string{"ouf"}, []byte{0x08}, []uint8{0x00}) ctx := context.TODO()