gitlab.com/Raven-IO/raven-delve@v1.22.4/pkg/astutil/astutil.go (about)

     1  // This package contains utility functions used by pkg/proc to generate
     2  // ast.Expr expressions.
     3  
     4  package astutil
     5  
     6  import (
     7  	"go/ast"
     8  	"go/token"
     9  	"strconv"
    10  )
    11  
    12  // Eql returns an expression evaluating 'x == y'.
    13  func Eql(x, y ast.Expr) *ast.BinaryExpr {
    14  	return &ast.BinaryExpr{Op: token.EQL, X: x, Y: y}
    15  }
    16  
    17  // Sel returns an expression evaluating 'x.sel'.
    18  func Sel(x ast.Expr, sel string) *ast.SelectorExpr {
    19  	return &ast.SelectorExpr{X: x, Sel: &ast.Ident{Name: sel}}
    20  }
    21  
    22  // PkgVar returns an expression evaluating 'pkg.v'.
    23  func PkgVar(pkg, v string) *ast.SelectorExpr {
    24  	return &ast.SelectorExpr{X: &ast.Ident{Name: pkg}, Sel: &ast.Ident{Name: v}}
    25  }
    26  
    27  // Int returns an expression representing the integer 'n'.
    28  func Int(n int64) *ast.BasicLit {
    29  	return &ast.BasicLit{Kind: token.INT, Value: strconv.FormatInt(n, 10)}
    30  }
    31  
    32  // And returns an expression evaluating 'x && y'.
    33  func And(x, y ast.Expr) ast.Expr {
    34  	if x == nil || y == nil {
    35  		return nil
    36  	}
    37  	return &ast.BinaryExpr{Op: token.LAND, X: x, Y: y}
    38  }
    39  
    40  // Or returns an expression evaluating 'x || y'.
    41  func Or(x, y ast.Expr) ast.Expr {
    42  	if x == nil || y == nil {
    43  		return nil
    44  	}
    45  	return &ast.BinaryExpr{Op: token.LOR, X: x, Y: y}
    46  }