Rewrote the template generator to make it more flexible and easier to debug.

The template generator now has better support for pointers to primitives.
Tweaked some CSS.
This commit is contained in:
Azareal 2018-11-18 15:28:27 +10:00
parent 17f85ceccf
commit d99fd01ed9
2 changed files with 293 additions and 208 deletions

View File

@ -40,6 +40,11 @@ type CTemplateConfig struct {
PackageName string PackageName string
} }
type OutBufferFrame struct {
Body string
Type string
}
// nolint // nolint
type CTemplateSet struct { type CTemplateSet struct {
templateList map[string]*parse.Tree templateList map[string]*parse.Tree
@ -76,16 +81,16 @@ func NewCTemplateSet() *CTemplateSet {
"and": "&&", "and": "&&",
"not": "!", "not": "!",
"or": "||", "or": "||",
"eq": true, "eq": "==",
"ge": true, "ge": ">=",
"gt": true, "gt": ">",
"le": true, "le": "<=",
"lt": true, "lt": "<",
"ne": true, "ne": "!=",
"add": true, "add": "+",
"subtract": true, "subtract": "-",
"multiply": true, "multiply": "*",
"divide": true, "divide": "/",
"dock": true, "dock": true,
"elapsed": true, "elapsed": true,
"lang": true, "lang": true,
@ -135,7 +140,6 @@ func (c *CTemplateSet) Compile(name string, fileDir string, expects string, expe
c.localDispStructIndex = 0 c.localDispStructIndex = 0
c.stats = make(map[string]int) c.stats = make(map[string]int)
c.expectsInt = expectsInt c.expectsInt = expectsInt
holdreflect := reflect.ValueOf(expectsInt)
res, err := ioutil.ReadFile(fileDir + "overrides/" + name) res, err := ioutil.ReadFile(fileDir + "overrides/" + name)
if err != nil { if err != nil {
@ -160,13 +164,13 @@ func (c *CTemplateSet) Compile(name string, fileDir string, expects string, expe
c.detail(name) c.detail(name)
fname := strings.TrimSuffix(name, filepath.Ext(name)) fname := strings.TrimSuffix(name, filepath.Ext(name))
var outBuf []OutBufferFrame
con := CContext{VarHolder: "tmpl_" + fname + "_vars", HoldReflect: reflect.ValueOf(expectsInt), TemplateName: fname, OutBuf: &outBuf}
c.templateList = map[string]*parse.Tree{fname: tree} c.templateList = map[string]*parse.Tree{fname: tree}
varholder := "tmpl_" + fname + "_vars"
c.detail(c.templateList) c.detail(c.templateList)
c.localVars = make(map[string]map[string]VarItemReflect) c.localVars = make(map[string]map[string]VarItemReflect)
c.localVars[fname] = make(map[string]VarItemReflect) c.localVars[fname] = make(map[string]VarItemReflect)
c.localVars[fname]["."] = VarItemReflect{".", varholder, holdreflect} c.localVars[fname]["."] = VarItemReflect{".", con.VarHolder, con.HoldReflect}
if c.Fragments == nil { if c.Fragments == nil {
c.Fragments = make(map[string]int) c.Fragments = make(map[string]int)
} }
@ -178,7 +182,7 @@ func (c *CTemplateSet) Compile(name string, fileDir string, expects string, expe
c.TemplateFragmentCount = make(map[string]int) c.TemplateFragmentCount = make(map[string]int)
} }
out += c.rootIterate(c.templateList[fname], varholder, holdreflect, fname) c.rootIterate(c.templateList[fname], con)
c.TemplateFragmentCount[fname] = c.fragmentCursor[fname] + 1 c.TemplateFragmentCount[fname] = c.fragmentCursor[fname] + 1
if len(c.langIndexToName) > 0 { if len(c.langIndexToName) > 0 {
@ -211,7 +215,6 @@ func (c *CTemplateSet) Compile(name string, fileDir string, expects string, expe
if !c.config.SkipHandles { if !c.config.SkipHandles {
fout += "\tcommon.Template_" + fname + "_handle = Template_" + fname + "\n" fout += "\tcommon.Template_" + fname + "_handle = Template_" + fname + "\n"
fout += "\tcommon.Ctemplates = append(common.Ctemplates,\"" + fname + "\")\n\tcommon.TmplPtrMap[\"" + fname + "\"] = &common.Template_" + fname + "_handle\n" fout += "\tcommon.Ctemplates = append(common.Ctemplates,\"" + fname + "\")\n\tcommon.TmplPtrMap[\"" + fname + "\"] = &common.Template_" + fname + "_handle\n"
} }
@ -232,7 +235,11 @@ func (c *CTemplateSet) Compile(name string, fileDir string, expects string, expe
if len(c.langIndexToName) > 0 { if len(c.langIndexToName) > 0 {
fout += "var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n" fout += "var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n"
} }
fout += varString + out + "return nil\n}\n" fout += varString
for _, frame := range outBuf {
fout += frame.Body
}
fout += "return nil\n}\n"
fout = strings.Replace(fout, `)) fout = strings.Replace(fout, `))
w.Write([]byte(`, " + ", -1) w.Write([]byte(`, " + ", -1)
@ -252,7 +259,18 @@ w.Write([]byte(`, " + ", -1)
return fout, nil return fout, nil
} }
func (c *CTemplateSet) rootIterate(tree *parse.Tree, varholder string, holdreflect reflect.Value, fname string) (out string) { type CContext struct {
VarHolder string
HoldReflect reflect.Value
TemplateName string
OutBuf *[]OutBufferFrame
}
func (con *CContext) Push(nType string, body string) {
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, nType})
}
func (c *CTemplateSet) rootIterate(tree *parse.Tree, con CContext) {
c.detail(tree.Root) c.detail(tree.Root)
treeLength := len(tree.Root.Nodes) treeLength := len(tree.Root.Nodes)
for index, node := range tree.Root.Nodes { for index, node := range tree.Root.Nodes {
@ -262,13 +280,12 @@ func (c *CTemplateSet) rootIterate(tree *parse.Tree, varholder string, holdrefle
if treeLength != (index + 1) { if treeLength != (index + 1) {
c.nextNode = tree.Root.Nodes[index+1].Type() c.nextNode = tree.Root.Nodes[index+1].Type()
} }
out += c.compileSwitch(varholder, holdreflect, fname, node) c.compileSwitch(con, node)
} }
return out
} }
func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value, templateName string, node parse.Node) (out string) { func (c *CTemplateSet) compileSwitch(con CContext, node parse.Node) {
c.detail("in compileSwitch") c.dumpCall("compileSwitch", con, node)
switch node := node.(type) { switch node := node.(type) {
case *parse.ActionNode: case *parse.ActionNode:
c.detail("Action Node") c.detail("Action Node")
@ -276,7 +293,7 @@ func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value
break break
} }
for _, cmd := range node.Pipe.Cmds { for _, cmd := range node.Pipe.Cmds {
out += c.compileSubswitch(varholder, holdreflect, templateName, cmd) c.compileSubSwitch(con, cmd)
} }
case *parse.IfNode: case *parse.IfNode:
c.detail("If Node:") c.detail("If Node:")
@ -285,110 +302,126 @@ func (c *CTemplateSet) compileSwitch(varholder string, holdreflect reflect.Value
for _, cmd := range node.Pipe.Cmds { for _, cmd := range node.Pipe.Cmds {
c.detail("If Node Bit:", cmd) c.detail("If Node Bit:", cmd)
c.detail("Bit Type:", reflect.ValueOf(cmd).Type().Name()) c.detail("Bit Type:", reflect.ValueOf(cmd).Type().Name())
expr += c.compileVarswitch(varholder, holdreflect, templateName, cmd) expr += c.compileExprSwitch(con, cmd)
c.detail("Expression Step:", c.compileVarswitch(varholder, holdreflect, templateName, cmd)) c.detail("Expression Step:", c.compileExprSwitch(con, cmd))
} }
c.detail("Expression:", expr) c.detail("Expression:", expr)
c.previousNode = c.currentNode c.previousNode = c.currentNode
c.currentNode = parse.NodeList c.currentNode = parse.NodeList
c.nextNode = -1 c.nextNode = -1
out = "if " + expr + " {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.List) + "}" con.Push("startif", "if "+expr+" {\n")
c.compileSwitch(con, node.List)
if node.ElseList == nil { if node.ElseList == nil {
c.detail("Selected Branch 1") c.detail("Selected Branch 1")
return out + "\n" con.Push("endif", "}\n")
} else {
c.detail("Selected Branch 2")
con.Push("endif", "}")
con.Push("startelse", " else {\n")
c.compileSwitch(con, node.ElseList)
con.Push("endelse", "}\n")
} }
c.detail("Selected Branch 2")
return out + " else {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.ElseList) + "}\n"
case *parse.ListNode: case *parse.ListNode:
c.detail("List Node") c.detail("List Node")
for _, subnode := range node.Nodes { for _, subnode := range node.Nodes {
out += c.compileSwitch(varholder, holdreflect, templateName, subnode) c.compileSwitch(con, subnode)
} }
case *parse.RangeNode: case *parse.RangeNode:
return c.compileRangeNode(varholder, holdreflect, templateName, node) c.compileRangeNode(con, node)
case *parse.TemplateNode: case *parse.TemplateNode:
return c.compileSubtemplate(varholder, holdreflect, node) c.compileSubTemplate(con, node)
case *parse.TextNode: case *parse.TextNode:
c.previousNode = c.currentNode c.previousNode = c.currentNode
c.currentNode = node.Type() c.currentNode = node.Type()
c.nextNode = 0 c.nextNode = 0
tmpText := bytes.TrimSpace(node.Text) tmpText := bytes.TrimSpace(node.Text)
if len(tmpText) == 0 { if len(tmpText) == 0 {
return "" return
} }
fragmentName := templateName + "_" + strconv.Itoa(c.fragmentCursor[templateName]) fragmentName := con.TemplateName + "_" + strconv.Itoa(c.fragmentCursor[con.TemplateName])
fragmentPrefix := templateName + "_frags[" + strconv.Itoa(c.fragmentCursor[templateName]) + "]" fragmentPrefix := con.TemplateName + "_frags[" + strconv.Itoa(c.fragmentCursor[con.TemplateName]) + "]"
_, ok := c.Fragments[fragmentName] _, ok := c.Fragments[fragmentName]
if !ok { if !ok {
c.Fragments[fragmentName] = len(node.Text) c.Fragments[fragmentName] = len(node.Text)
c.FragOut += fragmentPrefix + " = []byte(`" + string(node.Text) + "`)\n" c.FragOut += fragmentPrefix + " = []byte(`" + string(node.Text) + "`)\n"
} }
c.fragmentCursor[templateName] = c.fragmentCursor[templateName] + 1 c.fragmentCursor[con.TemplateName] = c.fragmentCursor[con.TemplateName] + 1
return "w.Write(" + fragmentPrefix + ")\n" con.Push("text", "w.Write("+fragmentPrefix+")\n")
default: default:
return c.unknownNode(node) c.unknownNode(node)
} }
return out
} }
func (c *CTemplateSet) compileRangeNode(varholder string, holdreflect reflect.Value, templateName string, node *parse.RangeNode) (out string) { func (c *CTemplateSet) compileRangeNode(con CContext, node *parse.RangeNode) {
c.detail("Range Node!") c.dumpCall("compileRangeNode", con, node)
c.detail(node.Pipe) c.detail("node.Pipe: ", node.Pipe)
var expr string
var outVal reflect.Value var outVal reflect.Value
for _, cmd := range node.Pipe.Cmds { for _, cmd := range node.Pipe.Cmds {
c.detail("Range Bit:", cmd) c.detail("Range Bit:", cmd)
out, outVal = c.compileReflectSwitch(varholder, holdreflect, templateName, cmd) // ! This bit is slightly suspect, hm.
expr, outVal = c.compileReflectSwitch(con, cmd)
} }
c.detail("Returned:", out) c.detail("Expr:", expr)
c.detail("Range Kind Switch!") c.detail("Range Kind Switch!")
var startIf = func(item reflect.Value, useCopy bool) {
con.Push("startif", "if len("+expr+") != 0 {\n")
con.Push("startloop", "for _, item := range "+expr+" {\n")
ccon := con
ccon.VarHolder = "item"
ccon.HoldReflect = item
c.compileSwitch(ccon, node.List)
con.Push("endloop", "}\n")
if node.ElseList != nil {
con.Push("endif", "}")
con.Push("startelse", " else {\n")
if !useCopy {
ccon = con
}
c.compileSwitch(ccon, node.ElseList)
con.Push("endelse", "}\n")
} else {
con.Push("endloop", "}\n")
}
}
switch outVal.Kind() { switch outVal.Kind() {
case reflect.Map: case reflect.Map:
var item reflect.Value var item reflect.Value
for _, key := range outVal.MapKeys() { for _, key := range outVal.MapKeys() {
item = outVal.MapIndex(key) item = outVal.MapIndex(key)
} }
c.detail("Range item:", item) c.detail("Range item:", item)
if !item.IsValid() { if !item.IsValid() {
panic("item" + "^\n" + "Invalid map. Maybe, it doesn't have any entries for the template engine to analyse?") panic("item" + "^\n" + "Invalid map. Maybe, it doesn't have any entries for the template engine to analyse?")
} }
startIf(item, true)
out = "if len(" + out + ") != 0 {\nfor _, item := range " + out + " {\n" + c.compileSwitch("item", item, templateName, node.List) + "}\n}"
if node.ElseList != nil {
out += " else {\n" + c.compileSwitch("item", item, templateName, node.ElseList) + "}\n"
}
case reflect.Slice: case reflect.Slice:
if outVal.Len() == 0 { if outVal.Len() == 0 {
panic("The sample data needs at-least one or more elements for the slices. We're looking into removing this requirement at some point!") panic("The sample data needs at-least one or more elements for the slices. We're looking into removing this requirement at some point!")
} }
item := outVal.Index(0) startIf(outVal.Index(0), false)
out = "if len(" + out + ") != 0 {\nfor _, item := range " + out + " {\n" + c.compileSwitch("item", item, templateName, node.List) + "}\n}"
if node.ElseList != nil {
out += " else {\n" + c.compileSwitch(varholder, holdreflect, templateName, node.ElseList) + "}"
}
case reflect.Invalid: case reflect.Invalid:
return "" return
} }
return out + "\n"
} }
func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) { func (c *CTemplateSet) compileSubSwitch(con CContext, node *parse.CommandNode) {
c.detail("in compileSubswitch") c.dumpCall("compileSubSwitch", con, node)
firstWord := node.Args[0] firstWord := node.Args[0]
switch n := firstWord.(type) { switch n := firstWord.(type) {
case *parse.FieldNode: case *parse.FieldNode:
c.detail("Field Node:", n.Ident) c.detail("Field Node:", n.Ident)
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Variable declarations are coming soon! */ /* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Variable declarations are coming soon! */
cur := holdreflect cur := con.HoldReflect
var varbit string var varBit string
if cur.Kind() == reflect.Interface { if cur.Kind() == reflect.Interface {
cur = cur.Elem() cur = cur.Elem()
varbit += ".(" + cur.Type().Name() + ")" varBit += ".(" + cur.Type().Name() + ")"
} }
// ! Might not work so well for non-struct pointers // ! Might not work so well for non-struct pointers
@ -413,22 +446,22 @@ func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Va
if !cur.IsValid() { if !cur.IsValid() {
c.error("Debug Data:") c.error("Debug Data:")
c.error("Holdreflect:", holdreflect) c.error("Holdreflect:", con.HoldReflect)
c.error("Holdreflect.Kind():", holdreflect.Kind()) c.error("Holdreflect.Kind():", con.HoldReflect.Kind())
if !c.config.SuperDebug { if !c.config.SuperDebug {
c.error("cur.Kind():", cur.Kind().String()) c.error("cur.Kind():", cur.Kind().String())
} }
c.error("") c.error("")
if !multiline { if !multiline {
panic(varholder + varbit + "^\n" + "Invalid value. Maybe, it doesn't exist?") panic(con.VarHolder + varBit + "^\n" + "Invalid value. Maybe, it doesn't exist?")
} }
panic(varbit + "^\n" + "Invalid value. Maybe, it doesn't exist?") panic(varBit + "^\n" + "Invalid value. Maybe, it doesn't exist?")
} }
c.detail("in-loop varbit: " + varbit) c.detail("in-loop varBit: " + varBit)
if cur.Kind() == reflect.Map { if cur.Kind() == reflect.Map {
cur = cur.MapIndex(reflect.ValueOf(id)) cur = cur.MapIndex(reflect.ValueOf(id))
varbit += "[\"" + id + "\"]" varBit += "[\"" + id + "\"]"
cur = skipPointers(cur, id) cur = skipPointers(cur, id)
if cur.Kind() == reflect.Struct || cur.Kind() == reflect.Interface { if cur.Kind() == reflect.Struct || cur.Kind() == reflect.Interface {
@ -447,8 +480,8 @@ func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Va
newVarByte = ":" newVarByte = ":"
c.localDispStructIndex++ c.localDispStructIndex++
} }
varholder = "disp" + dispStr con.VarHolder = "disp" + dispStr
varbit = varholder + " " + newVarByte + "= " + varholder + varbit + "\n" varBit = con.VarHolder + " " + newVarByte + "= " + con.VarHolder + varBit + "\n"
multiline = true multiline = true
} else { } else {
continue continue
@ -456,65 +489,65 @@ func (c *CTemplateSet) compileSubswitch(varholder string, holdreflect reflect.Va
} }
if cur.Kind() != reflect.Interface { if cur.Kind() != reflect.Interface {
cur = cur.FieldByName(id) cur = cur.FieldByName(id)
varbit += "." + id varBit += "." + id
} }
// TODO: Handle deeply nested pointers mixed with interfaces mixed with pointers better // TODO: Handle deeply nested pointers mixed with interfaces mixed with pointers better
if cur.Kind() == reflect.Interface { if cur.Kind() == reflect.Interface {
cur = cur.Elem() cur = cur.Elem()
varbit += ".(" varBit += ".("
// TODO: Surely, there's a better way of doing this? // TODO: Surely, there's a better way of doing this?
if cur.Type().PkgPath() != "main" && cur.Type().PkgPath() != "" { if cur.Type().PkgPath() != "main" && cur.Type().PkgPath() != "" {
c.importMap["html/template"] = "html/template" c.importMap["html/template"] = "html/template"
varbit += strings.TrimPrefix(cur.Type().PkgPath(), "html/") + "." varBit += strings.TrimPrefix(cur.Type().PkgPath(), "html/") + "."
} }
varbit += cur.Type().Name() + ")" varBit += cur.Type().Name() + ")"
} }
c.detail("End Cycle: ", varbit) c.detail("End Cycle: ", varBit)
} }
if multiline { if multiline {
assSplit := strings.Split(varbit, "\n") assSplit := strings.Split(varBit, "\n")
varbit = assSplit[len(assSplit)-1] varBit = assSplit[len(assSplit)-1]
assSplit = assSplit[:len(assSplit)-1] assSplit = assSplit[:len(assSplit)-1]
assLines = strings.Join(assSplit, "\n") + "\n" assLines = strings.Join(assSplit, "\n") + "\n"
} }
varbit = varholder + varbit c.compileVarSub(con, con.VarHolder+varBit, cur, assLines, func(in string) string {
out = c.compileVarsub(varbit, cur, assLines) for _, varItem := range c.varList {
if strings.HasPrefix(in, varItem.Destination) {
for _, varItem := range c.varList { in = strings.Replace(in, varItem.Destination, varItem.Name, 1)
if strings.HasPrefix(out, varItem.Destination) { }
out = strings.Replace(out, varItem.Destination, varItem.Name, 1)
} }
} return in
return out })
case *parse.DotNode: case *parse.DotNode:
c.detail("Dot Node:", node.String()) c.detail("Dot Node:", node.String())
return c.compileVarsub(varholder, holdreflect, "") c.compileVarSub(con, con.VarHolder, con.HoldReflect, "", nil)
case *parse.NilNode: case *parse.NilNode:
panic("Nil is not a command x.x") panic("Nil is not a command x.x")
case *parse.VariableNode: case *parse.VariableNode:
c.detail("Variable Node:", n.String()) c.detail("Variable Node:", n.String())
c.detail(n.Ident) c.detail(n.Ident)
varname, reflectVal := c.compileIfVarsub(n.String(), varholder, templateName, holdreflect) varname, reflectVal := c.compileIfVarSub(con, n.String())
return c.compileVarsub(varname, reflectVal, "") c.compileVarSub(con, varname, reflectVal, "", nil)
case *parse.StringNode: case *parse.StringNode:
return n.Quoted con.Push("stringnode", n.Quoted)
case *parse.IdentifierNode: case *parse.IdentifierNode:
c.detail("Identifier Node:", node) c.detail("Identifier Node:", node)
c.detail("Identifier Node Args:", node.Args) c.detail("Identifier Node Args:", node.Args)
out, outval, lit := c.compileIdentSwitch(varholder, holdreflect, templateName, node) out, outval, lit := c.compileIdentSwitch(con, node)
if lit { if lit {
return out con.Push("identifier", out)
return
} }
return c.compileVarsub(out, outval, "") c.compileVarSub(con, out, outval, "", nil)
default: default:
return c.unknownNode(node) c.unknownNode(node)
} }
} }
func (c *CTemplateSet) compileVarswitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) { func (c *CTemplateSet) compileExprSwitch(con CContext, node *parse.CommandNode) (out string) {
c.detail("in compileVarswitch") c.detail("in compileExprSwitch")
firstWord := node.Args[0] firstWord := node.Args[0]
switch n := firstWord.(type) { switch n := firstWord.(type) {
case *parse.FieldNode: case *parse.FieldNode:
@ -524,45 +557,44 @@ func (c *CTemplateSet) compileVarswitch(varholder string, holdreflect reflect.Va
fmt.Println("Field Bit:", id) fmt.Println("Field Bit:", id)
} }
} }
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */ /* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */
return c.compileBoolsub(n.String(), varholder, templateName, holdreflect) return c.compileBoolSub(n.String(), con)
case *parse.ChainNode: case *parse.ChainNode:
c.detail("Chain Node:", n.Node) c.detail("Chain Node:", n.Node)
c.detail("Node Args:", node.Args) c.detail("Node Args:", node.Args)
case *parse.IdentifierNode: case *parse.IdentifierNode:
c.detail("Identifier Node:", node) c.detail("Identifier Node:", node)
c.detail("Node Args:", node.Args) c.detail("Node Args:", node.Args)
return c.compileIdentSwitchN(varholder, holdreflect, templateName, node) return c.compileIdentSwitchN(con, node)
case *parse.DotNode: case *parse.DotNode:
return varholder return con.VarHolder
case *parse.VariableNode: case *parse.VariableNode:
c.detail("Variable Node:", n.String()) c.detail("Variable Node:", n.String())
c.detail("Node Identifier:", n.Ident) c.detail("Node Identifier:", n.Ident)
out, _ = c.compileIfVarsub(n.String(), varholder, templateName, holdreflect) out, _ = c.compileIfVarSub(con, n.String())
case *parse.NilNode: case *parse.NilNode:
panic("Nil is not a command x.x") panic("Nil is not a command x.x")
case *parse.PipeNode: case *parse.PipeNode:
c.detail("Pipe Node!") c.detail("Pipe Node!")
c.detail(n) c.detail(n)
c.detail("Node Args:", node.Args) c.detail("Node Args:", node.Args)
out += c.compileIdentSwitchN(varholder, holdreflect, templateName, node) out += c.compileIdentSwitchN(con, node)
c.detail("Out:", out) c.detail("Out:", out)
default: default:
return c.unknownNode(firstWord) c.unknownNode(firstWord)
} }
return out return out
} }
func (c *CTemplateSet) unknownNode(node parse.Node) (out string) { func (c *CTemplateSet) unknownNode(node parse.Node) {
fmt.Println("Unknown Kind:", reflect.ValueOf(node).Elem().Kind()) fmt.Println("Unknown Kind:", reflect.ValueOf(node).Elem().Kind())
fmt.Println("Unknown Type:", reflect.ValueOf(node).Elem().Type().Name()) fmt.Println("Unknown Type:", reflect.ValueOf(node).Elem().Type().Name())
panic("I don't know what node this is! Grr...") panic("I don't know what node this is! Grr...")
} }
func (c *CTemplateSet) compileIdentSwitchN(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string) { func (c *CTemplateSet) compileIdentSwitchN(con CContext, node *parse.CommandNode) (out string) {
c.detail("in compileIdentSwitchN") c.detail("in compileIdentSwitchN")
out, _, _ = c.compileIdentSwitch(varholder, holdreflect, templateName, node) out, _, _ = c.compileIdentSwitch(con, node)
return out return out
} }
@ -572,14 +604,14 @@ func (c *CTemplateSet) dumpSymbol(pos int, node *parse.CommandNode, symbol strin
c.detail("node.Args[pos + 2]", node.Args[pos+2]) c.detail("node.Args[pos + 2]", node.Args[pos+2])
} }
func (c *CTemplateSet) compareFunc(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, compare string) (out string) { func (c *CTemplateSet) compareFunc(con CContext, pos int, node *parse.CommandNode, compare string) (out string) {
c.dumpSymbol(pos, node, compare) c.dumpSymbol(pos, node, compare)
return c.compileIfVarsubN(node.Args[pos+1].String(), varholder, templateName, holdreflect) + " " + compare + " " + c.compileIfVarsubN(node.Args[pos+2].String(), varholder, templateName, holdreflect) return c.compileIfVarSubN(con, node.Args[pos+1].String()) + " " + compare + " " + c.compileIfVarSubN(con, node.Args[pos+2].String())
} }
func (c *CTemplateSet) simpleMath(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, symbol string) (out string, val reflect.Value) { func (c *CTemplateSet) simpleMath(con CContext, pos int, node *parse.CommandNode, symbol string) (out string, val reflect.Value) {
leftParam, val2 := c.compileIfVarsub(node.Args[pos+1].String(), varholder, templateName, holdreflect) leftParam, val2 := c.compileIfVarSub(con, node.Args[pos+1].String())
rightParam, val3 := c.compileIfVarsub(node.Args[pos+2].String(), varholder, templateName, holdreflect) rightParam, val3 := c.compileIfVarSub(con, node.Args[pos+2].String())
if val2.IsValid() { if val2.IsValid() {
val = val2 val = val2
@ -594,7 +626,7 @@ func (c *CTemplateSet) simpleMath(varholder string, holdreflect reflect.Value, t
return leftParam + " " + symbol + " " + rightParam, val return leftParam + " " + symbol + " " + rightParam, val
} }
func (c *CTemplateSet) compareJoin(varholder string, holdreflect reflect.Value, templateName string, pos int, node *parse.CommandNode, symbol string) (pos2 int, out string) { func (c *CTemplateSet) compareJoin(con CContext, pos int, node *parse.CommandNode, symbol string) (pos2 int, out string) {
c.detailf("Building %s function", symbol) c.detailf("Building %s function", symbol)
if pos == 0 { if pos == 0 {
fmt.Println("pos:", pos) fmt.Println("pos:", pos)
@ -606,12 +638,12 @@ func (c *CTemplateSet) compareJoin(varholder string, holdreflect reflect.Value,
panic(symbol + " is missing a right operand") panic(symbol + " is missing a right operand")
} }
left := c.compileBoolsub(node.Args[pos-1].String(), varholder, templateName, holdreflect) left := c.compileBoolSub(node.Args[pos-1].String(), con)
_, funcExists := c.funcMap[node.Args[pos+1].String()] _, funcExists := c.funcMap[node.Args[pos+1].String()]
var right string var right string
if !funcExists { if !funcExists {
right = c.compileBoolsub(node.Args[pos+1].String(), varholder, templateName, holdreflect) right = c.compileBoolSub(node.Args[pos+1].String(), con)
} }
out = left + " " + symbol + " " + right out = left + " " + symbol + " " + right
@ -626,26 +658,12 @@ func (c *CTemplateSet) compareJoin(varholder string, holdreflect reflect.Value,
return pos, out return pos, out
} }
func (c *CTemplateSet) compileIdentSwitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string, val reflect.Value, literal bool) { func (c *CTemplateSet) compileIdentSwitch(con CContext, node *parse.CommandNode) (out string, val reflect.Value, literal bool) {
c.detail("in compileIdentSwitch") c.dumpCall("compileIdentSwitch", con, node)
var litString = func(inner string) { var litString = func(inner string) {
out = "w.Write([]byte(" + inner + "))\n" out = "w.Write([]byte(" + inner + "))\n"
literal = true literal = true
} }
var compOpMappings = map[string]string{
"le": "<=",
"lt": "<",
"gt": ">",
"ge": ">=",
"eq": "==",
"ne": "!=",
}
var mathOpMappings = map[string]string{
"add": "+",
"subtract": "-",
"divide": "/",
"multiply": "*",
}
ArgLoop: ArgLoop:
for pos := 0; pos < len(node.Args); pos++ { for pos := 0; pos < len(node.Args); pos++ {
id := node.Args[pos] id := node.Args[pos]
@ -656,43 +674,40 @@ ArgLoop:
out += "!" out += "!"
case "or", "and": case "or", "and":
var rout string var rout string
pos, rout = c.compareJoin(varholder, holdreflect, templateName, pos, node, c.funcMap[id.String()].(string)) // TODO: Test this pos, rout = c.compareJoin(con, pos, node, c.funcMap[id.String()].(string)) // TODO: Test this
out += rout out += rout
case "le", "lt", "gt", "ge", "eq", "ne": case "le", "lt", "gt", "ge", "eq", "ne":
out += c.compareFunc(varholder, holdreflect, templateName, pos, node, compOpMappings[id.String()]) out += c.compareFunc(con, pos, node, c.funcMap[id.String()].(string))
break ArgLoop break ArgLoop
case "add", "subtract", "divide", "multiply": case "add", "subtract", "divide", "multiply":
rout, rval := c.simpleMath(varholder, holdreflect, templateName, pos, node, mathOpMappings[id.String()]) rout, rval := c.simpleMath(con, pos, node, c.funcMap[id.String()].(string))
out += rout out += rout
val = rval val = rval
break ArgLoop break ArgLoop
case "elapsed": case "elapsed":
leftOperand := node.Args[pos+1].String() leftOperand := node.Args[pos+1].String()
leftParam, _ := c.compileIfVarsub(leftOperand, varholder, templateName, holdreflect) leftParam, _ := c.compileIfVarSub(con, leftOperand)
// TODO: Refactor this // TODO: Refactor this
// TODO: Validate that this is actually a time.Time // TODO: Validate that this is actually a time.Time
litString("time.Since(" + leftParam + ").String()") litString("time.Since(" + leftParam + ").String()")
c.importMap["time"] = "time" c.importMap["time"] = "time"
break ArgLoop break ArgLoop
case "dock": case "dock":
var leftParam, rightParam string
// TODO: Implement string literals properly // TODO: Implement string literals properly
leftOperand := node.Args[pos+1].String() leftOperand := node.Args[pos+1].String()
rightOperand := node.Args[pos+2].String() rightOperand := node.Args[pos+2].String()
if len(leftOperand) == 0 || len(rightOperand) == 0 { if len(leftOperand) == 0 || len(rightOperand) == 0 {
panic("The left or right operand for function dock cannot be left blank") panic("The left or right operand for function dock cannot be left blank")
} }
if leftOperand[0] == '"' {
leftParam = leftOperand leftParam := leftOperand
} else { if leftOperand[0] != '"' {
leftParam, _ = c.compileIfVarsub(leftOperand, varholder, templateName, holdreflect) leftParam, _ = c.compileIfVarSub(con, leftParam)
} }
if rightOperand[0] == '"' { if rightOperand[0] == '"' {
panic("The right operand for function dock cannot be a string") panic("The right operand for function dock cannot be a string")
} }
rightParam, val3 := c.compileIfVarSub(con, rightOperand)
rightParam, val3 := c.compileIfVarsub(rightOperand, varholder, templateName, holdreflect)
if !val3.IsValid() { if !val3.IsValid() {
panic("val3 is invalid") panic("val3 is invalid")
} }
@ -722,8 +737,7 @@ ArgLoop:
if len(leftOperand) == 0 { if len(leftOperand) == 0 {
panic("The leftoperand for function level cannot be left blank") panic("The leftoperand for function level cannot be left blank")
} }
leftParam, _ := c.compileIfVarSub(con, leftOperand)
leftParam, _ := c.compileIfVarsub(leftOperand, varholder, templateName, holdreflect)
// TODO: Refactor this // TODO: Refactor this
litString("phrases.GetLevelPhrase(" + leftParam + ")") litString("phrases.GetLevelPhrase(" + leftParam + ")")
c.importMap[langPkg] = langPkg c.importMap[langPkg] = langPkg
@ -732,20 +746,18 @@ ArgLoop:
literal = true literal = true
break ArgLoop break ArgLoop
case "dyntmpl": case "dyntmpl":
var nameParam, pageParam, headParam string var pageParam, headParam string
// TODO: Implement string literals properly // TODO: Implement string literals properly
// TODO: Should we check to see if pos+3 is within the bounds of the slice? // TODO: Should we check to see if pos+3 is within the bounds of the slice?
nameOperand := node.Args[pos+1].String() nameOperand := node.Args[pos+1].String()
pageOperand := node.Args[pos+2].String() pageOperand := node.Args[pos+2].String()
headOperand := node.Args[pos+3].String() headOperand := node.Args[pos+3].String()
if len(nameOperand) == 0 || len(pageOperand) == 0 || len(headOperand) == 0 { if len(nameOperand) == 0 || len(pageOperand) == 0 || len(headOperand) == 0 {
panic("None of the three operands for function dyntmpl can be left blank") panic("None of the three operands for function dyntmpl can be left blank")
} }
if nameOperand[0] == '"' { nameParam := nameOperand
nameParam = nameOperand if nameOperand[0] != '"' {
} else { nameParam, _ = c.compileIfVarSub(con, nameParam)
nameParam, _ = c.compileIfVarsub(nameOperand, varholder, templateName, holdreflect)
} }
if pageOperand[0] == '"' { if pageOperand[0] == '"' {
panic("The page operand for function dyntmpl cannot be a string") panic("The page operand for function dyntmpl cannot be a string")
@ -754,11 +766,11 @@ ArgLoop:
panic("The head operand for function dyntmpl cannot be a string") panic("The head operand for function dyntmpl cannot be a string")
} }
pageParam, val3 := c.compileIfVarsub(pageOperand, varholder, templateName, holdreflect) pageParam, val3 := c.compileIfVarSub(con, pageOperand)
if !val3.IsValid() { if !val3.IsValid() {
panic("val3 is invalid") panic("val3 is invalid")
} }
headParam, val4 := c.compileIfVarsub(headOperand, varholder, templateName, holdreflect) headParam, val4 := c.compileIfVarSub(con, headOperand)
if !val4.IsValid() { if !val4.IsValid() {
panic("val4 is invalid") panic("val4 is invalid")
} }
@ -778,13 +790,14 @@ ArgLoop:
continue continue
} }
} }
out += c.compileIfVarsubN(id.String(), varholder, templateName, holdreflect) out += c.compileIfVarSubN(con, id.String())
} }
} }
c.retCall("compileIdentSwitch", out, val, literal)
return out, val, literal return out, val, literal
} }
func (c *CTemplateSet) compileReflectSwitch(varholder string, holdreflect reflect.Value, templateName string, node *parse.CommandNode) (out string, outVal reflect.Value) { func (c *CTemplateSet) compileReflectSwitch(con CContext, node *parse.CommandNode) (out string, outVal reflect.Value) {
c.detail("in compileReflectSwitch") c.detail("in compileReflectSwitch")
firstWord := node.Args[0] firstWord := node.Args[0]
switch n := firstWord.(type) { switch n := firstWord.(type) {
@ -796,12 +809,12 @@ func (c *CTemplateSet) compileReflectSwitch(varholder string, holdreflect reflec
} }
} }
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */ /* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */
return c.compileIfVarsub(n.String(), varholder, templateName, holdreflect) return c.compileIfVarSub(con, n.String())
case *parse.ChainNode: case *parse.ChainNode:
c.detail("Chain Node:", n.Node) c.detail("Chain Node:", n.Node)
c.detail("node.Args:", node.Args) c.detail("node.Args:", node.Args)
case *parse.DotNode: case *parse.DotNode:
return varholder, holdreflect return con.VarHolder, con.HoldReflect
case *parse.NilNode: case *parse.NilNode:
panic("Nil is not a command x.x") panic("Nil is not a command x.x")
default: default:
@ -810,25 +823,32 @@ func (c *CTemplateSet) compileReflectSwitch(varholder string, holdreflect reflec
return "", outVal return "", outVal
} }
func (c *CTemplateSet) compileIfVarsubN(varname string, varholder string, templateName string, cur reflect.Value) (out string) { func (c *CTemplateSet) compileIfVarSubN(con CContext, varname string) (out string) {
c.detail("in compileIfVarsubN") c.dumpCall("compileIfVarSubN", con, varname)
out, _ = c.compileIfVarsub(varname, varholder, templateName, cur) out, _ = c.compileIfVarSub(con, varname)
return out return out
} }
func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templateName string, cur reflect.Value) (out string, val reflect.Value) { func (c *CTemplateSet) compileIfVarSub(con CContext, varname string) (out string, val reflect.Value) {
c.detail("in compileIfVarsub") c.dumpCall("compileIfVarSub", con, varname)
cur := con.HoldReflect
if varname[0] != '.' && varname[0] != '$' { if varname[0] != '.' && varname[0] != '$' {
return varname, cur return varname, cur
} }
var stepInterface = func() {
if cur.Kind() == reflect.Interface {
cur = cur.Elem()
out += ".(" + cur.Type().Name() + ")"
}
}
bits := strings.Split(varname, ".") bits := strings.Split(varname, ".")
if varname[0] == '$' { if varname[0] == '$' {
var res VarItemReflect var res VarItemReflect
if varname[1] == '.' { if varname[1] == '.' {
res = c.localVars[templateName]["."] res = c.localVars[con.TemplateName]["."]
} else { } else {
res = c.localVars[templateName][strings.TrimPrefix(bits[0], "$")] res = c.localVars[con.TemplateName][strings.TrimPrefix(bits[0], "$")]
} }
out += res.Destination out += res.Destination
cur = res.Value cur = res.Value
@ -837,16 +857,16 @@ func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templat
cur = cur.Elem() cur = cur.Elem()
} }
} else { } else {
out += varholder out += con.VarHolder
if cur.Kind() == reflect.Interface { stepInterface()
cur = cur.Elem()
out += ".(" + cur.Type().Name() + ")"
}
} }
bits[0] = strings.TrimPrefix(bits[0], "$") bits[0] = strings.TrimPrefix(bits[0], "$")
c.detail("Cur Kind:", cur.Kind()) var dumpKind = func(pre string) {
c.detail("Cur Type:", cur.Type().Name()) c.detail(pre+" Kind:", cur.Kind())
c.detail(pre+" Type:", cur.Type().Name())
}
dumpKind("Cur")
for _, bit := range bits { for _, bit := range bits {
c.detail("Variable Field:", bit) c.detail("Variable Field:", bit)
if bit == "" { if bit == "" {
@ -865,31 +885,23 @@ func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templat
cur = cur.FieldByName(bit) cur = cur.FieldByName(bit)
out += "." + bit out += "." + bit
if cur.Kind() == reflect.Interface { stepInterface()
cur = cur.Elem()
out += ".(" + cur.Type().Name() + ")"
}
if !cur.IsValid() { if !cur.IsValid() {
fmt.Println("cur: ", cur) fmt.Println("cur: ", cur)
panic(out + "^\n" + "Invalid value. Maybe, it doesn't exist?") panic(out + "^\n" + "Invalid value. Maybe, it doesn't exist?")
} }
c.detail("Data Kind:", cur.Kind()) dumpKind("Data")
c.detail("Data Type:", cur.Type().Name())
} }
c.detail("Out Value:", out) c.detail("Out Value:", out)
c.detail("Out Kind:", cur.Kind()) dumpKind("Out")
c.detail("Out Type:", cur.Type().Name())
for _, varItem := range c.varList { for _, varItem := range c.varList {
if strings.HasPrefix(out, varItem.Destination) { if strings.HasPrefix(out, varItem.Destination) {
out = strings.Replace(out, varItem.Destination, varItem.Name, 1) out = strings.Replace(out, varItem.Destination, varItem.Name, 1)
} }
} }
c.detail("Out Value:", out) c.detail("Out Value:", out)
c.detail("Out Kind:", cur.Kind()) dumpKind("Out")
c.detail("Out Type:", cur.Type().Name())
_, ok := c.stats[out] _, ok := c.stats[out]
if ok { if ok {
@ -901,9 +913,9 @@ func (c *CTemplateSet) compileIfVarsub(varname string, varholder string, templat
return out, cur return out, cur
} }
func (c *CTemplateSet) compileBoolsub(varname string, varholder string, templateName string, val reflect.Value) string { func (c *CTemplateSet) compileBoolSub(varname string, con CContext) string {
c.detail("in compileBoolsub") c.detail("in compileBoolSub")
out, val := c.compileIfVarsub(varname, varholder, templateName, val) out, val := c.compileIfVarSub(con, varname)
// TODO: What if it's a pointer or an interface? I *think* we've got pointers handled somewhere, but not interfaces which we don't know the types of at compile time // TODO: What if it's a pointer or an interface? I *think* we've got pointers handled somewhere, but not interfaces which we don't know the types of at compile time
switch val.Kind() { switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
@ -915,21 +927,88 @@ func (c *CTemplateSet) compileBoolsub(varname string, varholder string, template
out = "len(" + out + ") != 0" out = "len(" + out + ") != 0"
default: default:
fmt.Println("Variable Name:", varname) fmt.Println("Variable Name:", varname)
fmt.Println("Variable Holder:", varholder) fmt.Println("Variable Holder:", con.VarHolder)
fmt.Println("Variable Kind:", val.Kind()) fmt.Println("Variable Kind:", con.HoldReflect.Kind())
panic("I don't know what this variable's type is o.o\n") panic("I don't know what this variable's type is o.o\n")
} }
return out return out
} }
func (c *CTemplateSet) compileVarsub(varname string, val reflect.Value, assLines string) (out string) { // For debugging the template generator
c.detail("in compileVarsub") func (c *CTemplateSet) debugParam(param interface{}, depth int) (pstr string) {
switch p := param.(type) {
case CContext:
return "con,"
case reflect.Value:
if p.Kind() == reflect.Ptr || p.Kind() == reflect.Interface {
for p.Kind() == reflect.Ptr || p.Kind() == reflect.Interface {
if p.Kind() == reflect.Ptr {
pstr += "*"
} else {
pstr += "£"
}
p = p.Elem()
}
}
kind := p.Kind().String()
if kind != "struct" {
pstr += kind
} else {
pstr += p.Type().Name()
}
return pstr + ","
case string:
return "\"" + p + "\","
case int:
return strconv.Itoa(p) + ","
case bool:
if p {
return "true,"
}
return "false,"
case func(string) string:
if p == nil {
return "nil,"
}
return "func(string) string),"
default:
return "?,"
}
}
func (c *CTemplateSet) dumpCall(name string, params ...interface{}) {
var pstr string
for _, param := range params {
pstr += c.debugParam(param, 0)
}
if len(pstr) > 0 {
pstr = pstr[:len(pstr)-1]
}
c.detail("called " + name + "(" + pstr + ")")
}
func (c *CTemplateSet) retCall(name string, params ...interface{}) {
var pstr string
for _, param := range params {
pstr += c.debugParam(param, 0)
}
if len(pstr) > 0 {
pstr = pstr[:len(pstr)-1]
}
c.detail("returned from " + name + " => (" + pstr + ")")
}
func (c *CTemplateSet) compileVarSub(con CContext, varname string, val reflect.Value, assLines string, onEnd func(string) string) {
c.dumpCall("compileVarSub", con, varname, val, assLines, onEnd)
if onEnd == nil {
onEnd = func(in string) string {
return in
}
}
// Is this a literal string? // Is this a literal string?
if len(varname) != 0 && varname[0] == '"' { if len(varname) != 0 && varname[0] == '"' {
return assLines + "w.Write([]byte(" + varname + "))\n" con.Push("varsub", onEnd(assLines+"w.Write([]byte("+varname+"))\n"))
return
} }
for _, varItem := range c.varList { for _, varItem := range c.varList {
if strings.HasPrefix(varname, varItem.Destination) { if strings.HasPrefix(varname, varItem.Destination) {
varname = strings.Replace(varname, varItem.Destination, varItem.Name, 1) varname = strings.Replace(varname, varItem.Destination, varItem.Name, 1)
@ -942,27 +1021,33 @@ func (c *CTemplateSet) compileVarsub(varname string, val reflect.Value, assLines
} else { } else {
c.stats[varname] = 1 c.stats[varname] = 1
} }
if val.Kind() == reflect.Interface { if val.Kind() == reflect.Interface {
val = val.Elem() val = val.Elem()
} }
if val.Kind() == reflect.Ptr {
for val.Kind() == reflect.Ptr {
val = val.Elem()
varname = "*" + varname
}
}
c.detail("varname: ", varname) c.detail("varname: ", varname)
c.detail("assLines: ", assLines) c.detail("assLines: ", assLines)
var base string
switch val.Kind() { switch val.Kind() {
case reflect.Int: case reflect.Int:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
out = "w.Write([]byte(strconv.Itoa(" + varname + ")))\n" base = "w.Write([]byte(strconv.Itoa(" + varname + ")))\n"
case reflect.Bool: case reflect.Bool:
out = "if " + varname + " {\nw.Write([]byte(\"true\"))} else {\nw.Write([]byte(\"false\"))\n}\n" base = "if " + varname + " {\nw.Write([]byte(\"true\"))} else {\nw.Write([]byte(\"false\"))\n}\n"
case reflect.String: case reflect.String:
if val.Type().Name() != "string" && !strings.HasPrefix(varname, "string(") { if val.Type().Name() != "string" && !strings.HasPrefix(varname, "string(") {
varname = "string(" + varname + ")" varname = "string(" + varname + ")"
} }
out = "w.Write([]byte(" + varname + "))\n" base = "w.Write([]byte(" + varname + "))\n"
case reflect.Int64: case reflect.Int64:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
out = "w.Write([]byte(strconv.FormatInt(" + varname + ", 10)))\n" base = "w.Write([]byte(strconv.FormatInt(" + varname + ", 10)))\n"
default: default:
if !val.IsValid() { if !val.IsValid() {
panic(assLines + varname + "^\n" + "Invalid value. Maybe, it doesn't exist?") panic(assLines + varname + "^\n" + "Invalid value. Maybe, it doesn't exist?")
@ -972,24 +1057,25 @@ func (c *CTemplateSet) compileVarsub(varname string, val reflect.Value, assLines
fmt.Println("Unknown Type:", val.Type().Name()) fmt.Println("Unknown Type:", val.Type().Name())
panic("-- I don't know what this variable's type is o.o\n") panic("-- I don't know what this variable's type is o.o\n")
} }
c.detail("out: ", out) c.detail("base: ", base)
return assLines + out con.Push("varsub", onEnd(assLines+base))
} }
func (c *CTemplateSet) compileSubtemplate(pvarholder string, pholdreflect reflect.Value, node *parse.TemplateNode) (out string) { func (c *CTemplateSet) compileSubTemplate(pcon CContext, node *parse.TemplateNode) {
c.detail("in compileSubtemplate") c.detail("in compileSubTemplate")
c.detail("Template Node: ", node.Name) c.detail("Template Node: ", node.Name)
fname := strings.TrimSuffix(node.Name, filepath.Ext(node.Name)) fname := strings.TrimSuffix(node.Name, filepath.Ext(node.Name))
varholder := "tmpl_" + fname + "_vars" con := pcon
var holdreflect reflect.Value con.VarHolder = "tmpl_" + fname + "_vars"
con.TemplateName = fname
if node.Pipe != nil { if node.Pipe != nil {
for _, cmd := range node.Pipe.Cmds { for _, cmd := range node.Pipe.Cmds {
firstWord := cmd.Args[0] firstWord := cmd.Args[0]
switch firstWord.(type) { switch firstWord.(type) {
case *parse.DotNode: case *parse.DotNode:
varholder = pvarholder con.VarHolder = pcon.VarHolder
holdreflect = pholdreflect con.HoldReflect = pcon.HoldReflect
case *parse.NilNode: case *parse.NilNode:
panic("Nil is not a command x.x") panic("Nil is not a command x.x")
default: default:
@ -1026,12 +1112,10 @@ func (c *CTemplateSet) compileSubtemplate(pvarholder string, pholdreflect reflec
c.detail("subtree.Root", subtree.Root) c.detail("subtree.Root", subtree.Root)
c.localVars[fname] = make(map[string]VarItemReflect) c.localVars[fname] = make(map[string]VarItemReflect)
c.localVars[fname]["."] = VarItemReflect{".", varholder, holdreflect} c.localVars[fname]["."] = VarItemReflect{".", con.VarHolder, con.HoldReflect}
c.fragmentCursor[fname] = 0 c.fragmentCursor[fname] = 0
c.rootIterate(subtree, con)
out += c.rootIterate(subtree, varholder, holdreflect, fname)
c.TemplateFragmentCount[fname] = c.fragmentCursor[fname] + 1 c.TemplateFragmentCount[fname] = c.fragmentCursor[fname] + 1
return out
} }
// TODO: Should we rethink the way the log methods work or their names? // TODO: Should we rethink the way the log methods work or their names?

View File

@ -854,6 +854,7 @@ input[type=checkbox]:checked + label .sel {
margin-bottom: 4px; margin-bottom: 4px;
padding-bottom: 2px; padding-bottom: 2px;
padding-top: 3px; padding-top: 3px;
margin-right: 3px;
} }
#poweredByHolder { #poweredByHolder {
display: flex; display: flex;