Skip to content

Instantly share code, notes, and snippets.

@trusch
Created August 16, 2018 08:45
Show Gist options
  • Select an option

  • Save trusch/cf4b7748781f20eeee3c97594d084fbc to your computer and use it in GitHub Desktop.

Select an option

Save trusch/cf4b7748781f20eeee3c97594d084fbc to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
)
func main() {
expr := "or(eq(a,1),eq(a,2))"
root, _ := parser.ParseExpr(expr)
printExpr(root, "")
}
func printExpr(expr ast.Expr, prefix string) {
switch e := expr.(type) {
case *ast.CallExpr:
fmt.Print(prefix, e.Fun, "(\n")
for _, arg := range e.Args {
printExpr(arg, prefix+" ")
}
fmt.Print(prefix, ")\n")
case *ast.Ident:
fmt.Printf("%v%v\n", prefix, e.String())
case *ast.BasicLit:
fmt.Printf("%v%v\n", prefix, e.Value)
default:
fmt.Printf("%v%v (%T)\n", prefix, e, e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment