Created
August 16, 2018 08:45
-
-
Save trusch/cf4b7748781f20eeee3c97594d084fbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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