Created
February 22, 2024 13:34
-
-
Save clarete/ab95942535a18a263ea19e4118f6add4 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" | |
"os" | |
"github.com/prometheus/prometheus/promql/parser" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Printf("wat?\n") | |
return | |
} | |
p := parser.NewParser(os.Args[1]) | |
expr, err := p.ParseExpr() | |
if err != nil { | |
fmt.Printf("Error: %s\n", err.Error()) | |
return | |
} | |
fmt.Println(visit(expr)) | |
} | |
func visit(e parser.Expr) string { | |
switch el := e.(type) { | |
case *parser.NumberLiteral: | |
return fmt.Sprintf("NumberLiteral val=%g", el.Val) | |
case *parser.BinaryExpr: | |
return fmt.Sprintf("BinaryExpr op=%s lhs=(%s) rhs=(%s)", | |
parser.ItemTypeStr[el.Op], visit(el.LHS), visit(el.RHS)) | |
default: | |
return fmt.Sprintf("%#v", el) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment