lang: Add logging and debug info via a new Init method

This expands the Stmt and Expr interfaces to add an Init method. This
is used to pass in Debug and Logf values, but is also used to validate
the AST. This gets rid of standalone use of the "log" package.
This commit is contained in:
James Shubin
2018-06-19 16:49:02 -04:00
parent 75633817a7
commit 3f42e5f702
6 changed files with 417 additions and 65 deletions

View File

@@ -23,15 +23,11 @@ import (
"github.com/purpleidea/mgmt/pgraph"
)
const (
// Debug enables debugging for some commonly used debug information.
Debug = false
)
// 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 {
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
Unify() ([]Invariant, error) // TODO: is this named correctly?
@@ -45,6 +41,7 @@ type Stmt interface {
// these can be stored as pointers in our graph data structure.
type Expr interface {
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
SetScope(*Scope) error // set the scope here and propagate it downwards
SetType(*types.Type) error // sets the type definitively, errors if incompatible
@@ -56,6 +53,15 @@ type Expr interface {
Value() (types.Value, error)
}
// Data provides some data to the node that could be useful during its lifetime.
type Data struct {
// Debug represents if we're running in debug mode or not.
Debug bool
// Logf is a logger which should be used.
Logf func(format string, v ...interface{})
}
// Scope represents a mapping between a variables identifier and the
// corresponding expression it is bound to. Local scopes in this language exist
// and are formed by nesting within if statements. Child scopes can shadow