Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package simplify
- import "fmt"
- import "hw1/expr"
- // Depth should return the simplified expresion
- var domaths bool
- func init() {
- domaths = true
- }
- func Simplify(e expr.Expr, env expr.Env) expr.Expr {
- //TODO implement the simplify
- // Case 1: Any Var should be replaced with its value
- v, ok := e.(expr.Var)
- if ok == true {
- v = v
- if e.Eval(env) == 0 {
- return e
- }
- ans := fmt.Sprintf("%f", e.Eval(env))
- tp, ok := expr.Parse(ans)
- if ok == nil {
- return tp
- }
- }
- // Case 2: Any Unary with Literal should be replaced with Literal
- u, ok := e.(expr.Unary)
- if ok == true {
- temp := Simplify(u.X, env)
- if temp == u.X {
- return e
- }
- l, ok3 := temp.(expr.Literal)
- if ok3 == true {
- l = l
- if u.Op == '-' {
- l = -1 * l
- }
- return l
- }
- }
- b, ok := e.(expr.Binary)
- if ok == true {
- var ans expr.Literal
- var ans2 expr.Literal
- // Case 4: Any Binary with addition of 0 should reduce
- l0, ok := b.X.(expr.Literal)
- if ok == true {
- l0 = l0
- if l0 == 0 {
- return b.Y
- }
- }
- l2, ok2 := b.Y.(expr.Literal)
- if ok2 == true {
- l2 = l2
- if l2 == 0 {
- return b.X
- }
- }
- // Case 3: Any Binary with both Literals return Literals
- l, ok2 := b.X.(expr.Literal)
- // Own Case: Binary with Var
- v, okk2 := b.X.(expr.Var)
- // Own case: One child is Binary, one is Var
- bl, bok := b.X.(expr.Binary)
- v = v
- bl = bl
- if ok2 == true || okk2 == true || bok == true {
- if ok2 == true {
- // fmt.Printf("hey i am here2\n")
- // fmt.Printf("yeaaa\n")
- // fmt.Printf("%v\n", b.X)
- // fmt.Printf("%v\n", b.Y)
- ans = l
- } else if okk2 == true {
- temp := Simplify(b.X, env)
- if b.X != temp {
- tpl, okkk := temp.(expr.Literal)
- okkk = okkk
- ans = tpl
- } else {
- domaths = false
- // if ans2 != 0 {
- // b.X = ans
- // b.Y = ans2
- // return b
- // }
- }
- } else if bok == true {
- domaths = true
- temp := Simplify(b.X, env)
- if b.X != temp {
- tp2, okkk := temp.(expr.Literal)
- okkk = okkk
- ans = tp2
- }
- }
- l2, ok3 := b.Y.(expr.Literal)
- v2, okk3 := b.Y.(expr.Var)
- bl2, bok2 := b.Y.(expr.Binary)
- v2 = v2
- bl2 = bl2
- if ok3 == true || okk3 == true || bok2 == true {
- if ok3 == true {
- ans2 = l2
- } else if okk3 == true {
- temp := Simplify(b.Y, env)
- if b.Y != temp {
- // fmt.Printf("hey i am here\n")
- // fmt.Printf("yeaaa\n")
- // fmt.Printf("%v\n", b.X)
- // fmt.Printf("%v\n", temp)
- tpl2, okkk2 := temp.(expr.Literal)
- okkk2 = okkk2
- ans2 = tpl2
- fmt.Printf("hey i am here\n")
- fmt.Printf("yeaaa\n")
- isbin, isbinok := b.X.(expr.Binary)
- isbin = isbin
- if isbinok == true {
- b.Y = ans2
- return b
- }
- } else {
- domaths = false
- // if ans != 0 {
- // b.X = ans
- // b.Y = ans2
- // return b
- // }
- }
- } else if bok2 == true {
- domaths = true
- temp := Simplify(bl2, env)
- if b.Y != temp {
- tp2, okkk := temp.(expr.Literal)
- okkk = okkk
- ans2 = tp2
- }
- }
- //
- // checkans, checkok := ans.(expr.Literal)
- // checkans2, checkok2 := ans2.(expr.Literal)
- if domaths == true {
- if b.Op == '+' {
- ans += ans2
- } else if b.Op == '-' {
- ans -= ans2
- } else if b.Op == '*' {
- ans *= ans2
- } else if b.Op == '/' {
- ans /= ans2
- }
- } else {
- return e
- }
- // fmt.Printf("%v\n", ans)
- // fmt.Printf("yeaaa\n")
- // fmt.Printf("%v\n", b.X)
- // fmt.Printf("%v\n", b.Y)
- return ans
- }
- }
- }
- panic("TODO: implement this!")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement