2017-06-26 15:36:52 +02:00
|
|
|
package chunks
|
2017-06-25 17:23:20 +02:00
|
|
|
|
2017-06-25 23:00:00 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
2017-06-25 17:23:20 +02:00
|
|
|
|
2017-06-26 18:41:41 +02:00
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
2017-06-25 17:23:20 +02:00
|
|
|
func (n *ASTSeq) Render(w io.Writer, ctx Context) error {
|
|
|
|
for _, c := range n.Children {
|
|
|
|
if err := c.Render(w, ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 18:41:41 +02:00
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
2017-06-27 14:46:06 +02:00
|
|
|
// TODO probably safe to remove this type and method, once the test suite is larger
|
2017-06-25 17:23:20 +02:00
|
|
|
func (n *ASTChunks) Render(w io.Writer, _ Context) error {
|
2017-06-26 21:36:05 +02:00
|
|
|
fmt.Println(MustYAML(n))
|
|
|
|
return fmt.Errorf("unimplemented: ASTChunks.Render")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
|
|
|
func (n *ASTGenericTag) Render(w io.Writer, ctx Context) error {
|
|
|
|
return n.render(w, ctx)
|
2017-06-25 17:23:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:41:41 +02:00
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
2017-06-25 17:23:20 +02:00
|
|
|
func (n *ASTText) Render(w io.Writer, _ Context) error {
|
2017-06-27 17:19:12 +02:00
|
|
|
_, err := w.Write([]byte(n.Source))
|
2017-06-25 17:23:20 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-26 18:41:41 +02:00
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
|
|
|
func renderASTSequence(w io.Writer, seq []ASTNode, ctx Context) error {
|
2017-06-25 23:00:00 +02:00
|
|
|
for _, n := range seq {
|
|
|
|
if err := n.Render(w, ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-06-25 17:23:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:41:41 +02:00
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
2017-06-25 23:00:00 +02:00
|
|
|
func (n *ASTControlTag) Render(w io.Writer, ctx Context) error {
|
2017-06-27 17:19:12 +02:00
|
|
|
cd, ok := FindControlDefinition(n.Tag)
|
2017-06-27 17:12:58 +02:00
|
|
|
if !ok {
|
2017-06-27 17:19:12 +02:00
|
|
|
return fmt.Errorf("unimplemented tag: %s", n.Tag)
|
2017-06-25 23:00:00 +02:00
|
|
|
}
|
2017-06-27 17:12:58 +02:00
|
|
|
f := cd.action(n)
|
|
|
|
return f(w, ctx)
|
2017-06-25 23:00:00 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:41:41 +02:00
|
|
|
// Render evaluates an AST node and writes the result to an io.Writer.
|
2017-06-25 23:00:00 +02:00
|
|
|
func (n *ASTObject) Render(w io.Writer, ctx Context) error {
|
2017-06-27 14:46:06 +02:00
|
|
|
// TODO separate this into parse and evaluate stages.
|
2017-06-27 17:19:12 +02:00
|
|
|
val, err := ctx.EvaluateExpr(n.Args)
|
2017-06-25 23:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte(fmt.Sprint(val)))
|
2017-06-25 17:23:20 +02:00
|
|
|
return err
|
|
|
|
}
|