Advertisement
jchero

tp

Apr 23rd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. package simplify
  2. import "fmt"
  3. import "hw1/expr"
  4.  
  5. // Depth should return the simplified expresion
  6. var domaths bool
  7. func init() {
  8. domaths = true
  9. }
  10.  
  11. func Simplify(e expr.Expr, env expr.Env) expr.Expr {
  12. //TODO implement the simplify
  13.  
  14. // Case 1: Any Var should be replaced with its value
  15. v, ok := e.(expr.Var)
  16. if ok == true {
  17. v = v
  18. if e.Eval(env) == 0 {
  19. return e
  20. }
  21. ans := fmt.Sprintf("%f", e.Eval(env))
  22. tp, ok := expr.Parse(ans)
  23. if ok == nil {
  24. return tp
  25. }
  26. }
  27.  
  28. // Case 2: Any Unary with Literal should be replaced with Literal
  29. u, ok := e.(expr.Unary)
  30. if ok == true {
  31. temp := Simplify(u.X, env)
  32. if temp == u.X {
  33. return e
  34. }
  35. l, ok3 := temp.(expr.Literal)
  36. if ok3 == true {
  37. l = l
  38. if u.Op == '-' {
  39. l = -1 * l
  40. }
  41. return l
  42. }
  43. }
  44.  
  45.  
  46. b, ok := e.(expr.Binary)
  47. if ok == true {
  48.  
  49. var ans expr.Literal
  50. var ans2 expr.Literal
  51.  
  52. // Case 4: Any Binary with addition of 0 should reduce
  53. l0, ok := b.X.(expr.Literal)
  54. if ok == true {
  55. l0 = l0
  56. if l0 == 0 {
  57. return b.Y
  58. }
  59. }
  60. l2, ok2 := b.Y.(expr.Literal)
  61. if ok2 == true {
  62. l2 = l2
  63. if l2 == 0 {
  64. return b.X
  65. }
  66. }
  67.  
  68. // Case 3: Any Binary with both Literals return Literals
  69. l, ok2 := b.X.(expr.Literal)
  70. // Own Case: Binary with Var
  71. v, okk2 := b.X.(expr.Var)
  72. // Own case: One child is Binary, one is Var
  73. bl, bok := b.X.(expr.Binary)
  74.  
  75. v = v
  76. bl = bl
  77. if ok2 == true || okk2 == true || bok == true {
  78.  
  79. if ok2 == true {
  80. // fmt.Printf("hey i am here2\n")
  81. // fmt.Printf("yeaaa\n")
  82. // fmt.Printf("%v\n", b.X)
  83. // fmt.Printf("%v\n", b.Y)
  84. ans = l
  85. } else if okk2 == true {
  86. temp := Simplify(b.X, env)
  87. if b.X != temp {
  88. tpl, okkk := temp.(expr.Literal)
  89. okkk = okkk
  90. ans = tpl
  91. } else {
  92. domaths = false
  93. // if ans2 != 0 {
  94. // b.X = ans
  95. // b.Y = ans2
  96. // return b
  97. // }
  98. }
  99. } else if bok == true {
  100. domaths = true
  101. temp := Simplify(b.X, env)
  102. if b.X != temp {
  103. tp2, okkk := temp.(expr.Literal)
  104. okkk = okkk
  105. ans = tp2
  106. }
  107. }
  108.  
  109.  
  110. l2, ok3 := b.Y.(expr.Literal)
  111. v2, okk3 := b.Y.(expr.Var)
  112. bl2, bok2 := b.Y.(expr.Binary)
  113. v2 = v2
  114. bl2 = bl2
  115. if ok3 == true || okk3 == true || bok2 == true {
  116. if ok3 == true {
  117. ans2 = l2
  118. } else if okk3 == true {
  119. temp := Simplify(b.Y, env)
  120. if b.Y != temp {
  121. // fmt.Printf("hey i am here\n")
  122. // fmt.Printf("yeaaa\n")
  123. // fmt.Printf("%v\n", b.X)
  124. // fmt.Printf("%v\n", temp)
  125. tpl2, okkk2 := temp.(expr.Literal)
  126. okkk2 = okkk2
  127. ans2 = tpl2
  128. fmt.Printf("hey i am here\n")
  129. fmt.Printf("yeaaa\n")
  130. isbin, isbinok := b.X.(expr.Binary)
  131. isbin = isbin
  132. if isbinok == true {
  133. b.Y = ans2
  134. return b
  135. }
  136. } else {
  137. domaths = false
  138. // if ans != 0 {
  139. // b.X = ans
  140. // b.Y = ans2
  141. // return b
  142. // }
  143. }
  144. } else if bok2 == true {
  145. domaths = true
  146. temp := Simplify(bl2, env)
  147. if b.Y != temp {
  148. tp2, okkk := temp.(expr.Literal)
  149. okkk = okkk
  150. ans2 = tp2
  151. }
  152. }
  153.  
  154. //
  155. // checkans, checkok := ans.(expr.Literal)
  156. // checkans2, checkok2 := ans2.(expr.Literal)
  157.  
  158. if domaths == true {
  159. if b.Op == '+' {
  160. ans += ans2
  161. } else if b.Op == '-' {
  162. ans -= ans2
  163. } else if b.Op == '*' {
  164. ans *= ans2
  165. } else if b.Op == '/' {
  166. ans /= ans2
  167. }
  168. } else {
  169. return e
  170. }
  171. // fmt.Printf("%v\n", ans)
  172. // fmt.Printf("yeaaa\n")
  173. // fmt.Printf("%v\n", b.X)
  174. // fmt.Printf("%v\n", b.Y)
  175. return ans
  176. }
  177. }
  178.  
  179.  
  180.  
  181. }
  182.  
  183.  
  184.  
  185. panic("TODO: implement this!")
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement