Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rewrite
- import "fmt"
- import (
- "bytes"
- "go/ast"
- "go/format"
- "go/parser"
- "go/token"
- "strings"
- "hw1/expr"
- "hw1/simplify"
- "strconv" // This package may be helpful...
- )
- // rewriteCalls should modify the passed AST
- func rewriteCalls(node ast.Node) {
- //TODO Write the rewriteCalls function
- fset := token.NewFileSet()
- ast.Inspect(node, func (node ast.Node) bool {
- switch v := node.(type) {
- case *ast.CallExpr:
- switch k := v.Fun.(type) {
- case *ast.SelectorExpr:
- if strings.Contains(k.Sel.Name, "ParseAndEval") {
- switch m := k.X.(type) {
- case *ast.Ident:
- m = m
- switch n := v.Args[0].(type) {
- case *ast.BasicLit:
- n = n
- tp, ok := expr.Parse(n.Value[1 : len(n.Value) - 1])
- ok = ok
- tp = tp
- lol := simplify.Simplify(tp, expr.Env{})
- lol = lol
- haha := expr.Format(lol)
- haha = strconv.Quote(haha)
- n.Value = haha
- // n.Value = haha
- }
- }
- }
- }
- }
- // If we return true, we keep recursing under this AST node.
- // If we return false, we won't visit anything under this AST node.
- return true
- })
- var buf bytes.Buffer
- format.Node(&buf, fset, node)
- fmt.Println(buf.String())
- // fmt.Printf("okkk\n\n")
- panic("TODO: implement this!")
- }
- func SimplifyParseAndEval(src string) string {
- fset := token.NewFileSet()
- f, err := parser.ParseFile(fset, "src.go", src, 0)
- if err != nil {
- panic(err)
- }
- rewriteCalls(f)
- var buf bytes.Buffer
- format.Node(&buf, fset, f)
- return buf.String()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement