go-vise

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

commit a713d562c6bc28620c669b63384634898e0604a5
parent ad518b92d8deb5ed83d1af5de632978abb5ab023
Author: lash <dev@holbrook.no>
Date:   Fri, 31 Mar 2023 13:24:14 +0100

Factor out map from add

Diffstat:
Mgo/state/state.go | 11+++++++++++
Mgo/vm/vm.go | 23+++++++++++++++--------
Mgo/vm/vm_test.go | 12++++++------
3 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/go/state/state.go b/go/state/state.go @@ -10,6 +10,7 @@ type State struct { CacheSize uint32 CacheUseSize uint32 Cache []map[string]string + CacheMap map[string]string ExecPath []string } @@ -37,6 +38,7 @@ func(st State) WithCacheSize(cacheSize uint32) State { func(st *State) Enter(input string) { m := make(map[string]string) st.Cache = append(st.Cache, m) + st.CacheMap = make(map[string]string) } func(st *State) Add(k string, v string) error { @@ -50,6 +52,15 @@ func(st *State) Add(k string, v string) error { return nil } +func(st *State) Map(k string) error { + m, err := st.Get() + if err != nil { + return err + } + st.CacheMap[k] = m[k] + return nil +} + func(st *State) Get() (map[string]string, error) { return st.Cache[len(st.Cache)-1], nil } diff --git a/go/vm/vm.go b/go/vm/vm.go @@ -48,15 +48,11 @@ func instructionSplit(b []byte) (string, []byte, error) { func RunMap(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, error) { head, tail, err := instructionSplit(instruction) - fn, err := rs.FuncFor(head) - if err != nil { - return st, err - } - r, err := fn(tail, ctx) if err != nil { return st, err } - st.Add(head, r) + _ = tail + st.Map(head) return st, nil } @@ -73,11 +69,22 @@ func RunCroak(instruction []byte, st state.State, rs resource.Fetcher, ctx conte } func RunLoad(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, error) { + head, tail, err := instructionSplit(instruction) + if err != nil { + return st, err + } + fn, err := rs.FuncFor(head) + if err != nil { + return st, err + } + r, err := fn(tail, ctx) + if err != nil { + return st, err + } + st.Add(head, r) return st, nil } func RunReload(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, error) { return st, nil } - - diff --git a/go/vm/vm_test.go b/go/vm/vm_test.go @@ -37,13 +37,13 @@ func (r *TestResource) Render(sym string, values map[string]string) (string, err if err != nil { return "", err } - t, err := template.New("tester").Option("missingkey=error").Parse(v) + tp, err := template.New("tester").Option("missingkey=error").Parse(v) if err != nil { return "", err } b := bytes.NewBuffer([]byte{}) - err = t.Execute(b, values) + err = tp.Execute(b, values) if err != nil { return "", err } @@ -63,7 +63,7 @@ func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) { func TestRun(t *testing.T) { st := state.NewState(5) rs := TestResource{} - b := []byte{0x00, 0x02} + b := []byte{0x00, 0x01} r, err := Run(b, st, &rs, context.TODO()) if err != nil { t.Errorf("error on valid opcode: %v", err) @@ -77,14 +77,14 @@ func TestRun(t *testing.T) { _ = r } -func TestRunMap(t *testing.T) { +func TestRunLoad(t *testing.T) { st := state.NewState(5) st.Enter("barbarbar") rs := TestResource{} sym := "one" ins := append([]byte{uint8(len(sym))}, []byte(sym)...) var err error - st, err = RunMap(ins, st, &rs, context.TODO()) + st, err = RunLoad(ins, st, &rs, context.TODO()) if err != nil { t.Error(err) } @@ -108,7 +108,7 @@ func TestRunMap(t *testing.T) { sym = "two" ins = append([]byte{uint8(len(sym))}, []byte(sym)...) - st, err = RunMap(ins, st, &rs, context.TODO()) + st, err = RunLoad(ins, st, &rs, context.TODO()) if err != nil { t.Error(err) }