forge/rep: code: compile recursively

This commit is contained in:
Helmut Merz 2023-08-02 09:33:07 +02:00
parent cea36b0320
commit fa2aa23e87
2 changed files with 24 additions and 10 deletions

View file

@ -19,17 +19,9 @@ func ParseJson(inp string) coderep {
func prepare(raw interface{}) coderep { func prepare(raw interface{}) coderep {
switch ri := raw.(type) { switch ri := raw.(type) {
case []interface{}: case []interface{}:
c := code{} return makeCode(ri)
for _, it := range ri {
c = append(c, it)
}
return c
case map[string]interface{}: case map[string]interface{}:
m := module{} return makeModule(ri)
for k, v := range ri {
m[k] = v
}
return m
} }
return nil return nil
} }

View file

@ -42,6 +42,12 @@ func (ci code) Compile(f forge.FE) forge.FPtr {
} }
case code: case code:
c.Append(i.Compile(f)) c.Append(i.Compile(f))
case []interface{}:
c.Append(makeCode(i).Compile(f))
case module:
c.Append(i.Compile(f))
case map[string]interface{}:
c.Append(makeModule(i).Compile(f))
} }
} }
return c.Reset() return c.Reset()
@ -57,6 +63,14 @@ func compStr(f forge.FE, s string) (forge.FItem, error) {
return nil, fmt.Errorf("not found: %s", s) return nil, fmt.Errorf("not found: %s", s)
} }
func makeCode(ri []interface{}) code {
c := code{}
for _, it := range ri {
c = append(c, it)
}
return c
}
// module - code definitions, represented by a map // module - code definitions, represented by a map
type module map[string]citem type module map[string]citem
@ -65,3 +79,11 @@ func (m module) Compile(f forge.FE) forge.FPtr {
v := voc.NewVoc(f.Voc()) v := voc.NewVoc(f.Voc())
return f.Code(v) return f.Code(v)
} }
func makeModule(ri map[string]interface{}) module {
m := module{}
for k, v := range ri {
m[k] = v
}
return m
}