Advertisement
jchero

Untitled

Apr 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.31 KB | None | 0 0
  1. package simplify
  2. import "fmt"
  3. import "hw1/expr"
  4.  
  5. // Depth should return the simplified expresion
  6. func Simplify(e expr.Expr, env expr.Env) expr.Expr {
  7.     //TODO implement the simplify
  8.  
  9.     // Case 1: Any Var should be replaced with its value
  10.     v, ok := e.(expr.Var)
  11.     if ok == true {
  12.         v = v
  13.         if e.Eval(env) == 0 {
  14.             return e
  15.         }
  16.         ans := fmt.Sprintf("%f", e.Eval(env))
  17.         tp, ok := expr.Parse(ans)
  18.         if ok == nil {
  19.             return tp
  20.         }
  21.     }
  22.  
  23.     // Case 2: Any Unary with Literal should be replaced with Literal
  24.     u, ok := e.(expr.Unary)
  25.     if ok == true {
  26.             temp := Simplify(u.X, env)
  27.             if temp == u.X {
  28.                 return e
  29.             }
  30.             l, ok3 := temp.(expr.Literal)
  31.             if ok3 == true {
  32.                 l = l
  33.                 if u.Op == '-' {
  34.                     l = -1 * l
  35.                 }
  36.                 return l
  37.             }
  38.         }
  39.  
  40.  
  41.     b, ok := e.(expr.Binary)
  42.     if ok == true {
  43.  
  44.  
  45.         // Case 4: Any Binary with addition of 0 should reduce
  46.         l0, ok := b.X.(expr.Literal)
  47.         if ok == true {
  48.             l0 = l0
  49.             if l0 == 0 {
  50.                 return b.Y
  51.             }
  52.         }
  53.         l2, ok2 := b.Y.(expr.Literal)
  54.         if ok2 == true {
  55.             l2 = l2
  56.             if l2 == 0 {
  57.                 return b.X
  58.             }
  59.         }
  60.  
  61.       // Case 3: Any Binary with both Literals return Literals
  62.         l, ok2 := b.X.(expr.Literal)
  63.         // Own Case: Binary with Var
  64.         v, okk2 := b.X.(expr.Var)
  65.         // Own case: One child is Binary, one is Var
  66.         bl, bok := b.X.(expr.Binary)
  67.         //TODO: put it HERE!!!!
  68.         v = v
  69.         if ok2 == true || okk2 == true || bok == true{
  70.             if okk2 == true {
  71.                 temp := Simplify(b.X, env)
  72.                 if b.X != temp {
  73.                     tpl, okkk := temp.(expr.Literal)
  74.                     okkk = okkk
  75.                     l = tpl
  76.                 } else if bok == true {
  77.                     bl = bl
  78.                     if bok == true {
  79.                         fmt.Printf("f\n")
  80.                         bltp := Simplify(bl, env)
  81.                         l = bltp.(expr.Literal)
  82.                     } else {
  83.                         return e
  84.                     }
  85.                 }
  86.             }
  87.             l2, ok3 := b.Y.(expr.Literal)
  88.             v2, okk3 := b.Y.(expr.Var)
  89.             v2 = v2
  90.             if ok3 == true || okk3 == true {
  91.                 if okk3 == true {
  92.                     temp := Simplify(b.Y, env)
  93.                     if b.Y != temp {
  94.                         tpl2, okkk2 := temp.(expr.Literal)
  95.                         okkk2 = okkk2
  96.                         l2 = tpl2
  97.                     } else {
  98.                         return e
  99.                     }
  100.                 }
  101.                 l = l
  102.                 l2 = l2
  103.                 if b.Op == '+' {
  104.                     l += l2
  105.                 } else if b.Op == '-' {
  106.                     l -= l2
  107.                 } else if b.Op == '*' {
  108.                     l *= l2
  109.                 } else if b.Op == '/' {
  110.                     l /= l2
  111.                 }
  112.                 return l
  113.             }
  114.         }
  115.  
  116.  
  117.  
  118.     }
  119.  
  120.  
  121.  
  122.     panic("TODO: implement this!")
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement