Factor out map from add

This commit is contained in:
lash 2023-03-31 13:24:14 +01:00
parent ad518b92d8
commit a713d562c6
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 32 additions and 14 deletions

View File

@ -10,6 +10,7 @@ type State struct {
CacheSize uint32 CacheSize uint32
CacheUseSize uint32 CacheUseSize uint32
Cache []map[string]string Cache []map[string]string
CacheMap map[string]string
ExecPath []string ExecPath []string
} }
@ -37,6 +38,7 @@ func(st State) WithCacheSize(cacheSize uint32) State {
func(st *State) Enter(input string) { func(st *State) Enter(input string) {
m := make(map[string]string) m := make(map[string]string)
st.Cache = append(st.Cache, m) st.Cache = append(st.Cache, m)
st.CacheMap = make(map[string]string)
} }
func(st *State) Add(k string, v string) error { func(st *State) Add(k string, v string) error {
@ -50,6 +52,15 @@ func(st *State) Add(k string, v string) error {
return nil 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) { func(st *State) Get() (map[string]string, error) {
return st.Cache[len(st.Cache)-1], nil return st.Cache[len(st.Cache)-1], nil
} }

View File

@ -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) { func RunMap(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, error) {
head, tail, err := instructionSplit(instruction) head, tail, err := instructionSplit(instruction)
fn, err := rs.FuncFor(head)
if err != nil { if err != nil {
return st, err return st, err
} }
r, err := fn(tail, ctx) _ = tail
if err != nil { st.Map(head)
return st, err
}
st.Add(head, r)
return st, nil 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) { 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 return st, nil
} }
func RunReload(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, error) { func RunReload(instruction []byte, st state.State, rs resource.Fetcher, ctx context.Context) (state.State, error) {
return st, nil return st, nil
} }

View File

@ -37,13 +37,13 @@ func (r *TestResource) Render(sym string, values map[string]string) (string, err
if err != nil { if err != nil {
return "", err 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 { if err != nil {
return "", err return "", err
} }
b := bytes.NewBuffer([]byte{}) b := bytes.NewBuffer([]byte{})
err = t.Execute(b, values) err = tp.Execute(b, values)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -63,7 +63,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, 0x02} b := []byte{0x00, 0x01}
r, err := Run(b, st, &rs, context.TODO()) r, err := Run(b, st, &rs, context.TODO())
if err != nil { if err != nil {
t.Errorf("error on valid opcode: %v", err) t.Errorf("error on valid opcode: %v", err)
@ -77,14 +77,14 @@ func TestRun(t *testing.T) {
_ = r _ = r
} }
func TestRunMap(t *testing.T) { func TestRunLoad(t *testing.T) {
st := state.NewState(5) st := state.NewState(5)
st.Enter("barbarbar") st.Enter("barbarbar")
rs := TestResource{} rs := TestResource{}
sym := "one" sym := "one"
ins := append([]byte{uint8(len(sym))}, []byte(sym)...) ins := append([]byte{uint8(len(sym))}, []byte(sym)...)
var err error var err error
st, err = RunMap(ins, st, &rs, context.TODO()) st, err = RunLoad(ins, st, &rs, context.TODO())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -108,7 +108,7 @@ func TestRunMap(t *testing.T) {
sym = "two" sym = "two"
ins = append([]byte{uint8(len(sym))}, []byte(sym)...) 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 { if err != nil {
t.Error(err) t.Error(err)
} }