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 && b.Op == '+' {
- return b.Y
- }
- }
- l2, ok2 := b.Y.(expr.Literal)
- if ok2 == true {
- l2 = l2
- if l2 == 0 && b.Op == '+' {
- return b.X
- }
- }
- // Case 5: Any binary operator performing multiplications with 0/1 should be reduced
- ll, lok := b.X.(expr.Literal)
- ll = ll
- if lok == true {
- if ll == 1 && b.Op == '*' {
- return Simplify(b.Y, env)
- } else if ll == 0 && b.Op == '*' {
- ans := fmt.Sprintf("%f", ll.Eval(env))
- tp, ok := expr.Parse(ans)
- if ok == nil {
- return tp
- }
- }
- }
- ll2, lok2 := b.Y.(expr.Literal)
- ll2 = ll2
- if lok2 == true {
- if ll2 == 1 && b.Op == '*' {
- return Simplify(b.X, env)
- } else if ll2 == 0 && b.Op == '*' {
- ans := fmt.Sprintf("%f", ll2.Eval(env))
- tp, ok := expr.Parse(ans)
- if ok == nil {
- return tp
- }
- }
- }
- // 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)
- // Own case: One child is Unary, one is Var
- u, uok := b.X.(expr.Unary)
- v = v
- bl = bl
- u = u
- if ok2 == true || okk2 == true || bok == true || uok == true {
- if ok2 == true { // Literal
- ans = l
- } else if okk2 == true { // Var
- temp := Simplify(b.X, env)
- if b.X != temp {
- tpl, okkk := temp.(expr.Literal)
- okkk = okkk
- ans = tpl
- } else {
- domaths = false
- }
- } else if bok == true { // Binary
- domaths = true
- temp := Simplify(b.X, env)
- if b.X != temp {
- tp2, okkk := temp.(expr.Literal)
- okkk = okkk
- ans = tp2
- }
- } else if uok == 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)
- u2, uok2 := b.Y.(expr.Unary)
- v2 = v2
- bl2 = bl2
- u2 = u2
- if ok3 == true || okk3 == true || bok2 == true || uok2 == true {
- if ok3 == true { // Literal
- ans2 = l2
- } else if okk3 == true { // Var
- temp := Simplify(b.Y, env)
- if b.Y != temp {
- tpl2, okkk2 := temp.(expr.Literal)
- okkk2 = okkk2
- ans2 = tpl2
- if domaths == false {
- b.Y = ans2
- return b
- }
- } else {
- domaths = false
- b.X = ans
- return b
- }
- } else if bok2 == true { // Binary
- domaths = true
- temp := Simplify(bl2, env)
- if b.Y != temp {
- tp2, okkk := temp.(expr.Literal)
- okkk = okkk
- ans2 = tp2
- }
- } else if uok2 == true {
- domaths = true
- temp := Simplify(b.Y, env)
- if b.Y != temp {
- tp2, okkk := temp.(expr.Literal)
- okkk = okkk
- ans2 = tp2
- }
- }
- // Calculations
- 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
- }
- return ans
- }
- }
- }
- panic("TODO: implement this!")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement