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-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 {
|
|
|
|
_, err := w.Write([]byte(n.chunk.Source))
|
|
|
|
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 {
|
|
|
|
switch n.chunk.Tag {
|
2017-06-26 02:11:29 +02:00
|
|
|
case "if", "unless":
|
2017-06-26 15:33:07 +02:00
|
|
|
val, err := ctx.EvaluateExpr(n.chunk.Args)
|
2017-06-25 23:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-26 02:11:29 +02:00
|
|
|
if n.chunk.Tag == "unless" {
|
|
|
|
val = (val == nil || val == false)
|
|
|
|
}
|
2017-06-25 23:26:14 +02:00
|
|
|
switch val {
|
|
|
|
default:
|
2017-06-26 18:41:41 +02:00
|
|
|
return renderASTSequence(w, n.body, ctx)
|
2017-06-25 23:26:14 +02:00
|
|
|
case nil, false:
|
|
|
|
for _, c := range n.branches {
|
|
|
|
switch c.chunk.Tag {
|
|
|
|
case "else":
|
|
|
|
val = true
|
|
|
|
case "elsif":
|
2017-06-26 15:33:07 +02:00
|
|
|
val, err = ctx.EvaluateExpr(c.chunk.Args)
|
2017-06-25 23:26:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if val != nil && val != false {
|
2017-06-26 18:41:41 +02:00
|
|
|
return renderASTSequence(w, c.body, ctx)
|
2017-06-25 23:26:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-25 23:00:00 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
2017-06-26 21:36:05 +02:00
|
|
|
return fmt.Errorf("unimplemented tag: %s", n.chunk.Tag)
|
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-26 18:41:41 +02:00
|
|
|
val, err := ctx.EvaluateExpr(n.chunk.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
|
|
|
|
}
|