From a7c9673bcf7e48fd25bd4174bbbf35ad3a915d48 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 22 Nov 2018 16:01:59 -0500 Subject: [PATCH] lang: Improve empty scope and output For some reason these were unnecessary methods on the structs, even when those structs contained nothing useful to offer. --- lang/interfaces/ast.go | 10 +++++----- lang/structs.go | 20 +++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lang/interfaces/ast.go b/lang/interfaces/ast.go index 1b495c3d..47305f24 100644 --- a/lang/interfaces/ast.go +++ b/lang/interfaces/ast.go @@ -88,9 +88,9 @@ type Scope struct { Chain []Stmt // chain of previously seen stmt's } -// Empty returns the zero, empty value for the scope, with all the internal +// EmptyScope returns the zero, empty value for the scope, with all the internal // lists initialized appropriately. -func (obj *Scope) Empty() *Scope { +func EmptyScope() *Scope { return &Scope{ Variables: make(map[string]Expr), //Functions: ???, @@ -146,9 +146,9 @@ type Output struct { // returned by Stmt //Exported []*Exports // TODO: add exported resources } -// Empty returns the zero, empty value for the output, with all the internal -// lists initialized appropriately. -func (obj *Output) Empty() *Output { +// EmptyOutput returns the zero, empty value for the output, with all the +// internal lists initialized appropriately. +func EmptyOutput() *Output { return &Output{ Resources: []engine.Res{}, Edges: []*Edge{}, diff --git a/lang/structs.go b/lang/structs.go index 9063a0c8..aedc8546 100644 --- a/lang/structs.go +++ b/lang/structs.go @@ -129,7 +129,7 @@ func (obj *StmtBind) Graph() (*pgraph.Graph, error) { // Output for the bind statement produces no output. Any values of interest come // from the use of the var which this binds the expression to. func (obj *StmtBind) Output() (*interfaces.Output, error) { - return (&interfaces.Output{}).Empty(), nil + return interfaces.EmptyOutput(), nil } // StmtRes is a representation of a resource and possibly some edges. @@ -1619,7 +1619,7 @@ func (obj *StmtFunc) Graph() (*pgraph.Graph, error) { // Output for the func statement produces no output. Any values of interest come // from the use of the func which this binds the function to. func (obj *StmtFunc) Output() (*interfaces.Output, error) { - return (&interfaces.Output{}).Empty(), nil + return interfaces.EmptyOutput(), nil } // StmtClass represents a user defined class. It's effectively a program body @@ -1780,7 +1780,7 @@ func (obj *StmtInclude) Interpolate() (interfaces.Stmt, error) { // handles the recursion scenario. func (obj *StmtInclude) SetScope(scope *interfaces.Scope) error { if scope == nil { - scope = scope.Empty() + scope = interfaces.EmptyScope() } stmt, exists := scope.Classes[obj.Name] @@ -1993,7 +1993,7 @@ func (obj *StmtImport) Graph() (*pgraph.Graph, error) { // import statement itself produces no output, as it is only used to populate // the scope so that others can use that to produce values and output. func (obj *StmtImport) Output() (*interfaces.Output, error) { - return (&interfaces.Output{}).Empty(), nil + return interfaces.EmptyOutput(), nil } // StmtComment is a representation of a comment. It is currently unused. It @@ -2055,7 +2055,7 @@ func (obj *StmtComment) Graph() (*pgraph.Graph, error) { // Output for the comment statement produces no output. func (obj *StmtComment) Output() (*interfaces.Output, error) { - return (&interfaces.Output{}).Empty(), nil + return interfaces.EmptyOutput(), nil } // ExprBool is a representation of a boolean. @@ -3527,7 +3527,8 @@ func (obj *ExprFunc) Value() (types.Value, error) { // ExprCall is a representation of a function call. This does not represent the // declaration or implementation of a new function value. type ExprCall struct { - typ *types.Type + scope *interfaces.Scope // store for referencing this later + typ *types.Type V types.Value // stored result (set with SetValue) @@ -3652,6 +3653,11 @@ func (obj *ExprCall) Interpolate() (interfaces.Expr, error) { // SetScope stores the scope for later use in this resource and it's children, // which it propagates this downwards to. func (obj *ExprCall) SetScope(scope *interfaces.Scope) error { + if scope == nil { + scope = interfaces.EmptyScope() + } + obj.scope = scope + for _, x := range obj.Args { if err := x.SetScope(scope); err != nil { return err @@ -4030,7 +4036,7 @@ func (obj *ExprVar) Interpolate() (interfaces.Expr, error) { // SetScope stores the scope for use in this resource. func (obj *ExprVar) SetScope(scope *interfaces.Scope) error { if scope == nil { - scope = scope.Empty() + scope = interfaces.EmptyScope() } obj.scope = scope return nil