lang: Add an Apply iterator to the Stmt and Expr API

This adds a new interface Node which must implement the Apply method.
This method traverse the entire AST and applies a function to each node.
Both Stmt and Expr must implement this.
This commit is contained in:
James Shubin
2018-07-03 20:35:35 -04:00
parent 3f42e5f702
commit 158bc1eb2a
2 changed files with 292 additions and 0 deletions

View File

@@ -23,10 +23,18 @@ import (
"github.com/purpleidea/mgmt/pgraph"
)
// Node represents either a Stmt or an Expr. It contains the minimum set of
// methods that they must both implement. In practice it is not used especially
// often since we usually know which kind of node we want.
type Node interface {
Apply(fn func(Node) error) error
}
// Stmt represents a statement node in the language. A stmt could be a resource,
// a `bind` statement, or even an `if` statement. (Different from an `if`
// expression.)
type Stmt interface {
Node
Init(*Data) error // initialize the populated node and validate
Interpolate() (Stmt, error) // return expanded form of AST as a new AST
SetScope(*Scope) error // set the scope here and propagate it downwards
@@ -40,6 +48,7 @@ type Stmt interface {
// easily copied and moved around. Expr also implements pgraph.Vertex so that
// these can be stored as pointers in our graph data structure.
type Expr interface {
Node
pgraph.Vertex // must implement this since we store these in our graphs
Init(*Data) error // initialize the populated node and validate
Interpolate() (Expr, error) // return expanded form of AST as a new AST