lang: Move interpret function to a separate package

This commit is contained in:
James Shubin
2021-10-20 17:00:09 -04:00
parent 09812a7bfc
commit de2914978d
3 changed files with 8 additions and 6 deletions

View File

@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
package lang // TODO: move this into a sub package of lang/$name? package interpret
import ( import (
"fmt" "fmt"
@@ -27,11 +27,11 @@ import (
"github.com/purpleidea/mgmt/util/errwrap" "github.com/purpleidea/mgmt/util/errwrap"
) )
// interpret runs the program and causes a graph generation as a side effect. // Interpret runs the program and causes a graph generation as a side effect.
// You should not run this on the AST if you haven't previously run the function // You should not run this on the AST if you haven't previously run the function
// graph engine so that output values have been produced! Type unification is // graph engine so that output values have been produced! Type unification is
// another important aspect which needs to have been completed. // another important aspect which needs to have been completed.
func interpret(ast interfaces.Stmt) (*pgraph.Graph, error) { func Interpret(ast interfaces.Stmt) (*pgraph.Graph, error) {
output, err := ast.Output() // contains resList, edgeList, etc... output, err := ast.Output() // contains resList, edgeList, etc...
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -36,6 +36,7 @@ import (
"github.com/purpleidea/mgmt/lang/funcs" "github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/funcs/vars" "github.com/purpleidea/mgmt/lang/funcs/vars"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/interpret"
"github.com/purpleidea/mgmt/lang/unification" "github.com/purpleidea/mgmt/lang/unification"
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/util" "github.com/purpleidea/mgmt/util"
@@ -1534,7 +1535,7 @@ func TestAstFunc2(t *testing.T) {
// run interpret! // run interpret!
funcs.RLock() // in case something is actually changing funcs.RLock() // in case something is actually changing
ograph, err := interpret(iast) ograph, err := interpret.Interpret(iast)
funcs.RUnlock() funcs.RUnlock()
if (!fail || !failInterpret) && err != nil { if (!fail || !failInterpret) && err != nil {
@@ -1829,7 +1830,7 @@ func TestAstInterpret0(t *testing.T) {
// perform type unification, run the function graph engine, and // perform type unification, run the function graph engine, and
// only gives you limited results... don't expect normal code to // only gives you limited results... don't expect normal code to
// run and produce meaningful things in this test... // run and produce meaningful things in this test...
graph, err := interpret(ast) graph, err := interpret.Interpret(ast)
if !fail && err != nil { if !fail && err != nil {
t.Errorf("test #%d: interpret failed with: %+v", index, err) t.Errorf("test #%d: interpret failed with: %+v", index, err)

View File

@@ -27,6 +27,7 @@ import (
_ "github.com/purpleidea/mgmt/lang/funcs/core" // import so the funcs register _ "github.com/purpleidea/mgmt/lang/funcs/core" // import so the funcs register
"github.com/purpleidea/mgmt/lang/funcs/vars" "github.com/purpleidea/mgmt/lang/funcs/vars"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/interpret"
"github.com/purpleidea/mgmt/lang/unification" "github.com/purpleidea/mgmt/lang/unification"
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/util" "github.com/purpleidea/mgmt/util"
@@ -331,7 +332,7 @@ func (obj *Lang) Interpret() (*pgraph.Graph, error) {
obj.funcs.RLock() obj.funcs.RLock()
} }
// this call returns the graph // this call returns the graph
graph, err := interpret(obj.ast) graph, err := interpret.Interpret(obj.ast)
if obj.funcs != nil { if obj.funcs != nil {
obj.funcs.RUnlock() obj.funcs.RUnlock()
} }